StoreIntegralServices.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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\integral;
  13. use app\Request;
  14. use app\services\BaseServices;
  15. use app\dao\activity\integral\StoreIntegralDao;
  16. use app\services\product\product\StoreDescriptionServices;
  17. use app\services\product\product\StoreProductServices;
  18. use app\services\product\product\StoreVisitServices;
  19. use app\services\product\sku\StoreProductAttrResultServices;
  20. use app\services\product\sku\StoreProductAttrServices;
  21. use app\services\product\sku\StoreProductAttrValueServices;
  22. use app\jobs\ProductLogJob;
  23. use crmeb\exceptions\AdminException;
  24. use crmeb\exceptions\ApiException;
  25. use crmeb\services\CacheService;
  26. /**
  27. *
  28. * Class StoreIntegralServices
  29. * @package app\services\activity
  30. * @method getOne(array $where, ?string $field = '*', array $with = []) 根据条件获取一条数据
  31. * @method get(int $id, ?array $field) 获取一条数据
  32. */
  33. class StoreIntegralServices extends BaseServices
  34. {
  35. const THODLCEG = 'ykGUKB';
  36. /**
  37. * StoreIntegralServices constructor.
  38. * @param StoreIntegralDao $dao
  39. */
  40. public function __construct(StoreIntegralDao $dao)
  41. {
  42. $this->dao = $dao;
  43. }
  44. /**
  45. * 获取指定条件下的条数
  46. * @param array $where
  47. */
  48. public function getCount(array $where)
  49. {
  50. $this->dao->count($where);
  51. }
  52. /**
  53. * 积分商品添加
  54. * @param int $id
  55. * @param array $data
  56. */
  57. public function saveData(int $id, array $data)
  58. {
  59. if ($data['num'] < $data['once_num']) {
  60. throw new AdminException('限制单次购买数量不能大于总购买数量');
  61. }
  62. $description = $data['description'];
  63. $detail = $data['attrs'];
  64. $items = $data['items'];
  65. $data['images'] = json_encode($data['images']);
  66. $data['price'] = min(array_column($detail, 'price'));
  67. $data['quota'] = $data['quota_show'] = array_sum(array_column($detail, 'quota'));
  68. $data['stock'] = array_sum(array_column($detail, 'stock'));
  69. unset($data['section_time'], $data['description'], $data['attrs'], $data['items']);
  70. /** @var StoreDescriptionServices $storeDescriptionServices */
  71. $storeDescriptionServices = app()->make(StoreDescriptionServices::class);
  72. /** @var StoreProductAttrServices $storeProductAttrServices */
  73. $storeProductAttrServices = app()->make(StoreProductAttrServices::class);
  74. /** @var StoreProductServices $storeProductServices */
  75. $storeProductServices = app()->make(StoreProductServices::class);
  76. if ($data['quota'] > $storeProductServices->value(['id' => $data['product_id']], 'stock')) {
  77. throw new AdminException('限量不能超过商品库存');
  78. }
  79. $this->transaction(function () use ($id, $data, $description, $detail, $items, $storeDescriptionServices, $storeProductAttrServices, $storeProductServices) {
  80. if ($id) {
  81. $res = $this->dao->update($id, $data);
  82. $storeDescriptionServices->saveDescription((int)$id, $description, 4);
  83. $skuList = $storeProductServices->validateProductAttr($items, $detail, (int)$id, 4);
  84. $storeProductAttrServices->saveProductAttr($skuList, (int)$id, 4);
  85. if (!$res) throw new AdminException('修改失败');
  86. } else {
  87. if (!$storeProductServices->getOne(['is_del' => 0, 'id' => $data['product_id']])) {
  88. throw new AdminException('无法添加回收站商品');
  89. }
  90. $data['add_time'] = time();
  91. $res = $this->dao->save($data);
  92. $storeDescriptionServices->saveDescription((int)$res->id, $description, 4);
  93. $skuList = $storeProductServices->validateProductAttr($items, $detail, (int)$res->id, 4, 1, true);
  94. $storeProductAttrServices->saveProductAttr($skuList, (int)$res->id, 4);
  95. if (!$res) throw new AdminException('添加失败');
  96. }
  97. });
  98. }
  99. /**
  100. * 批量添加商品
  101. * @param array $data
  102. */
  103. public function saveBatchData(array $data)
  104. {
  105. /** @var StoreProductServices $service */
  106. $service = app()->make(StoreProductServices::class);
  107. /** @var StoreDescriptionServices $storeDescriptionServices */
  108. $storeDescriptionServices = app()->make(StoreDescriptionServices::class);
  109. /** @var StoreProductAttrResultServices $storeProductAttrResultServices */
  110. $storeProductAttrResultServices = app()->make(StoreProductAttrResultServices::class);
  111. if (!$data) {
  112. throw new AdminException('请选择商品');
  113. }
  114. if (!$data['attrs']) throw new AdminException('请选择商品');
  115. $attrs = [];
  116. foreach ($data['attrs'] as $k => $v) {
  117. $attrs[$v['product_id']][] = $v;
  118. }
  119. foreach ($attrs as $k => $v) {
  120. $productInfo = $service->getOne(['id' => $k]);
  121. $productInfo = is_object($productInfo) ? $productInfo->toArray() : [];
  122. if ($productInfo) {
  123. $product = [];
  124. $result = $storeProductAttrResultServices->getResult(['product_id' => $productInfo['id'], 'type' => 0]);
  125. $product['product_id'] = $productInfo['id'];
  126. $product['description'] = $storeDescriptionServices->getDescription(['product_id' => $productInfo['id'], 'type' => 0]);
  127. $product['attrs'] = $v;
  128. $product['items'] = $result['attr'];
  129. $product['is_show'] = $data['is_show'] ?? 0;
  130. $product['title'] = $productInfo['store_name'];
  131. $product['unit_name'] = $productInfo['unit_name'];
  132. $product['image'] = $productInfo['image'];
  133. $product['images'] = $productInfo['slider_image'];
  134. $product['num'] = 0;
  135. $product['is_host'] = 0;
  136. $product['once_num'] = 0;
  137. $product['sort'] = 0;
  138. $this->saveData(0, $product);
  139. }
  140. }
  141. return true;
  142. }
  143. /**
  144. * 积分商品列表
  145. * @param array $where
  146. * @return array
  147. */
  148. public function systemPage(array $where)
  149. {
  150. [$page, $limit] = $this->getPageValue();
  151. $list = $this->dao->getList($where, $page, $limit);
  152. $count = $this->dao->count($where);
  153. return compact('list', 'count');
  154. }
  155. /**
  156. * 获取详情
  157. * @param int $id
  158. * @return array|\think\Model|null
  159. */
  160. public function getInfo(int $id)
  161. {
  162. $info = $this->dao->get($id);
  163. if (!$info) {
  164. throw new AdminException('商品不存在');
  165. }
  166. if ($info->is_del) {
  167. throw new AdminException('您查看的积分商品已被删除');
  168. }
  169. $info['price'] = floatval($info['price']);
  170. /** @var StoreDescriptionServices $storeDescriptionServices */
  171. $storeDescriptionServices = app()->make(StoreDescriptionServices::class);
  172. $info['description'] = $storeDescriptionServices->getDescription(['product_id' => $id, 'type' => 4]);
  173. $info['attrs'] = $this->attrList($id, $info['product_id']);
  174. return $info;
  175. }
  176. /**
  177. * 获取规格
  178. * @param int $id
  179. * @param int $pid
  180. * @return mixed
  181. */
  182. public function attrList(int $id, int $pid)
  183. {
  184. /** @var StoreProductAttrServices $storeProductAttrService */
  185. $storeProductAttrService = app()->make(StoreProductAttrServices::class);
  186. /** @var StoreProductAttrResultServices $storeProductAttrResultServices */
  187. $storeProductAttrResultServices = app()->make(StoreProductAttrResultServices::class);
  188. $integralResult = $storeProductAttrResultServices->value(['product_id' => $id, 'type' => 4], 'result');
  189. $items = json_decode($integralResult, true)['attr'];
  190. $productAttr = $storeProductAttrService->getProductAttr(['product_id' => $pid, 'type' => 0]);
  191. $pAttr = [];
  192. foreach ($productAttr as $key => $value) {
  193. $pAttr[$key]['value'] = $value['attr_name'];
  194. $pAttr[$key]['detailValue'] = '';
  195. $pAttr[$key]['attrHidden'] = true;
  196. $pAttr[$key]['detail'] = $value['attr_values'];
  197. }
  198. $integralAttr = $storeProductAttrService->getProductAttr(['product_id' => $id, 'type' => 4]);
  199. $iAttr = [];
  200. foreach ($integralAttr as $key => $value) {
  201. $iAttr[$key]['value'] = $value['attr_name'];
  202. $iAttr[$key]['detailValue'] = '';
  203. $iAttr[$key]['attrHidden'] = true;
  204. $iAttr[$key]['detail'] = $value['attr_values'];
  205. }
  206. $productAttr = $this->getAttr($pAttr, $pid, 0);
  207. $combinationAttr = $this->getAttr($iAttr, $id, 4);
  208. foreach ($productAttr as $pk => $pv) {
  209. foreach ($combinationAttr as &$sv) {
  210. if ($pv['detail'] == $sv['detail']) {
  211. $productAttr[$pk] = $sv;
  212. }
  213. }
  214. $productAttr[$pk]['detail'] = json_decode($productAttr[$pk]['detail']);
  215. }
  216. $attrs['items'] = $items;
  217. $attrs['value'] = $productAttr;
  218. foreach ($items as $key => $item) {
  219. $header[] = ['title' => $item['value'], 'key' => 'value' . ($key + 1), 'align' => 'center', 'minWidth' => 80];
  220. }
  221. $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 120];
  222. $header[] = ['title' => '兑换积分', 'slot' => 'price', 'type' => 1, '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. $header[] = ['title' => '条形码', 'key' => 'bar_code_number', 'align' => 'center', 'minWidth' => 80];
  229. $attrs['header'] = $header;
  230. return $attrs;
  231. }
  232. /**
  233. * 获得规格
  234. * @param $attr
  235. * @param $id
  236. * @param $type
  237. * @return array
  238. */
  239. public function getAttr($attr, $id, $type)
  240. {
  241. /** @var StoreProductAttrValueServices $storeProductAttrValueServices */
  242. $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  243. list($value, $head) = attr_format($attr);
  244. $valueNew = [];
  245. $count = 0;
  246. foreach ($value as $suk) {
  247. $detail = explode(',', $suk);
  248. $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,quota_show', 'suk');
  249. if (count($sukValue)) {
  250. foreach ($detail as $k => $v) {
  251. $valueNew[$count]['value' . ($k + 1)] = $v;
  252. }
  253. $valueNew[$count]['detail'] = json_encode(array_combine($head, $detail));
  254. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  255. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  256. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  257. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  258. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  259. $valueNew[$count]['quota'] = isset($sukValue[$suk]['quota_show']) && $sukValue[$suk]['quota_show'] ? intval($sukValue[$suk]['quota_show']) : 0;
  260. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  261. $valueNew[$count]['bar_code_number'] = $sukValue[$suk]['bar_code_number'] ?? '';
  262. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ? floatval($sukValue[$suk]['weight']) : 0;
  263. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ? floatval($sukValue[$suk]['volume']) : 0;
  264. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ? floatval($sukValue[$suk]['brokerage']) : 0;
  265. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ? floatval($sukValue[$suk]['brokerage_two']) : 0;
  266. $valueNew[$count]['_checked'] = $type != 0;
  267. $count++;
  268. }
  269. }
  270. return $valueNew;
  271. }
  272. /**
  273. * 积分商品详情
  274. * @param Request $request
  275. * @param int $id
  276. * @return mixed
  277. * @throws \think\db\exception\DataNotFoundException
  278. * @throws \think\db\exception\DbException
  279. * @throws \think\db\exception\ModelNotFoundException
  280. */
  281. public function integralDetail(Request $request, int $id)
  282. {
  283. $storeInfo = $this->dao->getOne(['id' => $id], '*', ['getPrice']);
  284. if (!$storeInfo) {
  285. throw new AdminException('商品不存在');
  286. } else {
  287. $storeInfo = $storeInfo->toArray();
  288. }
  289. $siteUrl = sys_config('site_url');
  290. $storeInfo['image'] = set_file_url($storeInfo['image'], $siteUrl);
  291. $storeInfo['image_base'] = set_file_url($storeInfo['image'], $siteUrl);
  292. $storeInfo['product_price'] = (int)$storeInfo['product_price'];
  293. $storeInfo['sale_stock'] = 0;
  294. if ($storeInfo['stock'] > 0) $storeInfo['sale_stock'] = 1;
  295. $uid = $request->uid();
  296. /** @var StoreDescriptionServices $storeDescriptionService */
  297. $storeDescriptionService = app()->make(StoreDescriptionServices::class);
  298. $storeInfo['description'] = $storeDescriptionService->getDescription(['product_id' => $id, 'type' => 4]);
  299. $data['storeInfo'] = get_thumb_water($storeInfo, 'big', ['image', 'images']);
  300. $storeInfoNew = get_thumb_water($storeInfo, 'small');
  301. $data['storeInfo']['small_image'] = $storeInfoNew['image'];
  302. /** @var StoreProductAttrServices $storeProductAttrServices */
  303. $storeProductAttrServices = app()->make(StoreProductAttrServices::class);
  304. list($productAttr, $productValue) = $storeProductAttrServices->getProductAttrDetail($id, $uid, 0, 4, $storeInfo['product_id']);
  305. foreach ($productValue as &$item) {
  306. $item['cost'] = (int)$item['cost'];
  307. $item['ot_price'] = (int)$item['ot_price'];
  308. $item['price'] = (int)$item['price'];
  309. $item['vip_price'] = (int)$item['vip_price'];
  310. }
  311. $data['productAttr'] = $productAttr;
  312. $data['productValue'] = $productValue;
  313. /** @var StoreVisitServices $storeVisit */
  314. $storeVisit = app()->make(StoreVisitServices::class);
  315. $storeVisit->setView($uid, $id, 'combination', $storeInfo['product_id'], 'view');
  316. $data['routine_contact_type'] = sys_config('routine_contact_type', 0);
  317. //浏览记录
  318. ProductLogJob::dispatch(['visit', ['uid' => $uid, 'product_id' => $storeInfo['product_id']]]);
  319. return $data;
  320. }
  321. /**
  322. * 修改销量和库存
  323. * @param $num
  324. * @param $integralId
  325. * @return bool
  326. */
  327. public function decIntegralStock(int $num, int $integralId, string $unique)
  328. {
  329. $product_id = $this->dao->value(['id' => $integralId], 'product_id');
  330. if ($unique) {
  331. /** @var StoreProductAttrValueServices $skuValueServices */
  332. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  333. //减去积分商品的sku库存增加销量
  334. $res = false !== $skuValueServices->decProductAttrStock($integralId, $unique, $num, 4);
  335. //减去积分商品库存
  336. $res = $res && $this->dao->decStockIncSales(['id' => $integralId, 'type' => 4], $num);
  337. //获取拼团的sku
  338. $sku = $skuValueServices->value(['product_id' => $integralId, 'unique' => $unique, 'type' => 4], 'suk');
  339. //减去当前普通商品sku的库存增加销量
  340. $res = $res && $skuValueServices->decStockIncSales(['product_id' => $product_id, 'suk' => $sku, 'type' => 0], $num);
  341. } else {
  342. $res = false !== $this->dao->decStockIncSales(['id' => $integralId, 'type' => 4], $num);
  343. }
  344. /** @var StoreProductServices $services */
  345. $services = app()->make(StoreProductServices::class);
  346. //减去普通商品库存
  347. $res = $res && $services->decProductStock($num, $product_id);
  348. return $res;
  349. }
  350. /**
  351. * 获取一条积分商品
  352. * @param $id
  353. * @return mixed
  354. */
  355. public function getIntegralOne($id)
  356. {
  357. return $this->dao->validProduct($id, '*');
  358. }
  359. /**
  360. * 验证积分商品下单库存限量
  361. * @param int $uid
  362. * @param int $integralId
  363. * @param int $num
  364. * @param string $unique
  365. * @return string
  366. * @throws \think\db\exception\DataNotFoundException
  367. * @throws \think\db\exception\DbException
  368. * @throws \think\db\exception\ModelNotFoundException
  369. */
  370. public function checkoutProductStock(int $uid, int $integralId, int $num = 1, string $unique = '')
  371. {
  372. /** @var StoreProductAttrValueServices $attrValueServices */
  373. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  374. if ($unique == '') {
  375. $unique = $attrValueServices->value(['product_id' => $integralId, 'type' => 4], 'unique');
  376. }
  377. $StoreIntegralInfo = $this->getIntegralOne($integralId);
  378. if (!$StoreIntegralInfo) {
  379. throw new ApiException('商品已下架或已删除');
  380. }
  381. /** @var StoreIntegralOrderServices $orderServices */
  382. $orderServices = app()->make(StoreIntegralOrderServices::class);
  383. $userBuyCount = $orderServices->getBuyCount($uid, $integralId);
  384. if ($StoreIntegralInfo['once_num'] < $num && $StoreIntegralInfo['once_num'] != -1) {
  385. throw new ApiException('每个订单限购{:num}件', ['num' => $StoreIntegralInfo['once_num']]);
  386. }
  387. if ($StoreIntegralInfo['num'] < ($userBuyCount + $num) && $StoreIntegralInfo['num'] != -1) {
  388. throw new ApiException('每人总共限购{:num}件', ['num' => $StoreIntegralInfo['num']]);
  389. }
  390. $res = $attrValueServices->getOne(['product_id' => $integralId, 'unique' => $unique, 'type' => 4]);
  391. if ($num > $res['quota']) {
  392. throw new ApiException('该商品库存不足{:num}', ['num' => $num]);
  393. }
  394. $product_stock = $attrValueServices->value(['product_id' => $StoreIntegralInfo['product_id'], 'suk' => $res['suk'], 'type' => 0], 'stock');
  395. if ($product_stock < $num) {
  396. throw new ApiException('该商品库存不足{:num}', ['num' => $num]);
  397. }
  398. return $unique;
  399. }
  400. /**
  401. * 获取推荐积分商品
  402. * @return array
  403. * @throws \think\db\exception\DataNotFoundException
  404. * @throws \think\db\exception\DbException
  405. * @throws \think\db\exception\ModelNotFoundException
  406. */
  407. public function getIntegralList(array $where)
  408. {
  409. [$page, $limit] = $this->getPageValue();
  410. $list = $this->dao->getList($where, $page, $limit, 'id,image,title,price,sales');
  411. return $list;
  412. }
  413. }