StoreCouponIssueServices.php 21 KB

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