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