StoreCouponUserServices.php 17 KB

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