StoreCouponUserServices.php 16 KB

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