UserCouponLogic.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace app\api\logic;
  3. use app\common\logic\BaseLogic;
  4. use app\common\model\coupon\CouponCategory;
  5. use app\common\model\coupon\CouponRules;
  6. use app\common\model\coupon\UserCoupon;
  7. use think\db\Query;
  8. use think\db\Where;
  9. use think\facade\Db;
  10. /**
  11. * @author 林海涛
  12. * @date 2024/7/18 下午4:49
  13. */
  14. class UserCouponLogic extends BaseLogic
  15. {
  16. public static function add($params)
  17. {
  18. Db::startTrans();
  19. try{
  20. $errMsgArr = [];
  21. $rules = CouponRules::whereIn('id',$params['coupon_ids'])->lock(true)->select();
  22. $userCoupon = UserCoupon::where('user_id',$params['user_id'])
  23. ->whereIn('coupon_id',$params['coupon_ids'])
  24. ->where('voucher_count','>',0)
  25. ->select();
  26. $updateData = [];
  27. $createData = [];
  28. foreach($rules as $v) {
  29. if ($v->expire_time < time()) {
  30. $errMsgArr[] = $v->event_name .'已过期';
  31. continue;
  32. }
  33. if($v->voucher_status == 2){
  34. $errMsgArr[] = $v->event_name .'已设置为过期';
  35. continue;
  36. }
  37. if($userCoupon->where('coupon_id',$v->id)->count()){
  38. $errMsgArr[] = $v->event_name . '已领取';
  39. continue;
  40. }
  41. $updateData[$v->id] = ['voucher_count' => --$v->voucher_count];
  42. $createData[] = ['user_id'=>$params['user_id'],'coupon_id' => $v->id,'voucher_count' =>1];
  43. }
  44. CouponRules::updateWhenCase($updateData);
  45. if(!empty($createData)){
  46. (new UserCoupon())->saveAll($createData);
  47. }
  48. Db::commit();
  49. return $errMsgArr;
  50. }catch(\Exception $e){
  51. Db::rollback();
  52. self::setError($e->getMessage());
  53. return false;
  54. }
  55. }
  56. /**
  57. * 我的优惠卷列表
  58. * @param $params
  59. * @return array|false
  60. */
  61. public static function userCouponList($params)
  62. {
  63. try{
  64. $where = [];
  65. if(isset($params['voucher_status']) && is_array($params['voucher_status'])){
  66. $where[] = ['voucher_status','IN',$params['voucher_status']];
  67. }
  68. $where[] = ['user_id','=',$params['user_id']];
  69. $data = UserCoupon::with(['couponRules'=>function(Query $query){
  70. $query->withoutField(['voucher_status','voucher_count']);
  71. $query->with(['couponWithCategory'=>function(Query $query){
  72. $query->field('id,name');
  73. }]);
  74. }])
  75. ->where($where)
  76. ->field(['id', 'user_id', 'coupon_id', 'voucher_status','voucher_count'])
  77. ->order(['id' => 'desc'])
  78. ->select()
  79. ->toArray();
  80. $couponIds = [];
  81. foreach($data as $k => $v){
  82. if($v['couponRules']){
  83. if($v['voucher_status'] !=2 &&($v['couponRules']['begin_use'] <time() || $data['couponRules']['expire_time'] > time())){
  84. $couponIds[] = $v['coupon_id'];
  85. $v['voucher_status'] = 2;
  86. }
  87. $v['couponRules']['begin_use'] = date("Y-m-d H:i:s",$v['couponRules']['begin_use'] );
  88. $v['couponRules']['expire_time'] = date("Y-m-d H:i:s",$v['couponRules']['expire_time'] );
  89. $v['couponRules']['goods_category_ids'] = array_column($v['couponRules']['couponWithCategory'],'id');
  90. $data[$k] = $v;
  91. }
  92. }
  93. if(!empty($couponIds)){
  94. UserCoupon::whereIn('coupon_id',$couponIds)->update(['voucher_status'=>2]);
  95. }
  96. return $data;
  97. }catch(\Exception $e){
  98. Db::rollback();
  99. self::setError($e->getMessage());
  100. return false;
  101. }
  102. }
  103. /**
  104. * 商品分类优惠卷领取
  105. * @param $params
  106. * @return array|false
  107. */
  108. public static function categoryCouponList($params)
  109. {
  110. try{
  111. $data = CouponRules::with(['couponWithCategory'=>function(Query $query) use($params){
  112. $query->where('goods_category_id',$params['goods_category_id']);
  113. }])->field(['id', 'amount', 'coupon_type','amount_require', 'discount_ratio', 'event_name', 'expire_time', 'max_deductible_price', 'mold_type', 'server_category_name', 'voucher_count'])
  114. ->order(['id' => 'desc'])
  115. ->select()
  116. ->toArray();
  117. return $data;
  118. }catch(\Exception $e){
  119. Db::rollback();
  120. self::setError($e->getMessage());
  121. return false;
  122. }
  123. }
  124. }