StoreCombinationServices.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\exceptions\ApiException;
  29. use crmeb\services\CacheService;
  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(400096);
  80. $data['image'] = $data['image'];
  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 AdminException(400090);
  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(100007);
  103. } else {
  104. if (!$storeProductServices->getOne(['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(100022);
  113. }
  114. });
  115. }
  116. /**
  117. * 拼团列表
  118. * @param array $where
  119. * @return array
  120. */
  121. public function systemPage(array $where)
  122. {
  123. [$page, $limit] = $this->getPageValue();
  124. $list = $this->dao->getList($where, $page, $limit);
  125. $count = $this->dao->count($where);
  126. /** @var StorePinkServices $storePinkServices */
  127. $storePinkServices = app()->make(StorePinkServices::class);
  128. $countAll = $storePinkServices->getPinkCount([]);
  129. $countTeam = $storePinkServices->getPinkCount(['k_id' => 0, 'status' => 2]);
  130. $countPeople = $storePinkServices->getPinkCount(['k_id' => 0]);
  131. $stopIds = [];
  132. foreach ($list as &$item) {
  133. $item['count_people'] = $countPeople[$item['id']] ?? 0;//拼团数量
  134. $item['count_people_all'] = $countAll[$item['id']] ?? 0;//参与人数
  135. $item['count_people_pink'] = $countTeam[$item['id']] ?? 0;//成团数量
  136. $item['stop_status'] = $item['stop_time'] < time() ? 1 : 0;
  137. if ($item['is_show']) {
  138. if ($item['start_time'] > time()) {
  139. $item['start_name'] = '未开始';
  140. } else if ($item['stop_time'] < time()) {
  141. $item['start_name'] = '已结束';
  142. $item['is_show'] = 0;
  143. $stopIds[] = $item['id'];
  144. } else if ($item['stop_time'] > time() && $item['start_time'] < time()) {
  145. $item['start_name'] = '进行中';
  146. }
  147. } else {
  148. $item['start_name'] = '已结束';
  149. }
  150. }
  151. if ($stopIds) {
  152. $this->dao->batchUpdate($stopIds, ['is_show' => 0]);
  153. }
  154. return compact('list', 'count');
  155. }
  156. /**
  157. * 获取详情
  158. * @param int $id
  159. * @return array|\think\Model|null
  160. */
  161. public function getInfo(int $id)
  162. {
  163. $info = $this->dao->get($id);
  164. if (!$info) {
  165. throw new ApiException(410294);
  166. }
  167. if ($info->is_del) {
  168. throw new ApiException(410311);
  169. }
  170. if ($info['start_time'])
  171. $start_time = date('Y-m-d H:i:s', $info['start_time']);
  172. if ($info['stop_time'])
  173. $stop_time = date('Y-m-d H:i:s', $info['stop_time']);
  174. if (isset($start_time) && isset($stop_time))
  175. $info['section_time'] = [$start_time, $stop_time];
  176. else
  177. $info['section_time'] = [];
  178. unset($info['start_time'], $info['stop_time']);
  179. $info['price'] = floatval($info['price']);
  180. $info['postage'] = floatval($info['postage']);
  181. $info['weight'] = floatval($info['weight']);
  182. $info['volume'] = floatval($info['volume']);
  183. $info['logistics'] = explode(',', $info['logistics']);
  184. /** @var StoreDescriptionServices $storeDescriptionServices */
  185. $storeDescriptionServices = app()->make(StoreDescriptionServices::class);
  186. $info['description'] = $storeDescriptionServices->getDescription(['product_id' => $id, 'type' => 3]);
  187. $info['attrs'] = $this->attrList($id, $info['product_id']);
  188. return $info;
  189. }
  190. /**
  191. * 获取规格
  192. * @param int $id
  193. * @param int $pid
  194. * @return mixed
  195. */
  196. public function attrList(int $id, int $pid)
  197. {
  198. /** @var StoreProductAttrResultServices $storeProductAttrResultServices */
  199. $storeProductAttrResultServices = app()->make(StoreProductAttrResultServices::class);
  200. $combinationResult = $storeProductAttrResultServices->value(['product_id' => $id, 'type' => 3], 'result');
  201. $items = json_decode($combinationResult, true)['attr'];
  202. $productAttr = $this->getAttr($items, $pid, 0);
  203. $combinationAttr = $this->getAttr($items, $id, 3);
  204. foreach ($productAttr as $pk => $pv) {
  205. foreach ($combinationAttr as &$sv) {
  206. if ($pv['detail'] == $sv['detail']) {
  207. $productAttr[$pk] = $sv;
  208. $productAttr[$pk]['r_price'] = $pv['price'];
  209. }
  210. }
  211. $productAttr[$pk]['detail'] = json_decode($productAttr[$pk]['detail']);
  212. $productAttr[$pk]['r_price'] = $productAttr[$pk]['r_price'] ?? $productAttr[$pk]['price'];
  213. }
  214. $attrs['items'] = $items;
  215. $attrs['value'] = $productAttr;
  216. foreach ($items as $key => $item) {
  217. $header[] = ['title' => $item['value'], 'key' => 'value' . ($key + 1), 'align' => 'center', 'minWidth' => 80];
  218. }
  219. $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 120];
  220. $header[] = ['title' => '拼团价', 'slot' => 'price', 'align' => 'center', 'minWidth' => 80];
  221. $header[] = ['title' => '成本价', 'key' => 'cost', 'align' => 'center', 'minWidth' => 80];
  222. $header[] = ['title' => '日常售价', 'key' => 'r_price', 'align' => 'center', 'minWidth' => 80];
  223. $header[] = ['title' => '库存', 'key' => 'stock', 'align' => 'center', 'minWidth' => 80];
  224. $header[] = ['title' => '限量', 'slot' => 'quota', 'type' => 1, 'align' => 'center', 'minWidth' => 80];
  225. $header[] = ['title' => '重量(KG)', 'key' => 'weight', 'align' => 'center', 'minWidth' => 80];
  226. $header[] = ['title' => '体积(m³)', 'key' => 'volume', 'align' => 'center', 'minWidth' => 80];
  227. $header[] = ['title' => '商品编号', 'key' => 'bar_code', 'align' => 'center', 'minWidth' => 80];
  228. $attrs['header'] = $header;
  229. return $attrs;
  230. }
  231. /**
  232. * 获得规格
  233. * @param $attr
  234. * @param $id
  235. * @param $type
  236. * @return array
  237. */
  238. public function getAttr($attr, $id, $type)
  239. {
  240. /** @var StoreProductAttrValueServices $storeProductAttrValueServices */
  241. $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  242. list($value, $head) = attr_format($attr);
  243. $valueNew = [];
  244. $count = 0;
  245. foreach ($value as $suk) {
  246. $detail = explode(',', $suk);
  247. $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');
  248. if (count($sukValue)) {
  249. foreach ($detail as $k => $v) {
  250. $valueNew[$count]['value' . ($k + 1)] = $v;
  251. }
  252. $valueNew[$count]['detail'] = json_encode(array_combine($head, $detail));
  253. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  254. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  255. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  256. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  257. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  258. $valueNew[$count]['quota'] = $sukValue[$suk]['quota'] ? intval($sukValue[$suk]['quota']) : 0;
  259. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  260. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ? floatval($sukValue[$suk]['weight']) : 0;
  261. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ? floatval($sukValue[$suk]['volume']) : 0;
  262. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ? floatval($sukValue[$suk]['brokerage']) : 0;
  263. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ? floatval($sukValue[$suk]['brokerage_two']) : 0;
  264. $valueNew[$count]['_checked'] = $type != 0;
  265. $count++;
  266. }
  267. }
  268. return $valueNew;
  269. }
  270. /**
  271. * 根据id获取拼团数据列表
  272. * @param array $ids
  273. * @param string $field
  274. * @return array
  275. * @throws \think\db\exception\DataNotFoundException
  276. * @throws \think\db\exception\DbException
  277. * @throws \think\db\exception\ModelNotFoundException
  278. */
  279. public function getCombinationList()
  280. {
  281. [$page, $limit] = $this->getPageValue();
  282. $list = $this->dao->combinationList(['is_del' => 0, 'is_show' => 1, 'pinkIngTime' => true, 'storeProductId' => true], $page, $limit);
  283. foreach ($list as &$item) {
  284. $item['image'] = set_file_url($item['image']);
  285. $item['price'] = floatval($item['price']);
  286. $item['product_price'] = floatval($item['product_price']);
  287. }
  288. return $list;
  289. }
  290. /**
  291. *首页获取拼团数据
  292. * @param $where
  293. * @return array
  294. * @throws \think\db\exception\DataNotFoundException
  295. * @throws \think\db\exception\DbException
  296. * @throws \think\db\exception\ModelNotFoundException
  297. */
  298. public function getHomeList($where)
  299. {
  300. [$page, $limit] = $this->getPageValue();
  301. $where['is_del'] = 0;
  302. $where['is_show'] = 1;
  303. $where['pinkIngTime'] = true;
  304. $where['storeProductId'] = true;
  305. $data = [];
  306. $list = $this->dao->getHomeList($where, $page, $limit);
  307. foreach ($list as &$item) {
  308. $item['image'] = set_file_url($item['image']);
  309. $item['price'] = floatval($item['price']);
  310. $item['product_price'] = floatval($item['product_price']);
  311. }
  312. $data['list'] = $list;
  313. return $data;
  314. }
  315. /**
  316. * 后台页面设计获取拼团列表
  317. * @param $where
  318. */
  319. public function getDiyCombinationList($where)
  320. {
  321. $where['pinkIngTime'] = true;
  322. $where['storeProductId'] = true;
  323. [$page, $limit] = $this->getPageValue();
  324. $list = $this->dao->diyCombinationList($where, $page, $limit);
  325. $count = $this->dao->getCount($where);
  326. $cateIds = implode(',', array_column($list, 'cate_id'));
  327. /** @var StoreCategoryServices $storeCategoryServices */
  328. $storeCategoryServices = app()->make(StoreCategoryServices::class);
  329. $cateList = $storeCategoryServices->getCateArray($cateIds);
  330. foreach ($list as &$item) {
  331. $cateName = array_filter($cateList, function ($val) use ($item) {
  332. if (in_array($val['id'], explode(',', $item['cate_id']))) {
  333. return $val;
  334. }
  335. });
  336. $item['cate_name'] = implode(',', array_column($cateName, 'cate_name'));
  337. $item['store_name'] = $item['title'];
  338. $item['price'] = floatval($item['price']);
  339. $item['is_product_type'] = 2;
  340. }
  341. return compact('count', 'list');
  342. }
  343. /**
  344. * 拼团商品详情
  345. * @param Request $request
  346. * @param int $id
  347. * @return mixed
  348. * @throws \think\db\exception\DataNotFoundException
  349. * @throws \think\db\exception\DbException
  350. * @throws \think\db\exception\ModelNotFoundException
  351. */
  352. public function combinationDetail(Request $request, int $id)
  353. {
  354. $uid = (int)$request->uid();
  355. $storeInfo = $this->dao->getOne(['id' => $id], '*', ['description', 'total']);
  356. if (!$storeInfo) {
  357. throw new ApiException(410311);
  358. } else {
  359. $storeInfo = $storeInfo->toArray();
  360. }
  361. $siteUrl = sys_config('site_url');
  362. $storeInfo['image'] = set_file_url($storeInfo['image'], $siteUrl);
  363. $storeInfo['image_base'] = set_file_url($storeInfo['image'], $siteUrl);
  364. $storeInfo['sale_stock'] = 0;
  365. if ($storeInfo['stock'] > 0) $storeInfo['sale_stock'] = 1;
  366. /** @var StoreProductRelationServices $storeProductRelationServices */
  367. $storeProductRelationServices = app()->make(StoreProductRelationServices::class);
  368. $storeInfo['userCollect'] = $storeProductRelationServices->isProductRelation(['uid' => $uid, 'product_id' => $id, 'type' => 'collect', 'category' => 'product']);
  369. $storeInfo['userLike'] = false;
  370. $storeInfo['store_name'] = $storeInfo['title'];
  371. $storeInfo['product_is_show'] = app()->make(StoreProductServices::class)->value($storeInfo['product_id'], 'is_show');
  372. if (sys_config('share_qrcode', 0) && request()->isWechat()) {
  373. /** @var QrcodeServices $qrcodeService */
  374. $qrcodeService = app()->make(QrcodeServices::class);
  375. $storeInfo['wechat_code'] = $qrcodeService->getTemporaryQrcode('combination-' . $id, $uid)->url;
  376. } else {
  377. $storeInfo['wechat_code'] = '';
  378. }
  379. $data['storeInfo'] = get_thumb_water($storeInfo, 'big', ['image', 'images']);
  380. $storeInfoNew = get_thumb_water($storeInfo, 'small');
  381. $data['storeInfo']['small_image'] = $storeInfoNew['image'];
  382. /** @var StorePinkServices $pinkService */
  383. $pinkService = app()->make(StorePinkServices::class);
  384. list($pink, $pinkAll) = $pinkService->getPinkList($id, true);//拼团列表
  385. $data['pink_ok_list'] = $pinkService->getPinkOkList($uid);
  386. $data['pink_ok_sum'] = $pinkService->getPinkOkSumTotalNum();
  387. $data['pink'] = $pink;
  388. $data['pinkAll'] = $pinkAll;
  389. /** @var StoreOrderServices $storeOrderServices */
  390. $storeOrderServices = app()->make(StoreOrderServices::class);
  391. $data['buy_num'] = $storeOrderServices->getBuyCount($uid, 'combination_id', $id);
  392. /** @var StoreProductReplyServices $storeProductReplyService */
  393. $storeProductReplyService = app()->make(StoreProductReplyServices::class);
  394. $data['reply'] = get_thumb_water($storeProductReplyService->getRecProductReply($storeInfo['product_id']), 'small', ['pics']);
  395. [$replyCount, $goodReply, $replyChance] = $storeProductReplyService->getProductReplyData((int)$storeInfo['product_id']);
  396. $data['replyChance'] = $replyChance;
  397. $data['replyCount'] = $replyCount;
  398. /** @var StoreProductAttrServices $storeProductAttrServices */
  399. $storeProductAttrServices = app()->make(StoreProductAttrServices::class);
  400. list($productAttr, $productValue) = $storeProductAttrServices->getProductAttrDetail($id, $uid, 0, 3, $storeInfo['product_id']);
  401. $data['productAttr'] = $productAttr;
  402. $data['productValue'] = $productValue;
  403. $data['routine_contact_type'] = sys_config('routine_contact_type', 0);
  404. //用户访问事件
  405. event('UserVisitListener', [$uid, $id, 'combination', $storeInfo['product_id'], 'view']);
  406. //浏览记录
  407. ProductLogJob::dispatch(['visit', ['uid' => $uid, 'product_id' => $storeInfo['product_id']]]);
  408. return $data;
  409. }
  410. /**
  411. * 修改销量和库存
  412. * @param $num
  413. * @param $CombinationId
  414. * @return bool
  415. */
  416. public function decCombinationStock(int $num, int $CombinationId, string $unique)
  417. {
  418. $product_id = $this->dao->value(['id' => $CombinationId], 'product_id');
  419. if ($unique) {
  420. /** @var StoreProductAttrValueServices $skuValueServices */
  421. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  422. //减去拼团商品的sku库存增加销量
  423. $res = false !== $skuValueServices->decProductAttrStock($CombinationId, $unique, $num, 3);
  424. //减去拼团库存
  425. $res = $res && $this->dao->decStockIncSales(['id' => $CombinationId, 'type' => 3], $num);
  426. //获取拼团的sku
  427. $sku = $skuValueServices->value(['product_id' => $CombinationId, 'unique' => $unique, 'type' => 3], 'suk');
  428. //减去当前普通商品sku的库存增加销量
  429. $res = $res && $skuValueServices->decStockIncSales(['product_id' => $product_id, 'suk' => $sku, 'type' => 0], $num);
  430. } else {
  431. $res = false !== $this->dao->decStockIncSales(['id' => $CombinationId, 'type' => 3], $num);
  432. }
  433. /** @var StoreProductServices $services */
  434. $services = app()->make(StoreProductServices::class);
  435. //减去普通商品库存
  436. $res = $res && $services->decProductStock($num, $product_id);
  437. return $res;
  438. }
  439. /**
  440. * 加库存减销量
  441. * @param int $num
  442. * @param int $CombinationId
  443. * @param string $unique
  444. * @return bool
  445. */
  446. public function incCombinationStock(int $num, int $CombinationId, string $unique)
  447. {
  448. $product_id = $this->dao->value(['id' => $CombinationId], 'product_id');
  449. if ($unique) {
  450. /** @var StoreProductAttrValueServices $skuValueServices */
  451. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  452. //增加拼团商品的sku库存,减去销量
  453. $res = false !== $skuValueServices->incProductAttrStock($CombinationId, $unique, $num, 3);
  454. //增加拼团库存
  455. $res = $res && $this->dao->incStockDecSales(['id' => $CombinationId, 'type' => 3], $num);
  456. //增加当前普通商品sku的库存,减去销量
  457. $suk = $skuValueServices->value(['unique' => $unique, 'product_id' => $CombinationId], 'suk');
  458. $productUnique = $skuValueServices->value(['suk' => $suk, 'product_id' => $product_id], 'unique');
  459. if ($productUnique) {
  460. $res = $res && $skuValueServices->incProductAttrStock($product_id, $productUnique, $num);
  461. }
  462. } else {
  463. $res = false !== $this->dao->incStockDecSales(['id' => $CombinationId, 'type' => 3], $num);
  464. }
  465. /** @var StoreProductServices $services */
  466. $services = app()->make(StoreProductServices::class);
  467. //增加普通商品库存
  468. $res = $res && $services->incProductStock($num, $product_id);
  469. return $res;
  470. }
  471. /**
  472. * 获取一条拼团数据
  473. * @param $id
  474. * @param $field
  475. * @return mixed
  476. */
  477. public function getCombinationOne($id, $field = '*')
  478. {
  479. return $this->dao->validProduct($id, $field);
  480. }
  481. /**
  482. * 获取拼团详情
  483. * @param Request $request
  484. * @param int $id
  485. * @return mixed
  486. * @throws \think\db\exception\DataNotFoundException
  487. * @throws \think\db\exception\DbException
  488. * @throws \think\db\exception\ModelNotFoundException
  489. */
  490. public function getPinkInfo(Request $request, int $id)
  491. {
  492. /** @var StorePinkServices $pinkService */
  493. $pinkService = app()->make(StorePinkServices::class);
  494. $is_ok = 0;//判断拼团是否完成
  495. $userBool = 0;//判断当前用户是否在团内 0未在 1在
  496. $pinkBool = 0;//判断拼团是否成功 0未在 1在
  497. $user = $request->user();
  498. if (!$id) throw new ApiException(100100);
  499. $pink = $pinkService->getPinkUserOne($id);
  500. if (!$pink) throw new ApiException(100100);
  501. $pink = $pink->toArray();
  502. if (isset($pink['is_refund']) && $pink['is_refund']) {
  503. if ($pink['is_refund'] != $pink['id']) {
  504. $id = $pink['is_refund'];
  505. return $this->getPinkInfo($request, $id);
  506. } else {
  507. throw new ApiException(410226);
  508. }
  509. }
  510. list($pinkAll, $pinkT, $count, $idAll, $uidAll) = $pinkService->getPinkMemberAndPinkK($pink);
  511. if ($pinkT['status'] == 2) {
  512. $pinkBool = 1;
  513. $is_ok = 1;
  514. } else if ($pinkT['status'] == 3) {
  515. $pinkBool = -1;
  516. $is_ok = 0;
  517. } else {
  518. if ($count < 1) {//组团完成
  519. $is_ok = 1;
  520. $pinkBool = $pinkService->pinkComplete($uidAll, $idAll, $user['uid'], $pinkT);
  521. } else {
  522. $pinkBool = $pinkService->pinkFail($pinkAll, $pinkT, $pinkBool);
  523. }
  524. $pinkT = $pinkService->getPinkUserOne($pinkT['id']);
  525. }
  526. if (!empty($pinkAll)) {
  527. foreach ($pinkAll as $v) {
  528. if ($v['uid'] == $user['uid']) $userBool = 1;
  529. }
  530. }
  531. if ($pinkT['uid'] == $user['uid']) $userBool = 1;
  532. $combinationOne = $this->getCombinationOne($pink['cid']);
  533. if (!$combinationOne) {
  534. throw new ApiException(410312);
  535. }
  536. $data['userInfo']['uid'] = $user['uid'];
  537. $data['userInfo']['nickname'] = $user['nickname'];
  538. $data['userInfo']['avatar'] = $user['avatar'];
  539. $data['is_ok'] = $is_ok;
  540. $data['userBool'] = $userBool;
  541. $data['pinkBool'] = $pinkBool;
  542. $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();
  543. $data['pinkT'] = $pinkT;
  544. $data['pinkAll'] = $pinkAll;
  545. $data['count'] = $count <= 0 ? 0 : $count;
  546. $data['store_combination_host'] = $this->dao->getCombinationHost();
  547. $data['current_pink_order'] = $pinkService->getCurrentPink($id, $user['uid']);
  548. /** @var StoreProductAttrServices $storeProductAttrServices */
  549. $storeProductAttrServices = app()->make(StoreProductAttrServices::class);
  550. /** @var StoreProductAttrValueServices $storeProductAttrValueServices */
  551. $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  552. list($productAttr, $productValue) = $storeProductAttrServices->getProductAttrDetail($combinationOne['id'], $user['uid'], 0, 3, $combinationOne['product_id']);
  553. foreach ($productValue as $k => $v) {
  554. $productValue[$k]['product_stock'] = $storeProductAttrValueServices->value(['product_id' => $combinationOne['product_id'], 'suk' => $v['suk'], 'type' => 0], 'stock');
  555. }
  556. $data['store_combination']['productAttr'] = $productAttr;
  557. $data['store_combination']['productValue'] = $productValue;
  558. /** @var StoreOrderServices $orderServices */
  559. $orderServices = app()->make(StoreOrderServices::class);
  560. $data['order_pid'] = $orderServices->value(['order_id' => $data['current_pink_order']], 'pid');
  561. return $data;
  562. }
  563. /**
  564. * 验证拼团下单库存限量
  565. * @param int $uid
  566. * @param int $combinationId
  567. * @param int $cartNum
  568. * @param string $unique
  569. * @return array
  570. * @throws \think\db\exception\DataNotFoundException
  571. * @throws \think\db\exception\DbException
  572. * @throws \think\db\exception\ModelNotFoundException
  573. */
  574. public function checkCombinationStock(int $uid, int $combinationId, int $cartNum = 1, string $unique = '')
  575. {
  576. /** @var StoreProductAttrValueServices $attrValueServices */
  577. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  578. if ($unique == '') {
  579. $unique = $attrValueServices->value(['product_id' => $combinationId, 'type' => 3], 'unique');
  580. }
  581. $attrInfo = $attrValueServices->getOne(['product_id' => $combinationId, 'unique' => $unique, 'type' => 3]);
  582. if (!$attrInfo || $attrInfo['product_id'] != $combinationId) {
  583. throw new ApiException(410305);
  584. }
  585. $StoreCombinationInfo = $productInfo = $this->getCombinationOne($combinationId, '*,title as store_name');
  586. if (!$StoreCombinationInfo) {
  587. throw new ApiException(410295);
  588. }
  589. /** @var StoreOrderServices $orderServices */
  590. $orderServices = app()->make(StoreOrderServices::class);
  591. $userBuyCount = $orderServices->getBuyCount($uid, 'combination_id', $combinationId);
  592. if ($StoreCombinationInfo['once_num'] < $cartNum) {
  593. throw new ApiException(410313, ['num' => $StoreCombinationInfo['once_num']]);
  594. }
  595. if ($StoreCombinationInfo['num'] < ($userBuyCount + $cartNum)) {
  596. throw new ApiException(410298, ['num' => $StoreCombinationInfo['num']]);
  597. }
  598. if ($cartNum > $attrInfo['quota']) {
  599. throw new ApiException(410296);
  600. }
  601. return [$attrInfo, $unique, $productInfo];
  602. }
  603. /**
  604. * 拼团统计
  605. * @param $id
  606. * @return array
  607. */
  608. public function combinationStatistics($id)
  609. {
  610. /** @var StorePinkServices $pinkServices */
  611. $pinkServices = app()->make(StorePinkServices::class);
  612. /** @var StoreOrderServices $orderServices */
  613. $orderServices = app()->make(StoreOrderServices::class);
  614. $people_count = $pinkServices->getDistinctCount([['cid', '=', $id]], 'uid', false);
  615. $spread_count = $pinkServices->getDistinctCount([['cid', '=', $id], ['k_id', '>', 0]], 'uid', false);
  616. $start_count = $pinkServices->count(['cid' => $id, 'k_id' => 0]);
  617. $success_count = $pinkServices->count(['cid' => $id, 'k_id' => 0, 'status' => 2]);
  618. $pay_price = $orderServices->sum([['combination_id', '=', $id], ['paid', '=', 1], ['pid', '<>', -1], ['refund_type', 'in', [0, 3]], ['is_del', '=', 0]], 'pay_price', false);
  619. $pay_count = $orderServices->getDistinctCount([['combination_id', '=', $id], ['paid', '=', 1], ['refund_type', 'in', [0, 3]], ['is_del', '=', 0]], 'uid', false);
  620. return compact('people_count', 'spread_count', 'start_count', 'success_count', 'pay_price', 'pay_count');
  621. }
  622. /**
  623. * 拼团订单
  624. * @param $id
  625. * @param array $where
  626. * @return array
  627. */
  628. public function combinationStatisticsOrder($id, $where = [])
  629. {
  630. /** @var StoreOrderServices $orderServices */
  631. $orderServices = app()->make(StoreOrderServices::class);
  632. [$page, $limit] = $this->getPageValue();
  633. $where = $where + ['paid' => 1, 'refund_status' => 0, 'is_del' => 0];
  634. $list = $orderServices->combinationStatisticsOrder($id, $where, $page, $limit);
  635. $count = $orderServices->combinationStatisticsCount($id, $where);
  636. foreach ($list as &$item) {
  637. if ($item['status'] == 0) {
  638. if ($item['paid'] == 0) {
  639. $item['status'] = '未支付';
  640. } else {
  641. $item['status'] = '未发货';
  642. }
  643. } elseif ($item['status'] == 1) {
  644. $item['status'] = '待收货';
  645. } elseif ($item['status'] == 2) {
  646. $item['status'] = '待评价';
  647. } elseif ($item['status'] == 3) {
  648. $item['status'] = '已完成';
  649. } elseif ($item['status'] == -2) {
  650. $item['status'] = '已退款';
  651. } else {
  652. $item['status'] = '未知';
  653. }
  654. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  655. $item['pay_time'] = $item['pay_time'] ? date('Y-m-d H:i:s', $item['pay_time']) : '';
  656. }
  657. return compact('list', 'count');
  658. }
  659. }