StoreCouponIssueServices.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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. $count = $this->dao->couponCount($where);
  63. return compact('list', 'count');
  64. }
  65. /**
  66. * 获取会员优惠券列表
  67. * @param array $where
  68. * @return array
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\DbException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. */
  73. public function getMemberCouponIssueList(array $where)
  74. {
  75. return $this->dao->getApiIssueList($where);
  76. }
  77. /**
  78. * 新增优惠券
  79. * @param $data
  80. * @return bool
  81. */
  82. public function saveCoupon($data)
  83. {
  84. if ($data['start_time'] && $data['start_use_time']) {
  85. if ($data['start_time'] < date('Y-m-d 00:00:00')) {
  86. throw new AdminException('开始领取时间不能小于当前时间');
  87. }
  88. if ($data['start_use_time'] < date('Y-m-d 00:00:00')) {
  89. throw new AdminException('开始使用时间不能小于当前时间');
  90. }
  91. if ($data['start_use_time'] < $data['start_time']) {
  92. throw new AdminException(400513);
  93. }
  94. }
  95. if (!in_array((int)$data['receive_type'], [1, 2, 3, 4])) {
  96. throw new AdminException(400758);
  97. }
  98. if (!in_array((int)$data['is_permanent'], [0, 1])) {
  99. throw new AdminException(400758);
  100. }
  101. if (empty($data['coupon_title'])) {
  102. throw new AdminException(400759);
  103. }
  104. if ($data['end_time'] && $data['end_use_time']) {
  105. if ($data['end_use_time'] < $data['end_time']) {
  106. throw new AdminException('用户领取数量不能大于优惠券发布数量');
  107. }
  108. }
  109. $data['start_use_time'] = strtotime((string)$data['start_use_time']);
  110. $data['end_use_time'] = strtotime((string)$data['end_use_time']);
  111. $data['start_time'] = strtotime((string)$data['start_time']);
  112. $data['end_time'] = strtotime((string)$data['end_time']);
  113. $data['title'] = $data['coupon_title'];
  114. $data['remain_count'] = $data['total_count'];
  115. $data['category_id'] = implode(',', $data['category_id']);
  116. if ($data['receive_type'] == 2 || $data['receive_type'] == 3) {
  117. $data['is_permanent'] = 1;
  118. $data['total_count'] = 0;
  119. }
  120. if ($data['is_permanent'] != 1 && $data['receive_limit'] > $data['total_count']) {
  121. throw new AdminException(500031);
  122. }
  123. $data['add_time'] = time();
  124. $res = $this->dao->save($data);
  125. if (($data['product_id'] !== '' || $data['category_id'] !== '') && $res) {
  126. $couponData = [];
  127. if ($data['product_id'] !== '') {
  128. $productIds = explode(',', $data['product_id']);
  129. foreach ($productIds as $product_id) {
  130. $couponData[] = ['product_id' => $product_id, 'coupon_id' => $res->id];
  131. }
  132. } elseif ($data['category_id'] !== '') {
  133. $categoryIds = explode(',', $data['category_id']);
  134. foreach ($categoryIds as $category_id) {
  135. $couponData[] = ['category_id' => $category_id, 'coupon_id' => $res->id];
  136. }
  137. }
  138. /** @var StoreCouponProductServices $storeCouponProductService */
  139. $storeCouponProductService = app()->make(StoreCouponProductServices::class);
  140. $storeCouponProductService->saveAll($couponData);
  141. }
  142. if (!$res) throw new AdminException(100022);
  143. return (int)$res->id;
  144. }
  145. /**
  146. * 修改状态
  147. * @param int $id
  148. * @return array
  149. * @throws \FormBuilder\Exception\FormBuilderException
  150. */
  151. public function createForm(int $id)
  152. {
  153. $issueInfo = $this->dao->get($id);
  154. if (-1 == $issueInfo['status'] || 1 == $issueInfo['is_del']) throw new AdminException(100007);
  155. $f = [FormBuilder::radio('status', '是否开启', $issueInfo['status'])->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]])];
  156. return create_form('状态修改', $f, $this->url('/marketing/coupon/released/status/' . $id), 'PUT');
  157. }
  158. /**
  159. * 领取记录
  160. * @param int $id
  161. * @return array
  162. */
  163. public function issueLog(int $id)
  164. {
  165. $coupon = $this->dao->get($id);
  166. if (!$coupon) {
  167. throw new AdminException(400515);
  168. }
  169. if ($coupon['receive_type'] != 4) {
  170. /** @var StoreCouponIssueUserServices $storeCouponIssueUserService */
  171. $storeCouponIssueUserService = app()->make(StoreCouponIssueUserServices::class);
  172. return $storeCouponIssueUserService->issueLog(['issue_coupon_id' => $id]);
  173. } else {//会员券
  174. /** @var StoreCouponUserServices $storeCouponUserService */
  175. $storeCouponUserService = app()->make(StoreCouponUserServices::class);
  176. return $storeCouponUserService->issueLog(['cid' => $id]);
  177. }
  178. }
  179. /**
  180. * 关注送优惠券
  181. * @param int $uid
  182. * @return bool
  183. * @throws \think\db\exception\DataNotFoundException
  184. * @throws \think\db\exception\DbException
  185. * @throws \think\db\exception\ModelNotFoundException
  186. */
  187. public function userFirstSubGiveCoupon(int $uid)
  188. {
  189. $couponList = $this->dao->getGiveCoupon(['receive_type' => 2]);
  190. $this->giveUserCoupon($uid, $couponList ?: []);
  191. return true;
  192. }
  193. /**
  194. * 订单金额达到预设金额赠送优惠卷
  195. * @param $uid
  196. * @param $total_price
  197. * @return bool
  198. * @throws \think\db\exception\DataNotFoundException
  199. * @throws \think\db\exception\DbException
  200. * @throws \think\db\exception\ModelNotFoundException
  201. */
  202. public function userTakeOrderGiveCoupon($uid, $total_price)
  203. {
  204. $couponList = $this->dao->getGiveCoupon([['is_full_give', '=', 1], ['full_reduction', '<=', $total_price]]);
  205. $this->giveUserCoupon((int)$uid, $couponList ?: []);
  206. return true;
  207. }
  208. /**
  209. * 下单之后赠送
  210. * @param $uid
  211. * @param $coupon_issue_ids 订单商品关联优惠券ids
  212. * @return array
  213. * @throws \think\db\exception\DataNotFoundException
  214. * @throws \think\db\exception\DbException
  215. * @throws \think\db\exception\ModelNotFoundException
  216. */
  217. public function orderPayGiveCoupon($uid, $coupon_issue_ids)
  218. {
  219. if (!$coupon_issue_ids) return [];
  220. $couponList = $this->dao->getGiveCoupon([['id', 'IN', $coupon_issue_ids]]);
  221. [$couponData, $issueUserData] = $this->giveUserCoupon($uid, $couponList ?: []);
  222. return $couponData;
  223. }
  224. /**
  225. * 发送优惠券
  226. * @param int $uid 发放人id
  227. * @param array $couponList 发送优惠券数据
  228. * @return array[]
  229. */
  230. public function giveUserCoupon(int $uid, array $couponList)
  231. {
  232. $couponData = $issueUserData = [];
  233. if ($uid && $couponList) {
  234. $time = time();
  235. $ids = array_column($couponList, 'id');
  236. /** @var StoreCouponIssueUserServices $issueUser */
  237. $issueUser = app()->make(StoreCouponIssueUserServices::class);
  238. foreach ($couponList as $item) {
  239. $data['cid'] = $item['id'];
  240. $data['uid'] = $uid;
  241. $data['coupon_title'] = $item['title'];
  242. $data['coupon_price'] = $item['coupon_price'];
  243. $data['use_min_price'] = $item['use_min_price'];
  244. if ($item['coupon_time']) {
  245. $data['add_time'] = $time;
  246. $data['end_time'] = $data['add_time'] + $item['coupon_time'] * 86400;
  247. } else {
  248. $data['add_time'] = $item['start_use_time'];
  249. $data['end_time'] = $item['end_use_time'];
  250. }
  251. $data['type'] = 'send';
  252. $issue['uid'] = $uid;
  253. $issue['issue_coupon_id'] = $item['id'];
  254. $issue['add_time'] = $time;
  255. $issueUserData[] = $issue;
  256. $couponData[] = $data;
  257. unset($data);
  258. unset($issue);
  259. }
  260. if ($couponData) {
  261. /** @var StoreCouponUserServices $storeCouponUser */
  262. $storeCouponUser = app()->make(StoreCouponUserServices::class);
  263. if (!$storeCouponUser->saveAll($couponData)) {
  264. throw new AdminException(100030);
  265. }
  266. }
  267. if ($issueUserData) {
  268. if (!$issueUser->saveAll($issueUserData)) {
  269. throw new AdminException(100031);
  270. }
  271. }
  272. }
  273. return [$couponData, $issueUserData];
  274. }
  275. /**
  276. * 获取优惠券列表
  277. * @param int $uid
  278. * @param array $where
  279. * @return array
  280. * @throws \think\db\exception\DataNotFoundException
  281. * @throws \think\db\exception\DbException
  282. * @throws \think\db\exception\ModelNotFoundException
  283. */
  284. public function getIssueCouponList(int $uid, array $where)
  285. {
  286. [$page, $limit] = $this->getPageValue();
  287. $cateId = [];
  288. if ($where['product_id'] == 0) {
  289. if ($where['type'] == -1) { // PC端获取优惠券
  290. $list = $this->dao->getPcIssueCouponList($uid, []);
  291. } else {
  292. $list = $this->dao->getIssueCouponList($uid, (int)$where['type'], 0, $page, $limit);
  293. if (!$list) $list = $this->dao->getIssueCouponList($uid, 1, 0, $page, $limit);
  294. if (!$list) $list = $this->dao->getIssueCouponList($uid, 2, 0, $page, $limit);
  295. }
  296. } else {
  297. /** @var StoreProductServices $storeProductService */
  298. $storeProductService = app()->make(StoreProductServices::class);
  299. /** @var StoreCategoryServices $storeCategoryService */
  300. $storeCategoryService = app()->make(StoreCategoryServices::class);
  301. $cateId = $storeProductService->value(['id' => $where['product_id']], 'cate_id');
  302. $cateId = explode(',', (string)$cateId);
  303. $cateId = array_merge($cateId, $storeCategoryService->cateIdByPid($cateId));
  304. $cateId = array_diff($cateId, [0]);
  305. if ($where['type'] == -1) { // PC端获取优惠券
  306. $list = $this->dao->getPcIssueCouponList($uid, $cateId, $where['product_id']);
  307. } else {
  308. if ($where['type'] == 1) {
  309. $typeId = $cateId;
  310. } elseif ($where['type'] == 2) {
  311. $typeId = $where['product_id'];
  312. } else {
  313. $typeId = 0;
  314. }
  315. $list = $this->dao->getIssueCouponList($uid, (int)$where['type'], $typeId, $page, $limit);
  316. }
  317. }
  318. foreach ($list as &$v) {
  319. $v['coupon_price'] = floatval($v['coupon_price']);
  320. $v['use_min_price'] = floatval($v['use_min_price']);
  321. $v['is_use'] = count($v['used']);
  322. if ($v['end_use_time']) {
  323. $v['start_use_time'] = date('Y/m/d', $v['start_use_time']);
  324. $v['end_use_time'] = date('Y/m/d', $v['end_use_time']);
  325. }
  326. if ($v['start_time']) {
  327. $v['start_time'] = date('Y/m/d', $v['start_time']);
  328. $v['end_time'] = date('Y/m/d', $v['end_time']);
  329. }
  330. }
  331. $data['list'] = $list;
  332. $data['count'] = $this->dao->getIssueCouponCount($where['product_id'], $cateId);
  333. return $data;
  334. }
  335. /**
  336. * 领取优惠券
  337. * @param $id
  338. * @param $user
  339. * @param bool $is_receive
  340. * @throws \think\db\exception\DataNotFoundException
  341. * @throws \think\db\exception\DbException
  342. * @throws \think\db\exception\ModelNotFoundException
  343. */
  344. public function issueUserCoupon($id, $user, bool $is_receive = false)
  345. {
  346. $issueCouponInfo = $this->dao->getInfo((int)$id);
  347. if ($user->is_money_level <= 0 && $issueCouponInfo['receive_type'] == 4) {
  348. throw new ApiException(400097);
  349. }
  350. $uid = $user->uid;
  351. if (!$issueCouponInfo) throw new ApiException(400516);
  352. /** @var StoreCouponIssueUserServices $issueUserService */
  353. $issueUserService = app()->make(StoreCouponIssueUserServices::class);
  354. /** @var StoreCouponUserServices $couponUserService */
  355. $couponUserService = app()->make(StoreCouponUserServices::class);
  356. $this->transaction(function () use ($issueUserService, $uid, $id, $couponUserService, $issueCouponInfo, $is_receive) {
  357. $issueUserService->save(['uid' => $uid, 'issue_coupon_id' => $id, 'add_time' => time()]);
  358. $couponUserService->addUserCoupon($uid, $issueCouponInfo, $is_receive ? 'get' : 'send');
  359. if ($issueCouponInfo['total_count'] > 0 && $is_receive) {
  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. * @throws \think\db\exception\DataNotFoundException
  400. * @throws \think\db\exception\DbException
  401. * @throws \think\db\exception\ModelNotFoundException
  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. * @throws \think\db\exception\DataNotFoundException
  483. * @throws \think\db\exception\DbException
  484. * @throws \think\db\exception\ModelNotFoundException
  485. */
  486. public function getOne(array $where)
  487. {
  488. if (!$where) throw new AdminException(100100);
  489. return $this->dao->getOne($where);
  490. }
  491. /**
  492. * 俩时间相差月份
  493. * @param $date1
  494. * @param $date2
  495. * @return float|int
  496. */
  497. public function getMonthNum($date1, $date2)
  498. {
  499. $date1_stamp = strtotime($date1);
  500. $date2_stamp = strtotime($date2);
  501. list($date_1['y'], $date_1['m']) = explode("-", date('Y-m', $date1_stamp));
  502. list($date_2['y'], $date_2['m']) = explode("-", date('Y-m', $date2_stamp));
  503. return abs($date_1['y'] - $date_2['y']) * 12 + $date_2['m'] - $date_1['m'];
  504. }
  505. /**
  506. * 给会员发放优惠券
  507. * @param $uid
  508. * @param int $couponId
  509. * @return bool
  510. * @throws \think\db\exception\DataNotFoundException
  511. * @throws \think\db\exception\DbException
  512. * @throws \think\db\exception\ModelNotFoundException
  513. */
  514. public function sendMemberCoupon($uid, $couponId = 0)
  515. {
  516. if (!$uid) return false;
  517. /** @var MemberCardServices $memberCardService */
  518. $memberCardService = app()->make(MemberCardServices::class);
  519. //看付费会员是否开启
  520. $isOpenMember = $memberCardService->isOpenMemberCard();
  521. if (!$isOpenMember) return false;
  522. /** @var UserServices $userService */
  523. $userService = app()->make(UserServices::class);
  524. $userInfo = $userService->getUserInfo((int)$uid);
  525. //看是否会员过期
  526. $checkMember = $userService->offMemberLevel($uid, $userInfo);
  527. if (!$checkMember) return false;
  528. /** @var MemberRightServices $memberRightService */
  529. $memberRightService = app()->make(MemberRightServices::class);
  530. //看是否开启会员送券
  531. $isSendCoupon = $memberRightService->getMemberRightStatus("coupon");
  532. if (!$isSendCoupon) return false;
  533. if ($userInfo && (($userInfo['is_money_level'] > 0) || $userInfo['is_ever_level'] == 1)) {
  534. if ($couponId) {//手动点击领取
  535. $couponWhere['id'] = $couponId;
  536. } else {//主动批量发放
  537. $couponWhere['status'] = 1;
  538. $couponWhere['receive_type'] = 4;
  539. $couponWhere['is_del'] = 0;
  540. }
  541. $couponInfo = $this->getMemberCouponIssueList($couponWhere);
  542. if ($couponInfo) {
  543. /** @var StoreCouponUserServices $couponUserService */
  544. $couponUserService = app()->make(StoreCouponUserServices::class);
  545. $couponIds = array_column($couponInfo, 'id');
  546. $couponUserMonth = $couponUserService->memberCouponUserGroupBymonth(['uid' => $uid, 'couponIds' => $couponIds]);
  547. $getTime = array();
  548. if ($couponUserMonth) {
  549. $getTime = array_column($couponUserMonth, 'num', 'time');
  550. }
  551. // 判断这个月是否领取过,而且领全了
  552. //if (in_array(date('Y-m', time()), $getTime)) return false;
  553. $timeKey = date('Y-m', time());
  554. if (array_key_exists($timeKey, $getTime) && $getTime[$timeKey] == count($couponIds)) return false;
  555. $monthNum = $this->getMonthNum(date('Y-m-d H:i:s', time()), date('Y-m-d H:i:s', $userInfo['overdue_time']));
  556. //判断是否领完所有月份
  557. if (count($getTime) >= $monthNum && (array_key_exists($timeKey, $getTime) && $getTime[$timeKey] == count($couponIds)) && $userInfo['is_ever_level'] != 1 && $monthNum > 0) return false;
  558. //看之前是否手动领取过某一张,领取过就不再领取。
  559. $couponUser = $couponUserService->getUserCounponByMonth(['uid' => $uid, 'cid' => $couponIds], 'id,cid');
  560. if ($couponUser) $couponUser = array_combine(array_column($couponUser, 'cid'), $couponUser);
  561. foreach ($couponInfo as $cv) {
  562. if (!isset($couponUser[$cv['id']])) {
  563. $this->memberIssueUserCoupon($cv['id'], $uid);
  564. }
  565. }
  566. }
  567. }
  568. return true;
  569. }
  570. /**
  571. * 获取今日新增优惠券
  572. * @throws \think\db\exception\DataNotFoundException
  573. * @throws \think\db\exception\DbException
  574. * @throws \think\db\exception\ModelNotFoundException
  575. */
  576. public function getTodayCoupon($uid)
  577. {
  578. $list = $this->dao->getTodayCoupon($uid);
  579. foreach ($list as $key => &$item) {
  580. $item['start_time'] = $item['start_time'] ? date('Y/m/d', $item['start_time']) : 0;
  581. $item['end_time'] = $item['end_time'] ? date('Y/m/d', $item['end_time']) : 0;
  582. $item['coupon_price'] = floatval($item['coupon_price']);
  583. $item['use_min_price'] = floatval($item['use_min_price']);
  584. if (isset($item['used']) && $item['used']) {
  585. unset($list[$key]);
  586. }
  587. }
  588. return array_merge($list);
  589. }
  590. /**
  591. * 获取新人券
  592. * @return array
  593. * @throws \think\db\exception\DataNotFoundException
  594. * @throws \think\db\exception\DbException
  595. * @throws \think\db\exception\ModelNotFoundException
  596. */
  597. public function getNewCoupon()
  598. {
  599. $list = $this->dao->getNewCoupon();
  600. foreach ($list as &$item) {
  601. $item['start_time'] = $item['start_time'] ? date('Y/m/d', $item['start_time']) : 0;
  602. $item['end_time'] = $item['end_time'] ? date('Y/m/d', $item['end_time']) : 0;
  603. $item['coupon_price'] = floatval($item['coupon_price']);
  604. $item['use_min_price'] = floatval($item['use_min_price']);
  605. }
  606. return $list;
  607. }
  608. /**
  609. * 获取列表
  610. * @param array $where
  611. * @return array
  612. * @throws \think\db\exception\DataNotFoundException
  613. * @throws \think\db\exception\DbException
  614. * @throws \think\db\exception\ModelNotFoundException
  615. */
  616. public function getCouponList(array $where)
  617. {
  618. [$page, $limit] = $this->getPageValue();
  619. $where['is_del'] = 0;
  620. $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';
  621. $list = $this->dao->getList($where, $page, $limit, $field);
  622. $count = $this->dao->count($where);
  623. return compact('list', 'count');
  624. }
  625. }