UserCouponLogic.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. 'coupon_id' => $v->id,
  44. 'code' => $v->code,
  45. 'voucher_status' => 0,
  46. 'voucher_count' =>1,
  47. 'amount' => $v->mold_type != 1 ? $v->amount:null,
  48. 'amount_require' => $v->amount_require,
  49. 'begin_use' => time(),
  50. 'discount_ratio' => $v->mold_type == 1 ? $v->discount_ratio:null,
  51. 'event_name' => $v->event_name,
  52. 'expire_time' => time()+$v->expire_time,
  53. 'max_deductible_price' => $v->mold_type == 1 ? $v->max_deductible_price:null,
  54. 'mold_type' => $v->mold_type,
  55. 'server_category_name' => $v->server_category_name
  56. ];
  57. }
  58. CouponRules::updateWhenCase($updateData);
  59. if(!empty($createData)){
  60. (new UserCoupon())->saveAll($createData);
  61. }
  62. Db::commit();
  63. return $errMsgArr;
  64. }catch(\Exception $e){
  65. Db::rollback();
  66. self::setError($e->getMessage());
  67. return false;
  68. }
  69. }
  70. /**
  71. * 我的优惠卷列表
  72. * @param $params
  73. * @return array|false
  74. */
  75. public static function userCouponList($params)
  76. {
  77. try{
  78. $where = [];
  79. if(isset($params['voucher_status']) && is_array($params['voucher_status'])){
  80. $where[] = ['voucher_status','IN',$params['voucher_status']];
  81. }
  82. $where[] = ['user_id','=',$params['user_id']];
  83. $data = UserCoupon::with(['couponRules'=>function(Query $query){
  84. $query->field(['id','code']);
  85. $query->with(['couponWithCategory'=>function(Query $query){
  86. $query->field('id,name');
  87. }]);
  88. }])
  89. ->where($where)
  90. ->where(function(Query $query){
  91. $query->where(' expire_time >'. (time()-86400*7));
  92. })
  93. ->field(['id', 'user_id', 'code','begin_use','expire_time', 'voucher_status','voucher_count'])
  94. ->append(['voucher_status_text'])
  95. ->order(['id' => 'desc'])
  96. ->select()
  97. ->toArray();
  98. $couponCodes = [];
  99. foreach($data as $k => $v){
  100. if($v['couponRules']){
  101. $v['couponRules']['goods_category_ids'] = array_column($v['couponRules']['couponWithCategory'],'id');
  102. $data[$k] = $v;
  103. }
  104. if($v['expire_time'] < time()){
  105. $couponCodes[] = $v['code'];
  106. $data[$k]['voucher_status_text'] = '已过期';
  107. }
  108. $data[$k]['begin_use'] = date("Y-m-d H:i:s",$v['begin_use'] );
  109. $data[$k]['expire_time'] = date("Y-m-d H:i:s",$v['expire_time'] );
  110. }
  111. if(!empty($couponCodes)){
  112. UserCoupon::whereIn('code',$couponCodes)->update(['voucher_status'=>2]);
  113. }
  114. return $data;
  115. } catch (\Exception $e){
  116. Db::rollback();
  117. self::setError($e->getMessage());
  118. return false;
  119. }
  120. }
  121. /**
  122. * 商品分类优惠卷领取
  123. * @param $params
  124. * @return array|false
  125. */
  126. public static function categoryCouponLists($params)
  127. {
  128. try{
  129. $data = CouponRules::with(['couponCategory'=>function ($query) use ($params) {
  130. $query->where('goods_category_id',$params['goods_category_id']);
  131. }])->where('remaining_count','>',0)
  132. ->where('voucher_status',1)
  133. ->order(['id' => 'desc'])
  134. ->field(['code','amount','amount_require','discount_ratio','server_category_name',
  135. 'event_name','expire_time','max_deductible_price','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. public static function categoryWithAmountLists($params)
  151. {
  152. try{
  153. $data = UserCoupon::with(['couponCategory'=>function ($query) use ($params) {
  154. $query->where('goods_category_id',$params['goods_category_id']);
  155. }])->where('user_id',$params['user_id'])
  156. ->where('voucher_count','>',0)
  157. ->where('voucher_status',0)
  158. ->where('amount_require','<=',$params['amount'])
  159. ->visible(['coupon_id','amount','amount_require','begin_use','discount_ratio','event_name','expire_time','max_deductible_price','server_category_name'])
  160. ->select()->toArray();
  161. foreach($data as $k => $v){
  162. $data[$k]['begin_use'] = date("Y-m-d H:i:s",$v['begin_use'] );
  163. $data[$k]['expire_time'] = date("Y-m-d H:i:s",$v['expire_time'] );
  164. }
  165. return $data;
  166. } catch(\Exception $e){
  167. self::setError($e->getMessage());
  168. return false;
  169. }
  170. }
  171. }