StoreCouponIssueServices.php 25 KB

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