CouponRulesLogic.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\logic\coupon;
  15. use app\common\model\coupon\CouponCategory;
  16. use app\common\model\coupon\CouponRules;
  17. use app\common\logic\BaseLogic;
  18. use think\db\Query;
  19. use think\facade\Db;
  20. /**
  21. * CouponRules逻辑
  22. * Class CouponRulesLogic
  23. * @package app\adminapi\logic\coupon
  24. */
  25. class CouponRulesLogic extends BaseLogic
  26. {
  27. /**
  28. * @notes 添加
  29. * @param array $params
  30. * @return bool
  31. * @author likeadmin
  32. * @date 2024/07/18 10:11
  33. */
  34. public static function add(array $params): bool
  35. {
  36. Db::startTrans();
  37. try {
  38. $model = CouponRules::create([
  39. 'amount' => $params['amount'],
  40. 'amount_require' => $params['amount_require'],
  41. 'begin_use' => $params['begin_use'] ? strtotime($params['begin_use']) :0,
  42. 'discount_ratio' => $params['discount_ratio'],
  43. 'event_name' => $params['event_name'],
  44. 'expire_time' => $params['expire_time'] ? strtotime($params['expire_time']) :0,
  45. 'max_deductible_price' => $params['max_deductible_price'],
  46. 'mold_type' => $params['mold_type'],
  47. 'server_category_name' => $params['server_category_name'],
  48. 'voucher_status' => $params['voucher_status'],
  49. 'voucher_count' => $params['voucher_count'],
  50. ]);
  51. if(!empty($params['goods_category_ids'])){
  52. $categoryArr = [];
  53. foreach($params['goods_category_ids'] as $v){
  54. $categoryArr[] = end($v);
  55. }
  56. $model->couponWithCategory()->saveAll($categoryArr);
  57. }
  58. Db::commit();
  59. return true;
  60. } catch (\Exception $e) {
  61. Db::rollback();
  62. self::setError($e->getMessage());
  63. return false;
  64. }
  65. }
  66. /**
  67. * @notes 编辑
  68. * @param array $params
  69. * @return bool
  70. * @author likeadmin
  71. * @date 2024/07/18 10:11
  72. */
  73. public static function edit(array $params): bool
  74. {
  75. Db::startTrans();
  76. try {
  77. CouponRules::where('id', $params['id'])->update([
  78. 'amount' => $params['amount'],
  79. 'amount_require' => $params['amount_require'],
  80. 'begin_use' => $params['begin_use'] ? strtotime($params['begin_use']) :0,
  81. 'discount_ratio' => $params['discount_ratio'],
  82. 'event_name' => $params['event_name'],
  83. 'expire_time' => $params['expire_time'] ? strtotime($params['expire_time']) :0,
  84. 'max_deductible_price' => $params['max_deductible_price'],
  85. 'mold_type' => $params['mold_type'],
  86. 'server_category_name' => $params['server_category_name'],
  87. 'voucher_status' => $params['voucher_status'],
  88. 'voucher_count' => $params['voucher_count'],
  89. ]);
  90. CouponCategory::where('coupon_id',$params['id'])->delete();
  91. if(!empty($params['goods_category_ids'])){
  92. $categoryArr = [];
  93. foreach($params['goods_category_ids'] as $v){
  94. if(!is_array($v)){
  95. $categoryArr[] = $v;
  96. continue;
  97. }
  98. $categoryArr[] = end($v);
  99. }
  100. // $exitCategory = CouponRules::hasWhere("couponWithCategory",function(Query $query)use($categoryArr){
  101. // $query->whereIn('goods_category_id',$categoryArr);
  102. // })->where('id','<>',$params['id'])
  103. // ->where('begin_use','>=',time())
  104. // ->where('expire_time','<=','expire_time')
  105. // ->where('voucher_status', '<>',3)
  106. // ->findOrEmpty();
  107. $model = CouponRules::find($params['id']);
  108. $model->couponWithCategory()->saveAll($categoryArr);
  109. }
  110. Db::commit();
  111. return true;
  112. } catch (\Exception $e) {
  113. Db::rollback();
  114. self::setError($e->getMessage());
  115. return false;
  116. }
  117. }
  118. /**
  119. * @notes 删除
  120. * @param array $params
  121. * @return bool
  122. * @author likeadmin
  123. * @date 2024/07/18 10:11
  124. */
  125. public static function delete(array $params): bool
  126. {
  127. CouponCategory::where('coupon_id',$params['id'])->delete();
  128. return CouponRules::destroy($params['id']);
  129. }
  130. /**
  131. * @notes 获取详情
  132. * @param $params
  133. * @return array
  134. * @author likeadmin
  135. * @date 2024/07/18 10:11
  136. */
  137. public static function detail($params): array
  138. {
  139. $rules = CouponRules::findOrEmpty($params['id'])->toArray();
  140. if(!empty($rules)){
  141. $rules['goods_category_ids'] = CouponCategory::where('coupon_id',$params['id'])->column('goods_category_id');
  142. }
  143. return $rules;
  144. }
  145. }