StoreCouponIssueServices.php 28 KB

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