StoreCouponIssueServices.php 27 KB

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