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