StoreCouponIssueServices.php 26 KB

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