StoreCouponIssueServices.php 26 KB

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