StoreCouponIssueServices.php 25 KB

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