StoreCouponIssueServices.php 26 KB

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