StoreCouponIssueServices.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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. $uid = $user->uid;
  341. if (!$issueCouponInfo) throw new ApiException(400516);
  342. /** @var StoreCouponIssueUserServices $issueUserService */
  343. $issueUserService = app()->make(StoreCouponIssueUserServices::class);
  344. /** @var StoreCouponUserServices $couponUserService */
  345. $couponUserService = app()->make(StoreCouponUserServices::class);
  346. $this->transaction(function () use ($issueUserService, $uid, $id, $couponUserService, $issueCouponInfo) {
  347. $issueUserService->save(['uid' => $uid, 'issue_coupon_id' => $id, 'add_time' => time()]);
  348. $couponUserService->addUserCoupon($uid, $issueCouponInfo, "send");
  349. });
  350. }
  351. /**
  352. * 会员发放优惠期券
  353. * @param $id
  354. * @param $uid
  355. * @throws \think\db\exception\DataNotFoundException
  356. * @throws \think\db\exception\DbException
  357. * @throws \think\db\exception\ModelNotFoundException
  358. */
  359. public function memberIssueUserCoupon($id, $uid)
  360. {
  361. $issueCouponInfo = $this->dao->getInfo((int)$id);
  362. if ($issueCouponInfo) {
  363. /** @var StoreCouponIssueUserServices $issueUserService */
  364. $issueUserService = app()->make(StoreCouponIssueUserServices::class);
  365. /** @var StoreCouponUserServices $couponUserService */
  366. $couponUserService = app()->make(StoreCouponUserServices::class);
  367. if ($issueCouponInfo->remain_count >= 0 || $issueCouponInfo->is_permanent) {
  368. $this->transaction(function () use ($issueUserService, $uid, $id, $couponUserService, $issueCouponInfo) {
  369. //$issueUserService->save(['uid' => $uid, 'issue_coupon_id' => $id, 'add_time' => time()]);
  370. $couponUserService->addMemberUserCoupon($uid, $issueCouponInfo, "send");
  371. // 如果会员劵需要限制数量时打开
  372. if ($issueCouponInfo['total_count'] > 0) {
  373. $issueCouponInfo['remain_count'] -= 1;
  374. $issueCouponInfo->save();
  375. }
  376. });
  377. }
  378. }
  379. }
  380. /**
  381. * 用户优惠劵列表
  382. * @param int $uid
  383. * @param $types
  384. * @return array
  385. * @throws \think\db\exception\DataNotFoundException
  386. * @throws \think\db\exception\DbException
  387. * @throws \think\db\exception\ModelNotFoundException
  388. */
  389. public function getUserCouponList(int $uid, $types)
  390. {
  391. /** @var UserServices $userServices */
  392. $userServices = app()->make(UserServices::class);
  393. if (!$userServices->getUserInfo($uid)) {
  394. throw new ApiException(100100);
  395. }
  396. /** @var StoreCouponUserServices $storeConponUser */
  397. $storeConponUser = app()->make(StoreCouponUserServices::class);
  398. return $storeConponUser->getUserCounpon($uid, $types);
  399. }
  400. /**
  401. * 后台发送优惠券
  402. * @param $coupon
  403. * @param $user
  404. * @return bool
  405. */
  406. public function setCoupon($coupon, $user)
  407. {
  408. $data = [];
  409. $issueData = [];
  410. /** @var StoreCouponUserServices $storeCouponUser */
  411. $storeCouponUser = app()->make(StoreCouponUserServices::class);
  412. /** @var StoreCouponIssueUserServices $storeCouponIssueUser */
  413. $storeCouponIssueUser = app()->make(StoreCouponIssueUserServices::class);
  414. foreach ($user as $k => $v) {
  415. $data[$k]['cid'] = $coupon['id'];
  416. $data[$k]['uid'] = $v;
  417. $data[$k]['coupon_title'] = $coupon['title'];
  418. $data[$k]['coupon_price'] = $coupon['coupon_price'];
  419. $data[$k]['use_min_price'] = $coupon['use_min_price'];
  420. $data[$k]['add_time'] = time();
  421. if ($coupon['coupon_time']) {
  422. $data[$k]['start_time'] = $data[$k]['add_time'];
  423. $data[$k]['end_time'] = $data[$k]['add_time'] + $coupon['coupon_time'] * 86400;
  424. } else {
  425. $data[$k]['start_time'] = $coupon['start_use_time'];
  426. $data[$k]['end_time'] = $coupon['end_use_time'];
  427. }
  428. $data[$k]['type'] = 'send';
  429. $issueData[$k]['uid'] = $v;
  430. $issueData[$k]['issue_coupon_id'] = $coupon['id'];
  431. $issueData[$k]['add_time'] = time();
  432. }
  433. if (!empty($data)) {
  434. if (!$storeCouponUser->saveAll($data)) {
  435. throw new AdminException(100030);
  436. }
  437. if (!$storeCouponIssueUser->saveAll($issueData)) {
  438. throw new AdminException(100031);
  439. }
  440. return true;
  441. }
  442. }
  443. /**
  444. * 获取下单可使用的优惠券列表
  445. * @param int $uid
  446. * @param $cartId
  447. * @param string $price
  448. * @param bool $new
  449. * @return array
  450. * @throws \Psr\SimpleCache\InvalidArgumentException
  451. * @throws \think\db\exception\DataNotFoundException
  452. * @throws \think\db\exception\DbException
  453. * @throws \think\db\exception\ModelNotFoundException
  454. */
  455. public function beUsableCouponList(int $uid, $cartId, bool $new, int $shippingType = 1)
  456. {
  457. /** @var StoreCartServices $services */
  458. $services = app()->make(StoreCartServices::class);
  459. $cartGroup = $services->getUserProductCartListV1($uid, $cartId, $new, [], $shippingType);
  460. /** @var StoreCouponUserServices $coupServices */
  461. $coupServices = app()->make(StoreCouponUserServices::class);
  462. return $coupServices->getUsableCouponList($uid, $cartGroup);
  463. }
  464. /**
  465. * 获取单个优惠券类型
  466. * @param array $where
  467. * @return mixed
  468. * @throws \think\db\exception\DataNotFoundException
  469. * @throws \think\db\exception\DbException
  470. * @throws \think\db\exception\ModelNotFoundException
  471. */
  472. public function getOne(array $where)
  473. {
  474. if (!$where) throw new AdminException(100100);
  475. return $this->dao->getOne($where);
  476. }
  477. /**
  478. * 俩时间相差月份
  479. * @param $date1
  480. * @param $date2
  481. * @return float|int
  482. */
  483. public function getMonthNum($date1, $date2)
  484. {
  485. $date1_stamp = strtotime($date1);
  486. $date2_stamp = strtotime($date2);
  487. list($date_1['y'], $date_1['m']) = explode("-", date('Y-m', $date1_stamp));
  488. list($date_2['y'], $date_2['m']) = explode("-", date('Y-m', $date2_stamp));
  489. return abs($date_1['y'] - $date_2['y']) * 12 + $date_2['m'] - $date_1['m'];
  490. }
  491. /**
  492. * 给会员发放优惠券
  493. * @param $uid
  494. * @param int $couponId
  495. * @return bool
  496. * @throws \think\db\exception\DataNotFoundException
  497. * @throws \think\db\exception\DbException
  498. * @throws \think\db\exception\ModelNotFoundException
  499. */
  500. public function sendMemberCoupon($uid, $couponId = 0)
  501. {
  502. if (!$uid) return false;
  503. /** @var MemberCardServices $memberCardService */
  504. $memberCardService = app()->make(MemberCardServices::class);
  505. //看付费会员是否开启
  506. $isOpenMember = $memberCardService->isOpenMemberCard();
  507. if (!$isOpenMember) return false;
  508. /** @var UserServices $userService */
  509. $userService = app()->make(UserServices::class);
  510. $userInfo = $userService->getUserInfo((int)$uid);
  511. //看是否会员过期
  512. $checkMember = $userService->offMemberLevel($uid, $userInfo);
  513. if (!$checkMember) return false;
  514. /** @var MemberRightServices $memberRightService */
  515. $memberRightService = app()->make(MemberRightServices::class);
  516. //看是否开启会员送券
  517. $isSendCoupon = $memberRightService->getMemberRightStatus("coupon");
  518. if (!$isSendCoupon) return false;
  519. if ($userInfo && (($userInfo['is_money_level'] > 0) || $userInfo['is_ever_level'] == 1)) {
  520. if ($couponId) {//手动点击领取
  521. $couponWhere['id'] = $couponId;
  522. } else {//主动批量发放
  523. $couponWhere['status'] = 1;
  524. $couponWhere['receive_type'] = 4;
  525. $couponWhere['is_del'] = 0;
  526. }
  527. $couponInfo = $this->getMemberCouponIssueList($couponWhere);
  528. if ($couponInfo) {
  529. /** @var StoreCouponUserServices $couponUserService */
  530. $couponUserService = app()->make(StoreCouponUserServices::class);
  531. $couponIds = array_column($couponInfo, 'id');
  532. $couponUserMonth = $couponUserService->memberCouponUserGroupBymonth(['uid' => $uid, 'couponIds' => $couponIds]);
  533. $getTime = array();
  534. if ($couponUserMonth) {
  535. $getTime = array_column($couponUserMonth, 'num', 'time');
  536. }
  537. // 判断这个月是否领取过,而且领全了
  538. //if (in_array(date('Y-m', time()), $getTime)) return false;
  539. $timeKey = date('Y-m', time());
  540. if (array_key_exists($timeKey, $getTime) && $getTime[$timeKey] == count($couponIds)) return false;
  541. $monthNum = $this->getMonthNum(date('Y-m-d H:i:s', time()), date('Y-m-d H:i:s', $userInfo['overdue_time']));
  542. //判断是否领完所有月份
  543. if (count($getTime) >= $monthNum && (array_key_exists($timeKey, $getTime) && $getTime[$timeKey] == count($couponIds)) && $userInfo['is_ever_level'] != 1 && $monthNum > 0) return false;
  544. //看之前是否手动领取过某一张,领取过就不再领取。
  545. $couponUser = $couponUserService->getUserCounponByMonth(['uid' => $uid, 'cid' => $couponIds], 'id,cid');
  546. if ($couponUser) $couponUser = array_combine(array_column($couponUser, 'cid'), $couponUser);
  547. foreach ($couponInfo as $cv) {
  548. if (!isset($couponUser[$cv['id']])) {
  549. $this->memberIssueUserCoupon($cv['id'], $uid);
  550. }
  551. }
  552. }
  553. }
  554. return true;
  555. }
  556. /**
  557. * 获取今日新增优惠券
  558. * @throws \think\db\exception\DataNotFoundException
  559. * @throws \think\db\exception\DbException
  560. * @throws \think\db\exception\ModelNotFoundException
  561. */
  562. public function getTodayCoupon($uid)
  563. {
  564. $list = $this->dao->getTodayCoupon($uid);
  565. foreach ($list as $key => &$item) {
  566. $item['start_time'] = $item['start_time'] ? date('Y/m/d', $item['start_time']) : 0;
  567. $item['end_time'] = $item['end_time'] ? date('Y/m/d', $item['end_time']) : 0;
  568. $item['coupon_price'] = floatval($item['coupon_price']);
  569. $item['use_min_price'] = floatval($item['use_min_price']);
  570. if (isset($item['used']) && $item['used']) {
  571. unset($list[$key]);
  572. }
  573. }
  574. return array_merge($list);
  575. }
  576. /**
  577. * 获取新人券
  578. * @return array
  579. * @throws \think\db\exception\DataNotFoundException
  580. * @throws \think\db\exception\DbException
  581. * @throws \think\db\exception\ModelNotFoundException
  582. */
  583. public function getNewCoupon()
  584. {
  585. $list = $this->dao->getNewCoupon();
  586. foreach ($list as &$item) {
  587. $item['start_time'] = $item['start_time'] ? date('Y/m/d', $item['start_time']) : 0;
  588. $item['end_time'] = $item['end_time'] ? date('Y/m/d', $item['end_time']) : 0;
  589. $item['coupon_price'] = floatval($item['coupon_price']);
  590. $item['use_min_price'] = floatval($item['use_min_price']);
  591. }
  592. return $list;
  593. }
  594. /**
  595. * 获取列表
  596. * @param array $where
  597. * @return array
  598. * @throws \think\db\exception\DataNotFoundException
  599. * @throws \think\db\exception\DbException
  600. * @throws \think\db\exception\ModelNotFoundException
  601. */
  602. public function getCouponList(array $where)
  603. {
  604. [$page, $limit] = $this->getPageValue();
  605. $where['is_del'] = 0;
  606. $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';
  607. $list = $this->dao->getList($where, $page, $limit, $field);
  608. $count = $this->dao->count($where);
  609. return compact('list', 'count');
  610. }
  611. }