StoreCouponIssueServices.php 26 KB

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