StoreCouponUserServices.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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(',', $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['title'] = $coupon['coupon_title'];
  163. $coupon['type'] = $coupon['applicable_type'];
  164. $coupon['use_min_price'] = floatval($coupon['use_min_price']);
  165. $coupon['coupon_price'] = floatval($coupon['coupon_price']);
  166. $result[] = $coupon;
  167. }
  168. }
  169. }
  170. return $result;
  171. }
  172. /**
  173. * 下单页面显示可用优惠券
  174. * @param $uid
  175. * @param $cartGroup
  176. * @param $price
  177. * @return array
  178. */
  179. public function getOldUsableCouponList(int $uid, array $cartGroup)
  180. {
  181. $cartPrice = $cateIds = [];
  182. $productId = Arr::getUniqueKey($cartGroup['valid'], 'product_id');
  183. foreach ($cartGroup['valid'] as $value) {
  184. $cartPrice[] = bcmul((string)$value['truePrice'], (string)$value['cart_num'], 2);
  185. }
  186. $maxPrice = count($cartPrice) ? max($cartPrice) : 0;
  187. if ($productId) {
  188. /** @var StoreProductCateServices $productCateServices */
  189. $productCateServices = app()->make(StoreProductCateServices::class);
  190. $cateId = $productCateServices->productIdByCateId($productId);
  191. if ($cateId) {
  192. /** @var StoreCategoryServices $cateServices */
  193. $cateServices = app()->make(StoreCategoryServices::class);
  194. $catePids = $cateServices->cateIdByPid($cateId);
  195. $cateIds = array_merge($cateId, $catePids);
  196. } else {
  197. $cateIds = $cateId;
  198. }
  199. }
  200. $productCouponList = $this->dao->productIdsByCoupon($productId, $uid, (string)$maxPrice);
  201. $cateCouponList = $this->dao->cateIdsByCoupon($cateIds, $uid, (string)$maxPrice);
  202. $list = array_merge($productCouponList, $cateCouponList);
  203. $couponIds = Arr::getUniqueKey($list, 'id');
  204. $sumCartPrice = array_sum($cartPrice);
  205. $list1 = $this->dao->getUserCoupon($couponIds, $uid, (string)$sumCartPrice);
  206. $list = array_merge($list, $list1);
  207. foreach ($list as &$item) {
  208. $item['add_time'] = date('Y/m/d', $item['add_time']);
  209. $item['end_time'] = date('Y/m/d', $item['end_time']);
  210. $item['title'] = $item['coupon_title'];
  211. $item['type'] = $item['applicable_type'] ?? 0;
  212. }
  213. return $list;
  214. }
  215. /**
  216. * 用户领取优惠券
  217. * @param $uid
  218. * @param $issueCouponInfo
  219. * @param string $type
  220. * @return mixed
  221. */
  222. public function addUserCoupon($uid, $issueCouponInfo, $type = 'get')
  223. {
  224. $data = [];
  225. $data['cid'] = $issueCouponInfo['id'];
  226. $data['uid'] = $uid;
  227. $data['coupon_title'] = $issueCouponInfo['title'];
  228. $data['coupon_price'] = $issueCouponInfo['coupon_price'];
  229. $data['use_min_price'] = $issueCouponInfo['use_min_price'];
  230. $data['add_time'] = time();
  231. if ($issueCouponInfo['coupon_time']) {
  232. $data['start_time'] = $data['add_time'];
  233. $data['end_time'] = $data['add_time'] + $issueCouponInfo['coupon_time'] * 86400;
  234. } else {
  235. $data['start_time'] = $issueCouponInfo['start_use_time'];
  236. $data['end_time'] = $issueCouponInfo['end_use_time'];
  237. }
  238. $data['type'] = $type;
  239. return $this->dao->save($data);
  240. }
  241. /**会员领取优惠券
  242. * @param $uid
  243. * @param $issueCouponInfo
  244. * @param string $type
  245. * @return mixed
  246. */
  247. public function addMemberUserCoupon($uid, $issueCouponInfo, $type = 'get')
  248. {
  249. $data = [];
  250. $data['cid'] = $issueCouponInfo['id'];
  251. $data['uid'] = $uid;
  252. $data['coupon_title'] = $issueCouponInfo['title'];
  253. $data['coupon_price'] = $issueCouponInfo['coupon_price'];
  254. $data['use_min_price'] = $issueCouponInfo['use_min_price'];
  255. $data['add_time'] = time();
  256. $data['start_time'] = strtotime(date('Y-m-d 00:00:00', time()));
  257. $data['end_time'] = strtotime(date('Y-m-d 23:59:59', strtotime('+30 day')));
  258. $data['type'] = $type;
  259. return $this->dao->save($data);
  260. }
  261. /**
  262. * 获取用户已领取的优惠卷
  263. * @param int $uid
  264. * @param $type
  265. * @return array
  266. * @throws \think\db\exception\DataNotFoundException
  267. * @throws \think\db\exception\DbException
  268. * @throws \think\db\exception\ModelNotFoundException
  269. */
  270. public function getUserCounpon(int $uid, $type)
  271. {
  272. $where = [];
  273. $where['uid'] = $uid;
  274. switch ($type) {
  275. case 0:
  276. case '':
  277. break;
  278. case 1:
  279. $where['status'] = 0;
  280. break;
  281. case 2:
  282. $where['status'] = 1;
  283. break;
  284. default:
  285. $where['status'] = 1;
  286. break;
  287. }
  288. [$page, $limit] = $this->getPageValue();
  289. $list = $this->dao->getCouponListByOrder($where, 'status ASC,add_time DESC', $page, $limit);
  290. return $list ? $this->tidyCouponList($list) : [];
  291. }
  292. /**
  293. * 格式化优惠券
  294. * @param $couponList
  295. * @return mixed
  296. */
  297. public function tidyCouponList($couponList)
  298. {
  299. $time = time();
  300. foreach ($couponList as &$coupon) {
  301. if ($coupon['status'] == '已使用') {
  302. $coupon['_type'] = 0;
  303. $coupon['_msg'] = '已使用';
  304. $coupon['pc_type'] = 0;
  305. $coupon['pc_msg'] = '已使用';
  306. } else if ($coupon['status'] == '已过期') {
  307. $coupon['is_fail'] = 1;
  308. $coupon['_type'] = 0;
  309. $coupon['_msg'] = '已过期';
  310. $coupon['pc_type'] = 0;
  311. $coupon['pc_msg'] = '已过期';
  312. } else if ($coupon['end_time'] < $time) {
  313. $coupon['is_fail'] = 1;
  314. $coupon['_type'] = 0;
  315. $coupon['_msg'] = '已过期';
  316. $coupon['pc_type'] = 0;
  317. $coupon['pc_msg'] = '已过期';
  318. } else if ($coupon['start_time'] > $time) {
  319. $coupon['_type'] = 0;
  320. $coupon['_msg'] = '未开始';
  321. $coupon['pc_type'] = 1;
  322. $coupon['pc_msg'] = '未开始';
  323. } else {
  324. if ($coupon['start_time'] + 3600 * 24 > $time) {
  325. $coupon['_type'] = 2;
  326. $coupon['_msg'] = '立即使用';
  327. $coupon['pc_type'] = 1;
  328. $coupon['pc_msg'] = '可使用';
  329. } else {
  330. $coupon['_type'] = 1;
  331. $coupon['_msg'] = '立即使用';
  332. $coupon['pc_type'] = 1;
  333. $coupon['pc_msg'] = '可使用';
  334. }
  335. }
  336. $coupon['add_time'] = $coupon['_add_time'] = $coupon['start_time'] ? date('Y/m/d', $coupon['start_time']) : date('Y/m/d', $coupon['add_time']);
  337. $coupon['end_time'] = $coupon['_end_time'] = date('Y/m/d', $coupon['end_time']);
  338. $coupon['use_min_price'] = floatval($coupon['use_min_price']);
  339. $coupon['coupon_price'] = floatval($coupon['coupon_price']);
  340. }
  341. return $couponList;
  342. }
  343. /** 获取会员优惠券列表
  344. * @param array $where
  345. * @return array
  346. * @throws \think\db\exception\DataNotFoundException
  347. * @throws \think\db\exception\DbException
  348. * @throws \think\db\exception\ModelNotFoundException
  349. */
  350. public function getMemberCoupon($uid)
  351. {
  352. if (!$uid) return [];
  353. /** @var StoreCouponIssueServices $couponIssueService */
  354. $couponIssueService = app()->make(StoreCouponIssueServices::class);
  355. $couponWhere['receive_type'] = 4;
  356. $couponInfo = $couponIssueService->getMemberCouponIssueList($couponWhere);
  357. $couponList = [];
  358. if ($couponInfo) {
  359. $couponIds = array_column($couponInfo, 'id');
  360. $couponType = array_column($couponInfo, 'type', 'id');
  361. $couponList = $this->dao->getCouponListByOrder(['uid' => $uid, 'coupon_ids' => $couponIds], 'add_time desc');
  362. if ($couponList) {
  363. foreach ($couponList as $k => $v) {
  364. $couponList[$k]['coupon_type'] = $couponIssueService->_couponType[$couponType[$v['cid']]];
  365. }
  366. }
  367. }
  368. return $couponList ? $this->tidyCouponList($couponList) : [];
  369. }
  370. /**根据月分组看会员发放优惠券情况
  371. * @param array $where
  372. * @return array
  373. * @throws \think\db\exception\DataNotFoundException
  374. * @throws \think\db\exception\DbException
  375. * @throws \think\db\exception\ModelNotFoundException
  376. */
  377. public function memberCouponUserGroupBymonth(array $where)
  378. {
  379. return $this->dao->memberCouponUserGroupBymonth($where);
  380. }
  381. /**会员券失效
  382. * @param $coupon_user_id
  383. * @return bool|mixed
  384. */
  385. public function memberCouponIsFail($coupon_user)
  386. {
  387. if (!$coupon_user) return false;
  388. if ($coupon_user['use_time'] == 0) {
  389. return $this->dao->update($coupon_user['id'], ['is_fail' => 1, 'status' => 2]);
  390. }
  391. }
  392. /**根据id查询会员优惠劵
  393. * @param $id
  394. * @return array|bool|\think\Model|null
  395. * @throws \think\db\exception\DataNotFoundException
  396. * @throws \think\db\exception\DbException
  397. * @throws \think\db\exception\ModelNotFoundException
  398. */
  399. public function getCouponUserOne($id)
  400. {
  401. if (!$id) return false;
  402. return $this->dao->getOne(['id' => $id]);
  403. }
  404. /**
  405. * 根据时间查询用户优惠券
  406. * @param array $where
  407. * @return array|bool|\think\Model|null
  408. * @throws \think\db\exception\DataNotFoundException
  409. * @throws \think\db\exception\DbException
  410. * @throws \think\db\exception\ModelNotFoundException
  411. */
  412. public function getUserCounponByMonth(array $where, string $field = '*')
  413. {
  414. if (!$where) return [];
  415. return $this->dao->getUserCounponByMonth($where, $field);
  416. }
  417. /**
  418. * 检查付费会员是否领取了会员券
  419. * @param $uid
  420. * @param $vipCouponIds
  421. * @return array
  422. * @throws \think\db\exception\DataNotFoundException
  423. * @throws \think\db\exception\DbException
  424. * @throws \think\db\exception\ModelNotFoundException
  425. */
  426. public function checkHave($uid, $vipCouponIds)
  427. {
  428. $list = $this->dao->getVipCouponList($uid);
  429. $have = [];
  430. foreach ($list as $item) {
  431. if ($vipCouponIds && in_array($item['cid'], $vipCouponIds)) {
  432. $have[$item['cid']] = true;
  433. } else {
  434. $have[$item['cid']] = false;
  435. }
  436. }
  437. return $have;
  438. }
  439. }