StoreIntegralServices.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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\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\services\CacheService;
  25. use think\exception\ValidateException;
  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. $description = $data['description'];
  60. $detail = $data['attrs'];
  61. $items = $data['items'];
  62. $data['images'] = json_encode($data['images']);
  63. $data['price'] = min(array_column($detail, 'price'));
  64. $data['quota'] = $data['quota_show'] = array_sum(array_column($detail, 'quota'));
  65. $data['stock'] = array_sum(array_column($detail, 'stock'));
  66. unset($data['section_time'], $data['description'], $data['attrs'], $data['items']);
  67. /** @var StoreDescriptionServices $storeDescriptionServices */
  68. $storeDescriptionServices = app()->make(StoreDescriptionServices::class);
  69. /** @var StoreProductAttrServices $storeProductAttrServices */
  70. $storeProductAttrServices = app()->make(StoreProductAttrServices::class);
  71. /** @var StoreProductServices $storeProductServices */
  72. $storeProductServices = app()->make(StoreProductServices::class);
  73. if ($data['quota'] > $storeProductServices->value(['id' => $data['product_id']], 'stock')) {
  74. throw new ValidateException('限量不能超过商品库存');
  75. }
  76. $this->transaction(function () use ($id, $data, $description, $detail, $items, $storeDescriptionServices, $storeProductAttrServices, $storeProductServices) {
  77. if ($id) {
  78. $res = $this->dao->update($id, $data);
  79. $storeDescriptionServices->saveDescription((int)$id, $description, 4);
  80. $skuList = $storeProductServices->validateProductAttr($items, $detail, (int)$id, 4);
  81. $valueGroup = $storeProductAttrServices->saveProductAttr($skuList, (int)$id, 4);
  82. if (!$res) throw new AdminException('修改失败');
  83. } else {
  84. if (!$storeProductServices->getOne(['is_show' => 1, 'is_del' => 0, 'id' => $data['product_id']])) {
  85. throw new AdminException('原商品已下架或移入回收站');
  86. }
  87. $data['add_time'] = time();
  88. $res = $this->dao->save($data);
  89. $storeDescriptionServices->saveDescription((int)$res->id, $description, 4);
  90. $skuList = $storeProductServices->validateProductAttr($items, $detail, (int)$res->id, 4);
  91. $valueGroup = $storeProductAttrServices->saveProductAttr($skuList, (int)$res->id, 4);
  92. if (!$res) throw new AdminException('添加失败');
  93. }
  94. $res = true;
  95. foreach ($valueGroup->toArray() as $item) {
  96. $res = $res && CacheService::setStock($item['unique'], (int)$item['quota_show'], 4);
  97. }
  98. if (!$res) {
  99. throw new AdminException('占用库存失败');
  100. }
  101. });
  102. }
  103. /**
  104. * 批量添加商品
  105. * @param array $data
  106. */
  107. public function saveBatchData(array $data)
  108. {
  109. /** @var StoreProductServices $service */
  110. $service = app()->make(StoreProductServices::class);
  111. /** @var StoreDescriptionServices $storeDescriptionServices */
  112. $storeDescriptionServices = app()->make(StoreDescriptionServices::class);
  113. /** @var StoreProductAttrResultServices $storeProductAttrResultServices */
  114. $storeProductAttrResultServices = app()->make(StoreProductAttrResultServices::class);
  115. if (!$data) {
  116. throw new ValidateException('请先添加产品!');
  117. }
  118. $attrs = [];
  119. foreach ($data['attrs'] as $k => $v) {
  120. $attrs[$v['product_id']][] = $v;
  121. }
  122. foreach ($attrs as $k => $v) {
  123. $productInfo = $service->getOne(['id' => $k]);
  124. $productInfo = is_object($productInfo) ? $productInfo->toArray() : [];
  125. if ($productInfo) {
  126. $product = [];
  127. $result = $storeProductAttrResultServices->getResult(['product_id' => $productInfo['id'], 'type' => 0]);
  128. $product['product_id'] = $productInfo['id'];
  129. $product['description'] = $storeDescriptionServices->getDescription(['product_id' => $productInfo['id'], 'type' => 0]);
  130. $product['attrs'] = $v;
  131. $product['items'] = $result['attr'];
  132. $product['is_show'] = $data['is_show'] ?? 0;
  133. $product['title'] = $productInfo['store_name'];
  134. $product['unit_name'] = $productInfo['unit_name'];
  135. $product['image'] = $productInfo['image'];
  136. $product['images'] = $productInfo['slider_image'];
  137. $product['num'] = 0;
  138. $product['is_host'] = 0;
  139. $product['once_num'] = 0;
  140. $product['sort'] = 0;
  141. $this->saveData(0, $product);
  142. }
  143. }
  144. return true;
  145. }
  146. /**
  147. * 积分商品列表
  148. * @param array $where
  149. * @return array
  150. */
  151. public function systemPage(array $where)
  152. {
  153. [$page, $limit] = $this->getPageValue();
  154. $list = $this->dao->getList($where, $page, $limit);
  155. $count = $this->dao->count($where);
  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 ValidateException('查看的商品不存在!');
  168. }
  169. if ($info->is_del) {
  170. throw new ValidateException('您查看的积分商品已被删除!');
  171. }
  172. $info['price'] = floatval($info['price']);
  173. /** @var StoreDescriptionServices $storeDescriptionServices */
  174. $storeDescriptionServices = app()->make(StoreDescriptionServices::class);
  175. $info['description'] = $storeDescriptionServices->getDescription(['product_id' => $id, 'type' => 4]);
  176. $info['attrs'] = $this->attrList($id, $info['product_id']);
  177. return $info;
  178. }
  179. /**
  180. * 获取规格
  181. * @param int $id
  182. * @param int $pid
  183. * @return mixed
  184. */
  185. public function attrList(int $id, int $pid)
  186. {
  187. /** @var StoreProductAttrResultServices $storeProductAttrResultServices */
  188. $storeProductAttrResultServices = app()->make(StoreProductAttrResultServices::class);
  189. $combinationResult = $storeProductAttrResultServices->value(['product_id' => $id, 'type' => 4], 'result');
  190. $items = json_decode($combinationResult, true)['attr'];
  191. $productAttr = $this->getAttr($items, $pid, 0);
  192. $combinationAttr = $this->getAttr($items, $id, 4);
  193. foreach ($productAttr as $pk => $pv) {
  194. foreach ($combinationAttr as &$sv) {
  195. if ($pv['detail'] == $sv['detail']) {
  196. $productAttr[$pk] = $sv;
  197. }
  198. }
  199. $productAttr[$pk]['detail'] = json_decode($productAttr[$pk]['detail']);
  200. }
  201. $attrs['items'] = $items;
  202. $attrs['value'] = $productAttr;
  203. foreach ($items as $key => $item) {
  204. $header[] = ['title' => $item['value'], 'key' => 'value' . ($key + 1), 'align' => 'center', 'minWidth' => 80];
  205. }
  206. $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 120];
  207. $header[] = ['title' => '兑换积分', 'key' => 'price', 'type' => 1, 'align' => 'center', 'minWidth' => 80];
  208. $header[] = ['title' => '库存', 'key' => 'stock', 'align' => 'center', 'minWidth' => 80];
  209. $header[] = ['title' => '兑换次数', 'key' => 'quota', 'type' => 1, 'align' => 'center', 'minWidth' => 80];
  210. $header[] = ['title' => '重量(KG)', 'key' => 'weight', 'align' => 'center', 'minWidth' => 80];
  211. $header[] = ['title' => '体积(m³)', 'key' => 'volume', 'align' => 'center', 'minWidth' => 80];
  212. $header[] = ['title' => '商品编号', 'key' => 'bar_code', 'align' => 'center', 'minWidth' => 80];
  213. $attrs['header'] = $header;
  214. return $attrs;
  215. }
  216. /**
  217. * 获得规格
  218. * @param $attr
  219. * @param $id
  220. * @param $type
  221. * @return array
  222. */
  223. public function getAttr($attr, $id, $type)
  224. {
  225. /** @var StoreProductAttrValueServices $storeProductAttrValueServices */
  226. $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  227. list($value, $head) = attr_format($attr);
  228. $valueNew = [];
  229. $count = 0;
  230. foreach ($value as $suk) {
  231. $detail = explode(',', $suk);
  232. $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,quota_show', 'suk');
  233. if (count($sukValue)) {
  234. foreach ($detail as $k => $v) {
  235. $valueNew[$count]['value' . ($k + 1)] = $v;
  236. }
  237. $valueNew[$count]['detail'] = json_encode(array_combine($head, $detail));
  238. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  239. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  240. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  241. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  242. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  243. // $valueNew[$count]['quota'] = $sukValue[$suk]['quota'] ? intval($sukValue[$suk]['quota']) : 0;
  244. $valueNew[$count]['quota'] = isset($sukValue[$suk]['quota_show']) && $sukValue[$suk]['quota_show'] ? intval($sukValue[$suk]['quota_show']) : 0;
  245. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  246. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ? floatval($sukValue[$suk]['weight']) : 0;
  247. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ? floatval($sukValue[$suk]['volume']) : 0;
  248. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ? floatval($sukValue[$suk]['brokerage']) : 0;
  249. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ? floatval($sukValue[$suk]['brokerage_two']) : 0;
  250. $valueNew[$count]['_checked'] = $type != 0;
  251. $count++;
  252. }
  253. }
  254. return $valueNew;
  255. }
  256. /**
  257. * 积分商品详情
  258. * @param Request $request
  259. * @param int $id
  260. * @return mixed
  261. * @throws \think\db\exception\DataNotFoundException
  262. * @throws \think\db\exception\DbException
  263. * @throws \think\db\exception\ModelNotFoundException
  264. */
  265. public function integralDetail(Request $request, int $id)
  266. {
  267. $storeInfo = $this->dao->getOne(['id' => $id], '*', ['getPrice']);
  268. if (!$storeInfo) {
  269. throw new ValidateException('商品不存在');
  270. } else {
  271. $storeInfo = $storeInfo->toArray();
  272. }
  273. $siteUrl = sys_config('site_url');
  274. $storeInfo['image'] = set_file_url($storeInfo['image'], $siteUrl);
  275. $storeInfo['image_base'] = set_file_url($storeInfo['image'], $siteUrl);
  276. $storeInfo['sale_stock'] = 0;
  277. if ($storeInfo['stock'] > 0) $storeInfo['sale_stock'] = 1;
  278. $uid = $request->uid();
  279. /** @var StoreDescriptionServices $storeDescriptionService */
  280. $storeDescriptionService = app()->make(StoreDescriptionServices::class);
  281. $storeInfo['description'] = $storeDescriptionService->getDescription(['product_id' => $id, 'type' => 4]);
  282. $data['storeInfo'] = get_thumb_water($storeInfo, 'big', ['image', 'images']);
  283. $storeInfoNew = get_thumb_water($storeInfo, 'small');
  284. $data['storeInfo']['small_image'] = $storeInfoNew['image'];
  285. /** @var StoreProductAttrServices $storeProductAttrServices */
  286. $storeProductAttrServices = app()->make(StoreProductAttrServices::class);
  287. list($productAttr, $productValue) = $storeProductAttrServices->getProductAttrDetail($id, $uid, 0, 4, $storeInfo['product_id']);
  288. $data['productAttr'] = $productAttr;
  289. $data['productValue'] = $productValue;
  290. /** @var StoreVisitServices $storeVisit */
  291. $storeVisit = app()->make(StoreVisitServices::class);
  292. $storeVisit->setView($uid, $id, 'combination', $storeInfo['product_id'], 'view');
  293. $data['routine_contact_type'] = sys_config('routine_contact_type', 0);
  294. //浏览记录
  295. ProductLogJob::dispatch(['visit', ['uid' => $uid, 'product_id' => $storeInfo['product_id']]]);
  296. return $data;
  297. }
  298. /**
  299. * 修改销量和库存
  300. * @param $num
  301. * @param $integralId
  302. * @return bool
  303. */
  304. public function decIntegralStock(int $num, int $integralId, string $unique)
  305. {
  306. $product_id = $this->dao->value(['id' => $integralId], 'product_id');
  307. if ($unique) {
  308. /** @var StoreProductAttrValueServices $skuValueServices */
  309. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  310. //减去积分商品的sku库存增加销量
  311. $res = false !== $skuValueServices->decProductAttrStock($integralId, $unique, $num, 4);
  312. //减去积分商品库存
  313. $res = $res && $this->dao->decStockIncSales(['id' => $integralId, 'type' => 4], $num);
  314. //获取拼团的sku
  315. $sku = $skuValueServices->value(['product_id' => $integralId, 'unique' => $unique, 'type' => 4], 'suk');
  316. //减去当前普通商品sku的库存增加销量
  317. $res = $res && $skuValueServices->decStockIncSales(['product_id' => $product_id, 'suk' => $sku, 'type' => 0], $num);
  318. } else {
  319. $res = false !== $this->dao->decStockIncSales(['id' => $integralId, 'type' => 4], $num);
  320. }
  321. /** @var StoreProductServices $services */
  322. $services = app()->make(StoreProductServices::class);
  323. //减去普通商品库存
  324. $res = $res && $services->decProductStock($num, $product_id);
  325. return $res;
  326. }
  327. /**
  328. * 获取一条积分商品
  329. * @param $id
  330. * @return mixed
  331. */
  332. public function getIntegralOne($id)
  333. {
  334. return $this->dao->validProduct($id, '*');
  335. }
  336. /**
  337. * 验证积分商品下单库存限量
  338. * @param int $uid
  339. * @param int $integralId
  340. * @param int $num
  341. * @param string $unique
  342. * @return string
  343. * @throws \think\db\exception\DataNotFoundException
  344. * @throws \think\db\exception\DbException
  345. * @throws \think\db\exception\ModelNotFoundException
  346. */
  347. public function checkoutProductStock(int $uid, int $integralId, int $num = 1, string $unique = '')
  348. {
  349. /** @var StoreProductAttrValueServices $attrValueServices */
  350. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  351. if ($unique == '') {
  352. $unique = $attrValueServices->value(['product_id' => $integralId, 'type' => 4], 'unique');
  353. }
  354. $StoreIntegralInfo = $this->getIntegralOne($integralId);
  355. if (!$StoreIntegralInfo) {
  356. throw new ValidateException('该商品已下架或删除');
  357. }
  358. /** @var StoreIntegralOrderServices $orderServices */
  359. $orderServices = app()->make(StoreIntegralOrderServices::class);
  360. $userBuyCount = $orderServices->getBuyCount($uid, $integralId);
  361. if ($StoreIntegralInfo['once_num'] < $num && $StoreIntegralInfo['once_num'] != -1) {
  362. throw new ValidateException('每个订单限购' . $StoreIntegralInfo['once_num'] . '件');
  363. }
  364. if ($StoreIntegralInfo['num'] < ($userBuyCount + $num) && $StoreIntegralInfo['num'] != -1) {
  365. throw new ValidateException('每人总共限购' . $StoreIntegralInfo['num'] . '件');
  366. }
  367. $res = $attrValueServices->getOne(['product_id' => $integralId, 'unique' => $unique, 'type' => 4]);
  368. if ($num > $res['quota']) {
  369. throw new ValidateException('该商品库存不足' . $num);
  370. }
  371. $product_stock = $attrValueServices->value(['product_id' => $StoreIntegralInfo['product_id'], 'suk' => $res['suk'], 'type' => 0], 'stock');
  372. if ($product_stock < $num) {
  373. throw new ValidateException('该商品库存不足' . $num);
  374. }
  375. if (!CacheService::checkStock($unique, $num, 4)) {
  376. throw new ValidateException('该商品库存不足' . $num);
  377. }
  378. return $unique;
  379. }
  380. /**
  381. * 获取推荐积分商品
  382. * @return array
  383. * @throws \think\db\exception\DataNotFoundException
  384. * @throws \think\db\exception\DbException
  385. * @throws \think\db\exception\ModelNotFoundException
  386. */
  387. public function getIntegralList(array $where)
  388. {
  389. [$page, $limit] = $this->getPageValue();
  390. $list = $this->dao->getList($where, $page, $limit, 'id,image,title,price,sales');
  391. return $list;
  392. }
  393. }