UserCoupon.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 getPageList($user_id, $is_use, $is_expire, $params)
  15. {
  16. return $this->with(['coupon'])->where('user_id', '=', $user_id)
  17. ->where('is_use', '=', $is_use ? 1 : 0)
  18. ->where('is_expire', '=', $is_expire ? 1 : 0)
  19. ->paginate($params);
  20. }
  21. /**
  22. * 获取用户优惠券列表
  23. */
  24. public function getList($user_id, $is_use = 0, $is_expire = 0)
  25. {
  26. return $this->with(['coupon'])->where('user_id', '=', $user_id)
  27. ->where('is_use', '=', $is_use ? 1 : 0)
  28. ->where('is_expire', '=', $is_expire ? 1 : 0)
  29. ->select();
  30. }
  31. /**
  32. * 获取用户优惠券总数量(可用)
  33. */
  34. public function getCount($user_id)
  35. {
  36. return $this->where('user_id', '=', $user_id)
  37. ->where('is_use', '=', 0)
  38. ->where('is_expire', '=', 0)
  39. ->count();
  40. }
  41. /**
  42. * 获取用户优惠券ID集
  43. */
  44. public function getUserCouponIds($user_id)
  45. {
  46. return $this->where('user_id', '=', $user_id)->column('coupon_id');
  47. }
  48. /**
  49. * 领取优惠券
  50. */
  51. public function receive($user, $coupon_id)
  52. {
  53. // 获取优惠券信息
  54. $coupon = Coupon::detail($coupon_id);
  55. // 验证优惠券是否可领取
  56. if (!$this->checkReceive($user, $coupon)) {
  57. return false;
  58. }
  59. // 添加领取记录
  60. return $this->add($user, $coupon);
  61. }
  62. /**
  63. * 批量领取优惠券
  64. */
  65. public function receiveList($user, $coupon_ids)
  66. {
  67. $coupon_arr = json_decode($coupon_ids, true);
  68. $model = new CouponModel();
  69. $list = $model->where('coupon_id', 'in', $coupon_arr)->select();
  70. // 开启事务
  71. $this->startTrans();
  72. try {
  73. $data = [];
  74. foreach ($list as $coupon) {
  75. // 验证优惠券是否可领取
  76. if ($this->checkReceive($user, $coupon)) {
  77. if ($coupon['expire_type'] == 10) {
  78. $start_time = time();
  79. $end_time = $start_time + ($coupon['expire_day'] * 86400);
  80. } else {
  81. $start_time = $coupon['start_time']['value'];
  82. $end_time = $coupon['end_time']['value'];
  83. }
  84. // 整理领取记录
  85. $data[] = [
  86. 'coupon_id' => $coupon['coupon_id'],
  87. 'name' => $coupon['name'],
  88. 'color' => $coupon['color']['value'],
  89. 'coupon_type' => $coupon['coupon_type']['value'],
  90. 'reduce_price' => $coupon['reduce_price'],
  91. 'discount' => $coupon->getData('discount'),
  92. 'min_price' => $coupon['min_price'],
  93. 'expire_type' => $coupon['expire_type'],
  94. 'expire_day' => $coupon['expire_day'],
  95. 'start_time' => $start_time,
  96. 'end_time' => $end_time,
  97. 'apply_range' => $coupon['apply_range'],
  98. 'user_id' => $user['user_id'],
  99. 'pv' => $coupon['pv'],
  100. 'app_id' => self::$app_id
  101. ];
  102. // 更新优惠券领取数量
  103. $coupon->setIncReceiveNum();
  104. }
  105. }
  106. $data && $this->saveAll($data);
  107. $this->commit();
  108. return true;
  109. } catch (\Exception $e) {
  110. $this->error = $e->getMessage();
  111. $this->rollback();
  112. return false;
  113. }
  114. }
  115. /**
  116. * 添加领取记录
  117. */
  118. private function add($user, Coupon $coupon)
  119. {
  120. // 计算有效期
  121. if ($coupon['expire_type'] == 10) {
  122. $start_time = time();
  123. $end_time = $start_time + ($coupon['expire_day'] * 86400);
  124. } else {
  125. $start_time = $coupon['start_time']['value'];
  126. $end_time = $coupon['end_time']['value'];
  127. }
  128. // 整理领取记录
  129. $data = [
  130. 'coupon_id' => $coupon['coupon_id'],
  131. 'name' => $coupon['name'],
  132. 'color' => $coupon['color']['value'],
  133. 'coupon_type' => $coupon['coupon_type']['value'],
  134. 'reduce_price' => $coupon['reduce_price'],
  135. 'discount' => $coupon->getData('discount'),
  136. 'min_price' => $coupon['min_price'],
  137. 'expire_type' => $coupon['expire_type'],
  138. 'expire_day' => $coupon['expire_day'],
  139. 'start_time' => $start_time,
  140. 'end_time' => $end_time,
  141. 'apply_range' => $coupon['apply_range'],
  142. 'user_id' => $user['user_id'],
  143. 'app_id' => self::$app_id
  144. ];
  145. return $this->transaction(function () use ($data, $coupon) {
  146. // 添加领取记录
  147. $status = $this->save($data);
  148. if ($status) {
  149. // 更新优惠券领取数量
  150. $coupon->setIncReceiveNum();
  151. }
  152. return $status;
  153. });
  154. }
  155. /**
  156. * 邀请有礼优惠券奖励
  157. * @param $coupon_ids
  158. * @param $user_id
  159. */
  160. public function addUserCoupon($coupon_ids, $user)
  161. {
  162. $model = new CouponModel();
  163. $list = $model->where('coupon_id', 'in', $coupon_ids)->select();
  164. foreach ($list as $coupon) {
  165. // 计算有效期
  166. if ($coupon['expire_type'] == 10) {
  167. $start_time = time();
  168. $end_time = $start_time + ($coupon['expire_day'] * 86400);
  169. } else {
  170. $start_time = $coupon['start_time']['value'];
  171. $end_time = $coupon['end_time']['value'];
  172. }
  173. // 整理领取记录
  174. $data[] = [
  175. 'coupon_id' => $coupon['coupon_id'],
  176. 'name' => $coupon['name'],
  177. 'color' => $coupon['color']['value'],
  178. 'coupon_type' => $coupon['coupon_type']['value'],
  179. 'reduce_price' => $coupon['reduce_price'],
  180. 'discount' => $coupon->getData('discount'),
  181. 'min_price' => $coupon['min_price'],
  182. 'expire_type' => $coupon['expire_type'],
  183. 'expire_day' => $coupon['expire_day'],
  184. 'start_time' => $start_time,
  185. 'end_time' => $end_time,
  186. 'apply_range' => $coupon['apply_range'],
  187. 'user_id' => $user['user_id'],
  188. 'app_id' => $user['app_id']
  189. ];
  190. }
  191. $this->saveAll($data);
  192. return true;
  193. }
  194. /**
  195. * 礼包购礼优惠券奖励
  196. */
  197. public function addNewUserCoupon($coupon_ids, $user_id, $app_id = null)
  198. {
  199. $model = new CouponModel();
  200. $coupon = json_decode($coupon_ids, true);
  201. foreach ($coupon as $key => $value) {
  202. //查询优惠券信息
  203. $couponInfo = $model->where('coupon_id', '=', $value['coupon_id'])->find();
  204. //计算有效期
  205. if ($couponInfo['expire_type'] == 10) {
  206. $start_time = time();
  207. $end_time = $start_time + ($couponInfo['expire_day'] * 86400);
  208. } else {
  209. $start_time = $couponInfo['start_time']['value'];
  210. $end_time = $couponInfo['end_time']['value'];
  211. }
  212. for ($i = 0; $i < $value['coupon_num']; $i++) {
  213. $data[] = [
  214. 'coupon_id' => $couponInfo['coupon_id'],
  215. 'name' => $couponInfo['name'],
  216. 'color' => $couponInfo['color']['value'],
  217. 'coupon_type' => $couponInfo['coupon_type']['value'],
  218. 'reduce_price' => $couponInfo['reduce_price'],
  219. 'discount' => $couponInfo->getData('discount'),
  220. 'min_price' => $couponInfo['min_price'],
  221. 'expire_type' => $couponInfo['expire_type'],
  222. 'expire_day' => $couponInfo['expire_day'],
  223. 'start_time' => $start_time,
  224. 'end_time' => $end_time,
  225. 'apply_range' => $couponInfo['apply_range'],
  226. 'user_id' => $user_id,
  227. 'app_id' => $app_id
  228. ];
  229. }
  230. }
  231. $this->saveAll($data);
  232. return true;
  233. }
  234. /**
  235. * 验证优惠券是否可领取
  236. */
  237. private function checkReceive($user, $coupon)
  238. {
  239. if (!$coupon) {
  240. $this->error = '优惠券不存在';
  241. return false;
  242. }
  243. if (!$coupon->checkReceive()) {
  244. $this->error = $coupon->getError();
  245. return false;
  246. }
  247. // 验证是否已领取
  248. $userCouponIds = $this->getUserCouponIds($user['user_id']);
  249. if (in_array($coupon['coupon_id'], $userCouponIds)) {
  250. $this->error = '该优惠券已领取';
  251. return false;
  252. }
  253. return true;
  254. }
  255. /**
  256. * 订单结算优惠券列表
  257. */
  258. public static function getUserCouponList($user_id, $orderPayPrice)
  259. {
  260. // 新增筛选条件: 最低消费金额
  261. // 获取用户可用的优惠券列表
  262. $list = (new self)->getList($user_id);
  263. $data = [];
  264. foreach ($list as $coupon) {
  265. // 最低消费金额
  266. if ($orderPayPrice < $coupon['min_price']) continue;
  267. // 有效期范围内
  268. if ($coupon['start_time']['value'] > time()) continue;
  269. $key = $coupon['user_coupon_id'];
  270. $data[$key] = [
  271. 'user_coupon_id' => $coupon['user_coupon_id'],
  272. 'name' => $coupon['name'],
  273. 'color' => $coupon['color'],
  274. 'coupon_type' => $coupon['coupon_type'],
  275. 'reduce_price' => $coupon['reduce_price'],
  276. 'discount' => $coupon['discount'],
  277. 'min_price' => $coupon['min_price'],
  278. 'expire_type' => $coupon['expire_type'],
  279. 'start_time' => $coupon['start_time'],
  280. 'end_time' => $coupon['end_time'],
  281. 'expire_day' => $coupon['expire_day'],
  282. 'free_limit' => $coupon['coupon']['free_limit'],
  283. 'apply_range' => $coupon['coupon']['apply_range'],
  284. 'product_ids' => $coupon['coupon']['product_ids'],
  285. 'category_ids' => $coupon['coupon']['category_ids'],
  286. ];
  287. // 计算打折金额
  288. if ($coupon['coupon_type']['value'] == 20) {
  289. $reducePrice = helper::bcmul($orderPayPrice, $coupon['discount'] / 10);
  290. $data[$key]['reduced_price'] = bcsub($orderPayPrice, $reducePrice, 2);
  291. } else
  292. $data[$key]['reduced_price'] = $coupon['reduce_price'];
  293. }
  294. // 根据折扣金额排序并返回
  295. return array_sort($data, 'reduced_price', true);
  296. }
  297. /**
  298. * @param $user_id int 用户id
  299. * @param $days int 连续签到天数
  300. * @param $sign_conf array 签到配置
  301. * @return int
  302. */
  303. public function setCoupon($user_id, $days, $sign_conf)
  304. {
  305. $coupon_num = 0;
  306. $arr = array_column($sign_conf['reward_data'], 'day');
  307. if (in_array($days, $arr)) {
  308. $key = array_search($days, $arr);
  309. if ($sign_conf['reward_data'][$key]['is_coupon'] == 'true') {
  310. $coupon = $sign_conf['reward_data'][$key]['coupon'];
  311. $coupon_arr = array_column($coupon, 'coupon_id');
  312. $coupon_str = implode(',', $coupon_arr);
  313. $model = new CouponModel();
  314. $res = $model->getWhereData($coupon_str)->toArray();
  315. $res_arr = array_column($res, 'coupon_id');
  316. foreach ($coupon as $key => $val) {
  317. $j = array_search($coupon[$key]['coupon_id'], $res_arr);
  318. for ($i = 0; $i < $coupon[$key]['num']; $i++) {
  319. $coupon_num = $coupon[$key]['num'];
  320. $result[] = [
  321. 'coupon_id' => $res[$j]['coupon_id'],
  322. 'name' => $res[$j]['name'],
  323. 'color' => $res[$j]['color']['value'],
  324. 'coupon_type' => $res[$j]['coupon_type']['value'],
  325. 'reduce_price' => $res[$j]['reduce_price'],
  326. 'discount' => $res[$j]['discount'],
  327. 'min_price' => $res[$j]['min_price'],
  328. 'expire_type' => $res[$j]['expire_type'],
  329. 'expire_day' => $res[$j]['expire_day'],
  330. 'start_time' => $res[$j]['start_time']['value'],
  331. 'end_time' => $res[$j]['end_time']['value'],
  332. 'apply_range' => $res[$j]['apply_range'],
  333. 'user_id' => $user_id,
  334. 'app_id' => self::$app_id,
  335. ];
  336. }
  337. }
  338. self::saveAll($result);
  339. }
  340. }
  341. return $coupon_num;
  342. }
  343. }