FreeShippingLogic.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\logic\free_shipping;
  20. use app\common\enum\FreeShippingEnum;
  21. use app\common\logic\BaseLogic;
  22. use app\common\model\FreeShipping;
  23. /**
  24. * 包邮活动
  25. */
  26. class FreeShippingLogic extends BaseLogic
  27. {
  28. /**
  29. * @notes 添加包邮活动
  30. */
  31. public static function add($params)
  32. {
  33. try {
  34. $params['start_time'] = strtotime($params['start_time']);
  35. $params['end_time'] = strtotime($params['end_time']);
  36. $params['status'] = FreeShippingEnum::WAIT;
  37. $params['region'] = json_encode($params['region']);
  38. return FreeShipping::create($params);
  39. return true;
  40. } catch (\Exception $e) {
  41. self::$error = $e->getMessage();
  42. return false;
  43. }
  44. }
  45. /**
  46. * @notes 包邮活动详情
  47. */
  48. public static function detail($params)
  49. {
  50. $activity = FreeShipping::withoutField('create_time,update_time,delete_time')
  51. ->findOrEmpty($params['id'])->toArray();
  52. return $activity;
  53. }
  54. /**
  55. * @notes 编辑包邮活动
  56. */
  57. public static function edit($params)
  58. {
  59. try {
  60. $params['start_time'] = strtotime($params['start_time']);
  61. $params['end_time'] = strtotime($params['end_time']);
  62. $params['region'] = json_encode($params['region']);
  63. $activity = FreeShipping::findOrEmpty($params['id']);
  64. switch ($activity->getData('status')) {
  65. case FreeShippingEnum::WAIT:
  66. FreeShipping::update($params);
  67. break;
  68. case FreeShippingEnum::ING:
  69. // 只允许编辑名称
  70. FreeShipping::update(['id' => $params['id'], 'name'=> $params['name']]);
  71. break;
  72. case FreeShippingEnum::END:
  73. throw new \Exception('进行中和已结束的活动不允许编辑');
  74. }
  75. return true;
  76. } catch(\Exception $e) {
  77. self::$error = $e->getMessage();
  78. return false;
  79. }
  80. }
  81. /**
  82. * @notes 开始包邮活动
  83. */
  84. public static function start($params)
  85. {
  86. try {
  87. $activity = FreeShipping::findOrEmpty($params['id']);
  88. if ($activity->getData('status') != FreeShippingEnum::WAIT) {
  89. throw new \Exception('只有未开始的活动才能进行开始操作');
  90. }
  91. $activity->status = FreeShippingEnum::ING;
  92. $activity->save();
  93. return true;
  94. } catch (\Exception $e) {
  95. self::$error = $e->getMessage();
  96. return false;
  97. }
  98. }
  99. /**
  100. * @notes 结束包邮活动
  101. */
  102. public static function end($params)
  103. {
  104. try {
  105. $activity = FreeShipping::findOrEmpty($params['id']);
  106. if ($activity->getData('status') != FreeShippingEnum::ING) {
  107. throw new \Exception('只有进行中的活动才能结束');
  108. }
  109. $activity->status = FreeShippingEnum::END;
  110. $activity->save();
  111. return true;
  112. } catch (\Exception $e) {
  113. self::$error = $e->getMessage();
  114. return false;
  115. }
  116. }
  117. /**
  118. * @notes 删除包邮活动
  119. */
  120. public static function delete($params)
  121. {
  122. try {
  123. FreeShipping::destroy($params['id']);
  124. return true;
  125. } catch (\Exception $e) {
  126. self::$error = $e->getMessage();
  127. return false;
  128. }
  129. }
  130. }