StoreCombinationServices.php 28 KB

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