UserCouponLogic.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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('code',$params['codes'])->where('voucher_status',1)->lock(true)->select();
  22. $userCoupon = UserCoupon::where('user_id',$params['user_id'])
  23. ->whereIn('code',$params['codes'])
  24. ->where('voucher_count','>',0)
  25. ->select();
  26. $updateData = [];
  27. $createData = [];
  28. foreach($rules as $v) {
  29. if($v->voucher_status == 2){
  30. $errMsgArr[] = $v->event_name .'已设置为过期';
  31. continue;
  32. }
  33. if($v->remaining_count <= 0 ){
  34. $errMsgArr[] = $v->event_name .'数量不足';
  35. }
  36. if($userCoupon->where('coupon_id',$v->id)->count()){
  37. $errMsgArr[] = $v->event_name . '已领取';
  38. continue;
  39. }
  40. $updateData[$v->id] = ['remaining_count' => --$v->remaining_count];
  41. $createData[] = [
  42. 'user_id'=>$params['user_id'],
  43. 'code' => $v->code,
  44. 'voucher_status' => 0,
  45. 'voucher_count' =>1,
  46. 'amount' => $v->mold_type != 1 ? $v->amount:null,
  47. 'amount_require' => $v->amount_require,
  48. 'begin_use' => time(),
  49. 'discount_ratio' => $v->mold_type == 1 ? $v->discount_ratio:null,
  50. 'event_name' => $v->event_name,
  51. 'expire_time' => time()+$v->expire_time,
  52. 'max_deductible_price' => $v->mold_type == 1 ? $v->max_deductible_price:null,
  53. 'mold_type' => $v->mold_type,
  54. 'server_category_name' => $v->server_category_name
  55. ];
  56. }
  57. CouponRules::updateWhenCase($updateData);
  58. if(!empty($createData)){
  59. (new UserCoupon())->saveAll($createData);
  60. }
  61. Db::commit();
  62. return $errMsgArr;
  63. }catch(\Exception $e){
  64. Db::rollback();
  65. self::setError($e->getMessage());
  66. return false;
  67. }
  68. }
  69. /**
  70. * 我的优惠卷列表
  71. * @param $params
  72. * @return array|false
  73. */
  74. public static function userCouponList($params)
  75. {
  76. try{
  77. $where = [];
  78. if(isset($params['voucher_status']) && is_array($params['voucher_status'])){
  79. $where[] = ['voucher_status','IN',$params['voucher_status']];
  80. }
  81. $where[] = ['user_id','=',$params['user_id']];
  82. $data = UserCoupon::with(['couponRules'=>function(Query $query){
  83. $query->field(['id','code']);
  84. $query->with(['couponWithCategory'=>function(Query $query){
  85. $query->field('id,name');
  86. }]);
  87. }])
  88. ->where($where)
  89. ->where(function(Query $query){
  90. $query->where(' expire_time >'. (time()-86400*7));
  91. })
  92. ->field(['id', 'user_id', 'code','begin_use','expire_time', 'voucher_status','voucher_count'])
  93. ->append(['voucher_status_text'])
  94. ->order(['id' => 'desc'])
  95. ->select()
  96. ->toArray();
  97. $couponCodes = [];
  98. foreach($data as $k => $v){
  99. if($v['couponRules']){
  100. $v['couponRules']['goods_category_ids'] = array_column($v['couponRules']['couponWithCategory'],'id');
  101. $data[$k] = $v;
  102. }
  103. if($v['expire_time'] < time()){
  104. $couponCodes[] = $v['code'];
  105. $data[$k]['voucher_status_text'] = '已过期';
  106. }
  107. $data[$k]['begin_use'] = date("Y-m-d H:i:s",$v['begin_use'] );
  108. $data[$k]['expire_time'] = date("Y-m-d H:i:s",$v['expire_time'] );
  109. }
  110. if(!empty($couponCodes)){
  111. UserCoupon::whereIn('code',$couponCodes)->update(['voucher_status'=>2]);
  112. }
  113. return $data;
  114. } catch (\Exception $e){
  115. Db::rollback();
  116. self::setError($e->getMessage());
  117. return false;
  118. }
  119. }
  120. /**
  121. * 商品分类优惠卷领取
  122. * @param $params
  123. * @return array|false
  124. */
  125. public static function categoryCouponList($params)
  126. {
  127. try{
  128. $data = CouponRules::alias('cr')
  129. ->leftJoin('coupon_category cc','cc.coupon_id = cr.id')
  130. ->where('cc.goods_category_id',$params['goods_category_id'])
  131. ->where('cr.remaining_count','>',0)
  132. ->where('cr.voucher_status',1)
  133. ->order(['cr.id' => 'desc'])
  134. ->field(['cr.id','cr.code','cr.amount','cr.amount_require','cr.discount_ratio','cr.server_category_name',
  135. 'cr.event_name','cr.expire_time','cr.max_deductible_price','cr.mold_type',])
  136. ->select();
  137. $codes = $data->column('code');
  138. $userCouponCodes = [];
  139. if(!empty($code)){
  140. $userCouponCodes = UserCoupon::where('code',$codes)
  141. ->where('user_id',$params['user_id'])->column('code');
  142. }
  143. $data = $data->whereNotIn('code',$userCouponCodes)->toArray();
  144. return $data;
  145. }catch(\Exception $e){
  146. self::setError($e->getMessage());
  147. return false;
  148. }
  149. }
  150. }