StoreCouponIssueServices.php 26 KB

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