UserCouponLogic.php 7.0 KB

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