StoreCouponIssueServices.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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\other\queue\QueueServices;
  17. use app\services\product\brand\StoreBrandServices;
  18. use app\services\product\category\StoreProductCategoryServices;
  19. use app\services\product\product\StoreProductServices;
  20. use app\services\user\member\MemberCardServices;
  21. use app\services\user\member\MemberRightServices;
  22. use app\services\user\UserServices;
  23. use crmeb\exceptions\AdminException;
  24. use crmeb\services\FormBuilder;
  25. use think\exception\ValidateException;
  26. /**
  27. *
  28. * Class StoreCouponIssueServices
  29. * @package app\services\activity\coupon
  30. * @mixin StoreCouponIssueDao
  31. */
  32. class StoreCouponIssueServices extends BaseServices
  33. {
  34. public $_couponType = [0 => "通用券", 1 => "品类券", 2 => '商品券'];
  35. /**
  36. * StoreCouponIssueServices constructor.
  37. * @param StoreCouponIssueDao $dao
  38. */
  39. public function __construct(StoreCouponIssueDao $dao)
  40. {
  41. $this->dao = $dao;
  42. }
  43. /**
  44. * 获取已发布列表
  45. * @param array $where
  46. * @return array
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\DbException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. */
  51. public function getCouponIssueList(array $where)
  52. {
  53. [$page, $limit] = $this->getPageValue();
  54. $where['is_del'] = 0;
  55. $list = $this->dao->getList($where, $page, $limit);
  56. foreach ($list as &$item) {
  57. if (!$item['coupon_time']) {
  58. $item['coupon_time'] = ceil(($item['end_use_time'] - $item['start_use_time']) / '86400');
  59. }
  60. }
  61. $count = $this->dao->count($where);
  62. return compact('list', 'count');
  63. }
  64. /**
  65. * 获取会员优惠券列表
  66. * @param array $where
  67. * @return array
  68. * @throws \think\db\exception\DataNotFoundException
  69. * @throws \think\db\exception\DbException
  70. * @throws \think\db\exception\ModelNotFoundException
  71. */
  72. public function getMemberCouponIssueList(array $where)
  73. {
  74. return $this->dao->getApiIssueList($where);
  75. }
  76. /**
  77. * 新增优惠券
  78. * @param $data
  79. * @return bool
  80. */
  81. public function saveCoupon($data)
  82. {
  83. $data['start_use_time'] = strtotime((string)$data['start_use_time']);
  84. $data['end_use_time'] = strtotime((string)$data['end_use_time']);
  85. $data['start_time'] = strtotime((string)$data['start_time']);
  86. $data['end_time'] = strtotime((string)$data['end_time']);
  87. $data['title'] = $data['coupon_title'];
  88. $data['remain_count'] = $data['total_count'];
  89. $data['add_time'] = time();
  90. $res = $this->dao->save($data);
  91. if ($data['product_id'] !== '' && $res) {
  92. $productIds = explode(',', $data['product_id']);
  93. $couponData = [];
  94. foreach ($productIds as $product_id) {
  95. $couponData[] = ['product_id' => $product_id, 'coupon_id' => $res->id];
  96. }
  97. /** @var StoreCouponProductServices $storeCouponProductService */
  98. $storeCouponProductService = app()->make(StoreCouponProductServices::class);
  99. $storeCouponProductService->saveAll($couponData);
  100. }
  101. if (!$res) throw new AdminException('添加优惠券失败!');
  102. return true;
  103. }
  104. /**
  105. * 修改状态
  106. * @param int $id
  107. * @return array
  108. * @throws \FormBuilder\Exception\FormBuilderException
  109. */
  110. public function createForm(int $id)
  111. {
  112. $issueInfo = $this->dao->get($id);
  113. if (-1 == $issueInfo['status'] || 1 == $issueInfo['is_del']) return $this->fail('状态错误,无法修改');
  114. $f = [FormBuilder::radio('status', '是否开启', $issueInfo['status'])->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]])];
  115. return create_form('状态修改', $f, $this->url('/marketing/coupon/released/status/' . $id), 'PUT');
  116. }
  117. /**
  118. * 领取记录
  119. * @param int $id
  120. * @return array
  121. */
  122. public function issueLog(int $id)
  123. {
  124. $coupon = $this->dao->get($id);
  125. if (!$coupon) {
  126. throw new ValidateException('优惠券不存在');
  127. }
  128. if ($coupon['receive_type'] != 4) {
  129. /** @var StoreCouponIssueUserServices $storeCouponIssueUserService */
  130. $storeCouponIssueUserService = app()->make(StoreCouponIssueUserServices::class);
  131. return $storeCouponIssueUserService->issueLog(['issue_coupon_id' => $id]);
  132. } else {//会员券
  133. /** @var StoreCouponUserServices $storeCouponUserService */
  134. $storeCouponUserService = app()->make(StoreCouponUserServices::class);
  135. return $storeCouponUserService->issueLog(['cid' => $id]);
  136. }
  137. }
  138. /**
  139. * 保存赠送给用户优惠券
  140. * @param int $uid
  141. * @param array $couponList
  142. * @param string $type
  143. * @return array
  144. */
  145. public function saveGiveCoupon(int $uid, array $couponList, string $type = 'get')
  146. {
  147. if (!$uid || !$couponList) {
  148. return [];
  149. }
  150. $couponData = [];
  151. $issueUserData = [];
  152. $time = time();
  153. /** @var StoreCouponIssueUserServices $issueUser */
  154. $issueUser = app()->make(StoreCouponIssueUserServices::class);
  155. foreach ($couponList as $item) {
  156. $data['cid'] = $item['id'];
  157. $data['uid'] = $uid;
  158. $data['coupon_title'] = $item['title'];
  159. $data['coupon_price'] = $item['coupon_price'];
  160. $data['use_min_price'] = $item['use_min_price'];
  161. if ($item['coupon_time']) {
  162. $data['add_time'] = $time;
  163. $data['end_time'] = $data['add_time'] + $item['coupon_time'] * 86400;
  164. } else {
  165. $data['add_time'] = $item['start_use_time'];
  166. $data['end_time'] = $item['end_use_time'];
  167. }
  168. $data['type'] = $type;
  169. $issue['uid'] = $uid;
  170. $issue['issue_coupon_id'] = $item['id'];
  171. $issue['add_time'] = $time;
  172. $issueUserData[] = $issue;
  173. $couponData[] = $data;
  174. unset($data);
  175. unset($issue);
  176. }
  177. if ($couponData) {
  178. /** @var StoreCouponUserServices $storeCouponUser */
  179. $storeCouponUser = app()->make(StoreCouponUserServices::class);
  180. if (!$storeCouponUser->saveAll($couponData)) {
  181. throw new AdminException('发劵失败');
  182. }
  183. }
  184. if ($issueUserData) {
  185. if (!$issueUser->saveAll($issueUserData)) {
  186. throw new AdminException('发劵失败');
  187. }
  188. }
  189. return $couponData;
  190. }
  191. /**
  192. * 新人礼赠送优惠券
  193. * @param int $uid
  194. */
  195. public function newcomerGiveCoupon(int $uid)
  196. {
  197. if (!sys_config('newcomer_status')) {
  198. return false;
  199. }
  200. $status = sys_config('register_coupon_status');
  201. if (!$status) {//未开启
  202. return true;
  203. }
  204. $couponIds = sys_config('register_give_coupon', []);
  205. if (!$couponIds) {
  206. return true;
  207. }
  208. /** @var UserServices $userServices */
  209. $userServices = app()->make(UserServices::class);
  210. $userInfo = $userServices->getUserInfo($uid);
  211. if (!$userInfo) {
  212. return true;
  213. }
  214. $couponList = $this->dao->getGiveCoupon([['id', 'IN', $couponIds]]);
  215. if ($couponList) {
  216. $this->saveGiveCoupon($uid, $couponList, 'newcomer');
  217. }
  218. return true;
  219. }
  220. /**
  221. * 会员卡激活赠送优惠券
  222. * @param int $uid
  223. */
  224. public function levelGiveCoupon(int $uid)
  225. {
  226. $status = sys_config('level_activate_status');
  227. if (!$status) {//是否需要激活
  228. return true;
  229. }
  230. $status = sys_config('level_coupon_status');
  231. if (!$status) {//未开启
  232. return true;
  233. }
  234. $couponIds = sys_config('level_give_coupon', []);
  235. if (!$couponIds) {
  236. return true;
  237. }
  238. /** @var UserServices $userServices */
  239. $userServices = app()->make(UserServices::class);
  240. $userInfo = $userServices->getUserInfo($uid);
  241. if (!$userInfo) {
  242. return true;
  243. }
  244. $couponList = $this->dao->getGiveCoupon([['id', 'IN', $couponIds]]);
  245. if ($couponList) {
  246. $this->saveGiveCoupon($uid, $couponList, 'activate_level');
  247. }
  248. return true;
  249. }
  250. /**
  251. * 关注送优惠券
  252. * @param int $uid
  253. */
  254. public function userFirstSubGiveCoupon(int $uid)
  255. {
  256. $couponList = $this->dao->getGiveCoupon(['receive_type' => 2]);
  257. if ($couponList) {
  258. $this->saveGiveCoupon($uid, $couponList, 'user_first');
  259. }
  260. return true;
  261. }
  262. /**
  263. * 订单金额达到预设金额赠送优惠卷
  264. * @param $uid
  265. */
  266. public function userTakeOrderGiveCoupon($uid, $total_price)
  267. {
  268. $couponList = $this->dao->getGiveCoupon([['is_full_give', '=', 1], ['full_reduction', '<=', $total_price]]);
  269. if ($couponList) {
  270. $res = [];
  271. foreach ($couponList as $item) {
  272. if ($total_price >= $item['full_reduction']) {
  273. $res[] = $item;
  274. }
  275. }
  276. $this->saveGiveCoupon($uid,$res);
  277. }
  278. return true;
  279. }
  280. /**
  281. * 下单之后赠送
  282. * @param $uid
  283. */
  284. public function orderPayGiveCoupon($uid, $coupon_issue_ids)
  285. {
  286. if (!$coupon_issue_ids) return [];
  287. $couponList = $this->dao->getGiveCoupon([['id', 'IN', $coupon_issue_ids]]);
  288. $couponData = [];
  289. if ($couponList) {
  290. $couponData = $this->saveGiveCoupon($uid, $couponList, 'order');
  291. }
  292. return $couponData;
  293. }
  294. /**
  295. * 获取商品关联
  296. * @param int $uid
  297. * @param int $product_id
  298. * @param string $field
  299. * @param int $limit
  300. * @return array
  301. * @throws \think\db\exception\DataNotFoundException
  302. * @throws \think\db\exception\DbException
  303. * @throws \think\db\exception\ModelNotFoundException
  304. */
  305. public function getProductCouponList(int $uid, int $product_id, string $field = '*', int $limit = 0)
  306. {
  307. if ($limit) {
  308. $page = 1;
  309. } else {
  310. [$page, $limit] = $this->getPageValue();
  311. }
  312. /** @var StoreProductServices $storeProductService */
  313. $storeProductService = app()->make(StoreProductServices::class);
  314. /** @var StoreProductCategoryServices $storeCategoryService */
  315. $storeCategoryService = app()->make(StoreProductCategoryServices::class);
  316. $productInfo = $storeProductService->getCacheProductInfo($product_id);
  317. $cateId = explode(',', $productInfo['cate_id']);
  318. $cateId = array_merge($cateId, $storeCategoryService->cateIdByPid($cateId));
  319. $cateId = array_unique(array_diff($cateId, [0]));
  320. $brandIds = [];
  321. if ($productInfo['brand_id']) {
  322. /** @var StoreBrandServices $storeBrandServices */
  323. $storeBrandServices = app()->make(StoreBrandServices::class);
  324. $brandInfo = $storeBrandServices->get((int)$productInfo['brand_id'], ['id', 'pid']);
  325. if ($brandInfo) {
  326. $brandIds = $brandInfo->toArray();
  327. $brandIds = array_diff($brandIds, [0]);
  328. }
  329. }
  330. $where = ['product_id' => $productInfo['pid'] ? : $product_id, 'cate_id' => $cateId, 'brand_id' => $brandIds];
  331. $list = $this->dao->getIssueCouponListNew($uid, $where, $field, $page, $limit);
  332. //门店ID
  333. $store_id = $productInfo['type'] == 1 ? ($productInfo['relation_id'] ?? 0) : 0;
  334. $result = [];
  335. foreach ($list as &$item) {
  336. if ($store_id && isset($item['applicable_type']) && isset($item['applicable_store_id'])) {
  337. $applicable_store_id = is_array($item['applicable_store_id']) ? $item['applicable_store_id'] : explode(',', $item['applicable_store_id']);
  338. //活动不适用该门店
  339. if ($item['applicable_type'] == 0 || ($item['applicable_type'] == 2 && !in_array($store_id, $applicable_store_id))) {
  340. continue;
  341. }
  342. }
  343. $item['coupon_price'] = floatval($item['coupon_price']);
  344. $item['use_min_price'] = floatval($item['use_min_price']);
  345. $item['is_use'] = $uid ? isset($item['used']) : false;
  346. if (!$item['end_time']) {
  347. $item['start_time'] = '';
  348. $item['end_time'] = '不限时';
  349. } else {
  350. $item['start_time'] = date('Y/m/d', $item['start_time']);
  351. $item['end_time'] = $item['end_time'] ? date('Y/m/d', $item['end_time']) : date('Y/m/d', time() + 86400);
  352. }
  353. $result[] = $item;
  354. }
  355. return $result;
  356. }
  357. /**
  358. * 获取优惠券列表
  359. * @param int $uid
  360. * @param array $where
  361. * @return array
  362. * @throws \think\db\exception\DataNotFoundException
  363. * @throws \think\db\exception\DbException
  364. * @throws \think\db\exception\ModelNotFoundException
  365. */
  366. public function getIssueCouponList(int $uid, array $where, string $field = '*', bool $is_product = false)
  367. {
  368. [$page, $limit] = $this->getPageValue();
  369. /** @var StoreProductServices $storeProductService */
  370. $storeProductService = app()->make(StoreProductServices::class);
  371. /** @var StoreProductCategoryServices $storeCategoryService */
  372. $storeCategoryService = app()->make(StoreProductCategoryServices::class);
  373. /** @var StoreBrandServices $storeBrandServices */
  374. $storeBrandServices = app()->make(StoreBrandServices::class);
  375. $typeId = 0;
  376. $cateId = 0;
  377. $bandId = 0;
  378. $store_id = 0;
  379. $product_id = 0;
  380. if ($where['product_id'] == 0) {
  381. if ($where['type'] == -1) { // PC端获取优惠券
  382. $list = $this->dao->getIssueCouponListNew($uid, $where, $field);
  383. } else {
  384. $list = $this->dao->getIssueCouponListNew($uid, $where, $field, $page, $limit);
  385. if (!$list) $list = $this->dao->getIssueCouponListNew($uid, ['type' => 1], $field, $page, $limit);
  386. if (!$list) $list = $this->dao->getIssueCouponListNew($uid, ['type' => 2], $field, $page, $limit);
  387. if (!$list) $list = $this->dao->getIssueCouponListNew($uid, ['type' => 3], $field, $page, $limit);
  388. }
  389. } else {
  390. $productInfo = $storeProductService->getOne(['id' => $where['product_id']], 'id,type,relation_id,cate_id,brand_id');
  391. //门店ID
  392. $store_id = $productInfo['type'] == 1 ? ($productInfo['relation_id'] ?? 0) : 0;
  393. $product_id = $productInfo['pid'] ? : $where['product_id'];
  394. if ($productInfo) {
  395. $cateId = $productInfo['cateId'];
  396. if ($cateId) {
  397. $cateId = explode(',', $cateId);
  398. $cateId = array_merge($cateId, $storeCategoryService->cateIdByPid($cateId));
  399. $cateId = array_diff($cateId, [0]);
  400. }
  401. if ($productInfo['brand_id']) {
  402. /** @var StoreBrandServices $storeBrandServices */
  403. $storeBrandServices = app()->make(StoreBrandServices::class);
  404. $brandInfo = $storeBrandServices->get((int)$productInfo['brand_id'], ['id', 'pid']);
  405. if ($brandInfo) {
  406. $bandId = $brandInfo->toArray();
  407. $bandId = array_diff($bandId, [0]);
  408. }
  409. }
  410. }
  411. if ($where['type'] == -1) { // PC端获取优惠券
  412. $list = $this->dao->getIssueCouponListNew($uid, ['product_id' => $product_id, 'cate_id' => $cateId, 'brand_id' => $bandId], $field);
  413. } else {
  414. $coupon_where = ['type' => $where['type']];
  415. if ($where['type'] == 1) {
  416. $category_type['cate_id'] = $cateId;
  417. } elseif ($where['type'] == 2) {
  418. $category_type['product_id'] = $where['product_id'];
  419. } elseif ($where['type'] == 3) {
  420. $category_type['brand_id'] = $where['brand_id'];
  421. }
  422. $list = $this->dao->getIssueCouponListNew($uid, $coupon_where, $field, $page, $limit);
  423. }
  424. }
  425. $result = [];
  426. if ($list) {
  427. $limit = 3;
  428. $field = ['id', 'image', 'store_name', 'price', 'IFNULL(sales,0) + IFNULL(ficti,0) as sales'];
  429. $category = $storeCategoryService->getColumn([], 'pid,cate_name', 'id');
  430. $brands = $storeBrandServices->getColumn([], 'id,pid', 'id');
  431. foreach ($list as &$item) {
  432. if ($store_id && isset($item['applicable_type']) && isset($item['applicable_store_id'])) {
  433. $applicable_store_id = is_array($item['applicable_store_id']) ? $item['applicable_store_id'] : explode(',', $item['applicable_store_id']);
  434. //活动不适用该门店
  435. if ($item['applicable_type'] == 0 || ($item['applicable_type'] == 2 && !in_array($store_id, $applicable_store_id))) {
  436. continue;
  437. }
  438. }
  439. $item['coupon_price'] = floatval($item['coupon_price']);
  440. $item['use_min_price'] = floatval($item['use_min_price']);
  441. $item['is_use'] = $uid ? isset($item['used']) : false;
  442. if (!$item['end_time']) {
  443. $item['start_time'] = '';
  444. $item['end_time'] = '不限时';
  445. } else {
  446. $item['start_time'] = date('Y/m/d', $item['start_time']);
  447. $item['end_time'] = $item['end_time'] ? date('Y/m/d', $item['end_time']) : date('Y/m/d', time() + 86400);
  448. }
  449. if (!$is_product) {//不需要返回优惠券使用商品
  450. $result[] = $item;
  451. continue;
  452. }
  453. $product_where = [];
  454. $product_where['is_show'] = 1;
  455. $product_where['is_del'] = 0;
  456. $product_where['use_min_price'] = $item['use_min_price'];
  457. switch ($item['type']) {
  458. case 0:
  459. break;
  460. case 1://品类券
  461. $product_where['salesOrder'] = 'desc';
  462. $category_type = ($category[$item['category_id'] ?? 0]['pid'] ?? 0) == 0 ? 1 : 2;
  463. if ($category_type == 1) {
  464. $product_where['cid'] = $item['category_id'];
  465. } else {
  466. $product_where['sid'] = $item['category_id'];
  467. }
  468. break;
  469. case 2://商品劵
  470. $product_where['salesOrder'] = 'desc';
  471. $product_where['ids'] = $item['product_id'];
  472. break;
  473. case 3://品牌券
  474. $product_where['salesOrder'] = 'desc';
  475. $product_where['brand_id'] = $brands[$item['brand_id']] ?? [];
  476. break;
  477. default:
  478. $item['products'] = [];
  479. }
  480. $item['products'] = get_thumb_water($storeProductService->getSearchList($product_where, 0, $limit, $field, $item['type'] == 0 ? 'rand' : '', []));
  481. $result[] = $item;
  482. }
  483. }
  484. $data['list'] = $result;
  485. $data['count'] = $this->dao->getIssueCouponCount($product_id, $cateId, $bandId);
  486. return $data;
  487. }
  488. /**
  489. * 给用户发送优惠券
  490. * @param $id
  491. * @param $user
  492. * @param bool $more
  493. * @param string $type
  494. * @return mixed
  495. * @throws \think\db\exception\DataNotFoundException
  496. * @throws \think\db\exception\DbException
  497. * @throws \think\db\exception\ModelNotFoundException
  498. */
  499. public function issueUserCoupon($id, $user, bool $more = true, string $type = 'get')
  500. {
  501. $issueCouponInfo = $this->dao->getInfo((int)$id);
  502. $uid = $user['uid'];
  503. if (!$issueCouponInfo) throw new ValidateException('领取的优惠劵已领完或已过期!');
  504. /** @var MemberRightServices $memberRightService */
  505. $memberRightService = app()->make(MemberRightServices::class);
  506. if ($issueCouponInfo->receive_type == 4 && (!$user['is_money_level'] || !$memberRightService->getMemberRightStatus("coupon"))) {
  507. if (!$user['is_money_level']) throw new ValidateException('您不是付费会员!');
  508. if (!$memberRightService->getMemberRightStatus("coupon")) throw new ValidateException('暂时无法领取!');
  509. }
  510. /** @var StoreCouponIssueUserServices $issueUserService */
  511. $issueUserService = app()->make(StoreCouponIssueUserServices::class);
  512. /** @var StoreCouponUserServices $couponUserService */
  513. $couponUserService = app()->make(StoreCouponUserServices::class);
  514. //是否多次领取
  515. if (!$more) {
  516. if ($issueUserService->getOne(['uid' => $uid, 'issue_coupon_id' => $id])) {
  517. throw new ValidateException('已领取过该优惠劵!');
  518. }
  519. }
  520. if ($issueCouponInfo->remain_count <= 0 && !$issueCouponInfo->is_permanent) throw new ValidateException('抱歉优惠券已经领取完了!');
  521. $userCounpon = $this->transaction(function () use ($issueUserService, $uid, $id, $couponUserService, $issueCouponInfo, $type) {
  522. $issueUserService->save(['uid' => $uid, 'issue_coupon_id' => $id, 'add_time' => time()]);
  523. $res = $couponUserService->addUserCoupon($uid, $issueCouponInfo, $type);
  524. if ($issueCouponInfo['total_count'] > 0) {
  525. $issueCouponInfo['remain_count'] -= 1;
  526. $issueCouponInfo->save();
  527. }
  528. return $res;
  529. });
  530. return $userCounpon;
  531. }
  532. /**
  533. * 会员发放优惠期券
  534. * @param $id
  535. * @param $uid
  536. * @throws \think\db\exception\DataNotFoundException
  537. * @throws \think\db\exception\DbException
  538. * @throws \think\db\exception\ModelNotFoundException
  539. */
  540. public function memberIssueUserCoupon($id, $uid)
  541. {
  542. $issueCouponInfo = $this->dao->getInfo((int)$id);
  543. if ($issueCouponInfo) {
  544. /** @var StoreCouponIssueUserServices $issueUserService */
  545. $issueUserService = app()->make(StoreCouponIssueUserServices::class);
  546. /** @var StoreCouponUserServices $couponUserService */
  547. $couponUserService = app()->make(StoreCouponUserServices::class);
  548. if ($issueCouponInfo->remain_count >= 0 || $issueCouponInfo->is_permanent) {
  549. $this->transaction(function () use ($issueUserService, $uid, $id, $couponUserService, $issueCouponInfo) {
  550. //$issueUserService->save(['uid' => $uid, 'issue_coupon_id' => $id, 'add_time' => time()]);
  551. $couponUserService->addMemberUserCoupon($uid, $issueCouponInfo, "send");
  552. // 如果会员劵需要限制数量时打开
  553. if ($issueCouponInfo['total_count'] > 0) {
  554. $issueCouponInfo['remain_count'] -= 1;
  555. $issueCouponInfo->save();
  556. }
  557. });
  558. }
  559. }
  560. }
  561. /**
  562. * 用户优惠劵列表
  563. * @param int $uid
  564. * @param $types
  565. * @return array
  566. */
  567. public function getUserCouponList(int $uid, $types)
  568. {
  569. /** @var StoreCouponUserServices $storeConponUser */
  570. $storeConponUser = app()->make(StoreCouponUserServices::class);
  571. return $storeConponUser->getUserCounpon($uid, $types);
  572. }
  573. /**
  574. * 后台发送优惠券
  575. * @param $coupon
  576. * @param $user
  577. * @return bool
  578. */
  579. public function setCoupon($coupon, $user, $redisKey, $queueId)
  580. {
  581. if (!$redisKey || !$queueId || !$user) return false;
  582. $data = [];
  583. $issueData = [];
  584. /** @var StoreCouponUserServices $storeCouponUser */
  585. $storeCouponUser = app()->make(StoreCouponUserServices::class);
  586. /** @var StoreCouponIssueUserServices $storeCouponIssueUser */
  587. $storeCouponIssueUser = app()->make(StoreCouponIssueUserServices::class);
  588. /** @var QueueServices $queueService */
  589. $queueService = app()->make(QueueServices::class);
  590. $i = 0;
  591. $time = time();
  592. foreach ($user as $k => $v) {
  593. $data[$i]['cid'] = $coupon['id'];
  594. $data[$i]['uid'] = $v;
  595. $data[$i]['coupon_title'] = $coupon['title'];
  596. $data[$i]['coupon_price'] = $coupon['coupon_price'];
  597. $data[$i]['use_min_price'] = $coupon['use_min_price'];
  598. $data[$i]['add_time'] = $time;
  599. if ($coupon['coupon_time']) {
  600. $data[$i]['start_time'] = $time;
  601. $data[$i]['end_time'] = $time + $coupon['coupon_time'] * 86400;
  602. } else {
  603. $data[$i]['start_time'] = $coupon['start_use_time'];
  604. $data[$i]['end_time'] = $coupon['end_use_time'];
  605. }
  606. $data[$i]['type'] = 'send';
  607. $issueData[$i]['uid'] = $v;
  608. $issueData[$i]['issue_coupon_id'] = $coupon['id'];
  609. $issueData[$i]['add_time'] = time();
  610. $i++;
  611. }
  612. $res = $this->transaction(function () use ($data, $issueData, $storeCouponUser, $storeCouponIssueUser) {
  613. $res1 = $res2 = true;
  614. if ($data) {
  615. $res1 = $storeCouponUser->saveAll($data);
  616. }
  617. if ($issueData) {
  618. $res2 = $storeCouponIssueUser->saveAll($issueData);
  619. }
  620. return $res1 && $res2;
  621. });
  622. if (!$res) {
  623. //发券失败后将队列状态置为失败
  624. $queueService->setQueueFail($queueId['id'], $redisKey);
  625. } else {
  626. //发券成功的用户踢出集合
  627. $queueService->doSuccessSremRedis($user, $redisKey, $queueId['type']);
  628. }
  629. return true;
  630. }
  631. /**
  632. * 获取下单可使用的优惠券列表
  633. * @param int $uid
  634. * @param $cartId
  635. * @param bool $new
  636. * @param int $shipping_type
  637. * @param int $store_id
  638. * @return array
  639. * @throws \Psr\SimpleCache\InvalidArgumentException
  640. * @throws \think\db\exception\DataNotFoundException
  641. * @throws \think\db\exception\DbException
  642. * @throws \think\db\exception\ModelNotFoundException
  643. */
  644. public function beUsableCouponList(int $uid, $cartId, bool $new, int $shipping_type = 1, int $store_id = 0)
  645. {
  646. /** @var StoreCartServices $services */
  647. $services = app()->make(StoreCartServices::class);
  648. $cartGroup = $services->getUserProductCartListV1($uid, $cartId, $new, [], $shipping_type, $store_id);
  649. /** @var StoreCouponUserServices $coupServices */
  650. $coupServices = app()->make(StoreCouponUserServices::class);
  651. return $coupServices->getUsableCouponList($uid, $cartGroup, $store_id);
  652. }
  653. /**获取单个优惠券类型
  654. * @param array $where
  655. * @return mixed
  656. */
  657. public function getOne(array $where)
  658. {
  659. if (!$where) throw new AdminException('参数缺失!');
  660. return $this->dao->getOne($where);
  661. }
  662. /**
  663. * 俩时间相差月份
  664. * @param $date1
  665. * @param $date2
  666. * @return float|int
  667. */
  668. public function getMonthNum($date1, $date2)
  669. {
  670. $date1_stamp = strtotime($date1);
  671. $date2_stamp = strtotime($date2);
  672. [$date_1['y'], $date_1['m']] = explode("-", date('Y-m', $date1_stamp));
  673. [$date_2['y'], $date_2['m']] = explode("-", date('Y-m', $date2_stamp));
  674. return abs($date_1['y'] - $date_2['y']) * 12 + $date_2['m'] - $date_1['m'];
  675. }
  676. /**
  677. * 给会员发放优惠券
  678. * @param $uid
  679. * @throws \think\db\exception\DataNotFoundException
  680. * @throws \think\db\exception\DbException
  681. * @throws \think\db\exception\ModelNotFoundException
  682. */
  683. public function sendMemberCoupon($uid, $couponId = 0)
  684. {
  685. if (!$uid) return false;
  686. /** @var UserServices $userService */
  687. $userService = app()->make(UserServices::class);
  688. /** @var StoreCouponUserServices $couponUserService */
  689. $couponUserService = app()->make(StoreCouponUserServices::class);
  690. /** @var MemberRightServices $memberRightService */
  691. $memberRightService = app()->make(MemberRightServices::class);
  692. /** @var MemberCardServices $memberCardService */
  693. $memberCardService = app()->make(MemberCardServices::class);
  694. //看付费会员是否开启
  695. $isOpenMember = $memberCardService->isOpenMemberCardCache();
  696. if (!$isOpenMember) return false;
  697. $userInfo = $userService->getUserInfo((int)$uid);
  698. //看是否会员过期
  699. if ($userInfo['is_ever_level'] == 0 && $userInfo['is_money_level'] > 0 && $userInfo['overdue_time'] < time()) {
  700. return false;
  701. }
  702. //看是否开启会员送券
  703. $isSendCoupon = $memberRightService->getMemberRightStatus("coupon");
  704. if (!$isSendCoupon) return false;
  705. if ($userInfo && (($userInfo['is_money_level'] > 0) || $userInfo['is_ever_level'] == 1)) {
  706. $monthNum = $this->getMonthNum(date('Y-m-d H:i:s', time()), date('Y-m-d H:i:s', $userInfo['overdue_time']));
  707. if ($couponId) {//手动点击领取
  708. $couponWhere['id'] = $couponId;
  709. $couponInfo = $this->getMemberCouponIssueList($couponWhere);
  710. } else {//主动批量发放
  711. $couponWhere['status'] = 1;
  712. $couponWhere['receive_type'] = 4;
  713. $couponWhere['is_del'] = 0;
  714. $couponInfo = $this->getMemberCouponIssueList($couponWhere);
  715. }
  716. if ($couponInfo) {
  717. $couponIds = array_column($couponInfo, 'id');
  718. $couponUserMonth = $couponUserService->memberCouponUserGroupBymonth(['uid' => $uid, 'couponIds' => $couponIds]);
  719. $getTime = array();
  720. if ($couponUserMonth) {
  721. $getTime = array_column($couponUserMonth, 'num', 'time');
  722. }
  723. // 判断这个月是否领取过,而且领全了
  724. //if (in_array(date('Y-m', time()), $getTime)) return false;
  725. $timeKey = date('Y-m', time());
  726. if (array_key_exists($timeKey, $getTime) && $getTime[$timeKey] == count($couponIds)) return false;
  727. //判断是否领完所有月份
  728. if (count($getTime) >= $monthNum && (array_key_exists($timeKey, $getTime) && $getTime[$timeKey] == count($couponIds)) && $userInfo['is_ever_level'] != 1 && $monthNum > 0) return false;
  729. foreach ($couponInfo as $cv) {
  730. //看之前是否手动领取过某一张,领取过就不再领取。
  731. $couponUser = $couponUserService->getUserCounponByMonth(['uid' => $uid, 'cid' => $cv['id']]);
  732. if (!$couponUser) {
  733. $this->memberIssueUserCoupon($cv['id'], $uid);
  734. }
  735. }
  736. }
  737. }
  738. return true;
  739. }
  740. /**
  741. * 获取今日新增优惠券
  742. * @throws \think\db\exception\DataNotFoundException
  743. * @throws \think\db\exception\DbException
  744. * @throws \think\db\exception\ModelNotFoundException
  745. */
  746. public function getTodayCoupon($uid)
  747. {
  748. $list = $this->dao->getTodayCoupon($uid);
  749. foreach ($list as $key => &$item) {
  750. $item['start_time'] = $item['start_time'] ? date('Y/m/d', $item['start_time']) : 0;
  751. $item['end_time'] = $item['end_time'] ? date('Y/m/d', $item['end_time']) : 0;
  752. $item['coupon_price'] = floatval($item['coupon_price']);
  753. $item['use_min_price'] = floatval($item['use_min_price']);
  754. if (isset($item['used']) && $item['used']) {
  755. unset($list[$key]);
  756. }
  757. }
  758. return array_merge($list);
  759. }
  760. /**
  761. * 获取新人券
  762. * @return array
  763. * @throws \think\db\exception\DataNotFoundException
  764. * @throws \think\db\exception\DbException
  765. * @throws \think\db\exception\ModelNotFoundException
  766. */
  767. public function getNewCoupon()
  768. {
  769. $list = $this->dao->getNewCoupon();
  770. foreach ($list as &$item) {
  771. $item['start_time'] = $item['start_time'] ? date('Y/m/d', $item['start_time']) : 0;
  772. $item['end_time'] = $item['end_time'] ? date('Y/m/d', $item['end_time']) : 0;
  773. $item['coupon_price'] = floatval($item['coupon_price']);
  774. $item['use_min_price'] = floatval($item['use_min_price']);
  775. }
  776. return $list;
  777. }
  778. /**
  779. * 获取可以使用优惠券
  780. * @param int $uid
  781. * @param array $cartList
  782. * @param array $promotions
  783. * @param int $store_id
  784. * @param bool $isMax
  785. * @return array|mixed
  786. * @throws \think\db\exception\DataNotFoundException
  787. * @throws \think\db\exception\DbException
  788. * @throws \think\db\exception\ModelNotFoundException
  789. */
  790. public function getCanUseCoupon(int $uid, array $cartList = [], array $promotions = [], int $store_id = 0, bool $isMax = true)
  791. {
  792. $userInfo = [];
  793. if ($uid) {
  794. /** @var UserServices $userService */
  795. $userService = app()->make(UserServices::class);
  796. $userInfo = $userService->getUserCacheInfo($uid);
  797. }
  798. if ($userInfo && isset($userInfo['is_money_level']) && $userInfo['is_money_level'] > 0) {
  799. $where = ['receive_type' => [1, 4]];
  800. } else {
  801. $where = ['receive_type' => 1];
  802. }
  803. /** @var StoreCouponUserServices $coupServices */
  804. $coupServices = app()->make(StoreCouponUserServices::class);
  805. //用户持有有效券
  806. $userCoupons = $coupServices->getUserAllCoupon($uid);
  807. if ($userCoupons) {
  808. $where['not_id'] = array_column($userCoupons, 'cid');
  809. }
  810. $counpons = $this->dao->getValidList($where, 0, 0, ['used' => function ($query) use ($uid) {
  811. $query->where('uid', $uid);
  812. }]);
  813. //用户已经领取的 && 没有领取的
  814. $counpons = array_merge($counpons, $userCoupons);
  815. $result = [];
  816. if ($counpons) {
  817. $promotionsList = [];
  818. if($promotions){
  819. $promotionsList = array_combine(array_column($promotions, 'id'), $promotions);
  820. }
  821. //验证是否适用门店
  822. $isApplicableStore = function ($couponInfo) use ($store_id) {
  823. if ($store_id && isset($couponInfo['applicable_type']) && isset($couponInfo['applicable_store_id'])) {
  824. $applicable_store_id = is_array($couponInfo['applicable_store_id']) ? $couponInfo['applicable_store_id'] : explode(',', $couponInfo['applicable_store_id']);
  825. //活动不适用该门店
  826. if ($couponInfo['applicable_type'] == 0 || ($couponInfo['applicable_type'] == 2 && !in_array($store_id, $applicable_store_id))) {
  827. return false;
  828. }
  829. }
  830. return true;
  831. };
  832. //
  833. $isOverlay = function($cart) use ($promotionsList) {
  834. $productInfo = $cart['productInfo'] ?? [];
  835. if (!$productInfo) {
  836. return false;
  837. }
  838. //门店独立商品 不使用优惠券
  839. $isBranchProduct = isset($productInfo['type']) && isset($productInfo['pid']) && $productInfo['type'] == 1 && !$productInfo['pid'];
  840. if ($isBranchProduct) {
  841. return false;
  842. }
  843. if (isset($cart['promotions_id']) && $cart['promotions_id']) {
  844. foreach ($cart['promotions_id'] as $key => $promotions_id) {
  845. $promotions = $promotionsList[$promotions_id] ?? [];
  846. if ($promotions && $promotions['promotions_type'] != 4){
  847. $overlay = is_string($promotions['overlay']) ? explode(',', $promotions['overlay']) : $promotions['overlay'];
  848. if (!in_array(5, $overlay)) {
  849. return false;
  850. }
  851. }
  852. }
  853. }
  854. return true;
  855. };
  856. /** @var StoreProductCategoryServices $storeCategoryServices */
  857. $storeCategoryServices = app()->make(StoreProductCategoryServices::class);
  858. /** @var StoreBrandServices $storeBrandServices */
  859. $storeBrandServices = app()->make(StoreBrandServices::class);
  860. foreach ($counpons as $coupon) {
  861. if (isset($coupon['used']) && $coupon['used']) {//所有优惠券中 已经领取跳过
  862. continue;
  863. }
  864. if (!$isApplicableStore($coupon)) {//不适用门店跳过
  865. continue;
  866. }
  867. $price = 0;
  868. $count = 0;
  869. if (isset($coupon['coupon_applicable_type'])) {//已经领取 有效优惠券
  870. $coupon['type'] = $coupon['coupon_applicable_type'];
  871. $coupon['used'] = ['id' => $coupon['id'], 'cid' => $coupon['cid']];
  872. }
  873. //&& in_array($promotions['promotions_type'], $overlay)
  874. switch ($coupon['type']) {
  875. case 0:
  876. foreach ($cartList as $cart) {
  877. if (!$isOverlay($cart)) continue;
  878. $price = bcadd((string)$price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
  879. $count++;
  880. }
  881. break;
  882. case 1://品类券
  883. $cateGorys = $storeCategoryServices->getAllById((int)$coupon['category_id']);
  884. if ($cateGorys) {
  885. $cateIds = array_column($cateGorys, 'id');
  886. foreach ($cartList as $cart) {
  887. if (!$isOverlay($cart)) continue;
  888. if (isset($cart['productInfo']['cate_id']) && array_intersect(explode(',', $cart['productInfo']['cate_id']), $cateIds)) {
  889. $price = bcadd((string)$price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
  890. $count++;
  891. }
  892. }
  893. }
  894. break;
  895. case 2://商品券
  896. foreach ($cartList as $cart) {
  897. if (!$isOverlay($cart)) continue;
  898. $product_id = isset($cart['productInfo']['pid']) && $cart['productInfo']['pid'] ? $cart['productInfo']['pid'] : ($cart['product_id'] ?? 0);
  899. if ($product_id && in_array($product_id, explode(',', $coupon['product_id']))) {
  900. $price = bcadd((string)$price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
  901. $count++;
  902. }
  903. }
  904. break;
  905. case 3://品牌券
  906. $brands = $storeBrandServices->getAllById((int)$coupon['brand_id']);
  907. if ($brands) {
  908. $brandIds = array_column($brands, 'id');
  909. foreach ($cartList as $cart) {
  910. if (!$isOverlay($cart)) continue;
  911. if (isset($cart['productInfo']['brand_id']) && in_array($cart['productInfo']['brand_id'], $brandIds)) {
  912. $price = bcadd((string)$price, (string)bcmul((string)$cart['truePrice'], (string)$cart['cart_num'], 2), 2);
  913. $count++;
  914. }
  915. }
  916. }
  917. break;
  918. }
  919. if ($count && $coupon['use_min_price'] <= $price) {
  920. //满减券
  921. if ($coupon['coupon_type'] == 1) {
  922. $couponPrice = $coupon['coupon_price'] > $price ? $price : $coupon['coupon_price'];
  923. } else {
  924. if ($coupon['coupon_price'] <= 0) {//0折
  925. $couponPrice = $price;
  926. } else if ($coupon['coupon_price'] >= 100) {
  927. $couponPrice = 0;
  928. } else {
  929. $truePrice = (float)bcmul((string)$price, bcdiv((string)$coupon['coupon_price'], '100', 2), 2);
  930. $couponPrice = (float)bcsub((string)$price, (string)$truePrice, 2);
  931. }
  932. }
  933. $coupon['coupon_price'] = floatval($coupon['coupon_price']);
  934. $coupon['use_min_price'] = floatval($coupon['use_min_price']);
  935. $coupon['true_coupon_price'] = $couponPrice;
  936. $result[] = $coupon;
  937. }
  938. }
  939. }
  940. if ($result) {
  941. $trueCouponPrice = array_column($result, 'true_coupon_price');
  942. array_multisort($trueCouponPrice, SORT_DESC, $result);
  943. }
  944. //最优的一个
  945. if ($isMax) {
  946. $useCoupon = $result[0] ?? [];
  947. return $useCoupon;
  948. }
  949. return $result;
  950. }
  951. }