UserCoupon.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. namespace app\api\model\plus\coupon;
  3. use app\common\library\helper;
  4. use app\common\model\plus\coupon\UserCoupon as UserCouponModel;
  5. use app\api\model\plus\coupon\Coupon as CouponModel;
  6. /**
  7. * 用户优惠券模型
  8. */
  9. class UserCoupon extends UserCouponModel
  10. {
  11. /**
  12. * 获取用户优惠券列表
  13. */
  14. public function getList($user_id, $shop_supplier_id = -1, $is_use = false, $is_expire = false)
  15. {
  16. $model = $this;
  17. if($shop_supplier_id != -1){
  18. $model = $model->where('shop_supplier_id', '=', $shop_supplier_id);
  19. }
  20. return $model->with(['supplier', 'coupon'])->where('user_id', '=', $user_id)
  21. ->where('is_use', '=', $is_use ? 1 : 0)
  22. ->where('is_expire', '=', $is_expire ? 1 : 0)
  23. ->select();
  24. }
  25. /**
  26. * 获取用户优惠券总数量(可用)
  27. */
  28. public function getCount($user_id)
  29. {
  30. return $this->where('user_id', '=', $user_id)
  31. ->where('is_use', '=', 0)
  32. ->where('is_expire', '=', 0)
  33. ->count();
  34. }
  35. /**
  36. * 获取用户优惠券ID集
  37. */
  38. public function getUserCouponIds($user_id)
  39. {
  40. return $this->where('user_id', '=', $user_id)->column('coupon_id');
  41. }
  42. /**
  43. * 领取优惠券
  44. */
  45. public function receive($user, $coupon_id)
  46. {
  47. // 获取优惠券信息
  48. $coupon = Coupon::detail($coupon_id);
  49. // 验证优惠券是否可领取
  50. if (!$this->checkReceive($user, $coupon)) {
  51. return false;
  52. }
  53. // 添加领取记录
  54. return $this->add($user, $coupon);
  55. }
  56. /**
  57. * 批量领取优惠券
  58. */
  59. public function receiveList($user, $coupon_ids)
  60. {
  61. $coupon_arr = json_decode($coupon_ids, true);
  62. foreach($coupon_arr as $coupon_id){
  63. try{
  64. $this->receive($user, $coupon_id);
  65. }catch (\Exception $e){
  66. }
  67. }
  68. return true;
  69. }
  70. /**
  71. * 添加领取记录
  72. */
  73. private function add($user, Coupon $coupon)
  74. {
  75. // 计算有效期
  76. if ($coupon['expire_type'] == 10) {
  77. $start_time = time();
  78. $end_time = $start_time + ($coupon['expire_day'] * 86400);
  79. } else {
  80. $start_time = $coupon['start_time']['value'];
  81. $end_time = $coupon['end_time']['value'];
  82. }
  83. // 整理领取记录
  84. $data = [
  85. 'coupon_id' => $coupon['coupon_id'],
  86. 'name' => $coupon['name'],
  87. 'color' => $coupon['color']['value'],
  88. 'coupon_type' => $coupon['coupon_type']['value'],
  89. 'reduce_price' => $coupon['reduce_price'],
  90. 'discount' => $coupon->getData('discount'),
  91. 'min_price' => $coupon['min_price'],
  92. 'expire_type' => $coupon['expire_type'],
  93. 'expire_day' => $coupon['expire_day'],
  94. 'start_time' => $start_time,
  95. 'end_time' => $end_time,
  96. 'apply_range' => $coupon['apply_range'],
  97. 'user_id' => $user['user_id'],
  98. 'app_id' => self::$app_id,
  99. 'shop_supplier_id' => $coupon['shop_supplier_id']
  100. ];
  101. return $this->transaction(function () use ($data, $coupon) {
  102. // 添加领取记录
  103. $status = $this->save($data);
  104. if ($status) {
  105. // 更新优惠券领取数量
  106. $coupon->setIncReceiveNum();
  107. }
  108. return $status;
  109. });
  110. }
  111. /**
  112. * 邀请有礼优惠券奖励
  113. * @param $coupon_ids
  114. * @param $user_id
  115. */
  116. public function addUserCoupon($coupon_ids, $user_id)
  117. {
  118. $model = new CouponModel();
  119. $list = $model->where('coupon_id', 'in', $coupon_ids)->select();
  120. $data = [];
  121. foreach ($list as $coupon) {
  122. // 计算有效期
  123. if ($coupon['expire_type'] == 10) {
  124. $start_time = time();
  125. $end_time = $start_time + ($coupon['expire_day'] * 86400);
  126. } else {
  127. $start_time = $coupon['start_time']['value'];
  128. $end_time = $coupon['end_time']['value'];
  129. }
  130. // 整理领取记录
  131. $data[] = [
  132. 'coupon_id' => $coupon['coupon_id'],
  133. 'name' => $coupon['name'],
  134. 'color' => $coupon['color']['value'],
  135. 'coupon_type' => $coupon['coupon_type']['value'],
  136. 'reduce_price' => $coupon['reduce_price'],
  137. 'discount' => $coupon->getData('discount'),
  138. 'min_price' => $coupon['min_price'],
  139. 'expire_type' => $coupon['expire_type'],
  140. 'expire_day' => $coupon['expire_day'],
  141. 'start_time' => $start_time,
  142. 'end_time' => $end_time,
  143. 'apply_range' => $coupon['apply_range'],
  144. 'user_id' => $user_id,
  145. 'app_id' => self::$app_id,
  146. 'shop_supplier_id' => $coupon['shop_supplier_id']
  147. ];
  148. }
  149. $this->saveAll($data);
  150. return true;
  151. }
  152. /**
  153. * 验证优惠券是否可领取
  154. */
  155. private function checkReceive($user, $coupon)
  156. {
  157. if (!$coupon) {
  158. $this->error = '优惠券不存在';
  159. return false;
  160. }
  161. if (!$coupon->checkReceive()) {
  162. $this->error = $coupon->getError();
  163. return false;
  164. }
  165. // 验证是否已领取
  166. $userCouponIds = $this->getUserCouponIds($user['user_id']);
  167. if (in_array($coupon['coupon_id'], $userCouponIds)) {
  168. $this->error = '该优惠券已领取';
  169. return false;
  170. }
  171. return true;
  172. }
  173. /**
  174. * 订单结算优惠券列表
  175. */
  176. public static function getUserCouponList($user_id, $orderPayPrice, $shop_supplier_id)
  177. {
  178. // 新增筛选条件: 最低消费金额
  179. // 获取用户可用的优惠券列表
  180. $list = (new self)->getList($user_id, $shop_supplier_id);
  181. $data = [];
  182. foreach ($list as $coupon) {
  183. // 最低消费金额
  184. if ($orderPayPrice < $coupon['min_price']) continue;
  185. // 有效期范围内
  186. if ($coupon['start_time']['value'] > time()) continue;
  187. if($coupon['expire_day']>0 && $coupon['end_time']['value'] <= time()) continue;
  188. $key = $coupon['user_coupon_id'];
  189. $data[$key] = [
  190. 'user_coupon_id' => $coupon['user_coupon_id'],
  191. 'name' => $coupon['name'],
  192. 'color' => $coupon['color'],
  193. 'coupon_type' => $coupon['coupon_type'],
  194. 'reduce_price' => $coupon['reduce_price'],
  195. 'discount' => $coupon['discount'],
  196. 'min_price' => $coupon['min_price'],
  197. 'expire_type' => $coupon['expire_type'],
  198. 'start_time' => $coupon['start_time'],
  199. 'end_time' => $coupon['end_time'],
  200. 'expire_day' => $coupon['expire_day'],
  201. 'free_limit' => $coupon['coupon']['free_limit'],
  202. 'apply_range' => $coupon['coupon']['apply_range'],
  203. 'product_ids' => $coupon['coupon']['product_ids'],
  204. ];
  205. // 计算打折金额
  206. if ($coupon['coupon_type']['value'] == 20) {
  207. $reducePrice = helper::bcmul($orderPayPrice, $coupon['discount'] / 10);
  208. $data[$key]['reduced_price'] = bcsub($orderPayPrice, $reducePrice, 2);
  209. } else
  210. $data[$key]['reduced_price'] = $coupon['reduce_price'];
  211. }
  212. // 根据折扣金额排序并返回
  213. return array_sort($data, 'reduced_price', true);
  214. }
  215. /**
  216. * @param $user_id int 用户id
  217. * @param $days int 连续签到天数
  218. * @param $sign_conf array 签到配置
  219. * @return int
  220. */
  221. public function setCoupon($user_id, $days, $sign_conf)
  222. {
  223. $coupon_num = 0;
  224. $arr = array_column($sign_conf['reward_data'], 'day');
  225. if (in_array($days, $arr)) {
  226. $key = array_search($days, $arr);
  227. if ($sign_conf['reward_data'][$key]['is_coupon'] == 'true') {
  228. $coupon = $sign_conf['reward_data'][$key]['coupon'];
  229. $coupon_arr = array_column($coupon, 'coupon_id');
  230. $coupon_str = implode(',', $coupon_arr);
  231. $model = new CouponModel();
  232. $res = $model->getWhereData($coupon_str)->toArray();
  233. $res_arr = array_column($res, 'coupon_id');
  234. $result = [];
  235. foreach ($coupon as $key => $val) {
  236. $j = array_search($coupon[$key]['coupon_id'], $res_arr);
  237. for ($i = 0; $i < $coupon[$key]['num']; $i++) {
  238. $coupon_num = $coupon[$key]['num'];
  239. $result[] = [
  240. 'coupon_id' => $res[$j]['coupon_id'],
  241. 'name' => $res[$j]['name'],
  242. 'color' => $res[$j]['color']['value'],
  243. 'coupon_type' => $res[$j]['coupon_type']['value'],
  244. 'reduce_price' => $res[$j]['reduce_price'],
  245. 'discount' => $res[$j]['discount'],
  246. 'min_price' => $res[$j]['min_price'],
  247. 'expire_type' => $res[$j]['expire_type'],
  248. 'expire_day' => $res[$j]['expire_day'],
  249. 'start_time' => $res[$j]['start_time']['value'],
  250. 'end_time' => $res[$j]['end_time']['value'],
  251. 'apply_range' => $res[$j]['apply_range'],
  252. 'user_id' => $user_id,
  253. 'app_id' => self::$app_id,
  254. ];
  255. }
  256. }
  257. self::saveAll($result);
  258. }
  259. }
  260. return $coupon_num;
  261. }
  262. }