StoreCombinationServices.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\activity\combination;
  13. use app\Request;
  14. use app\services\BaseServices;
  15. use app\dao\activity\combination\StoreCombinationDao;
  16. use app\services\diy\DiyServices;
  17. use app\services\order\StoreOrderServices;
  18. use app\services\other\QrcodeServices;
  19. use app\services\product\branch\StoreBranchProductServices;
  20. use app\services\product\ensure\StoreProductEnsureServices;
  21. use app\services\product\label\StoreProductLabelServices;
  22. use app\services\product\product\StoreDescriptionServices;
  23. use app\services\user\UserRelationServices;
  24. use app\services\product\product\StoreProductReplyServices;
  25. use app\services\product\product\StoreProductServices;
  26. use app\services\product\sku\StoreProductAttrResultServices;
  27. use app\services\product\sku\StoreProductAttrServices;
  28. use app\services\product\sku\StoreProductAttrValueServices;
  29. use app\jobs\product\ProductLogJob;
  30. use app\services\user\UserServices;
  31. use crmeb\exceptions\AdminException;
  32. use crmeb\services\CacheService;
  33. use crmeb\services\SystemConfigService;
  34. use think\exception\ValidateException;
  35. /**
  36. * 拼团商品
  37. * Class StoreCombinationServices
  38. * @package app\services\activity\combination
  39. * @mixin StoreCombinationDao
  40. */
  41. class StoreCombinationServices extends BaseServices
  42. {
  43. const THODLCEG = 'ykGUKB';
  44. /**
  45. * 商品活动类型
  46. */
  47. const TYPE = 2;
  48. /**
  49. * StoreCombinationServices constructor.
  50. * @param StoreCombinationDao $dao
  51. */
  52. public function __construct(StoreCombinationDao $dao)
  53. {
  54. $this->dao = $dao;
  55. }
  56. /**
  57. * 获取指定条件下的条数
  58. * @param array $where
  59. */
  60. public function getCount(array $where)
  61. {
  62. $this->dao->count($where);
  63. }
  64. /**
  65. * @param array $productIds
  66. * @return mixed
  67. * @author 等风来
  68. * @email 136327134@qq.com
  69. * @date 2022/11/28
  70. */
  71. public function getPinkIdsArrayCache(array $productIds)
  72. {
  73. return $this->dao->cacheTag()->remember(md5('pink_ids_' . json_encode($productIds)), function () use ($productIds) {
  74. return $this->dao->getPinkIdsArray($productIds, ['id']);
  75. });
  76. }
  77. /**
  78. * 获取是否有拼团商品
  79. * */
  80. public function validCombination()
  81. {
  82. return $this->dao->count([
  83. 'is_del' => 0,
  84. 'is_show' => 1,
  85. 'pinkIngTime' => true
  86. ]);
  87. }
  88. /**
  89. * 拼团商品添加
  90. * @param int $id
  91. * @param array $data
  92. */
  93. public function saveData(int $id, array $data)
  94. {
  95. /** @var StoreProductServices $storeProductServices */
  96. $storeProductServices = app()->make(StoreProductServices::class);
  97. $productInfo = $storeProductServices->getOne(['is_show' => 1, 'is_del' => 0, 'is_verify' => 1, 'id' => $data['product_id']]);
  98. if (!$productInfo) {
  99. throw new AdminException('原商品已下架或移入回收站');
  100. }
  101. if ($productInfo['is_vip_product'] || $productInfo['is_presale_product']) {
  102. throw new AdminException('该商品是预售或svip专享');
  103. }
  104. $data['product_type'] = $productInfo['product_type'];
  105. if ($data['product_type'] == 4 && !$data['delivery_type']) {
  106. $data['delivery_type'] = $productInfo['delivery_type'];
  107. }
  108. $data['type'] = $productInfo['type'] ?? 0;
  109. $data['relation_id'] = $productInfo['relation_id'] ?? 0;
  110. $custom_form = $productInfo['custom_form'] ?? [];
  111. $data['custom_form'] = is_array($custom_form) ? json_encode($custom_form) : $custom_form;
  112. $data['system_form_id'] = $productInfo['system_form_id'] ?? 0;
  113. $store_label_id = $productInfo['store_label_id'] ?? [];
  114. $data['store_label_id'] = is_array($store_label_id) ? implode(',', $store_label_id) : $store_label_id;
  115. $ensure_id = $productInfo['ensure_id'] ?? [];
  116. $data['ensure_id'] = is_array($ensure_id) ? implode(',', $ensure_id) : $ensure_id;
  117. $specs = $productInfo['specs'] ?? [];
  118. $data['specs'] = is_array($specs) ? json_encode($specs) : $specs;
  119. if (in_array($data['product_type'], [1, 2, 3])) {
  120. $data['freight'] = 2;
  121. $data['temp_id'] = 0;
  122. $data['postage'] = 0;
  123. } else {
  124. if ($data['freight'] == 1) {
  125. $data['temp_id'] = 0;
  126. $data['postage'] = 0;
  127. } elseif ($data['freight'] == 2) {
  128. $data['temp_id'] = 0;
  129. } elseif ($data['freight'] == 3) {
  130. $data['postage'] = 0;
  131. }
  132. if ($data['freight'] == 2 && !$data['postage']) {
  133. throw new AdminException('请设置运费金额');
  134. }
  135. if ($data['freight'] == 3 && !$data['temp_id']) {
  136. throw new AdminException('请选择运费模版');
  137. }
  138. }
  139. $description = $data['description'];
  140. $detail = $data['attrs'];
  141. $items = $data['items'];
  142. $data['start_time'] = strtotime($data['section_time'][0]);
  143. $data['stop_time'] = strtotime($data['section_time'][1]);
  144. if ($data['stop_time'] < strtotime(date('Y-m-d', time()))) throw new AdminException('结束时间不能小于今天');
  145. $data['image'] = $data['images'][0] ?? '';
  146. $data['images'] = json_encode($data['images']);
  147. $data['price'] = min(array_column($detail, 'price'));
  148. $data['quota'] = $data['quota_show'] = array_sum(array_column($detail, 'quota'));
  149. if ($data['quota'] > $productInfo['stock']) {
  150. throw new ValidateException('限量不能超过商品库存');
  151. }
  152. $data['stock'] = array_sum(array_column($detail, 'stock'));
  153. unset($data['section_time'], $data['description'], $data['attrs'], $data['items']);
  154. /** @var StoreDescriptionServices $storeDescriptionServices */
  155. $storeDescriptionServices = app()->make(StoreDescriptionServices::class);
  156. /** @var StoreProductAttrServices $storeProductAttrServices */
  157. $storeProductAttrServices = app()->make(StoreProductAttrServices::class);
  158. $this->transaction(function () use ($id, $data, $description, $detail, $items, $storeDescriptionServices, $storeProductAttrServices) {
  159. if ($id) {
  160. $res = $this->dao->update($id, $data);
  161. if (!$res) throw new AdminException('修改失败');
  162. } else {
  163. $data['add_time'] = time();
  164. $res = $this->dao->save($data);
  165. if (!$res) throw new AdminException('添加失败');
  166. $id = (int)$res->id;
  167. }
  168. $storeDescriptionServices->saveDescription((int)$id, $description, 3);
  169. $skuList = $storeProductAttrServices->validateProductAttr($items, $detail, (int)$id, 3);
  170. $valueGroup = $storeProductAttrServices->saveProductAttr($skuList, (int)$id, 3);
  171. $res = true;
  172. foreach ($valueGroup as $item) {
  173. $res = $res && CacheService::setStock($item['unique'], (int)$item['quota_show'], 3);
  174. }
  175. if (!$res) {
  176. throw new AdminException('占用库存失败');
  177. }
  178. });
  179. $this->dao->cacheTag()->clear();
  180. }
  181. /**
  182. * 拼团列表
  183. * @param array $where
  184. * @return array
  185. */
  186. public function systemPage(array $where)
  187. {
  188. [$page, $limit] = $this->getPageValue();
  189. $list = $this->dao->getList($where, $page, $limit);
  190. $count = $this->dao->count($where);
  191. /** @var StorePinkServices $storePinkServices */
  192. $storePinkServices = app()->make(StorePinkServices::class);
  193. $countAll = $storePinkServices->getPinkCount([]);
  194. $countTeam = $storePinkServices->getPinkCount(['k_id' => 0, 'status' => 2]);
  195. $countPeople = $storePinkServices->getPinkCount(['k_id' => 0]);
  196. foreach ($list as &$item) {
  197. $item['count_people'] = $countPeople[$item['id']] ?? 0;//拼团数量
  198. $item['count_people_all'] = $countAll[$item['id']] ?? 0;//参与人数
  199. $item['count_people_pink'] = $countTeam[$item['id']] ?? 0;//成团数量
  200. $item['stop_status'] = $item['stop_time'] < time() ? 1 : 0;
  201. if ($item['is_show']) {
  202. if ($item['start_time'] > time())
  203. $item['start_name'] = '未开始';
  204. else if ($item['stop_time'] < time())
  205. $item['start_name'] = '已结束';
  206. else if ($item['stop_time'] > time() && $item['start_time'] < time()) {
  207. $item['start_name'] = '进行中';
  208. }
  209. } else $item['start_name'] = '已结束';
  210. }
  211. return compact('list', 'count');
  212. }
  213. /**
  214. * 获取详情
  215. * @param int $id
  216. * @return array|\think\Model|null
  217. */
  218. public function getInfo(int $id)
  219. {
  220. $info = $this->dao->get($id);
  221. if (!$info) {
  222. throw new ValidateException('查看的商品不存在!');
  223. }
  224. if ($info->is_del) {
  225. throw new ValidateException('您查看的团团商品已被删除!');
  226. }
  227. if ($info['start_time'])
  228. $start_time = date('Y-m-d H:i:s', $info['start_time']);
  229. if ($info['stop_time'])
  230. $stop_time = date('Y-m-d H:i:s', $info['stop_time']);
  231. if (isset($start_time) && isset($stop_time))
  232. $info['section_time'] = [$start_time, $stop_time];
  233. else
  234. $info['section_time'] = [];
  235. unset($info['start_time'], $info['stop_time']);
  236. $info['price'] = floatval($info['price']);
  237. $info['postage'] = floatval($info['postage']);
  238. $info['weight'] = floatval($info['weight']);
  239. $info['volume'] = floatval($info['volume']);
  240. if (!$info['delivery_type']) {
  241. $info['delivery_type'] = [1];
  242. }
  243. if ($info['postage']) {
  244. $info['freight'] = 2;
  245. } elseif ($info['temp_id']) {
  246. $info['freight'] = 3;
  247. } else {
  248. $info['freight'] = 1;
  249. }
  250. /** @var StoreDescriptionServices $storeDescriptionServices */
  251. $storeDescriptionServices = app()->make(StoreDescriptionServices::class);
  252. $info['description'] = $storeDescriptionServices->getDescription(['product_id' => $id, 'type' => 3]);
  253. $info['attrs'] = $this->attrList($id, $info['product_id']);
  254. return $info;
  255. }
  256. /**
  257. * 获取规格
  258. * @param int $id
  259. * @param int $pid
  260. * @return mixed
  261. */
  262. public function attrList(int $id, int $pid)
  263. {
  264. /** @var StoreProductAttrResultServices $storeProductAttrResultServices */
  265. $storeProductAttrResultServices = app()->make(StoreProductAttrResultServices::class);
  266. $combinationResult = $storeProductAttrResultServices->value(['product_id' => $id, 'type' => 3], 'result');
  267. $items = json_decode($combinationResult, true)['attr'];
  268. $productAttr = $this->getAttr($items, $pid, 0);
  269. $combinationAttr = $this->getAttr($items, $id, 3);
  270. foreach ($productAttr as $pk => &$pv) {
  271. $pv['r_price'] = $pv['price'];
  272. foreach ($combinationAttr as &$sv) {
  273. if ($pv['detail'] == $sv['detail']) {
  274. $sv['r_price'] = $pv['price'];
  275. $pv = $sv;
  276. }
  277. }
  278. $pv['detail'] = json_decode($pv['detail']);
  279. }
  280. $attrs['items'] = $items;
  281. $attrs['value'] = $productAttr;
  282. foreach ($items as $key => $item) {
  283. $header[] = ['title' => $item['value'], 'key' => 'value' . ($key + 1), 'align' => 'center', 'minWidth' => 80];
  284. }
  285. $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 120];
  286. $header[] = ['title' => '拼团价', 'key' => 'price', 'type' => 1, 'align' => 'center', 'minWidth' => 80];
  287. $header[] = ['title' => '成本价', 'key' => 'cost', 'align' => 'center', 'minWidth' => 80];
  288. $header[] = ['title' => '日常售价', 'key' => 'r_price', 'align' => 'center', 'minWidth' => 80];
  289. $header[] = ['title' => '库存', 'key' => 'stock', 'align' => 'center', 'minWidth' => 80];
  290. $header[] = ['title' => '限量', 'key' => 'quota', 'type' => 1, 'align' => 'center', 'minWidth' => 80];
  291. $header[] = ['title' => '重量(KG)', 'key' => 'weight', 'align' => 'center', 'minWidth' => 80];
  292. $header[] = ['title' => '体积(m³)', 'key' => 'volume', 'align' => 'center', 'minWidth' => 80];
  293. $header[] = ['title' => '商品条形码', 'key' => 'bar_code', 'align' => 'center', 'minWidth' => 80];
  294. $header[] = ['title' => '商品编号', 'key' => 'code', 'align' => 'center', 'minWidth' => 80];
  295. $attrs['header'] = $header;
  296. return $attrs;
  297. }
  298. /**
  299. * 获得规格
  300. * @param $attr
  301. * @param $id
  302. * @param $type
  303. * @return array
  304. */
  305. public function getAttr($attr, $id, $type)
  306. {
  307. /** @var StoreProductAttrValueServices $storeProductAttrValueServices */
  308. $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  309. $value = attr_format($attr)[1];
  310. $valueNew = [];
  311. $count = 0;
  312. foreach ($value as $key => $item) {
  313. $detail = $item['detail'];
  314. // sort($item['detail'], SORT_STRING);
  315. $suk = implode(',', $item['detail']);
  316. $sukValue = $storeProductAttrValueServices->getSkuArray(['product_id' => $id, 'type' => $type, 'suk' => $suk], 'bar_code,code,cost,price,ot_price,stock,image as pic,weight,volume,brokerage,brokerage_two,quota,quota_show', 'suk');
  317. if (count($sukValue)) {
  318. foreach (array_values($detail) as $k => $v) {
  319. $valueNew[$count]['value' . ($k + 1)] = $v;
  320. }
  321. $valueNew[$count]['detail'] = json_encode($detail);
  322. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  323. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  324. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  325. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  326. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  327. // $valueNew[$count]['quota'] = $sukValue[$suk]['quota'] ? intval($sukValue[$suk]['quota']) : 0;
  328. $valueNew[$count]['quota'] = isset($sukValue[$suk]['quota_show']) && $sukValue[$suk]['quota_show'] ? intval($sukValue[$suk]['quota_show']) : 0;
  329. $valueNew[$count]['code'] = $sukValue[$suk]['code'] ?? '';
  330. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  331. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ? floatval($sukValue[$suk]['weight']) : 0;
  332. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ? floatval($sukValue[$suk]['volume']) : 0;
  333. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ? floatval($sukValue[$suk]['brokerage']) : 0;
  334. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ? floatval($sukValue[$suk]['brokerage_two']) : 0;
  335. $valueNew[$count]['_checked'] = $type != 0;
  336. $count++;
  337. }
  338. }
  339. return $valueNew;
  340. }
  341. /**
  342. * 根据id获取拼团数据列表
  343. * @param array $ids
  344. * @param string $field
  345. * @return array
  346. * @throws \think\db\exception\DataNotFoundException
  347. * @throws \think\db\exception\DbException
  348. * @throws \think\db\exception\ModelNotFoundException
  349. */
  350. public function getCombinationList()
  351. {
  352. [$page, $limit] = $this->getPageValue();
  353. $list = $this->dao->combinationList(['is_del' => 0, 'is_show' => 1, 'pinkIngTime' => true, 'storeProductId' => true], $page, $limit);
  354. foreach ($list as &$item) {
  355. $item['image'] = set_file_url($item['image']);
  356. $item['price'] = floatval($item['price']);
  357. $item['product_price'] = floatval($item['product_price']);
  358. }
  359. return $list;
  360. }
  361. /**
  362. * 拼团商品详情
  363. * @param Request $request
  364. * @param int $id
  365. * @return mixed
  366. * @throws \think\db\exception\DataNotFoundException
  367. * @throws \think\db\exception\DbException
  368. * @throws \think\db\exception\ModelNotFoundException
  369. */
  370. public function combinationDetail(Request $request, int $id)
  371. {
  372. $uid = (int)$request->uid();
  373. $storeInfo = $this->dao->cacheTag()->remember('' . $id, function () use ($id) {
  374. $storeInfo = $this->dao->getOne(['id' => $id], '*', ['descriptions', 'total']);
  375. if (!$storeInfo) {
  376. throw new ValidateException('商品不存在');
  377. } else {
  378. $storeInfo = $storeInfo->toArray();
  379. }
  380. return $storeInfo;
  381. }, 600);
  382. /** @var DiyServices $diyServices */
  383. $diyServices = app()->make(DiyServices::class);
  384. $infoDiy = $diyServices->getProductDetailDiy();
  385. //diy控制参数
  386. if (!isset($infoDiy['is_specs']) || !$infoDiy['is_specs']) {
  387. $storeInfo['specs'] = [];
  388. }
  389. $configData = SystemConfigService::more(['site_url', 'routine_contact_type', 'site_name', 'share_qrcode', 'store_self_mention', 'store_func_status', 'product_poster_title']);
  390. $siteUrl = $configData['site_url'] ?? '';
  391. $storeInfo['image'] = set_file_url($storeInfo['image'], $siteUrl);
  392. $storeInfo['image_base'] = set_file_url($storeInfo['image'], $siteUrl);
  393. $storeInfo['sale_stock'] = 0;
  394. if ($storeInfo['stock'] > 0) $storeInfo['sale_stock'] = 1;
  395. //品牌名称
  396. /** @var StoreProductServices $storeProductServices */
  397. $storeProductServices = app()->make(StoreProductServices::class);
  398. $productInfo = $storeProductServices->getCacheProductInfo((int)$storeInfo['product_id']);
  399. $storeInfo['brand_name'] = $storeProductServices->productIdByBrandName($storeInfo['product_id'], $productInfo);
  400. $delivery_type = $storeInfo['delivery_type'] ?? $productInfo['delivery_type'];
  401. $storeInfo['delivery_type'] = is_string($delivery_type) ? explode(',', $delivery_type) : $delivery_type;
  402. /**
  403. * 判断配送方式
  404. */
  405. $storeInfo['delivery_type'] = $storeProductServices->getDeliveryType((int)$storeInfo['id'], (int)$storeInfo['type'], (int)$storeInfo['relation_id'], $storeInfo['delivery_type']);
  406. $storeInfo['store_label'] = $storeInfo['ensure'] = [];
  407. if ($storeInfo['store_label_id']) {
  408. /** @var StoreProductLabelServices $storeProductLabelServices */
  409. $storeProductLabelServices = app()->make(StoreProductLabelServices::class);
  410. $storeInfo['store_label'] = $storeProductLabelServices->getLabelCache($storeInfo['store_label_id'], ['id', 'label_name']);
  411. }
  412. if ($storeInfo['ensure_id'] && isset($infoDiy['is_ensure']) && $infoDiy['is_ensure']) {
  413. /** @var StoreProductEnsureServices $storeProductEnsureServices */
  414. $storeProductEnsureServices = app()->make(StoreProductEnsureServices::class);
  415. $storeInfo['ensure'] = $storeProductEnsureServices->getEnsurCache($storeInfo['ensure_id'], ['id', 'name', 'image', 'desc']);
  416. }
  417. /** @var UserRelationServices $userRelationServices */
  418. $userRelationServices = app()->make(UserRelationServices::class);
  419. $storeInfo['userCollect'] = $userRelationServices->isProductRelation(['uid' => $uid, 'relation_id' => $id, 'type' => 'collect', 'category' => UserRelationServices::CATEGORY_PRODUCT]);
  420. $storeInfo['userLike'] = 0;
  421. /** @var QrcodeServices $qrcodeService */
  422. $qrcodeService = app()->make(QrcodeServices::class);
  423. if (($configData['share_qrcode'] ?? 0) && request()->isWechat()) {
  424. $storeInfo['code_base'] = $qrcodeService->getTemporaryQrcode('combination-' . $id, $uid)->url;
  425. } else {
  426. $storeInfo['code_base'] = $qrcodeService->getWechatQrcodePath($id . '_product_combination_detail_wap.jpg', '/pages/activity/goods_combination_details/index?id=' . $id);
  427. }
  428. $storeInfo['small_image'] = get_thumb_water($storeInfo['image']);
  429. $data['storeInfo'] = $storeInfo;
  430. /** @var StorePinkServices $pinkService */
  431. $pinkService = app()->make(StorePinkServices::class);
  432. [$pink, $pindAll] = $pinkService->getPinkList($id, true, 1);//拼团进行中列表
  433. $data['pink_ok_list'] = $pinkService->getPinkOkList($uid);
  434. $data['pink_ok_sum'] = $pinkService->getPinkOkSumTotalNum();
  435. $data['pink'] = $pink;
  436. $data['pindAll'] = $pindAll;
  437. /** @var StoreOrderServices $storeOrderServices */
  438. $storeOrderServices = app()->make(StoreOrderServices::class);
  439. $data['buy_num'] = $storeOrderServices->getBuyCount($uid, 3, $id);
  440. $data['reply'] = [];
  441. $data['replyChance'] = $data['replyCount'] = 0;
  442. if (isset($infoDiy['is_reply']) && $infoDiy['is_reply']) {
  443. /** @var StoreProductReplyServices $storeProductReplyService */
  444. $storeProductReplyService = app()->make(StoreProductReplyServices::class);
  445. $reply = $storeProductReplyService->getRecProductReplyCache((int)$storeInfo['product_id'], (int)($infoDiy['reply_num'] ?? 1));
  446. $data['reply'] = $reply ? get_thumb_water($reply, 'small', ['pics']) : [];
  447. [$replyCount, $goodReply, $replyChance] = $storeProductReplyService->getProductReplyData((int)$storeInfo['product_id']);
  448. $data['replyChance'] = $replyChance;
  449. $data['replyCount'] = $replyCount;
  450. }
  451. /** @var StoreProductAttrServices $storeProductAttrServices */
  452. $storeProductAttrServices = app()->make(StoreProductAttrServices::class);
  453. [$productAttr, $productValue] = $storeProductAttrServices->getProductAttrDetailCache($id, $uid, 0, 3, $storeInfo['product_id'], $productInfo);
  454. $data['productAttr'] = $productAttr;
  455. $data['productValue'] = $productValue;
  456. $data['routine_contact_type'] = sys_config('routine_contact_type', 0);
  457. $data['store_func_status'] = (int)($configData['store_func_status'] ?? 1);//门店是否开启
  458. $data['store_self_mention'] = $data['store_func_status'] ? (int)($configData['store_self_mention'] ?? 0) : 0;//门店自提是否开启
  459. $data['site_name'] = sys_config('site_name');
  460. $data['share_qrcode'] = sys_config('share_qrcode', 0);
  461. $data['product_poster_title'] = $configData['product_poster_title'] ?? '';
  462. //浏览记录
  463. ProductLogJob::dispatch(['visit', ['uid' => $uid, 'id' => $id, 'product_id' => $storeInfo['product_id']], 'combination']);
  464. return $data;
  465. }
  466. /**
  467. * 修改销量和库存
  468. * @param int $num
  469. * @param int $CombinationId
  470. * @param string $unique
  471. * @param int $store_id
  472. * @return bool
  473. */
  474. public function decCombinationStock(int $num, int $CombinationId, string $unique, int $store_id = 0)
  475. {
  476. $product_id = $this->dao->value(['id' => $CombinationId], 'product_id');
  477. $res = true;
  478. if ($product_id) {
  479. if ($unique) {
  480. /** @var StoreProductAttrValueServices $skuValueServices */
  481. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  482. //减去拼团商品的sku库存增加销量
  483. $res = $res && $skuValueServices->decProductAttrStock($CombinationId, $unique, $num, 3);
  484. //拼团商品sku
  485. $sku = $skuValueServices->value(['product_id' => $CombinationId, 'unique' => $unique, 'type' => 3], 'suk');
  486. //平台普通商品sku unique
  487. $productUnique = $skuValueServices->value(['suk' => $sku, 'product_id' => $product_id, 'type' => 0], 'unique');
  488. /** @var StoreProductServices $services */
  489. $services = app()->make(StoreProductServices::class);
  490. //减去当前普通商品、sku的库存增加销量
  491. $res = $res && $services->decProductStock($num, (int)$product_id, (string)$productUnique, $store_id);
  492. }
  493. //减去拼团商品库存
  494. $res = $res && $this->dao->decStockIncSales(['id' => $CombinationId, 'type' => 3], $num);
  495. }
  496. return $res;
  497. }
  498. /**
  499. * 加库存减销量
  500. * @param int $num
  501. * @param int $CombinationId
  502. * @param string $unique
  503. * @param int $store_id
  504. * @return bool
  505. */
  506. public function incCombinationStock(int $num, int $CombinationId, string $unique, int $store_id = 0)
  507. {
  508. $product_id = $this->dao->value(['id' => $CombinationId], 'product_id');
  509. $res = false;
  510. if ($product_id) {
  511. if ($unique) {
  512. /** @var StoreProductAttrValueServices $skuValueServices */
  513. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  514. //增加拼团商品的sku库存,减去销量
  515. $res = false !== $skuValueServices->incProductAttrStock($CombinationId, $unique, $num, 3);
  516. //拼团商品sku
  517. $suk = $skuValueServices->value(['unique' => $unique, 'product_id' => $CombinationId, 'type' => 3], 'suk');
  518. //平台商品sku unique
  519. $productUnique = $skuValueServices->value(['suk' => $suk, 'product_id' => $product_id, 'type' => 0], 'unique');
  520. /** @var StoreProductServices $services */
  521. $services = app()->make(StoreProductServices::class);
  522. //增加当前普通商品sku的库存,减去销量
  523. $res = $res && $services->incProductStock($num, (int)$product_id, (string)$productUnique, $store_id);
  524. }
  525. //增加拼团库存
  526. $res = $res && $this->dao->incStockDecSales(['id' => $CombinationId, 'type' => 3], $num);
  527. }
  528. return $res;
  529. }
  530. /**
  531. * 获取一条拼团数据
  532. * @param $id
  533. * @return mixed
  534. */
  535. public function getCombinationOne($id, $field = '*')
  536. {
  537. return $this->dao->validProduct($id, $field);
  538. }
  539. /**
  540. * 获取拼团详情
  541. * @param int $uid
  542. * @param int $id
  543. * @param array $user
  544. * @return array
  545. * @throws \think\db\exception\DataNotFoundException
  546. * @throws \think\db\exception\DbException
  547. * @throws \think\db\exception\ModelNotFoundException
  548. */
  549. public function getPinkInfo(int $uid, int $id, array $user = [])
  550. {
  551. if (!$id || !$uid) throw new ValidateException('参数错误');
  552. $is_ok = 0;//判断拼团是否完成
  553. $userBool = 0;//判断当前用户是否在团内 0未在 1在
  554. $pinkBool = 0;//判断拼团是否成功 0未在 1在
  555. if (!$user) {
  556. $userServices = app()->make(UserServices::class);
  557. $user = $userServices->getUserCacheInfo($uid);
  558. if (!$user) {
  559. throw new ValidateException('参数错误');
  560. }
  561. $user = $user->toArray();
  562. }
  563. /** @var StorePinkServices $pinkService */
  564. $pinkService = app()->make(StorePinkServices::class);
  565. $pink = $pinkService->getPinkUserOne($id);
  566. if (!$pink) throw new ValidateException('参数错误');
  567. $pink = $pink->toArray();
  568. if (isset($pink['is_refund']) && $pink['is_refund']) {
  569. if ($pink['is_refund'] != $pink['id']) {
  570. $id = $pink['is_refund'];
  571. return $this->getPinkInfo($uid, $id, $user);
  572. } else {
  573. throw new ValidateException('订单已退款');
  574. }
  575. }
  576. [$pinkAll, $pinkT, $count, $idAll, $uidAll] = $pinkService->getPinkMemberAndPinkK($pink);
  577. if ($pinkT['status'] == 2) {
  578. $pinkBool = 1;
  579. $is_ok = 1;
  580. } else if ($pinkT['status'] == 3) {
  581. $pinkBool = -1;
  582. $is_ok = 0;
  583. } else {
  584. if ($count < 1) {//组团完成
  585. $is_ok = 1;
  586. $pinkBool = $pinkService->pinkComplete($uidAll, $idAll, $user['uid'], $pinkT);
  587. } else {
  588. $pinkBool = $pinkService->pinkFail($pinkAll, $pinkT, $pinkBool);
  589. }
  590. //更新pinkT 数据 可能成功或失败
  591. $pinkT = $pinkService->getPinkUserOne($pinkT['id']);
  592. }
  593. if (!empty($pinkAll)) {
  594. foreach ($pinkAll as $v) {
  595. if ($v['uid'] == $user['uid']) $userBool = 1;
  596. }
  597. }
  598. if ($pinkT['uid'] == $user['uid']) $userBool = 1;
  599. $combinationOne = $this->getCombinationOne($pink['cid']);
  600. if (!$combinationOne) {
  601. throw new ValidateException('拼团不存在或已下架,请手动申请退款!');
  602. }
  603. $combinationOne = $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();
  604. $data['userInfo']['uid'] = $user['uid'];
  605. $data['userInfo']['nickname'] = $user['nickname'];
  606. $data['userInfo']['avatar'] = $user['avatar'];
  607. $data['is_ok'] = $is_ok;
  608. $data['userBool'] = $userBool;
  609. $data['pinkBool'] = $pinkBool;
  610. $delivery_type = $combinationOne['delivery_type'] ?? [];
  611. $combinationOne['delivery_type'] = is_string($delivery_type) ? explode(',', $delivery_type) : $delivery_type;
  612. $data['store_combination'] = $combinationOne;
  613. $data['pinkT'] = $pinkT;
  614. $data['pinkAll'] = $pinkAll;
  615. $data['count'] = $count <= 0 ? 0 : $count;
  616. $data['store_combination_host'] = $this->dao->getCombinationHost();
  617. $data['current_pink_order'] = $pinkService->getCurrentPink($id, $user['uid']);
  618. /** @var StoreProductAttrServices $storeProductAttrServices */
  619. $storeProductAttrServices = app()->make(StoreProductAttrServices::class);
  620. /** @var StoreProductAttrValueServices $storeProductAttrValueServices */
  621. $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  622. [$productAttr, $productValue] = $storeProductAttrServices->getProductAttrDetail($combinationOne['id'], $user['uid'], 0, 3, $combinationOne['product_id']);
  623. foreach ($productValue as $k => $v) {
  624. $productValue[$k]['product_stock'] = $storeProductAttrValueServices->value(['product_id' => $combinationOne['product_id'], 'suk' => $v['suk'], 'type' => 0], 'stock');
  625. }
  626. $data['store_combination']['productAttr'] = $productAttr;
  627. $data['store_combination']['productValue'] = $productValue;
  628. $data['store_func_status'] = (int)(sys_config('store_func_status', 1));
  629. $data['store_self_mention'] = $data['store_func_status'] ? (int)(sys_config('store_self_mention', 0)) : 0;//门店自提是否开启
  630. return $data;
  631. }
  632. /**
  633. * 验证拼团下单库存限量
  634. * @param int $uid
  635. * @param int $combinationId
  636. * @param int $cartNum
  637. * @param string $unique
  638. * @return array
  639. * @throws \think\db\exception\DataNotFoundException
  640. * @throws \think\db\exception\DbException
  641. * @throws \think\db\exception\ModelNotFoundException
  642. */
  643. public function checkCombinationStock(int $uid, int $combinationId, int $cartNum = 1, string $unique = '')
  644. {
  645. /** @var StoreProductAttrValueServices $attrValueServices */
  646. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  647. if ($unique == '') {
  648. $unique = $attrValueServices->value(['product_id' => $combinationId, 'type' => 3], 'unique');
  649. }
  650. $attrInfo = $attrValueServices->getOne(['product_id' => $combinationId, 'unique' => $unique, 'type' => 3]);
  651. if (!$attrInfo || $attrInfo['product_id'] != $combinationId) {
  652. throw new ValidateException('请选择有效的商品属性');
  653. }
  654. $StoreCombinationInfo = $productInfo = $this->getCombinationOne($combinationId, '*,title as store_name');
  655. if (!$StoreCombinationInfo) {
  656. throw new ValidateException('该商品已下架或删除');
  657. }
  658. /** @var StoreOrderServices $orderServices */
  659. $orderServices = app()->make(StoreOrderServices::class);
  660. $userBuyCount = $orderServices->getBuyCount($uid, 3, $combinationId);
  661. if ($StoreCombinationInfo['once_num'] < $cartNum) {
  662. throw new ValidateException('每个订单限购' . $StoreCombinationInfo['once_num'] . '件');
  663. }
  664. if ($StoreCombinationInfo['num'] < ($userBuyCount + $cartNum)) {
  665. throw new ValidateException('每人总共限购' . $StoreCombinationInfo['num'] . '件');
  666. }
  667. if ($cartNum > $attrInfo['quota']) {
  668. throw new ValidateException('该商品库存不足' . $cartNum);
  669. }
  670. return [$attrInfo, $unique, $productInfo];
  671. }
  672. /**
  673. * 拼团统计
  674. * @param $id
  675. * @return array
  676. */
  677. public function combinationStatistics(int $id)
  678. {
  679. /** @var StorePinkServices $pinkServices */
  680. $pinkServices = app()->make(StorePinkServices::class);
  681. /** @var StoreOrderServices $orderServices */
  682. $orderServices = app()->make(StoreOrderServices::class);
  683. $people_count = $pinkServices->getDistinctCount([['cid', '=', $id]], 'uid', false);
  684. $spread_count = $pinkServices->getDistinctCount([['cid', '=', $id], ['k_id', '>', 0]], 'uid', false);
  685. $start_count = $pinkServices->count(['cid' => $id, 'k_id' => 0]);
  686. $success_count = $pinkServices->count(['cid' => $id, 'k_id' => 0, 'status' => 2]);
  687. $pay_price = $orderServices->sum(['type' => 3, 'activity_id' => $id, 'paid' => 1, 'pid' => [0, -1]], 'pay_price', true);
  688. $pay_count = $orderServices->getDistinctCount(['type' => 3, 'activity_id' => $id, 'paid' => 1, 'pid' => [0, -1]], 'uid');
  689. return compact('people_count', 'spread_count', 'start_count', 'success_count', 'pay_price', 'pay_count');
  690. }
  691. /**
  692. * 拼团订单
  693. * @param $id
  694. * @param array $where
  695. * @return array
  696. */
  697. public function combinationStatisticsOrder(int $id, array $where = [])
  698. {
  699. /** @var StoreOrderServices $orderServices */
  700. $orderServices = app()->make(StoreOrderServices::class);
  701. [$page, $limit] = $this->getPageValue();
  702. $list = $orderServices->activityStatisticsOrder($id, 3, $where, $page, $limit);
  703. $where['type'] = 3;
  704. $where['activity_id'] = $id;
  705. $count = $orderServices->count($where);
  706. foreach ($list as &$item) {
  707. if ($item['is_del'] || $item['is_system_del']) {
  708. $item['status'] = '已删除';
  709. } else if ($item['paid'] == 0 && $item['status'] == 0) {
  710. $item['status'] = '未支付';
  711. } else if ($item['paid'] == 1 && $item['status'] == 4 && in_array($item['shipping_type'], [1, 3]) && $item['refund_status'] == 0) {
  712. $item['status'] = '部分发货';
  713. } else if ($item['paid'] == 1 && $item['refund_status'] == 2) {
  714. $item['status'] = '已退款';
  715. } else if ($item['paid'] == 1 && $item['status'] == 5 && $item['refund_status'] == 0) {
  716. $item['status'] = $item['shipping_type'] == 2 ? '部分核销' : '部分收货';
  717. $item['_status'] = 12;//已支付 部分核销
  718. } else if ($item['paid'] == 1 && $item['refund_status'] == 1) {
  719. $item['status'] = '申请退款';
  720. } else if ($item['paid'] == 1 && $item['refund_status'] == 4) {
  721. $item['status'] = '退款中';
  722. } else if ($item['paid'] == 1 && $item['status'] == 0 && in_array($item['shipping_type'], [1, 3]) && $item['refund_status'] == 0) {
  723. $item['status'] = '未发货';
  724. $item['_status'] = 2;//已支付 未发货
  725. } else if ($item['paid'] == 1 && in_array($item['status'], [0, 1]) && $item['shipping_type'] == 2 && $item['refund_status'] == 0) {
  726. $item['status'] = '未核销';
  727. } else if ($item['paid'] == 1 && in_array($item['status'], [1, 5]) && in_array($item['shipping_type'], [1, 3]) && $item['refund_status'] == 0) {
  728. $item['status'] = '待收货';
  729. } else if ($item['paid'] == 1 && $item['status'] == 2 && $item['refund_status'] == 0) {
  730. $item['status'] = '待评价';
  731. } else if ($item['paid'] == 1 && $item['status'] == 3 && $item['refund_status'] == 0) {
  732. $item['status'] = '已完成';
  733. } else if ($item['paid'] == 1 && $item['refund_status'] == 3) {
  734. $item['status'] = '部分退款';
  735. } else {
  736. $item['status'] = '未知';
  737. }
  738. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  739. $item['pay_time'] = $item['pay_time'] ? date('Y-m-d H:i:s', $item['pay_time']) : '';
  740. }
  741. return compact('list', 'count');
  742. }
  743. }