StoreCombinationServices.php 33 KB

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