StoreCouponIssueServices.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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\StoreCouponIssueDao;
  15. use app\services\order\StoreCartServices;
  16. use app\services\product\product\StoreCategoryServices;
  17. use app\services\product\product\StoreProductServices;
  18. use app\services\user\member\MemberCardServices;
  19. use app\services\user\member\MemberRightServices;
  20. use app\services\user\UserServices;
  21. use crmeb\exceptions\AdminException;
  22. use crmeb\exceptions\ApiException;
  23. use crmeb\services\FormBuilder;
  24. /**
  25. *
  26. * Class StoreCouponIssueServices
  27. * @package app\services\coupon
  28. * @method getUserIssuePrice(string $price) 获取金大于额的优惠卷金额
  29. * @method getCouponInfo($id)
  30. * @method getColumn(array $where, string $field, ?string $key)
  31. * @method productCouponList(array $where, string $field)
  32. * @method checkProductCoupon($product_id)
  33. */
  34. class StoreCouponIssueServices extends BaseServices
  35. {
  36. public $_couponType = [0 => "通用券", 1 => "品类券", 2 => '商品券'];
  37. /**
  38. * StoreCouponIssueServices constructor.
  39. * @param StoreCouponIssueDao $dao
  40. */
  41. public function __construct(StoreCouponIssueDao $dao)
  42. {
  43. $this->dao = $dao;
  44. }
  45. /**
  46. * 获取已发布列表
  47. * @param array $where
  48. * @return array
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\DbException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. */
  53. public function getCouponIssueList(array $where)
  54. {
  55. [$page, $limit] = $this->getPageValue();
  56. $where['is_del'] = 0;
  57. $list = $this->dao->getList($where, $page, $limit);
  58. foreach ($list as &$item) {
  59. $item['use_time'] = date('Y-m-d', $item['start_use_time']) . ' ~ ' . date('Y-m-d', $item['end_use_time']);
  60. }
  61. $count = $this->dao->count($where);
  62. return compact('list', 'count');
  63. }
  64. /**
  65. * 获取会员优惠券列表
  66. * @param array $where
  67. * @return array
  68. * @throws \think\db\exception\DataNotFoundException
  69. * @throws \think\db\exception\DbException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. */
  72. public function getMemberCouponIssueList(array $where)
  73. {
  74. return $this->dao->getApiIssueList($where);
  75. }
  76. /**
  77. * 新增优惠券
  78. * @param $data
  79. * @return bool
  80. */
  81. public function saveCoupon($data)
  82. {
  83. if ($data['start_time'] && $data['start_use_time']) {
  84. if ($data['start_use_time'] < $data['start_time']) {
  85. throw new AdminException(400513);
  86. }
  87. }
  88. if (!in_array((int)$data['receive_type'], [1, 2, 3, 4])) {
  89. throw new AdminException(400758);
  90. }
  91. if (!in_array((int)$data['is_permanent'], [0, 1])) {
  92. throw new AdminException(400758);
  93. }
  94. if (empty($data['coupon_title'])) {
  95. throw new AdminException(400759);
  96. }
  97. if ($data['end_time'] && $data['end_use_time']) {
  98. if ($data['end_use_time'] < $data['end_time']) {
  99. throw new AdminException(400514);
  100. }
  101. }
  102. $data['start_use_time'] = strtotime((string)$data['start_use_time']);
  103. $data['end_use_time'] = strtotime((string)$data['end_use_time']);
  104. $data['start_time'] = strtotime((string)$data['start_time']);
  105. $data['end_time'] = strtotime((string)$data['end_time']);
  106. $data['title'] = $data['coupon_title'];
  107. $data['remain_count'] = $data['total_count'];
  108. $data['category_id'] = implode(',', $data['category_id']);
  109. if ($data['receive_type'] == 2 || $data['receive_type'] == 3) {
  110. $data['is_permanent'] = 1;
  111. $data['total_count'] = 0;
  112. }
  113. if ($data['is_permanent'] != 1 && $data['receive_limit'] > $data['total_count']) {
  114. throw new AdminException(500031);
  115. }
  116. $data['add_time'] = time();
  117. $res = $this->dao->save($data);
  118. if (($data['product_id'] !== '' || $data['category_id'] !== '') && $res) {
  119. $couponData = [];
  120. if ($data['product_id'] !== '') {
  121. $productIds = explode(',', $data['product_id']);
  122. foreach ($productIds as $product_id) {
  123. $couponData[] = ['product_id' => $product_id, 'coupon_id' => $res->id];
  124. }
  125. } elseif ($data['category_id'] !== '') {
  126. $categoryIds = explode(',', $data['category_id']);
  127. foreach ($categoryIds as $category_id) {
  128. $couponData[] = ['category_id' => $category_id, 'coupon_id' => $res->id];
  129. }
  130. }
  131. /** @var StoreCouponProductServices $storeCouponProductService */
  132. $storeCouponProductService = app()->make(StoreCouponProductServices::class);
  133. $storeCouponProductService->saveAll($couponData);
  134. }
  135. if (!$res) throw new AdminException(100022);
  136. return (int)$res->id;
  137. }
  138. /**
  139. * 修改状态
  140. * @param int $id
  141. * @return array
  142. * @throws \FormBuilder\Exception\FormBuilderException
  143. */
  144. public function createForm(int $id)
  145. {
  146. $issueInfo = $this->dao->get($id);
  147. if (-1 == $issueInfo['status'] || 1 == $issueInfo['is_del']) throw new AdminException(100007);
  148. $f = [FormBuilder::radio('status', '是否开启', $issueInfo['status'])->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]])];
  149. return create_form('状态修改', $f, $this->url('/marketing/coupon/released/status/' . $id), 'PUT');
  150. }
  151. /**
  152. * 领取记录
  153. * @param int $id
  154. * @return array
  155. */
  156. public function issueLog(int $id)
  157. {
  158. $coupon = $this->dao->get($id);
  159. if (!$coupon) {
  160. throw new AdminException(400515);
  161. }
  162. if ($coupon['receive_type'] != 4) {
  163. /** @var StoreCouponIssueUserServices $storeCouponIssueUserService */
  164. $storeCouponIssueUserService = app()->make(StoreCouponIssueUserServices::class);
  165. return $storeCouponIssueUserService->issueLog(['issue_coupon_id' => $id]);
  166. } else {//会员券
  167. /** @var StoreCouponUserServices $storeCouponUserService */
  168. $storeCouponUserService = app()->make(StoreCouponUserServices::class);
  169. return $storeCouponUserService->issueLog(['cid' => $id]);
  170. }
  171. }
  172. /**
  173. * 关注送优惠券
  174. * @param int $uid
  175. * @return bool
  176. * @throws \think\db\exception\DataNotFoundException
  177. * @throws \think\db\exception\DbException
  178. * @throws \think\db\exception\ModelNotFoundException
  179. */
  180. public function userFirstSubGiveCoupon(int $uid)
  181. {
  182. $couponList = $this->dao->getGiveCoupon(['receive_type' => 2]);
  183. $this->giveUserCoupon($uid, $couponList ?: []);
  184. return true;
  185. }
  186. /**
  187. * 订单金额达到预设金额赠送优惠卷
  188. * @param $uid
  189. * @param $total_price
  190. * @return bool
  191. * @throws \think\db\exception\DataNotFoundException
  192. * @throws \think\db\exception\DbException
  193. * @throws \think\db\exception\ModelNotFoundException
  194. */
  195. public function userTakeOrderGiveCoupon($uid, $total_price)
  196. {
  197. $couponList = $this->dao->getGiveCoupon([['is_full_give', '=', 1], ['full_reduction', '<=', $total_price]]);
  198. $this->giveUserCoupon((int)$uid, $couponList ?: []);
  199. return true;
  200. }
  201. /**
  202. * 下单之后赠送
  203. * @param $uid
  204. * @param $coupon_issue_ids 订单商品关联优惠券ids
  205. * @return array
  206. * @throws \think\db\exception\DataNotFoundException
  207. * @throws \think\db\exception\DbException
  208. * @throws \think\db\exception\ModelNotFoundException
  209. */
  210. public function orderPayGiveCoupon($uid, $coupon_issue_ids)
  211. {
  212. if (!$coupon_issue_ids) return [];
  213. $couponList = $this->dao->getGiveCoupon([['id', 'IN', $coupon_issue_ids]]);
  214. [$couponData, $issueUserData] = $this->giveUserCoupon($uid, $couponList ?: []);
  215. return $couponData;
  216. }
  217. /**
  218. * 发送优惠券
  219. * @param int $uid 发放人id
  220. * @param array $couponList 发送优惠券数据
  221. * @return array[]
  222. */
  223. public function giveUserCoupon(int $uid, array $couponList)
  224. {
  225. $couponData = $issueUserData = [];
  226. if ($uid && $couponList) {
  227. $time = time();
  228. $ids = array_column($couponList, 'id');
  229. /** @var StoreCouponIssueUserServices $issueUser */
  230. $issueUser = app()->make(StoreCouponIssueUserServices::class);
  231. foreach ($couponList as $item) {
  232. $data['cid'] = $item['id'];
  233. $data['uid'] = $uid;
  234. $data['coupon_title'] = $item['title'];
  235. $data['coupon_price'] = $item['coupon_price'];
  236. $data['use_min_price'] = $item['use_min_price'];
  237. if ($item['coupon_time']) {
  238. $data['add_time'] = $time;
  239. $data['end_time'] = $data['add_time'] + $item['coupon_time'] * 86400;
  240. } else {
  241. $data['add_time'] = $item['start_use_time'];
  242. $data['end_time'] = $item['end_use_time'];
  243. }
  244. $data['type'] = 'send';
  245. $issue['uid'] = $uid;
  246. $issue['issue_coupon_id'] = $item['id'];
  247. $issue['add_time'] = $time;
  248. $issueUserData[] = $issue;
  249. $couponData[] = $data;
  250. unset($data);
  251. unset($issue);
  252. }
  253. if ($couponData) {
  254. /** @var StoreCouponUserServices $storeCouponUser */
  255. $storeCouponUser = app()->make(StoreCouponUserServices::class);
  256. if (!$storeCouponUser->saveAll($couponData)) {
  257. throw new AdminException(100030);
  258. }
  259. }
  260. if ($issueUserData) {
  261. if (!$issueUser->saveAll($issueUserData)) {
  262. throw new AdminException(100031);
  263. }
  264. }
  265. }
  266. return [$couponData, $issueUserData];
  267. }
  268. /**
  269. * 获取优惠券列表
  270. * @param int $uid
  271. * @param array $where
  272. * @return array
  273. * @throws \think\db\exception\DataNotFoundException
  274. * @throws \think\db\exception\DbException
  275. * @throws \think\db\exception\ModelNotFoundException
  276. */
  277. public function getIssueCouponList(int $uid, array $where)
  278. {
  279. [$page, $limit] = $this->getPageValue();
  280. $cateId = [];
  281. if ($where['product_id'] == 0) {
  282. if ($where['type'] == -1) { // PC端获取优惠券
  283. $list = $this->dao->getPcIssueCouponList($uid, []);
  284. } else {
  285. $list = $this->dao->getIssueCouponList($uid, (int)$where['type'], 0, $page, $limit);
  286. if (!$list) $list = $this->dao->getIssueCouponList($uid, 1, 0, $page, $limit);
  287. if (!$list) $list = $this->dao->getIssueCouponList($uid, 2, 0, $page, $limit);
  288. }
  289. } else {
  290. /** @var StoreProductServices $storeProductService */
  291. $storeProductService = app()->make(StoreProductServices::class);
  292. /** @var StoreCategoryServices $storeCategoryService */
  293. $storeCategoryService = app()->make(StoreCategoryServices::class);
  294. $cateId = $storeProductService->value(['id' => $where['product_id']], 'cate_id');
  295. $cateId = explode(',', (string)$cateId);
  296. $cateId = array_merge($cateId, $storeCategoryService->cateIdByPid($cateId));
  297. $cateId = array_diff($cateId, [0]);
  298. if ($where['type'] == -1) { // PC端获取优惠券
  299. $list = $this->dao->getPcIssueCouponList($uid, $cateId, $where['product_id']);
  300. } else {
  301. if ($where['type'] == 1) {
  302. $typeId = $cateId;
  303. } elseif ($where['type'] == 2) {
  304. $typeId = $where['product_id'];
  305. } else {
  306. $typeId = 0;
  307. }
  308. $list = $this->dao->getIssueCouponList($uid, (int)$where['type'], $typeId, $page, $limit);
  309. }
  310. }
  311. foreach ($list as &$v) {
  312. $v['coupon_price'] = floatval($v['coupon_price']);
  313. $v['use_min_price'] = floatval($v['use_min_price']);
  314. $v['is_use'] = count($v['used']);
  315. if ($v['end_use_time']) {
  316. $v['start_use_time'] = date('Y/m/d', $v['start_use_time']);
  317. $v['end_use_time'] = date('Y/m/d', $v['end_use_time']);
  318. }
  319. if ($v['start_time']) {
  320. $v['start_time'] = date('Y/m/d', $v['start_time']);
  321. $v['end_time'] = date('Y/m/d', $v['end_time']);
  322. }
  323. }
  324. $data['list'] = $list;
  325. $data['count'] = $this->dao->getIssueCouponCount($where['product_id'], $cateId);
  326. return $data;
  327. }
  328. /**
  329. * 领取优惠券
  330. * @param $id
  331. * @param $user
  332. * @param bool $is_receive
  333. * @throws \think\db\exception\DataNotFoundException
  334. * @throws \think\db\exception\DbException
  335. * @throws \think\db\exception\ModelNotFoundException
  336. */
  337. public function issueUserCoupon($id, $user, bool $is_receive = false)
  338. {
  339. $issueCouponInfo = $this->dao->getInfo((int)$id);
  340. if ($user->is_money_level <= 0 && $issueCouponInfo['receive_type'] == 4) {
  341. throw new ApiException(400097);
  342. }
  343. $uid = $user->uid;
  344. if (!$issueCouponInfo) throw new ApiException(400516);
  345. /** @var StoreCouponIssueUserServices $issueUserService */
  346. $issueUserService = app()->make(StoreCouponIssueUserServices::class);
  347. /** @var StoreCouponUserServices $couponUserService */
  348. $couponUserService = app()->make(StoreCouponUserServices::class);
  349. $this->transaction(function () use ($issueUserService, $uid, $id, $couponUserService, $issueCouponInfo) {
  350. $issueUserService->save(['uid' => $uid, 'issue_coupon_id' => $id, 'add_time' => time()]);
  351. $couponUserService->addUserCoupon($uid, $issueCouponInfo, "send");
  352. });
  353. }
  354. /**
  355. * 会员发放优惠期券
  356. * @param $id
  357. * @param $uid
  358. * @throws \think\db\exception\DataNotFoundException
  359. * @throws \think\db\exception\DbException
  360. * @throws \think\db\exception\ModelNotFoundException
  361. */
  362. public function memberIssueUserCoupon($id, $uid)
  363. {
  364. $issueCouponInfo = $this->dao->getInfo((int)$id);
  365. if ($issueCouponInfo) {
  366. /** @var StoreCouponIssueUserServices $issueUserService */
  367. $issueUserService = app()->make(StoreCouponIssueUserServices::class);
  368. /** @var StoreCouponUserServices $couponUserService */
  369. $couponUserService = app()->make(StoreCouponUserServices::class);
  370. if ($issueCouponInfo->remain_count >= 0 || $issueCouponInfo->is_permanent) {
  371. $this->transaction(function () use ($issueUserService, $uid, $id, $couponUserService, $issueCouponInfo) {
  372. //$issueUserService->save(['uid' => $uid, 'issue_coupon_id' => $id, 'add_time' => time()]);
  373. $couponUserService->addMemberUserCoupon($uid, $issueCouponInfo, "send");
  374. // 如果会员劵需要限制数量时打开
  375. if ($issueCouponInfo['total_count'] > 0) {
  376. $issueCouponInfo['remain_count'] -= 1;
  377. $issueCouponInfo->save();
  378. }
  379. });
  380. }
  381. }
  382. }
  383. /**
  384. * 用户优惠劵列表
  385. * @param int $uid
  386. * @param $types
  387. * @return array
  388. * @throws \think\db\exception\DataNotFoundException
  389. * @throws \think\db\exception\DbException
  390. * @throws \think\db\exception\ModelNotFoundException
  391. */
  392. public function getUserCouponList(int $uid, $types)
  393. {
  394. /** @var UserServices $userServices */
  395. $userServices = app()->make(UserServices::class);
  396. if (!$userServices->getUserInfo($uid)) {
  397. throw new ApiException(100100);
  398. }
  399. /** @var StoreCouponUserServices $storeConponUser */
  400. $storeConponUser = app()->make(StoreCouponUserServices::class);
  401. return $storeConponUser->getUserCounpon($uid, $types);
  402. }
  403. /**
  404. * 后台发送优惠券
  405. * @param $coupon
  406. * @param $user
  407. * @return bool
  408. */
  409. public function setCoupon($coupon, $user)
  410. {
  411. $data = [];
  412. $issueData = [];
  413. /** @var StoreCouponUserServices $storeCouponUser */
  414. $storeCouponUser = app()->make(StoreCouponUserServices::class);
  415. /** @var StoreCouponIssueUserServices $storeCouponIssueUser */
  416. $storeCouponIssueUser = app()->make(StoreCouponIssueUserServices::class);
  417. foreach ($user as $k => $v) {
  418. $data[$k]['cid'] = $coupon['id'];
  419. $data[$k]['uid'] = $v;
  420. $data[$k]['coupon_title'] = $coupon['title'];
  421. $data[$k]['coupon_price'] = $coupon['coupon_price'];
  422. $data[$k]['use_min_price'] = $coupon['use_min_price'];
  423. $data[$k]['add_time'] = time();
  424. if ($coupon['coupon_time']) {
  425. $data[$k]['start_time'] = $data[$k]['add_time'];
  426. $data[$k]['end_time'] = $data[$k]['add_time'] + $coupon['coupon_time'] * 86400;
  427. } else {
  428. $data[$k]['start_time'] = $coupon['start_use_time'];
  429. $data[$k]['end_time'] = $coupon['end_use_time'];
  430. }
  431. $data[$k]['type'] = 'send';
  432. $issueData[$k]['uid'] = $v;
  433. $issueData[$k]['issue_coupon_id'] = $coupon['id'];
  434. $issueData[$k]['add_time'] = time();
  435. }
  436. if (!empty($data)) {
  437. if (!$storeCouponUser->saveAll($data)) {
  438. throw new AdminException(100030);
  439. }
  440. if (!$storeCouponIssueUser->saveAll($issueData)) {
  441. throw new AdminException(100031);
  442. }
  443. return true;
  444. }
  445. }
  446. /**
  447. * 获取下单可使用的优惠券列表
  448. * @param int $uid
  449. * @param $cartId
  450. * @param string $price
  451. * @param bool $new
  452. * @return array
  453. * @throws \Psr\SimpleCache\InvalidArgumentException
  454. * @throws \think\db\exception\DataNotFoundException
  455. * @throws \think\db\exception\DbException
  456. * @throws \think\db\exception\ModelNotFoundException
  457. */
  458. public function beUsableCouponList(int $uid, $cartId, bool $new, int $shippingType = 1)
  459. {
  460. /** @var StoreCartServices $services */
  461. $services = app()->make(StoreCartServices::class);
  462. $cartGroup = $services->getUserProductCartListV1($uid, $cartId, $new, [], $shippingType);
  463. /** @var StoreCouponUserServices $coupServices */
  464. $coupServices = app()->make(StoreCouponUserServices::class);
  465. return $coupServices->getUsableCouponList($uid, $cartGroup);
  466. }
  467. /**
  468. * 获取单个优惠券类型
  469. * @param array $where
  470. * @return mixed
  471. * @throws \think\db\exception\DataNotFoundException
  472. * @throws \think\db\exception\DbException
  473. * @throws \think\db\exception\ModelNotFoundException
  474. */
  475. public function getOne(array $where)
  476. {
  477. if (!$where) throw new AdminException(100100);
  478. return $this->dao->getOne($where);
  479. }
  480. /**
  481. * 俩时间相差月份
  482. * @param $date1
  483. * @param $date2
  484. * @return float|int
  485. */
  486. public function getMonthNum($date1, $date2)
  487. {
  488. $date1_stamp = strtotime($date1);
  489. $date2_stamp = strtotime($date2);
  490. list($date_1['y'], $date_1['m']) = explode("-", date('Y-m', $date1_stamp));
  491. list($date_2['y'], $date_2['m']) = explode("-", date('Y-m', $date2_stamp));
  492. return abs($date_1['y'] - $date_2['y']) * 12 + $date_2['m'] - $date_1['m'];
  493. }
  494. /**
  495. * 给会员发放优惠券
  496. * @param $uid
  497. * @param int $couponId
  498. * @return bool
  499. * @throws \think\db\exception\DataNotFoundException
  500. * @throws \think\db\exception\DbException
  501. * @throws \think\db\exception\ModelNotFoundException
  502. */
  503. public function sendMemberCoupon($uid, $couponId = 0)
  504. {
  505. if (!$uid) return false;
  506. /** @var MemberCardServices $memberCardService */
  507. $memberCardService = app()->make(MemberCardServices::class);
  508. //看付费会员是否开启
  509. $isOpenMember = $memberCardService->isOpenMemberCard();
  510. if (!$isOpenMember) return false;
  511. /** @var UserServices $userService */
  512. $userService = app()->make(UserServices::class);
  513. $userInfo = $userService->getUserInfo((int)$uid);
  514. //看是否会员过期
  515. $checkMember = $userService->offMemberLevel($uid, $userInfo);
  516. if (!$checkMember) return false;
  517. /** @var MemberRightServices $memberRightService */
  518. $memberRightService = app()->make(MemberRightServices::class);
  519. //看是否开启会员送券
  520. $isSendCoupon = $memberRightService->getMemberRightStatus("coupon");
  521. if (!$isSendCoupon) return false;
  522. if ($userInfo && (($userInfo['is_money_level'] > 0) || $userInfo['is_ever_level'] == 1)) {
  523. if ($couponId) {//手动点击领取
  524. $couponWhere['id'] = $couponId;
  525. } else {//主动批量发放
  526. $couponWhere['status'] = 1;
  527. $couponWhere['receive_type'] = 4;
  528. $couponWhere['is_del'] = 0;
  529. }
  530. $couponInfo = $this->getMemberCouponIssueList($couponWhere);
  531. if ($couponInfo) {
  532. /** @var StoreCouponUserServices $couponUserService */
  533. $couponUserService = app()->make(StoreCouponUserServices::class);
  534. $couponIds = array_column($couponInfo, 'id');
  535. $couponUserMonth = $couponUserService->memberCouponUserGroupBymonth(['uid' => $uid, 'couponIds' => $couponIds]);
  536. $getTime = array();
  537. if ($couponUserMonth) {
  538. $getTime = array_column($couponUserMonth, 'num', 'time');
  539. }
  540. // 判断这个月是否领取过,而且领全了
  541. //if (in_array(date('Y-m', time()), $getTime)) return false;
  542. $timeKey = date('Y-m', time());
  543. if (array_key_exists($timeKey, $getTime) && $getTime[$timeKey] == count($couponIds)) return false;
  544. $monthNum = $this->getMonthNum(date('Y-m-d H:i:s', time()), date('Y-m-d H:i:s', $userInfo['overdue_time']));
  545. //判断是否领完所有月份
  546. if (count($getTime) >= $monthNum && (array_key_exists($timeKey, $getTime) && $getTime[$timeKey] == count($couponIds)) && $userInfo['is_ever_level'] != 1 && $monthNum > 0) return false;
  547. //看之前是否手动领取过某一张,领取过就不再领取。
  548. $couponUser = $couponUserService->getUserCounponByMonth(['uid' => $uid, 'cid' => $couponIds], 'id,cid');
  549. if ($couponUser) $couponUser = array_combine(array_column($couponUser, 'cid'), $couponUser);
  550. foreach ($couponInfo as $cv) {
  551. if (!isset($couponUser[$cv['id']])) {
  552. $this->memberIssueUserCoupon($cv['id'], $uid);
  553. }
  554. }
  555. }
  556. }
  557. return true;
  558. }
  559. /**
  560. * 获取今日新增优惠券
  561. * @throws \think\db\exception\DataNotFoundException
  562. * @throws \think\db\exception\DbException
  563. * @throws \think\db\exception\ModelNotFoundException
  564. */
  565. public function getTodayCoupon($uid)
  566. {
  567. $list = $this->dao->getTodayCoupon($uid);
  568. foreach ($list as $key => &$item) {
  569. $item['start_time'] = $item['start_time'] ? date('Y/m/d', $item['start_time']) : 0;
  570. $item['end_time'] = $item['end_time'] ? date('Y/m/d', $item['end_time']) : 0;
  571. $item['coupon_price'] = floatval($item['coupon_price']);
  572. $item['use_min_price'] = floatval($item['use_min_price']);
  573. if (isset($item['used']) && $item['used']) {
  574. unset($list[$key]);
  575. }
  576. }
  577. return array_merge($list);
  578. }
  579. /**
  580. * 获取新人券
  581. * @return array
  582. * @throws \think\db\exception\DataNotFoundException
  583. * @throws \think\db\exception\DbException
  584. * @throws \think\db\exception\ModelNotFoundException
  585. */
  586. public function getNewCoupon()
  587. {
  588. $list = $this->dao->getNewCoupon();
  589. foreach ($list as &$item) {
  590. $item['start_time'] = $item['start_time'] ? date('Y/m/d', $item['start_time']) : 0;
  591. $item['end_time'] = $item['end_time'] ? date('Y/m/d', $item['end_time']) : 0;
  592. $item['coupon_price'] = floatval($item['coupon_price']);
  593. $item['use_min_price'] = floatval($item['use_min_price']);
  594. }
  595. return $list;
  596. }
  597. /**
  598. * 获取列表
  599. * @param array $where
  600. * @return array
  601. * @throws \think\db\exception\DataNotFoundException
  602. * @throws \think\db\exception\DbException
  603. * @throws \think\db\exception\ModelNotFoundException
  604. */
  605. public function getCouponList(array $where)
  606. {
  607. [$page, $limit] = $this->getPageValue();
  608. $where['is_del'] = 0;
  609. $field = 'id, coupon_title, type, coupon_price, use_min_price, receive_type, is_permanent, add_time, start_time, end_time, start_use_time, end_use_time, coupon_time, status, total_count, remain_count';
  610. $list = $this->dao->getList($where, $page, $limit, $field);
  611. $count = $this->dao->count($where);
  612. return compact('list', 'count');
  613. }
  614. }