StoreNewcomerServices.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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\newcomer;
  13. use app\dao\activity\newcomer\StoreNewcomerDao;
  14. use app\jobs\product\ProductLogJob;
  15. use app\services\BaseServices;
  16. use app\services\diy\DiyServices;
  17. use app\services\order\StoreOrderServices;
  18. use app\services\product\branch\StoreBranchProductServices;
  19. use app\services\product\ensure\StoreProductEnsureServices;
  20. use app\services\product\label\StoreProductLabelServices;
  21. use app\services\product\product\StoreDescriptionServices;
  22. use app\services\product\product\StoreProductReplyServices;
  23. use app\services\product\product\StoreProductServices;
  24. use app\services\product\sku\StoreProductAttrServices;
  25. use app\services\product\sku\StoreProductAttrValueServices;
  26. use app\services\user\UserRelationServices;
  27. use app\services\user\UserServices;
  28. use crmeb\exceptions\AdminException;
  29. use crmeb\services\SystemConfigService;
  30. use think\exception\ValidateException;
  31. /**
  32. * Class StoreNewcomerServices
  33. * @package app\services\activity\newcomer
  34. * @mixin StoreNewcomerDao
  35. */
  36. class StoreNewcomerServices extends BaseServices
  37. {
  38. /**
  39. * 商品活动类型
  40. */
  41. const TYPE = 7;
  42. /**
  43. * StoreNewcomerServices constructor.
  44. * @param StoreNewcomerDao $dao
  45. */
  46. public function __construct(StoreNewcomerDao $dao)
  47. {
  48. $this->dao = $dao;
  49. }
  50. /**
  51. * 获取新人礼商品详情
  52. * @param int $id
  53. * @return array
  54. * @throws \think\db\exception\DataNotFoundException
  55. * @throws \think\db\exception\DbException
  56. * @throws \think\db\exception\ModelNotFoundException
  57. */
  58. public function getInfo(int $id)
  59. {
  60. $info = $this->dao->get($id, ['*'], ['product']);
  61. $res = [];
  62. if ($info) {
  63. $res = $info->toArray();
  64. $product = $res['product'] ?? [];
  65. unset($res['product'], $product['id'], $product['price']);
  66. $res = array_merge($res, $product);
  67. }
  68. return $res;
  69. }
  70. /**
  71. * 获取新人专享商品
  72. * @param array $where
  73. * @param string $field
  74. * @param array $with
  75. * @return array
  76. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\DbException
  78. * @throws \think\db\exception\ModelNotFoundException
  79. */
  80. public function getCustomerProduct(array $where = [], string $field = '*', array $with = ['product', 'attrValue'])
  81. {
  82. [$page, $limit] = $this->getPageValue();
  83. $where['is_del'] = 0;
  84. $list = $this->dao->getList($where, $field, $page, $limit, $with);
  85. $res = [];
  86. if ($list) {
  87. foreach ($list as &$item) {
  88. $product = $item['product'] ?? [];
  89. if ($product) {
  90. unset($item['product'], $product['id'], $product['price']);
  91. $item = array_merge($item, $product);
  92. $res[] = $item;
  93. }
  94. }
  95. }
  96. return $res;
  97. }
  98. /**
  99. * 保存新人礼专属商品
  100. * @param $data
  101. * @return bool
  102. */
  103. public function saveNewcomer($data)
  104. {
  105. $productIds = [];
  106. $oldProductIds = $this->dao->getColumn(['is_del' => 0], 'id,product_id');
  107. if ($oldProductIds) $oldProductIds = array_column($oldProductIds, 'product_id');
  108. if ($data) {
  109. $productIds = array_column($data, 'product_id');
  110. $this->transaction(function () use ($data) {
  111. foreach ($data as $product) {
  112. $this->saveData(0, $product);
  113. }
  114. });
  115. }
  116. $deleteIds = array_merge(array_diff($oldProductIds, $productIds));
  117. //清空现有新人礼商品
  118. if ($deleteIds) {
  119. $this->dao->update(['is_del' => 0, 'product_id' => $deleteIds], ['is_del' => 1 ,'update_time' => time()]);
  120. }
  121. return true;
  122. }
  123. /**
  124. * 保存数据
  125. * @param int $id
  126. * @param array $info
  127. */
  128. public function saveData(int $id, array $info)
  129. {
  130. if (!$info || !isset($info['product_id']) || !$info['product_id']) {
  131. throw new ValidateException('请重新选择新人专享商品');
  132. }
  133. $attr = $info['attr'];
  134. if ($attr) {
  135. foreach ($attr as $a) {
  136. if (!isset($a['unique']) || !isset($a['price'])) throw new ValidateException('请重新选择新人专享商品');
  137. if (!$a['price']) {
  138. throw new ValidateException('请填写商品专享价');
  139. }
  140. }
  141. } else {
  142. if (!$info['price']) {
  143. throw new ValidateException('请填写商品专享价');
  144. }
  145. }
  146. /** @var StoreProductServices $storeProductServices */
  147. $storeProductServices = app()->make(StoreProductServices::class);
  148. $productInfo = $storeProductServices->getOne(['is_show' => 1, 'is_del' => 0, 'is_verify' => 1, 'id' => $info['product_id']]);
  149. if (!$productInfo) {
  150. throw new AdminException('原商品已下架或移入回收站');
  151. }
  152. if ($productInfo['is_vip_product'] || $productInfo['is_presale_product']) {
  153. throw new AdminException('该商品是预售或svip专享');
  154. }
  155. if (!$id) {
  156. $newcomer = $this->dao->getOne(['product_id' => $info['product_id'], 'is_del' => 0]);
  157. $id = $newcomer['id'] ?? 0;
  158. }
  159. $data = [];
  160. $data['product_id'] = $productInfo['id'];
  161. $data['type'] = $productInfo['type'] ?? 0;
  162. $data['product_type'] = $productInfo['product_type'];
  163. $data['relation_id'] = $productInfo['relation_id'] ?? 0;
  164. $data['price'] = $info['price'] ?? 0;
  165. if ($attr) $data['price'] = min(array_column($attr, 'price'));
  166. /** @var StoreProductAttrValueServices $productAttrValueServices */
  167. $productAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  168. /** @var StoreProductAttrServices $productAttrServices */
  169. $productAttrServices = app()->make(StoreProductAttrServices::class);
  170. $attrValue = $productAttrValueServices->getList(['product_id' => $info['product_id'], 'type' => 0]);
  171. $skus = array_column($attr, 'unique');
  172. $attr = array_combine($skus, $attr);
  173. $this->transaction(function () use ($id, $data, $attrValue, $skus, $attr, $productAttrValueServices, $productAttrServices) {
  174. $newcomerAttrValue = [];
  175. if ($id) {
  176. $data['update_time'] = time();
  177. $res = $this->dao->update($id, $data);
  178. $newcomerAttrValue = $productAttrValueServices->getList(['product_id' => $id, 'type' => 7]);
  179. if (!$res) throw new AdminException('修改失败');
  180. } else {
  181. $data['add_time'] = time();
  182. $res = $this->dao->save($data);
  183. if (!$res) throw new AdminException('添加失败');
  184. $id = (int)$res->id;
  185. }
  186. $detail = [];
  187. foreach ($attrValue as $item) {
  188. if (in_array($item['unique'], $skus)) {
  189. $item['product_id'] = $id;
  190. $item['type'] = 7;
  191. if (isset($attr[$item['unique']]['price'])) $item['price'] = $attr[$item['unique']]['price'];
  192. $item['unique'] = $productAttrServices->createAttrUnique((int)$id, $item['suk']);
  193. unset($item['id']);
  194. $detail[] = $item;
  195. }
  196. }
  197. if ($newcomerAttrValue) {
  198. foreach ($newcomerAttrValue as $item) {
  199. if (in_array($item['unique'], $skus)) {
  200. $item['product_id'] = $id;
  201. $item['type'] = 7;
  202. if (isset($attr[$item['unique']]['price'])) $item['price'] = $attr[$item['unique']]['price'];
  203. unset($item['id']);
  204. $detail[] = $item;
  205. }
  206. }
  207. }
  208. $productAttrValueServices->delete(['product_id' => $id, 'type' => 7]);
  209. if ($detail) $productAttrValueServices->saveAll($detail);
  210. });
  211. }
  212. /**
  213. * 获取新人专享商品详情
  214. * @param int $id
  215. * @return array
  216. * @throws \think\db\exception\DataNotFoundException
  217. * @throws \think\db\exception\DbException
  218. * @throws \think\db\exception\ModelNotFoundException
  219. */
  220. public function newcomerDetail(int $uid, int $id)
  221. {
  222. $storeInfo = $this->getInfo($id);
  223. if (!$storeInfo) {
  224. throw new ValidateException('新人商品已下架或删除');
  225. }
  226. /** @var DiyServices $diyServices */
  227. $diyServices = app()->make(DiyServices::class);
  228. $infoDiy = $diyServices->getProductDetailDiy();
  229. //diy控制参数
  230. if (!isset($infoDiy['is_specs']) || !$infoDiy['is_specs']) {
  231. $storeInfo['specs'] = [];
  232. }
  233. $configData = SystemConfigService::more(['site_url', 'routine_contact_type', 'site_name', 'share_qrcode', 'store_self_mention', 'store_func_status', 'product_poster_title']);
  234. $siteUrl = $configData['site_url'] ?? '';
  235. $storeInfo['image'] = set_file_url($storeInfo['image'], $siteUrl);
  236. $storeInfo['image_base'] = set_file_url($storeInfo['image'], $siteUrl);
  237. /** @var StoreDescriptionServices $descriptionServices */
  238. $descriptionServices = app()->make(StoreDescriptionServices::class);
  239. $storeInfo['description'] = $descriptionServices->getDescription(['product_id' => $storeInfo['product_id']]);
  240. //品牌名称
  241. /** @var StoreProductServices $storeProductServices */
  242. $storeProductServices = app()->make(StoreProductServices::class);
  243. $productInfo = $storeProductServices->getCacheProductInfo((int)$storeInfo['product_id']);
  244. $storeInfo['brand_name'] = $storeProductServices->productIdByBrandName((int)$storeInfo['product_id'], $productInfo);
  245. $delivery_type = $storeInfo['delivery_type'] ?? $productInfo['delivery_type'];
  246. $storeInfo['delivery_type'] = is_string($delivery_type) ? explode(',', $delivery_type) : $delivery_type;
  247. /**
  248. * 判断配送方式
  249. */
  250. $storeInfo['delivery_type'] = $storeProductServices->getDeliveryType((int)$storeInfo['type'], (int)$storeInfo['type'], (int)$storeInfo['relation_id'],$storeInfo['delivery_type']);
  251. $storeInfo['total'] = $productInfo['sales'] + $productInfo['ficti'];
  252. $storeInfo['store_label'] = $storeInfo['ensure'] = [];
  253. if ($storeInfo['store_label_id']) {
  254. /** @var StoreProductLabelServices $storeProductLabelServices */
  255. $storeProductLabelServices = app()->make(StoreProductLabelServices::class);
  256. $storeInfo['store_label'] = $storeProductLabelServices->getColumn([['id', 'in', $storeInfo['store_label_id']]], 'id,label_name');
  257. }
  258. if ($storeInfo['ensure_id'] && isset($infoDiy['is_ensure']) && $infoDiy['is_ensure']) {
  259. /** @var StoreProductEnsureServices $storeProductEnsureServices */
  260. $storeProductEnsureServices = app()->make(StoreProductEnsureServices::class);
  261. $storeInfo['ensure'] = $storeProductEnsureServices->getColumn([['id', 'in', $storeInfo['ensure_id']]], 'id,name,image,desc');
  262. }
  263. /** @var StoreOrderServices $storeOrderServices */
  264. $storeOrderServices = app()->make(StoreOrderServices::class);
  265. $data['buy_num'] = $storeOrderServices->getBuyCount($uid, 7, $id);
  266. /** @var UserRelationServices $userRelationServices */
  267. $userRelationServices = app()->make(UserRelationServices::class);
  268. $storeInfo['userCollect'] = $userRelationServices->isProductRelation(['uid' => $uid, 'relation_id' => $storeInfo['product_id'], 'type' => 'collect', 'category' => UserRelationServices::CATEGORY_PRODUCT]);
  269. $storeInfo['userLike'] = 0;
  270. $storeInfo['uid'] = $uid;
  271. //商品详情
  272. $storeInfo['small_image'] = get_thumb_water($storeInfo['image']);
  273. $data['storeInfo'] = $storeInfo;
  274. $data['reply'] = [];
  275. $data['replyChance'] = $data['replyCount'] = 0;
  276. if (isset($infoDiy['is_reply']) && $infoDiy['is_reply']) {
  277. /** @var StoreProductReplyServices $storeProductReplyService */
  278. $storeProductReplyService = app()->make(StoreProductReplyServices::class);
  279. $reply = $storeProductReplyService->getRecProductReply((int)$storeInfo['product_id'], (int)($infoDiy['reply_num'] ?? 1));
  280. $data['reply'] = $reply ? get_thumb_water($reply, 'small', ['pics']) : [];
  281. [$replyCount, $goodReply, $replyChance] = $storeProductReplyService->getProductReplyData((int)$storeInfo['product_id']);
  282. $data['replyChance'] = $replyChance;
  283. $data['replyCount'] = $replyCount;
  284. }
  285. /** @var StoreProductAttrServices $storeProductAttrServices */
  286. $storeProductAttrServices = app()->make(StoreProductAttrServices::class);
  287. [$productAttr, $productValue] = $storeProductAttrServices->getProductAttrDetail($id, $uid, 0, 7, $storeInfo['product_id']);
  288. $data['productAttr'] = $productAttr;
  289. $data['productValue'] = $productValue;
  290. $data['routine_contact_type'] = $configData['routine_contact_type'] ?? 0;
  291. $data['store_func_status'] = (int)($configData['store_func_status'] ?? 1);//门店是否开启
  292. $data['store_self_mention'] = $data['store_func_status'] ? (int)($configData['store_self_mention'] ?? 0) : 0;//门店自提是否开启
  293. $data['site_name'] = $configData['site_name'] ?? '';
  294. $data['share_qrcode'] = $configData['share_qrcode'] ?? 0;
  295. $data['product_poster_title'] = $configData['product_poster_title'] ?? '';
  296. //浏览记录
  297. ProductLogJob::dispatch(['visit', ['uid' => $uid, 'id' => $id, 'product_id' => $storeInfo['product_id']], 'newcomer']);
  298. return $data;
  299. }
  300. /**
  301. * 修改秒杀库存
  302. * @param int $num
  303. * @param int $newcomerId
  304. * @param string $unique
  305. * @param int $store_id
  306. * @return bool
  307. */
  308. public function decNewcomerStock(int $num, int $newcomerId, string $unique = '', int $store_id = 0)
  309. {
  310. $product_id = $this->dao->value(['id' => $newcomerId], 'product_id');
  311. $res = true;
  312. if ($product_id && $unique) {
  313. /** @var StoreProductAttrValueServices $skuValueServices */
  314. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  315. //新人商品sku
  316. $suk = $skuValueServices->value(['unique' => $unique, 'product_id' => $newcomerId, 'type' => 7], 'suk');
  317. //平台普通商品sku unique
  318. $productUnique = $skuValueServices->value(['suk' => $suk, 'product_id' => $product_id, 'type' => 0], 'unique');
  319. /** @var StoreProductServices $services */
  320. $services = app()->make(StoreProductServices::class);
  321. //减去当前普通商品sku的库存增加销量
  322. $res = false !== $services->decProductStock($num, (int)$product_id, (string)$productUnique, $store_id);
  323. }
  324. return $res;
  325. }
  326. /**
  327. * 加库存减销量
  328. * @param int $num
  329. * @param int $newcomerId
  330. * @param string $unique
  331. * @param int $store_id
  332. * @return bool
  333. */
  334. public function incNewcomerStock(int $num, int $newcomerId, string $unique = '', int $store_id = 0)
  335. {
  336. $product_id = $this->dao->value(['id' => $newcomerId], 'product_id');
  337. $res = true;
  338. if ($product_id && $unique) {
  339. /** @var StoreProductAttrValueServices $skuValueServices */
  340. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  341. //新人商品sku
  342. $suk = $skuValueServices->value(['unique' => $unique, 'product_id' => $newcomerId, 'type' => 7], 'suk');
  343. //平台商品sku unique
  344. $productUnique = $skuValueServices->value(['suk' => $suk, 'product_id' => $product_id, 'type' => 0], 'unique');
  345. /** @var StoreProductServices $services */
  346. $services = app()->make(StoreProductServices::class);
  347. //减去当前普通商品sku的库存增加销量
  348. $res = $services->incProductStock($num, (int)$product_id, (string)$productUnique, $store_id);
  349. }
  350. return $res;
  351. }
  352. /**
  353. * 下单|加入购物车验证秒杀商品库存
  354. * @param int $uid
  355. * @param int $newcomerId
  356. * @param int $cartNum
  357. * @param string $unique
  358. * @return array
  359. * @throws \think\db\exception\DataNotFoundException
  360. * @throws \think\db\exception\DbException
  361. * @throws \think\db\exception\ModelNotFoundException
  362. */
  363. public function checkNewcomerStock(int $uid, int $newcomerId, int $cartNum = 1, string $unique = '')
  364. {
  365. if (!$this->checkUserNewcomer($uid)) {
  366. throw new ValidateException('您已无法享受新人专享价');
  367. }
  368. /** @var StoreProductAttrValueServices $attrValueServices */
  369. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  370. if ($unique == '') {
  371. $unique = $attrValueServices->value(['product_id' => $newcomerId, 'type' => 7], 'unique');
  372. }
  373. //检查商品活动状态
  374. $newcomerInfo = $this->getInfo($newcomerId);
  375. if (!$newcomerInfo) {
  376. throw new ValidateException('该活动已下架');
  377. }
  378. $attrInfo = $attrValueServices->getOne(['product_id' => $newcomerId, 'unique' => $unique, 'type' => 7]);
  379. if (!$attrInfo || $attrInfo['product_id'] != $newcomerId) {
  380. throw new ValidateException('请选择有效的商品属性');
  381. }
  382. $suk = $attrInfo['suk'];
  383. $productAttrInfo = $attrValueServices->getOne(['suk' => $suk, 'product_id' => $newcomerInfo['product_id'], 'type' => 0]);
  384. if (!$productAttrInfo) {
  385. throw new ValidateException('请选择有效的商品属性');
  386. }
  387. //库存要验证愿商品库存
  388. if ($cartNum > $productAttrInfo['stock']) {
  389. throw new ValidateException('该商品库存不足' . $cartNum);
  390. }
  391. return [$attrInfo, $unique, $newcomerInfo];
  392. }
  393. /**
  394. * 验证用户是否可以享受首单优惠
  395. * @param int $uid
  396. * @param $userInfo
  397. * @return array
  398. */
  399. public function checkUserFirstDiscount(int $uid, $userInfo = [])
  400. {
  401. if (!$uid) {
  402. return [];
  403. }
  404. //开启新人礼
  405. if (!sys_config('newcomer_status')) {
  406. return [];
  407. }
  408. //开启首单优惠
  409. if (!sys_config('first_order_status')) {
  410. return [];
  411. }
  412. if (!$userInfo) {
  413. /** @var UserServices $userServices */
  414. $userServices = app()->make(UserServices::class);
  415. $userInfo = $userServices->getUserInfo($uid);
  416. if (!$userInfo) {
  417. return [];
  418. }
  419. }
  420. if (isset($userInfo['is_first_order']) && $userInfo['is_first_order'] != 0) {
  421. return [];
  422. }
  423. $timeStatus = sys_config('newcomer_limit_status', 1);
  424. if ($timeStatus) {//设置限时,且超过时间不再享受
  425. $time = sys_config('newcomer_limit_time');
  426. if ($time) {
  427. $time = (int)bcsub((string)time(), (string)bcmul((string)$time, '86400'));
  428. if ($time > $userInfo['add_time']) {
  429. return [];
  430. }
  431. }
  432. }
  433. /** @var StoreOrderServices $storeOrderServices */
  434. $storeOrderServices = app()->make(StoreOrderServices::class);
  435. $count = $storeOrderServices->getCount([['type', 'not in', [7]], ['uid', '=', $uid]]);
  436. if ($count) {//有过订单
  437. return [];
  438. }
  439. $discount = sys_config('first_order_discount', 100);
  440. $discount_limit = sys_config('first_order_discount_limit', 0);
  441. return [$discount, $discount_limit];
  442. }
  443. /**
  444. * 验证用户是否可以购买新人专享
  445. * @param int $uid
  446. * @param $userInfo
  447. * @return bool
  448. */
  449. public function checkUserNewcomer(int $uid, $userInfo = [])
  450. {
  451. if (!$uid) {
  452. return false;
  453. }
  454. //开启新人礼
  455. if (!sys_config('newcomer_status')) {
  456. return false;
  457. }
  458. //开启新人专享
  459. if (!sys_config('register_price_status')) {
  460. return false;
  461. }
  462. if (!$userInfo) {
  463. /** @var UserServices $userServices */
  464. $userServices = app()->make(UserServices::class);
  465. $userInfo = $userServices->getUserInfo($uid);
  466. if (!$userInfo) {
  467. return false;
  468. }
  469. }
  470. if (isset($userInfo['is_newcomer']) && $userInfo['is_newcomer'] != 0) {
  471. return false;
  472. }
  473. $timeStatus = sys_config('newcomer_limit_status', 1);
  474. if ($timeStatus) {//设置限时,且超过时间不再享受
  475. $time = sys_config('newcomer_limit_time');
  476. if ($time) {
  477. $time = (int)bcsub((string)time(), (string)bcmul((string)$time, '86400'));
  478. if ($time > $userInfo['add_time']) {
  479. return false;
  480. }
  481. }
  482. }
  483. /** @var StoreOrderServices $orderServices */
  484. $orderServices = app()->make(StoreOrderServices::class);
  485. $count = $orderServices->count(['uid' => $uid, 'type' => 9]);
  486. if ($count) {
  487. return false;
  488. }
  489. return true;
  490. }
  491. /**
  492. * 用户会员卡激活状态
  493. * @param int $uid
  494. * @return bool|mixed
  495. */
  496. public function setUserLevel(int $uid)
  497. {
  498. if (!$uid) {
  499. return false;
  500. }
  501. //开启会员卡 且不需要激活 直接设置为已激活状态
  502. if (sys_config('member_func_status') && !sys_config('level_activate_status')) {
  503. /** @var UserServices $userServices */
  504. $userServices = app()->make(UserServices::class);
  505. return $userServices->update($uid, ['level_status' => 1]);
  506. }
  507. return true;
  508. }
  509. /**
  510. * 用户注册设置:首单优惠、新人专享是否可用 -1:不可用 0:未使用 1:已使用
  511. * @param int $uid
  512. * @return false|mixed
  513. */
  514. public function setUserNewcomer(int $uid)
  515. {
  516. if (!$uid) {
  517. return false;
  518. }
  519. //是否开启新人礼
  520. $newcomer_status = sys_config('newcomer_status');
  521. $update = [];
  522. if ($newcomer_status) {
  523. //开启首单优惠
  524. $first_order = sys_config('first_order_status');
  525. $update['is_first_order'] = $first_order ? 0 : -1;
  526. //开启新人专享
  527. $is_newcomer = sys_config('register_price_status');
  528. $update['is_newcomer'] = $is_newcomer ? 0 : -1;
  529. } else {//不可用,数据记录已使用
  530. $update = ['is_first_order' => -1, 'is_newcomer' => -1];
  531. }
  532. /** @var UserServices $userServices */
  533. $userServices = app()->make(UserServices::class);
  534. return $userServices->update($uid, $update);
  535. }
  536. /**
  537. * 下单修改用户首单优惠
  538. * @param int $uid
  539. * @param $orderInfo
  540. * @return bool
  541. */
  542. public function updateUserNewcomer(int $uid, $orderInfo)
  543. {
  544. if (!$uid || !$orderInfo) {
  545. return false;
  546. }
  547. $update = [];
  548. //首单优惠
  549. if (isset($orderInfo['first_order_price']) && $orderInfo['first_order_price'] > 0) {
  550. $update['is_first_order'] = 1;
  551. }
  552. //新人专享
  553. if (isset($orderInfo['type']) && $orderInfo['type'] == 7) {
  554. $update['is_newcomer'] = 1;
  555. }
  556. if ($update) {
  557. /** @var UserServices $userServices */
  558. $userServices = app()->make(UserServices::class);
  559. $userServices->update($uid, $update);
  560. $userServices->cacheTag()->clear();
  561. }
  562. return true;
  563. }
  564. /**
  565. * 获取新人专享商品
  566. * @param int $uid
  567. * @param array $where
  568. * @return array
  569. * @throws \think\db\exception\DataNotFoundException
  570. * @throws \think\db\exception\DbException
  571. * @throws \think\db\exception\ModelNotFoundException
  572. */
  573. public function getDiyNewcomerList(int $uid = 0, array $where = [])
  574. {
  575. if ($uid) {//验证权限
  576. //验证用户是否享受过新人礼
  577. if (!$this->checkUserNewcomer($uid)) {
  578. return [];
  579. }
  580. } else {//验证是否开启 无登录:如果开启新人礼默认要展示
  581. //开启新人礼 || 开启新人专享
  582. if (!sys_config('newcomer_status') || !sys_config('register_price_status')) {
  583. return [];
  584. }
  585. }
  586. [$page, $limit] = $this->getPageValue();
  587. $where['is_del'] = 0;
  588. $list = $this->dao->getList($where, '*', $page, $limit, ['product']);
  589. $res = [];
  590. if ($list) {
  591. foreach ($list as &$item) {
  592. $product = $item['product'] ?? [];
  593. if ($product) {
  594. unset($item['product'], $product['id'], $product['price'], $product['sales']);
  595. $item = array_merge($item, $product);
  596. $res[] = $item;
  597. }
  598. }
  599. }
  600. return $res;
  601. }
  602. }