StoreCouponUserServices.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\activity\coupon;
  13. use app\services\BaseServices;
  14. use app\dao\activity\coupon\StoreCouponUserDao;
  15. use app\services\product\product\StoreCategoryServices;
  16. use app\services\product\product\StoreProductCateServices;
  17. use crmeb\utils\Arr;
  18. /**
  19. *
  20. * Class StoreCouponUserServices
  21. * @package app\services\coupon
  22. * @method useCoupon(int $id) 使用优惠券修改优惠券状态
  23. * @method delUserCoupon(array $where)
  24. */
  25. class StoreCouponUserServices extends BaseServices
  26. {
  27. /**
  28. * StoreCouponUserServices constructor.
  29. * @param StoreCouponUserDao $dao
  30. */
  31. public function __construct(StoreCouponUserDao $dao)
  32. {
  33. $this->dao = $dao;
  34. }
  35. /**
  36. * 获取列表
  37. * @param array $where
  38. * @return array
  39. */
  40. public function issueLog(array $where)
  41. {
  42. [$page, $limit] = $this->getPageValue();
  43. $list = $this->dao->getList($where, 'uid,add_time', ['userInfo'], $page, $limit);
  44. foreach ($list as &$item) {
  45. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  46. }
  47. $count = $this->dao->count($where);
  48. return compact('list', 'count');
  49. }
  50. /**
  51. * 获取列表
  52. * @param array $where
  53. * @return array
  54. * @throws \think\db\exception\DataNotFoundException
  55. * @throws \think\db\exception\DbException
  56. * @throws \think\db\exception\ModelNotFoundException
  57. */
  58. public function systemPage(array $where)
  59. {
  60. /** @var StoreCouponUserUserServices $storeCouponUserUserService */
  61. $storeCouponUserUserService = app()->make(StoreCouponUserUserServices::class);
  62. return $storeCouponUserUserService->getList($where);
  63. }
  64. /**
  65. * 获取用户优惠券
  66. * @param int $id
  67. */
  68. public function getUserCouponList(int $id)
  69. {
  70. [$page, $limit] = $this->getPageValue();
  71. $list = $this->dao->getList(['uid' => $id], '*', ['issue'], $page, $limit);
  72. foreach ($list as &$item) {
  73. $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  74. $item['_end_time'] = date('Y-m-d H:i:s', $item['end_time']);
  75. if (!$item['coupon_time']) {
  76. $item['coupon_time'] = bcdiv((string)($item['end_use_time'] - $item['start_use_time']), '86400', 0);
  77. }
  78. }
  79. $count = $this->dao->count(['uid' => $id]);
  80. return compact('list', 'count');
  81. }
  82. /**
  83. * 恢复优惠券
  84. * @param int $id
  85. * @return bool|mixed
  86. */
  87. public function recoverCoupon(int $id)
  88. {
  89. $status = $this->dao->value(['id' => $id], 'status');
  90. if ($status) return $this->dao->update($id, ['status' => 0, 'use_time' => '']);
  91. else return true;
  92. }
  93. /**
  94. * 过期优惠卷失效
  95. */
  96. public function checkInvalidCoupon()
  97. {
  98. $this->dao->update([['end_time', '<', time()], ['status', '=', '0']], ['status' => 2]);
  99. }
  100. /**
  101. * 获取用户有效优惠劵数量
  102. * @param int $uid
  103. * @return int
  104. */
  105. public function getUserValidCouponCount(int $uid)
  106. {
  107. $this->checkInvalidCoupon();
  108. return $this->dao->getCount(['uid' => $uid, 'status' => 0]);
  109. }
  110. /**
  111. * 下单页面显示可用优惠券
  112. * @param $uid
  113. * @param $cartGroup
  114. * @param $price
  115. * @return array
  116. */
  117. public function getUsableCouponList(int $uid, array $cartGroup)
  118. {
  119. $userCoupons = $this->dao->getUserAllCoupon($uid);
  120. $result = [];
  121. if ($userCoupons) {
  122. $cartInfo = $cartGroup['valid'];
  123. foreach ($userCoupons as $coupon) {
  124. $price = 0;
  125. $count = 0;
  126. switch ($coupon['applicable_type']) {
  127. case 0:
  128. case 3:
  129. foreach ($cartInfo as $cart) {
  130. $price += bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2);
  131. $count++;
  132. }
  133. break;
  134. case 1://品类券
  135. /** @var StoreCategoryServices $storeCategoryServices */
  136. $storeCategoryServices = app()->make(StoreCategoryServices::class);
  137. $coupon_category = explode(',', (string)$coupon['category_id']);
  138. $category_ids = $storeCategoryServices->getAllById($coupon_category);
  139. if ($category_ids) {
  140. $cateIds = array_column($category_ids, 'id');
  141. foreach ($cartInfo as $cart) {
  142. if (isset($cart['productInfo']['cate_id']) && array_intersect(explode(',', $cart['productInfo']['cate_id']), $cateIds)) {
  143. $price += bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2);
  144. $count++;
  145. }
  146. }
  147. }
  148. break;
  149. case 2:
  150. foreach ($cartInfo as $cart) {
  151. if (isset($cart['product_id']) && in_array($cart['product_id'], explode(',', $coupon['product_id']))) {
  152. $price += bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2);
  153. $count++;
  154. }
  155. }
  156. break;
  157. }
  158. if ($count && $coupon['use_min_price'] <= $price) {
  159. $coupon['start_time'] = $coupon['start_time'] ? date('Y/m/d', $coupon['start_time']) : date('Y/m/d', $coupon['add_time']);
  160. $coupon['add_time'] = date('Y/m/d', $coupon['add_time']);
  161. $coupon['end_time'] = date('Y/m/d', $coupon['end_time']);
  162. $coupon['start_use_time'] = $coupon['start_use_time'] > 0 ? date('Y/m/d', $coupon['start_use_time']) : 0;
  163. $coupon['end_use_time'] = $coupon['start_use_time'] > 0 ? date('Y/m/d', $coupon['end_use_time']) : 0;
  164. $coupon['title'] = $coupon['coupon_title'];
  165. $coupon['type'] = $coupon['applicable_type'];
  166. $coupon['use_min_price'] = floatval($coupon['use_min_price']);
  167. $coupon['coupon_price'] = floatval($coupon['coupon_price']);
  168. $result[] = $coupon;
  169. }
  170. }
  171. }
  172. return $result;
  173. }
  174. /**
  175. * 下单页面显示可用优惠券
  176. * @param $uid
  177. * @param $cartGroup
  178. * @param $price
  179. * @return array
  180. */
  181. public function getOldUsableCouponList(int $uid, array $cartGroup)
  182. {
  183. $cartPrice = $cateIds = [];
  184. $productId = Arr::getUniqueKey($cartGroup['valid'], 'product_id');
  185. foreach ($cartGroup['valid'] as $value) {
  186. $cartPrice[] = bcmul((string)$value['truePrice'], (string)$value['cart_num'], 2);
  187. }
  188. $maxPrice = count($cartPrice) ? max($cartPrice) : 0;
  189. if ($productId) {
  190. /** @var StoreProductCateServices $productCateServices */
  191. $productCateServices = app()->make(StoreProductCateServices::class);
  192. $cateId = $productCateServices->productIdByCateId($productId);
  193. if ($cateId) {
  194. /** @var StoreCategoryServices $cateServices */
  195. $cateServices = app()->make(StoreCategoryServices::class);
  196. $catePids = $cateServices->cateIdByPid($cateId);
  197. $cateIds = array_merge($cateId, $catePids);
  198. } else {
  199. $cateIds = $cateId;
  200. }
  201. }
  202. $productCouponList = $this->dao->productIdsByCoupon($productId, $uid, (string)$maxPrice);
  203. $cateCouponList = $this->dao->cateIdsByCoupon($cateIds, $uid, (string)$maxPrice);
  204. $list = array_merge($productCouponList, $cateCouponList);
  205. $couponIds = Arr::getUniqueKey($list, 'id');
  206. $sumCartPrice = array_sum($cartPrice);
  207. $list1 = $this->dao->getUserCoupon($couponIds, $uid, (string)$sumCartPrice);
  208. $list = array_merge($list, $list1);
  209. foreach ($list as &$item) {
  210. $item['add_time'] = date('Y/m/d', $item['add_time']);
  211. $item['end_time'] = date('Y/m/d', $item['end_time']);
  212. $item['title'] = $item['coupon_title'];
  213. $item['type'] = $item['applicable_type'] ?? 0;
  214. }
  215. return $list;
  216. }
  217. /**
  218. * 用户领取优惠券
  219. * @param $uid
  220. * @param $issueCouponInfo
  221. * @param string $type
  222. * @return mixed
  223. */
  224. public function addUserCoupon($uid, $issueCouponInfo, $type = 'get')
  225. {
  226. $data = [];
  227. $data['cid'] = $issueCouponInfo['id'];
  228. $data['uid'] = $uid;
  229. $data['coupon_title'] = $issueCouponInfo['title'];
  230. $data['coupon_price'] = $issueCouponInfo['coupon_price'];
  231. $data['use_min_price'] = $issueCouponInfo['use_min_price'];
  232. $data['add_time'] = time();
  233. if ($issueCouponInfo['coupon_time']) {
  234. $data['start_time'] = $data['add_time'];
  235. $data['end_time'] = $data['add_time'] + $issueCouponInfo['coupon_time'] * 86400;
  236. } else {
  237. $data['start_time'] = $issueCouponInfo['start_use_time'];
  238. $data['end_time'] = $issueCouponInfo['end_use_time'];
  239. }
  240. $data['type'] = $type;
  241. return $this->dao->save($data);
  242. }
  243. /**会员领取优惠券
  244. * @param $uid
  245. * @param $issueCouponInfo
  246. * @param string $type
  247. * @return mixed
  248. */
  249. public function addMemberUserCoupon($uid, $issueCouponInfo, $type = 'get')
  250. {
  251. $data = [];
  252. $data['cid'] = $issueCouponInfo['id'];
  253. $data['uid'] = $uid;
  254. $data['coupon_title'] = $issueCouponInfo['title'];
  255. $data['coupon_price'] = $issueCouponInfo['coupon_price'];
  256. $data['use_min_price'] = $issueCouponInfo['use_min_price'];
  257. $data['add_time'] = time();
  258. $data['start_time'] = strtotime(date('Y-m-d 00:00:00', time()));
  259. $data['end_time'] = strtotime(date('Y-m-d 23:59:59', strtotime('+30 day')));
  260. $data['type'] = $type;
  261. return $this->dao->save($data);
  262. }
  263. /**
  264. * 获取用户已领取的优惠卷
  265. * @param int $uid
  266. * @param $type
  267. * @return array
  268. * @throws \think\db\exception\DataNotFoundException
  269. * @throws \think\db\exception\DbException
  270. * @throws \think\db\exception\ModelNotFoundException
  271. */
  272. public function getUserCounpon(int $uid, $type)
  273. {
  274. $where = [];
  275. $where['uid'] = $uid;
  276. switch ($type) {
  277. case 0:
  278. case '':
  279. break;
  280. case 1:
  281. $where['status'] = 0;
  282. break;
  283. case 2:
  284. $where['status'] = 1;
  285. break;
  286. default:
  287. $where['status'] = 1;
  288. break;
  289. }
  290. [$page, $limit] = $this->getPageValue();
  291. $list = $this->dao->getCouponListByOrder($where, 'status ASC,add_time DESC', $page, $limit);
  292. return $list ? $this->tidyCouponList($list) : [];
  293. }
  294. /**
  295. * 格式化优惠券
  296. * @param $couponList
  297. * @return mixed
  298. */
  299. public function tidyCouponList($couponList)
  300. {
  301. $time = time();
  302. foreach ($couponList as &$coupon) {
  303. if ($coupon['status'] == '已使用') {
  304. $coupon['_type'] = 0;
  305. $coupon['_msg'] = '已使用';
  306. $coupon['pc_type'] = 0;
  307. $coupon['pc_msg'] = '已使用';
  308. } else if ($coupon['status'] == '已过期') {
  309. $coupon['is_fail'] = 1;
  310. $coupon['_type'] = 0;
  311. $coupon['_msg'] = '已过期';
  312. $coupon['pc_type'] = 0;
  313. $coupon['pc_msg'] = '已过期';
  314. } else if ($coupon['end_time'] < $time) {
  315. $coupon['is_fail'] = 1;
  316. $coupon['_type'] = 0;
  317. $coupon['_msg'] = '已过期';
  318. $coupon['pc_type'] = 0;
  319. $coupon['pc_msg'] = '已过期';
  320. } else if ($coupon['start_time'] > $time) {
  321. $coupon['_type'] = 0;
  322. $coupon['_msg'] = '未开始';
  323. $coupon['pc_type'] = 1;
  324. $coupon['pc_msg'] = '未开始';
  325. } else {
  326. if ($coupon['start_time'] + 3600 * 24 > $time) {
  327. $coupon['_type'] = 2;
  328. $coupon['_msg'] = '立即使用';
  329. $coupon['pc_type'] = 1;
  330. $coupon['pc_msg'] = '可使用';
  331. } else {
  332. $coupon['_type'] = 1;
  333. $coupon['_msg'] = '立即使用';
  334. $coupon['pc_type'] = 1;
  335. $coupon['pc_msg'] = '可使用';
  336. }
  337. }
  338. $coupon['add_time'] = $coupon['_add_time'] = $coupon['start_time'] ? date('Y/m/d', $coupon['start_time']) : date('Y/m/d', $coupon['add_time']);
  339. $coupon['end_time'] = $coupon['_end_time'] = date('Y/m/d', $coupon['end_time']);
  340. $coupon['use_min_price'] = floatval($coupon['use_min_price']);
  341. $coupon['coupon_price'] = floatval($coupon['coupon_price']);
  342. }
  343. return $couponList;
  344. }
  345. /** 获取会员优惠券列表
  346. * @param array $where
  347. * @return array
  348. * @throws \think\db\exception\DataNotFoundException
  349. * @throws \think\db\exception\DbException
  350. * @throws \think\db\exception\ModelNotFoundException
  351. */
  352. public function getMemberCoupon($uid)
  353. {
  354. if (!$uid) return [];
  355. /** @var StoreCouponIssueServices $couponIssueService */
  356. $couponIssueService = app()->make(StoreCouponIssueServices::class);
  357. $couponWhere['receive_type'] = 4;
  358. $couponInfo = $couponIssueService->getMemberCouponIssueList($couponWhere);
  359. $couponList = [];
  360. if ($couponInfo) {
  361. $couponIds = array_column($couponInfo, 'id');
  362. $couponType = array_column($couponInfo, 'type', 'id');
  363. $couponList = $this->dao->getCouponListByOrder(['uid' => $uid, 'coupon_ids' => $couponIds], 'add_time desc');
  364. if ($couponList) {
  365. foreach ($couponList as $k => $v) {
  366. $couponList[$k]['coupon_type'] = $couponIssueService->_couponType[$couponType[$v['cid']]];
  367. }
  368. }
  369. }
  370. return $couponList ? $this->tidyCouponList($couponList) : [];
  371. }
  372. /**根据月分组看会员发放优惠券情况
  373. * @param array $where
  374. * @return array
  375. * @throws \think\db\exception\DataNotFoundException
  376. * @throws \think\db\exception\DbException
  377. * @throws \think\db\exception\ModelNotFoundException
  378. */
  379. public function memberCouponUserGroupBymonth(array $where)
  380. {
  381. return $this->dao->memberCouponUserGroupBymonth($where);
  382. }
  383. /**会员券失效
  384. * @param $coupon_user_id
  385. * @return bool|mixed
  386. */
  387. public function memberCouponIsFail($coupon_user)
  388. {
  389. if (!$coupon_user) return false;
  390. if ($coupon_user['use_time'] == 0) {
  391. return $this->dao->update($coupon_user['id'], ['is_fail' => 1, 'status' => 2]);
  392. }
  393. }
  394. /**根据id查询会员优惠劵
  395. * @param $id
  396. * @return array|bool|\think\Model|null
  397. * @throws \think\db\exception\DataNotFoundException
  398. * @throws \think\db\exception\DbException
  399. * @throws \think\db\exception\ModelNotFoundException
  400. */
  401. public function getCouponUserOne($id)
  402. {
  403. if (!$id) return false;
  404. return $this->dao->getOne(['id' => $id]);
  405. }
  406. /**
  407. * 根据时间查询用户优惠券
  408. * @param array $where
  409. * @return array|bool|\think\Model|null
  410. * @throws \think\db\exception\DataNotFoundException
  411. * @throws \think\db\exception\DbException
  412. * @throws \think\db\exception\ModelNotFoundException
  413. */
  414. public function getUserCounponByMonth(array $where, string $field = '*')
  415. {
  416. if (!$where) return [];
  417. return $this->dao->getUserCounponByMonth($where, $field);
  418. }
  419. /**
  420. * 检查付费会员是否领取了会员券
  421. * @param $uid
  422. * @param $vipCouponIds
  423. * @return array
  424. * @throws \think\db\exception\DataNotFoundException
  425. * @throws \think\db\exception\DbException
  426. * @throws \think\db\exception\ModelNotFoundException
  427. */
  428. public function checkHave($uid, $vipCouponIds)
  429. {
  430. $list = $this->dao->getVipCouponList($uid);
  431. $have = [];
  432. foreach ($list as $item) {
  433. if ($vipCouponIds && in_array($item['cid'], $vipCouponIds)) {
  434. $have[$item['cid']] = true;
  435. } else {
  436. $have[$item['cid']] = false;
  437. }
  438. }
  439. return $have;
  440. }
  441. }