1
0

UserCouponLogic.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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::where($where)
  84. ->where(function(Query $query){
  85. $query->where(' expire_time >'. (time()-86400*7));
  86. })
  87. ->field(['id','code','amount','amount_require','begin_use','voucher_status','discount_ratio','event_name','expire_time','max_deductible_price','server_category_name','mold_type'])
  88. ->append(['voucher_status_text'])
  89. ->order(['id' => 'desc'])
  90. ->select()
  91. ->toArray();
  92. $couponCodes = [];
  93. foreach($data as $k => $v){
  94. if($v['expire_time'] < time()){
  95. $couponCodes[] = $v['code'];
  96. $data[$k]['voucher_status_text'] = '已过期';
  97. }
  98. $data[$k]['begin_use'] = date("Y-m-d H:i:s",$v['begin_use'] );
  99. $data[$k]['expire_time'] = date("Y-m-d H:i:s",$v['expire_time'] );
  100. $data[$k]['amount_require'] = '满'.$v['amount_require'].'可用';
  101. }
  102. if(!empty($couponCodes)){
  103. UserCoupon::whereIn('code',$couponCodes)->update(['voucher_status'=>2]);
  104. }
  105. return $data;
  106. } catch (\Exception $e){
  107. Db::rollback();
  108. self::setError($e->getMessage());
  109. return false;
  110. }
  111. }
  112. /**
  113. * 商品分类优惠卷领取
  114. * @param $params
  115. * @return array|false
  116. */
  117. public static function categoryCouponLists($params)
  118. {
  119. try{
  120. $data = CouponRules::with(['couponCategory'=>function ($query) use ($params) {
  121. $query->where('goods_category_id',$params['goods_category_id']);
  122. }])->where('remaining_count','>',0)
  123. ->where('voucher_status',1)
  124. ->order(['id' => 'desc'])
  125. ->field(['code','amount','amount_require','discount_ratio','server_category_name',
  126. 'event_name','expire_time','max_deductible_price','mold_type',])
  127. ->select();
  128. foreach($data as $k => $v){
  129. $data[$k]['amount_require'] = '满'.$v['amount_require'].'可用';
  130. }
  131. $codes = $data->column('code');
  132. $userCouponCodes = [];
  133. if(!empty($code)){
  134. $userCouponCodes = UserCoupon::where('code',$codes)
  135. ->where('user_id',$params['user_id'])->column('code');
  136. }
  137. $data = $data->whereNotIn('code',$userCouponCodes)->toArray();
  138. return $data;
  139. }catch(\Exception $e){
  140. self::setError($e->getMessage());
  141. return false;
  142. }
  143. }
  144. public static function categoryWithAmountLists($params)
  145. {
  146. try{
  147. $data = UserCoupon::with(['couponCategory'=>function ($query) use ($params) {
  148. $query->where('goods_category_id',$params['goods_category_id']);
  149. }])->where('user_id',$params['user_id'])
  150. ->where('voucher_count','>',0)
  151. ->where('voucher_status',0)
  152. ->where('amount_require','<=',$params['amount'])
  153. ->visible(['id','amount','amount_require','begin_use','discount_ratio','event_name','expire_time','max_deductible_price','server_category_name','mold_type'])
  154. ->select()->toArray();
  155. foreach($data as $k => $v){
  156. $data[$k]['begin_use'] = date("Y-m-d H:i:s",$v['begin_use'] );
  157. $data[$k]['expire_time'] = date("Y-m-d H:i:s",$v['expire_time'] );
  158. $data[$k]['amount_require'] = '满'.$v['amount_require'].'可用';
  159. }
  160. return $data;
  161. } catch(\Exception $e){
  162. self::setError($e->getMessage());
  163. return false;
  164. }
  165. }
  166. }