StoreCouponIssueServices.php 26 KB

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