StoreCombinationServices.php 31 KB

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