StoreCombinationServices.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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\combination;
  13. use app\Request;
  14. use app\services\BaseServices;
  15. use app\dao\activity\combination\StoreCombinationDao;
  16. use app\services\order\StoreOrderServices;
  17. use app\services\other\QrcodeServices;
  18. use app\services\product\product\StoreCategoryServices;
  19. use app\services\product\product\StoreDescriptionServices;
  20. use app\services\product\product\StoreProductRelationServices;
  21. use app\services\product\product\StoreProductReplyServices;
  22. use app\services\product\product\StoreProductServices;
  23. use app\services\product\sku\StoreProductAttrResultServices;
  24. use app\services\product\sku\StoreProductAttrServices;
  25. use app\services\product\sku\StoreProductAttrValueServices;
  26. use crmeb\exceptions\AdminException;
  27. use app\jobs\ProductLogJob;
  28. use crmeb\services\CacheService;
  29. use think\exception\ValidateException;
  30. /**
  31. *
  32. * Class StoreCombinationServices
  33. * @package app\services\activity
  34. * @method getPinkIdsArray(array $ids, array $field)
  35. * @method getOne(array $where, ?string $field = '*', array $with = []) 根据条件获取一条数据
  36. * @method get(int $id, array $field) 获取一条数据
  37. */
  38. class StoreCombinationServices extends BaseServices
  39. {
  40. /**
  41. * StoreCombinationServices constructor.
  42. * @param StoreCombinationDao $dao
  43. */
  44. public function __construct(StoreCombinationDao $dao)
  45. {
  46. $this->dao = $dao;
  47. }
  48. /**
  49. * 获取指定条件下的条数
  50. * @param array $where
  51. */
  52. public function getCount(array $where)
  53. {
  54. $this->dao->count($where);
  55. }
  56. /**
  57. * 获取是否有拼团商品
  58. * */
  59. public function validCombination()
  60. {
  61. return $this->dao->count([
  62. 'is_del' => 0,
  63. 'is_show' => 1,
  64. 'pinkIngTime' => true
  65. ]);
  66. }
  67. /**
  68. * 拼团商品添加
  69. * @param int $id
  70. * @param array $data
  71. */
  72. public function saveData(int $id, array $data)
  73. {
  74. $description = $data['description'];
  75. $detail = $data['attrs'];
  76. $items = $data['items'];
  77. $data['start_time'] = strtotime($data['section_time'][0]);
  78. $data['stop_time'] = strtotime($data['section_time'][1]);
  79. if ($data['stop_time'] < strtotime(date('Y-m-d', time()))) throw new AdminException('结束时间不能小于今天');
  80. $data['image'] = $data['images'][0];
  81. $data['images'] = json_encode($data['images']);
  82. $data['price'] = min(array_column($detail, 'price'));
  83. $data['quota'] = $data['quota_show'] = array_sum(array_column($detail, 'quota'));
  84. $data['stock'] = array_sum(array_column($detail, 'stock'));
  85. $data['logistics'] = implode(',', $data['logistics']);
  86. unset($data['section_time'], $data['description'], $data['attrs'], $data['items']);
  87. /** @var StoreDescriptionServices $storeDescriptionServices */
  88. $storeDescriptionServices = app()->make(StoreDescriptionServices::class);
  89. /** @var StoreProductAttrServices $storeProductAttrServices */
  90. $storeProductAttrServices = app()->make(StoreProductAttrServices::class);
  91. /** @var StoreProductServices $storeProductServices */
  92. $storeProductServices = app()->make(StoreProductServices::class);
  93. if ($data['quota'] > $storeProductServices->value(['id' => $data['product_id']], 'stock')) {
  94. throw new ValidateException('限量不能超过商品库存');
  95. }
  96. $this->transaction(function () use ($id, $data, $description, $detail, $items, $storeDescriptionServices, $storeProductAttrServices, $storeProductServices) {
  97. if ($id) {
  98. $res = $this->dao->update($id, $data);
  99. $storeDescriptionServices->saveDescription((int)$id, $description, 3);
  100. $skuList = $storeProductServices->validateProductAttr($items, $detail, (int)$id, 3);
  101. $valueGroup = $storeProductAttrServices->saveProductAttr($skuList, (int)$id, 3);
  102. if (!$res) throw new AdminException('修改失败');
  103. } else {
  104. if (!$storeProductServices->getOne(['is_show' => 1, 'is_del' => 0, 'id' => $data['product_id']])) {
  105. throw new AdminException('原商品已下架或移入回收站');
  106. }
  107. $data['add_time'] = time();
  108. $res = $this->dao->save($data);
  109. $storeDescriptionServices->saveDescription((int)$res->id, $description, 3);
  110. $skuList = $storeProductServices->validateProductAttr($items, $detail, (int)$res->id, 3);
  111. $valueGroup = $storeProductAttrServices->saveProductAttr($skuList, (int)$res->id, 3);
  112. if (!$res) throw new AdminException('添加失败');
  113. }
  114. $res = true;
  115. foreach ($valueGroup->toArray() as $item) {
  116. $res = $res && CacheService::setStock($item['unique'], (int)$item['quota_show'], 3);
  117. }
  118. if (!$res) {
  119. throw new AdminException('占用库存失败');
  120. }
  121. });
  122. }
  123. /**
  124. * 拼团列表
  125. * @param array $where
  126. * @return array
  127. */
  128. public function systemPage(array $where)
  129. {
  130. [$page, $limit] = $this->getPageValue();
  131. $list = $this->dao->getList($where, $page, $limit);
  132. $count = $this->dao->count($where);
  133. /** @var StorePinkServices $storePinkServices */
  134. $storePinkServices = app()->make(StorePinkServices::class);
  135. $countAll = $storePinkServices->getPinkCount([]);
  136. $countTeam = $storePinkServices->getPinkCount(['k_id' => 0, 'status' => 2]);
  137. $countPeople = $storePinkServices->getPinkCount(['k_id' => 0]);
  138. foreach ($list as &$item) {
  139. $item['count_people'] = $countPeople[$item['id']] ?? 0;//拼团数量
  140. $item['count_people_all'] = $countAll[$item['id']] ?? 0;//参与人数
  141. $item['count_people_pink'] = $countTeam[$item['id']] ?? 0;//成团数量
  142. $item['stop_status'] = $item['stop_time'] < time() ? 1 : 0;
  143. if ($item['is_show']) {
  144. if ($item['start_time'] > time())
  145. $item['start_name'] = '未开始';
  146. else if ($item['stop_time'] < time())
  147. $item['start_name'] = '已结束';
  148. else if ($item['stop_time'] > time() && $item['start_time'] < time()) {
  149. $item['start_name'] = '进行中';
  150. }
  151. } else $item['start_name'] = '已结束';
  152. }
  153. return compact('list', 'count');
  154. }
  155. /**
  156. * 获取详情
  157. * @param int $id
  158. * @return array|\think\Model|null
  159. */
  160. public function getInfo(int $id)
  161. {
  162. $info = $this->dao->get($id);
  163. if (!$info) {
  164. throw new ValidateException('查看的商品不存在!');
  165. }
  166. if ($info->is_del) {
  167. throw new ValidateException('您查看的团团商品已被删除!');
  168. }
  169. if ($info['start_time'])
  170. $start_time = date('Y-m-d H:i:s', $info['start_time']);
  171. if ($info['stop_time'])
  172. $stop_time = date('Y-m-d H:i:s', $info['stop_time']);
  173. if (isset($start_time) && isset($stop_time))
  174. $info['section_time'] = [$start_time, $stop_time];
  175. else
  176. $info['section_time'] = [];
  177. unset($info['start_time'], $info['stop_time']);
  178. $info['price'] = floatval($info['price']);
  179. $info['postage'] = floatval($info['postage']);
  180. $info['weight'] = floatval($info['weight']);
  181. $info['volume'] = floatval($info['volume']);
  182. $info['logistics'] = explode(',', $info['logistics']);
  183. /** @var StoreDescriptionServices $storeDescriptionServices */
  184. $storeDescriptionServices = app()->make(StoreDescriptionServices::class);
  185. $info['description'] = $storeDescriptionServices->getDescription(['product_id' => $id, 'type' => 3]);
  186. $info['attrs'] = $this->attrList($id, $info['product_id']);
  187. return $info;
  188. }
  189. /**
  190. * 获取规格
  191. * @param int $id
  192. * @param int $pid
  193. * @return mixed
  194. */
  195. public function attrList(int $id, int $pid)
  196. {
  197. /** @var StoreProductAttrResultServices $storeProductAttrResultServices */
  198. $storeProductAttrResultServices = app()->make(StoreProductAttrResultServices::class);
  199. $combinationResult = $storeProductAttrResultServices->value(['product_id' => $id, 'type' => 3], 'result');
  200. $items = json_decode($combinationResult, true)['attr'];
  201. $productAttr = $this->getAttr($items, $pid, 0);
  202. $combinationAttr = $this->getAttr($items, $id, 3);
  203. foreach ($productAttr as $pk => $pv) {
  204. foreach ($combinationAttr as &$sv) {
  205. if ($pv['detail'] == $sv['detail']) {
  206. $productAttr[$pk] = $sv;
  207. }
  208. }
  209. $productAttr[$pk]['detail'] = json_decode($productAttr[$pk]['detail']);
  210. $productAttr[$pk]['r_price'] = $productAttr[$pk]['price'];
  211. }
  212. $attrs['items'] = $items;
  213. $attrs['value'] = $productAttr;
  214. foreach ($items as $key => $item) {
  215. $header[] = ['title' => $item['value'], 'key' => 'value' . ($key + 1), 'align' => 'center', 'minWidth' => 80];
  216. }
  217. $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 120];
  218. $header[] = ['title' => '拼团价', 'slot' => 'price', 'align' => 'center', 'minWidth' => 80];
  219. $header[] = ['title' => '成本价', 'key' => 'cost', 'align' => 'center', 'minWidth' => 80];
  220. $header[] = ['title' => '日常售价', 'key' => 'r_price', 'align' => 'center', 'minWidth' => 80];
  221. $header[] = ['title' => '库存', 'key' => 'stock', 'align' => 'center', 'minWidth' => 80];
  222. $header[] = ['title' => '限量', 'key' => 'quota', 'type' => 1, 'align' => 'center', 'minWidth' => 80];
  223. $header[] = ['title' => '重量(KG)', 'key' => 'weight', 'align' => 'center', 'minWidth' => 80];
  224. $header[] = ['title' => '体积(m³)', 'key' => 'volume', 'align' => 'center', 'minWidth' => 80];
  225. $header[] = ['title' => '商品编号', 'key' => 'bar_code', 'align' => 'center', 'minWidth' => 80];
  226. $attrs['header'] = $header;
  227. return $attrs;
  228. }
  229. /**
  230. * 获得规格
  231. * @param $attr
  232. * @param $id
  233. * @param $type
  234. * @return array
  235. */
  236. public function getAttr($attr, $id, $type)
  237. {
  238. /** @var StoreProductAttrValueServices $storeProductAttrValueServices */
  239. $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  240. list($value, $head) = attr_format($attr);
  241. $valueNew = [];
  242. $count = 0;
  243. foreach ($value as $suk) {
  244. $detail = explode(',', $suk);
  245. $sukValue = $storeProductAttrValueServices->getColumn(['product_id' => $id, 'type' => $type, 'suk' => $suk], 'bar_code,cost,price,ot_price,stock,image as pic,weight,volume,brokerage,brokerage_two,quota', 'suk');
  246. if (count($sukValue)) {
  247. foreach ($detail as $k => $v) {
  248. $valueNew[$count]['value' . ($k + 1)] = $v;
  249. }
  250. $valueNew[$count]['detail'] = json_encode(array_combine($head, $detail));
  251. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  252. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  253. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  254. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  255. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  256. $valueNew[$count]['quota'] = $sukValue[$suk]['quota'] ? intval($sukValue[$suk]['quota']) : 0;
  257. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  258. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ? floatval($sukValue[$suk]['weight']) : 0;
  259. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ? floatval($sukValue[$suk]['volume']) : 0;
  260. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ? floatval($sukValue[$suk]['brokerage']) : 0;
  261. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ? floatval($sukValue[$suk]['brokerage_two']) : 0;
  262. $valueNew[$count]['_checked'] = $type != 0;
  263. $count++;
  264. }
  265. }
  266. return $valueNew;
  267. }
  268. /**
  269. * 根据id获取拼团数据列表
  270. * @param array $ids
  271. * @param string $field
  272. * @return array
  273. * @throws \think\db\exception\DataNotFoundException
  274. * @throws \think\db\exception\DbException
  275. * @throws \think\db\exception\ModelNotFoundException
  276. */
  277. public function getCombinationList()
  278. {
  279. [$page, $limit] = $this->getPageValue();
  280. $list = $this->dao->combinationList(['is_del' => 0, 'is_show' => 1, 'pinkIngTime' => true, 'storeProductId' => true], $page, $limit);
  281. foreach ($list as &$item) {
  282. $item['image'] = set_file_url($item['image']);
  283. $item['price'] = floatval($item['price']);
  284. $item['product_price'] = floatval($item['product_price']);
  285. }
  286. return $list;
  287. }
  288. /**
  289. *首页获取拼团数据
  290. * @param $where
  291. * @return array
  292. * @throws \think\db\exception\DataNotFoundException
  293. * @throws \think\db\exception\DbException
  294. * @throws \think\db\exception\ModelNotFoundException
  295. */
  296. public function getHomeList($where)
  297. {
  298. [$page, $limit] = $this->getPageValue();
  299. $where['is_del'] = 0;
  300. $where['is_show'] = 1;
  301. $where['pinkIngTime'] = true;
  302. $where['storeProductId'] = true;
  303. $data = [];
  304. $list = $this->dao->getHomeList($where, $page, $limit);
  305. foreach ($list as &$item) {
  306. $item['image'] = set_file_url($item['image']);
  307. $item['price'] = floatval($item['price']);
  308. $item['product_price'] = floatval($item['product_price']);
  309. }
  310. $data['list'] = $list;
  311. return $data;
  312. }
  313. /**
  314. * 后台页面设计获取拼团列表
  315. * @param $where
  316. */
  317. public function getDiyCombinationList($where)
  318. {
  319. $where['pinkIngTime'] = true;
  320. $where['storeProductId'] = true;
  321. [$page, $limit] = $this->getPageValue();
  322. $list = $this->dao->diyCombinationList($where, $page, $limit);
  323. $count = $this->dao->getCount($where);
  324. $cateIds = implode(',', array_column($list, 'cate_id'));
  325. /** @var StoreCategoryServices $storeCategoryServices */
  326. $storeCategoryServices = app()->make(StoreCategoryServices::class);
  327. $cateList = $storeCategoryServices->getCateArray($cateIds);
  328. foreach ($list as &$item) {
  329. $cateName = array_filter($cateList, function ($val) use ($item) {
  330. if (in_array($val['id'], explode(',', $item['cate_id']))) {
  331. return $val;
  332. }
  333. });
  334. $item['cate_name'] = implode(',', array_column($cateName, 'cate_name'));
  335. $item['store_name'] = $item['title'];
  336. $item['price'] = floatval($item['price']);
  337. $item['is_product_type'] = 2;
  338. }
  339. return compact('count', 'list');
  340. }
  341. /**
  342. * 拼团商品详情
  343. * @param Request $request
  344. * @param int $id
  345. * @return mixed
  346. * @throws \think\db\exception\DataNotFoundException
  347. * @throws \think\db\exception\DbException
  348. * @throws \think\db\exception\ModelNotFoundException
  349. */
  350. public function combinationDetail(Request $request, int $id)
  351. {
  352. $uid = (int)$request->uid();
  353. $storeInfo = $this->dao->getOne(['id' => $id], '*', ['description', 'total']);
  354. if (!$storeInfo) {
  355. throw new ValidateException('商品不存在');
  356. } else {
  357. $storeInfo = $storeInfo->toArray();
  358. }
  359. $siteUrl = sys_config('site_url');
  360. $storeInfo['image'] = set_file_url($storeInfo['image'], $siteUrl);
  361. $storeInfo['image_base'] = set_file_url($storeInfo['image'], $siteUrl);
  362. $storeInfo['sale_stock'] = 0;
  363. if ($storeInfo['stock'] > 0) $storeInfo['sale_stock'] = 1;
  364. /** @var StoreProductRelationServices $storeProductRelationServices */
  365. $storeProductRelationServices = app()->make(StoreProductRelationServices::class);
  366. $storeInfo['userCollect'] = $storeProductRelationServices->isProductRelation(['uid' => $uid, 'product_id' => $id, 'type' => 'collect', 'category' => 'product']);
  367. $storeInfo['userLike'] = false;
  368. $storeInfo['store_name'] = $storeInfo['title'];
  369. /** @var QrcodeServices $qrcodeService */
  370. $qrcodeService = app()->make(QrcodeServices::class);
  371. $storeInfo['code_base'] = $qrcodeService->getWechatQrcodePath($id . '_product_combination_detail_wap.jpg', '/pages/activity/goods_combination_details/index?id=' . $id);
  372. $data['storeInfo'] = get_thumb_water($storeInfo, 'big', ['image', 'images']);
  373. $storeInfoNew = get_thumb_water($storeInfo, 'small');
  374. $data['storeInfo']['small_image'] = $storeInfoNew['image'];
  375. /** @var StorePinkServices $pinkService */
  376. $pinkService = app()->make(StorePinkServices::class);
  377. list($pink, $pindAll) = $pinkService->getPinkList($id, true);//拼团列表
  378. $data['pink_ok_list'] = $pinkService->getPinkOkList($uid);
  379. $data['pink_ok_sum'] = $pinkService->getPinkOkSumTotalNum();
  380. $data['pink'] = $pink;
  381. $data['pindAll'] = $pindAll;
  382. /** @var StoreOrderServices $storeOrderServices */
  383. $storeOrderServices = app()->make(StoreOrderServices::class);
  384. $data['buy_num'] = $storeOrderServices->getBuyCount($uid, 'combination_id', $id);
  385. /** @var StoreProductReplyServices $storeProductReplyService */
  386. $storeProductReplyService = app()->make(StoreProductReplyServices::class);
  387. $data['reply'] = get_thumb_water($storeProductReplyService->getRecProductReply($storeInfo['product_id']), 'small', ['pics']);
  388. [$replyCount, $goodReply, $replyChance] = $storeProductReplyService->getProductReplyData((int)$storeInfo['product_id']);
  389. $data['replyChance'] = $replyChance;
  390. $data['replyCount'] = $replyCount;
  391. /** @var StoreProductAttrServices $storeProductAttrServices */
  392. $storeProductAttrServices = app()->make(StoreProductAttrServices::class);
  393. list($productAttr, $productValue) = $storeProductAttrServices->getProductAttrDetail($id, $uid, 0, 3, $storeInfo['product_id']);
  394. $data['productAttr'] = $productAttr;
  395. $data['productValue'] = $productValue;
  396. $data['routine_contact_type'] = sys_config('routine_contact_type', 0);
  397. //用户访问事件
  398. event('user.userVisit', [$uid, $id, 'combination', $storeInfo['product_id'], 'view']);
  399. //浏览记录
  400. ProductLogJob::dispatch(['visit', ['uid' => $uid, 'product_id' => $storeInfo['product_id']]]);
  401. return $data;
  402. }
  403. /**
  404. * 修改销量和库存
  405. * @param $num
  406. * @param $CombinationId
  407. * @return bool
  408. */
  409. public function decCombinationStock(int $num, int $CombinationId, string $unique)
  410. {
  411. $product_id = $this->dao->value(['id' => $CombinationId], 'product_id');
  412. if ($unique) {
  413. /** @var StoreProductAttrValueServices $skuValueServices */
  414. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  415. //减去拼团商品的sku库存增加销量
  416. $res = false !== $skuValueServices->decProductAttrStock($CombinationId, $unique, $num, 3);
  417. //减去拼团库存
  418. $res = $res && $this->dao->decStockIncSales(['id' => $CombinationId, 'type' => 3], $num);
  419. //获取拼团的sku
  420. $sku = $skuValueServices->value(['product_id' => $CombinationId, 'unique' => $unique, 'type' => 3], 'suk');
  421. //减去当前普通商品sku的库存增加销量
  422. $res = $res && $skuValueServices->decStockIncSales(['product_id' => $product_id, 'suk' => $sku, 'type' => 0], $num);
  423. } else {
  424. $res = false !== $this->dao->decStockIncSales(['id' => $CombinationId, 'type' => 3], $num);
  425. }
  426. /** @var StoreProductServices $services */
  427. $services = app()->make(StoreProductServices::class);
  428. //减去普通商品库存
  429. $res = $res && $services->decProductStock($num, $product_id);
  430. return $res;
  431. }
  432. /**
  433. * 加库存减销量
  434. * @param int $num
  435. * @param int $CombinationId
  436. * @param string $unique
  437. * @return bool
  438. */
  439. public function incCombinationStock(int $num, int $CombinationId, string $unique)
  440. {
  441. $product_id = $this->dao->value(['id' => $CombinationId], 'product_id');
  442. if ($unique) {
  443. /** @var StoreProductAttrValueServices $skuValueServices */
  444. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  445. //增加拼团商品的sku库存,减去销量
  446. $res = false !== $skuValueServices->incProductAttrStock($CombinationId, $unique, $num, 3);
  447. //增加拼团库存
  448. $res = $res && $this->dao->incStockDecSales(['id' => $CombinationId, 'type' => 3], $num);
  449. //增加当前普通商品sku的库存,减去销量
  450. $suk = $skuValueServices->value(['unique' => $unique, 'product_id' => $CombinationId], 'suk');
  451. $productUnique = $skuValueServices->value(['suk' => $suk, 'product_id' => $product_id], 'unique');
  452. if ($productUnique) {
  453. $res = $res && $skuValueServices->incProductAttrStock($product_id, $productUnique, $num);
  454. }
  455. } else {
  456. $res = false !== $this->dao->incStockDecSales(['id' => $CombinationId, 'type' => 3], $num);
  457. }
  458. /** @var StoreProductServices $services */
  459. $services = app()->make(StoreProductServices::class);
  460. //增加普通商品库存
  461. $res = $res && $services->incProductStock($num, $product_id);
  462. return $res;
  463. }
  464. /**
  465. * 获取一条拼团数据
  466. * @param $id
  467. * @param $field
  468. * @return mixed
  469. */
  470. public function getCombinationOne($id, $field = '*')
  471. {
  472. return $this->dao->validProduct($id, $field);
  473. }
  474. /**
  475. * 获取拼团详情
  476. * @param Request $request
  477. * @param int $id
  478. * @return mixed
  479. * @throws \think\db\exception\DataNotFoundException
  480. * @throws \think\db\exception\DbException
  481. * @throws \think\db\exception\ModelNotFoundException
  482. */
  483. public function getPinkInfo(Request $request, int $id)
  484. {
  485. /** @var StorePinkServices $pinkService */
  486. $pinkService = app()->make(StorePinkServices::class);
  487. $is_ok = 0;//判断拼团是否完成
  488. $userBool = 0;//判断当前用户是否在团内 0未在 1在
  489. $pinkBool = 0;//判断拼团是否成功 0未在 1在
  490. $user = $request->user();
  491. if (!$id) throw new ValidateException('参数错误');
  492. $pink = $pinkService->getPinkUserOne($id);
  493. if (!$pink) throw new ValidateException('参数错误');
  494. $pink = $pink->toArray();
  495. if (isset($pink['is_refund']) && $pink['is_refund']) {
  496. if ($pink['is_refund'] != $pink['id']) {
  497. $id = $pink['is_refund'];
  498. return $this->getPinkInfo($request, $id);
  499. } else {
  500. throw new ValidateException('订单已退款');
  501. }
  502. }
  503. list($pinkAll, $pinkT, $count, $idAll, $uidAll) = $pinkService->getPinkMemberAndPinkK($pink);
  504. if ($pinkT['status'] == 2) {
  505. $pinkBool = 1;
  506. $is_ok = 1;
  507. } else if ($pinkT['status'] == 3) {
  508. $pinkBool = -1;
  509. $is_ok = 0;
  510. } else {
  511. if ($count < 1) {//组团完成
  512. $is_ok = 1;
  513. $pinkBool = $pinkService->pinkComplete($uidAll, $idAll, $user['uid'], $pinkT);
  514. } else {
  515. $pinkBool = $pinkService->pinkFail($pinkAll, $pinkT, $pinkBool);
  516. }
  517. }
  518. if (!empty($pinkAll)) {
  519. foreach ($pinkAll as $v) {
  520. if ($v['uid'] == $user['uid']) $userBool = 1;
  521. }
  522. }
  523. if ($pinkT['uid'] == $user['uid']) $userBool = 1;
  524. $combinationOne = $this->getCombinationOne($pink['cid']);
  525. if (!$combinationOne) {
  526. throw new ValidateException('拼团不存在或已下架,请手动申请退款!');
  527. }
  528. $data['userInfo']['uid'] = $user['uid'];
  529. $data['userInfo']['nickname'] = $user['nickname'];
  530. $data['userInfo']['avatar'] = $user['avatar'];
  531. $data['is_ok'] = $is_ok;
  532. $data['userBool'] = $userBool;
  533. $data['pinkBool'] = $pinkBool;
  534. $data['store_combination'] = $combinationOne->hidden(['mer_id', 'images', 'attr', 'info', 'sort', 'sales', 'stock', 'add_time', 'is_host', 'is_show', 'is_del', 'combination', 'mer_use', 'is_postage', 'postage', 'start_time', 'stop_time', 'cost', 'browse', 'product_price'])->toArray();
  535. $data['pinkT'] = $pinkT;
  536. $data['pinkAll'] = $pinkAll;
  537. $data['count'] = $count <= 0 ? 0 : $count;
  538. $data['store_combination_host'] = $this->dao->getCombinationHost();
  539. $data['current_pink_order'] = $pinkService->getCurrentPink($id, $user['uid']);
  540. /** @var StoreProductAttrServices $storeProductAttrServices */
  541. $storeProductAttrServices = app()->make(StoreProductAttrServices::class);
  542. /** @var StoreProductAttrValueServices $storeProductAttrValueServices */
  543. $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  544. list($productAttr, $productValue) = $storeProductAttrServices->getProductAttrDetail($combinationOne['id'], $user['uid'], 0, 3, $combinationOne['product_id']);
  545. foreach ($productValue as $k => $v) {
  546. $productValue[$k]['product_stock'] = $storeProductAttrValueServices->value(['product_id' => $combinationOne['product_id'], 'suk' => $v['suk'], 'type' => 0], 'stock');
  547. }
  548. $data['store_combination']['productAttr'] = $productAttr;
  549. $data['store_combination']['productValue'] = $productValue;
  550. return $data;
  551. }
  552. /**
  553. * 验证拼团下单库存限量
  554. * @param int $uid
  555. * @param int $combinationId
  556. * @param int $cartNum
  557. * @param string $unique
  558. * @return string
  559. * @throws \think\db\exception\DataNotFoundException
  560. * @throws \think\db\exception\DbException
  561. * @throws \think\db\exception\ModelNotFoundException
  562. */
  563. public function checkoutProductStock(int $uid, int $combinationId, int $cartNum = 1, string $unique = '')
  564. {
  565. /** @var StoreProductAttrValueServices $attrValueServices */
  566. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  567. if ($unique == '') {
  568. $unique = $attrValueServices->value(['product_id' => $combinationId, 'type' => 3], 'unique');
  569. }
  570. /** @var StoreCombinationServices $combinationService */
  571. $combinationService = app()->make(StoreCombinationServices::class);
  572. $StoreCombinationInfo = $combinationService->getCombinationOne($combinationId);
  573. if (!$StoreCombinationInfo) {
  574. throw new ValidateException('该商品已下架或删除');
  575. }
  576. /** @var StoreOrderServices $orderServices */
  577. $orderServices = app()->make(StoreOrderServices::class);
  578. $userBuyCount = $orderServices->getBuyCount($uid, 'combination_id', $combinationId);
  579. if ($StoreCombinationInfo['once_num'] < $cartNum) {
  580. throw new ValidateException('每个订单限购' . $StoreCombinationInfo['once_num'] . '件');
  581. }
  582. if ($StoreCombinationInfo['num'] < ($userBuyCount + $cartNum)) {
  583. throw new ValidateException('每人总共限购' . $StoreCombinationInfo['num'] . '件');
  584. }
  585. $res = $attrValueServices->getOne(['product_id' => $combinationId, 'unique' => $unique, 'type' => 3]);
  586. if ($cartNum > $res['quota']) {
  587. throw new ValidateException('该商品库存不足' . $cartNum);
  588. }
  589. $product_stock = $attrValueServices->value(['product_id' => $StoreCombinationInfo['product_id'], 'suk' => $res['suk'], 'type' => 0], 'stock');
  590. if ($product_stock < $cartNum) {
  591. throw new ValidateException('该商品库存不足' . $cartNum);
  592. }
  593. return $unique;
  594. }
  595. /**
  596. * 验证拼团下单库存限量
  597. * @param int $uid
  598. * @param int $combinationId
  599. * @param int $cartNum
  600. * @param string $unique
  601. * @return array
  602. * @throws \think\db\exception\DataNotFoundException
  603. * @throws \think\db\exception\DbException
  604. * @throws \think\db\exception\ModelNotFoundException
  605. */
  606. public function checkCombinationStock(int $uid, int $combinationId, int $cartNum = 1, string $unique = '')
  607. {
  608. /** @var StoreProductAttrValueServices $attrValueServices */
  609. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  610. if ($unique == '') {
  611. $unique = $attrValueServices->value(['product_id' => $combinationId, 'type' => 3], 'unique');
  612. }
  613. $attrInfo = $attrValueServices->getOne(['product_id' => $combinationId, 'unique' => $unique, 'type' => 3]);
  614. if (!$attrInfo || $attrInfo['product_id'] != $combinationId) {
  615. throw new ValidateException('请选择有效的商品属性');
  616. }
  617. $StoreCombinationInfo = $productInfo = $this->getCombinationOne($combinationId, '*,title as store_name');
  618. if (!$StoreCombinationInfo) {
  619. throw new ValidateException('该商品已下架或删除');
  620. }
  621. /** @var StoreOrderServices $orderServices */
  622. $orderServices = app()->make(StoreOrderServices::class);
  623. $userBuyCount = $orderServices->getBuyCount($uid, 'combination_id', $combinationId);
  624. if ($StoreCombinationInfo['once_num'] < $cartNum) {
  625. throw new ValidateException('每个订单限购' . $StoreCombinationInfo['once_num'] . '件');
  626. }
  627. if ($StoreCombinationInfo['num'] < ($userBuyCount + $cartNum)) {
  628. throw new ValidateException('每人总共限购' . $StoreCombinationInfo['num'] . '件');
  629. }
  630. if ($cartNum > $attrInfo['quota']) {
  631. throw new ValidateException('该商品库存不足' . $cartNum);
  632. }
  633. return [$attrInfo, $unique, $productInfo];
  634. }
  635. }