StoreDiscountsServices.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. namespace app\services\activity\discounts;
  12. use app\dao\activity\discounts\StoreDiscountsDao;
  13. use app\services\BaseServices;
  14. use app\services\product\branch\StoreBranchProductServices;
  15. use app\services\product\product\StoreProductServices;
  16. use app\services\product\sku\StoreProductAttrResultServices;
  17. use app\services\product\sku\StoreProductAttrServices;
  18. use app\services\product\sku\StoreProductAttrValueServices;
  19. use app\services\user\label\UserLabelServices;
  20. use crmeb\exceptions\AdminException;
  21. use crmeb\services\CacheService;
  22. /**
  23. * 优惠套餐
  24. * Class StoreDiscountsServices
  25. * @package app\services\activity\discounts
  26. * @mixin StoreDiscountsDao
  27. */
  28. class StoreDiscountsServices extends BaseServices
  29. {
  30. /**
  31. * 商品活动类型
  32. */
  33. const TYPE = 5;
  34. /**
  35. * @param StoreDiscountsDao $dao
  36. */
  37. public function __construct(StoreDiscountsDao $dao)
  38. {
  39. $this->dao = $dao;
  40. }
  41. /**
  42. * 保存数据
  43. * @param $data
  44. * @return mixed
  45. */
  46. public function saveDiscounts($data)
  47. {
  48. if ($data['freight'] == 2 && !$data['postage']) {
  49. throw new AdminException('请设置运费金额');
  50. }
  51. if ($data['freight'] == 3 && !$data['temp_id']) {
  52. throw new AdminException('请选择运费模版');
  53. }
  54. /** @var StoreProductAttrServices $storeProductAttrServices */
  55. $storeProductAttrServices = app()->make(StoreProductAttrServices::class);
  56. /** @var StoreDiscountsProductsServices $storeDiscountsProductsServices */
  57. $storeDiscountsProductsServices = app()->make(StoreDiscountsProductsServices::class);
  58. return $this->transaction(function () use ($data, $storeProductAttrServices, $storeDiscountsProductsServices) {
  59. //添加优惠套餐
  60. $discountsData['title'] = $data['title'];
  61. $discountsData['image'] = $data['image'];
  62. $discountsData['type'] = $data['type'];
  63. $discountsData['is_limit'] = $data['is_limit'];
  64. $discountsData['limit_num'] = $data['is_limit'] ? $data['limit_num'] : 0;
  65. $discountsData['link_ids'] = implode(',', $data['link_ids']);
  66. $discountsData['is_time'] = $data['is_time'];
  67. $discountsData['start_time'] = $data['is_time'] ? strtotime($data['time'][0]) : 0;
  68. $discountsData['stop_time'] = $data['is_time'] ? strtotime($data['time'][1]) + 86399 : 0;
  69. $discountsData['sort'] = $data['sort'];
  70. $discountsData['add_time'] = time();
  71. $discountsData['free_shipping'] = $data['free_shipping'];
  72. $discountsData['status'] = $data['status'];
  73. $discountsData['freight'] = $data['freight'];
  74. $discountsData['postage'] = $data['postage'];
  75. $discountsData['custom_form'] = json_encode($data['custom_form']);
  76. $discountsData['system_form_id'] = $data['system_form_id'] ?? 0;
  77. $product_ids = [];
  78. foreach ($data['products'] as $product) {
  79. if ($product['type']) {
  80. $product_ids = [];
  81. $product_ids[] = $product['product_id'];
  82. break;
  83. } else {
  84. $product_ids[] = $product['product_id'];
  85. }
  86. }
  87. $discountsData['product_ids'] = implode(',', $product_ids);
  88. if ($data['id']) {
  89. unset($discountsData['add_time']);
  90. $this->dao->update($data['id'], $discountsData);
  91. $storeDiscountsProductsServices->del($data['id']);
  92. $discountsId = $data['id'];
  93. } else {
  94. $discountsId = $this->dao->save($discountsData)->id;
  95. }
  96. if (!$discountsId) throw new AdminException('添加失败');
  97. if ($discountsData['is_limit']) CacheService::setStock(md5($discountsId), (int)$discountsData['limit_num'], 5);
  98. //添加优惠套餐内商品
  99. /** @var StoreProductServices $productService */
  100. $productService = app()->make(StoreProductServices::class);
  101. foreach ($data['products'] as $item) {
  102. $stock = $productService->value(['id' => $item['product_id'], 'is_del' => 0, 'is_show' => 1], 'stock') ?? 0;
  103. if ((int)$stock <= 0) {
  104. throw new AdminException('商品库存不足,请选择其他商品');
  105. }
  106. $productData = [];
  107. $productData['discount_id'] = $discountsId;
  108. $productData['product_id'] = $item['product_id'];
  109. $productData['product_type'] = $item['product_type'] ?? 0;
  110. $productData['title'] = $item['store_name'];
  111. $productData['image'] = $item['image'];
  112. $productData['type'] = $item['type'];
  113. $productData['temp_id'] = $item['temp_id'];
  114. $discountsProducts = $storeDiscountsProductsServices->save($productData);
  115. $skuList = $storeProductAttrServices->validateProductAttr($item['items'], $item['attr'], (int)$discountsProducts->id, 5);
  116. $valueGroup = $storeProductAttrServices->saveProductAttr($skuList, (int)$discountsProducts->id, 5);
  117. if (!$discountsProducts || !$valueGroup) throw new AdminException('添加失败');
  118. }
  119. return true;
  120. });
  121. }
  122. /**
  123. * 获取列表
  124. * @param array $where
  125. * @return array
  126. */
  127. public function getList($where = [])
  128. {
  129. [$page, $limit] = $this->getPageValue();
  130. $list = $this->dao->getList($where, ['products'], $page, $limit);
  131. $count = $this->dao->count($where + ['is_del' => 0]);
  132. $time = time();
  133. foreach ($list as &$item) {
  134. if (!$this->checkDiscount($item, 0) || ($item['stop_time'] && $item['stop_time'] < $time)) {
  135. $item['status'] = 0;
  136. $this->dao->update(['id' => $item['id']], ['status' => 0]);
  137. }
  138. $item['start_time'] = $item['start_time'] ? date('Y-m-d', $item['start_time']) : 0;
  139. $item['stop_time'] = $item['stop_time'] ? date('Y-m-d', $item['stop_time']) : 0;
  140. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  141. }
  142. return compact('list', 'count');
  143. }
  144. /**
  145. * 获取详情
  146. * @param $id
  147. * @return array
  148. * @throws \think\db\exception\DataNotFoundException
  149. * @throws \think\db\exception\DbException
  150. * @throws \think\db\exception\ModelNotFoundException
  151. */
  152. public function getInfo($id)
  153. {
  154. $discounts = $this->dao->get($id);
  155. if ($discounts) {
  156. $discounts = $discounts->toArray();
  157. } else {
  158. throw new AdminException('套餐商品未找到!');
  159. }
  160. /** @var StoreDiscountsProductsServices $discountsProducts */
  161. $discountsProducts = app()->make(StoreDiscountsProductsServices::class);
  162. /** @var UserLabelServices $userLabelServices */
  163. $userLabelServices = app()->make(UserLabelServices::class);
  164. $discounts['products'] = $discountsProducts->dao->getList(['discount_id' => $discounts['id']]);
  165. foreach ($discounts['products'] as &$item) {
  166. /** @var StoreProductAttrResultServices $storeProductAttrResultServices */
  167. $storeProductAttrResultServices = app()->make(StoreProductAttrResultServices::class);
  168. $discountsResult = $storeProductAttrResultServices->value(['product_id' => $item['id'], 'type' => 5], 'result');
  169. $item['items'] = json_decode($discountsResult, true)['attr'];
  170. $item['attr'] = $this->getattr($item['items'], $item['id'], 5);
  171. $item['store_name'] = $item['title'];
  172. }
  173. if ($discounts['start_time']) {
  174. $discounts['time'] = [date('Y-m-d', $discounts['start_time']), date('Y-m-d', $discounts['stop_time'])];
  175. } else {
  176. $discounts['time'] = [];
  177. }
  178. $link_ids = explode(',', $discounts['link_ids']);
  179. $discounts['link_ids'] = $userLabelServices->getLabelList(['ids' => $link_ids], ['id', 'label_name']);
  180. return $discounts;
  181. }
  182. /**
  183. * 获取规格
  184. * @param $attr
  185. * @param $id
  186. * @param $type
  187. * @return array
  188. */
  189. public function getattr($attr, $id, $type)
  190. {
  191. /** @var StoreProductAttrValueServices $storeProductAttrValueServices */
  192. $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  193. $value = attr_format($attr)[1];
  194. $valueNew = [];
  195. $count = 0;
  196. foreach ($value as $key => $item) {
  197. $detail = $item['detail'];
  198. $suk = implode(',', $item['detail']);
  199. $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');
  200. if (count($sukValue)) {
  201. $valueNew[$count]['value'] = '';
  202. foreach (array_values($detail) as $k => $v) {
  203. $valueNew[$count]['value' . ($k + 1)] = $v;
  204. $valueNew[$count]['value'] .= $valueNew[$count]['value'] == '' ? $v : ',' . $v;
  205. }
  206. $valueNew[$count]['detail'] = $detail;
  207. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  208. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  209. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  210. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  211. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  212. // $valueNew[$count]['quota'] = $sukValue[$suk]['quota'] ? intval($sukValue[$suk]['quota']) : 0;
  213. $valueNew[$count]['quota'] = isset($sukValue[$suk]['quota_show']) && $sukValue[$suk]['quota_show'] ? intval($sukValue[$suk]['quota_show']) : 0;
  214. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  215. $valueNew[$count]['code'] = $sukValue[$suk]['code'] ?? '';
  216. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ? floatval($sukValue[$suk]['weight']) : 0;
  217. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ? floatval($sukValue[$suk]['volume']) : 0;
  218. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ? floatval($sukValue[$suk]['brokerage']) : 0;
  219. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ? floatval($sukValue[$suk]['brokerage_two']) : 0;
  220. $count++;
  221. }
  222. }
  223. return $valueNew;
  224. }
  225. /**
  226. * 修改状态
  227. * @param $id
  228. * @param $status
  229. * @return mixed
  230. */
  231. public function setStatus($id, $status)
  232. {
  233. $discounts = $this->dao->get($id, ['*'], ['products']);
  234. if ($discounts) {
  235. $discounts = $discounts->toArray();
  236. } else {
  237. throw new AdminException('套餐商品未找到!');
  238. }
  239. //上架
  240. if ($status) {
  241. if ($discounts['stop_time'] && $discounts['stop_time'] < time()) {
  242. throw new AdminException('套餐活动已结束');
  243. }
  244. if (!$this->checkDiscount($discounts, 0)) {
  245. throw new AdminException('套餐内商品已下架或者库存不足');
  246. }
  247. }
  248. return $this->dao->update($id, ['status' => $status]);
  249. }
  250. /**
  251. * 删除商品
  252. * @param $id
  253. * @return mixed
  254. */
  255. public function del($id)
  256. {
  257. return $this->dao->update($id, ['is_del' => 1]);
  258. }
  259. /**
  260. * 获取优惠套餐列表
  261. * @param int $product_id
  262. * @param int $uid
  263. * @param int $limit
  264. * @return array
  265. */
  266. public function getDiscounts(int $product_id, int $uid, int $limit = 0)
  267. {
  268. $page = $limit ? 1 : 0;
  269. $list = $this->dao->getDiscounts($product_id, '*', $page, $limit);
  270. foreach ($list as $key => &$discounts) {
  271. $discounts = $this->checkDiscount($discounts, $uid);
  272. if (!$discounts) unset($list[$key]);
  273. }
  274. return $list;
  275. }
  276. /**
  277. * 购买|退款处理套餐限量
  278. * @param int $discountId
  279. * @param bool $is_dec
  280. * @return bool
  281. */
  282. public function changeDiscountLimit(int $discountId, bool $is_dec = true)
  283. {
  284. $is_limit = $this->dao->value(['id' => $discountId], 'is_limit');
  285. $res = true;
  286. //开启限量
  287. if ($is_limit) {
  288. if ($is_dec) $res = $res && $this->dao->decLimitNum($discountId);
  289. else $res = $res && $this->dao->incLimitNum($discountId);
  290. }
  291. return $res;
  292. }
  293. /**
  294. * 优惠套餐库存减少
  295. * @param int $num
  296. * @param int $discountId
  297. * @param int $discount_product_id
  298. * @param int $product_id
  299. * @param string $unique
  300. * @param int $store_id
  301. * @return bool
  302. */
  303. public function decDiscountStock(int $num, int $discountId, int $discount_product_id, int $product_id, string $unique, int $store_id = 0)
  304. {
  305. if (!$discountId || !$discount_product_id || !$product_id) return false;
  306. $res = true;
  307. if ($unique) {
  308. /** @var StoreProductAttrValueServices $skuValueServices */
  309. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  310. //套餐商品sku
  311. $suk = $skuValueServices->value(['unique' => $unique, 'product_id' => $discount_product_id, 'type' => 5], 'suk');
  312. //平台商品sku unique
  313. $productUnique = $skuValueServices->value(['suk' => $suk, 'product_id' => $product_id, 'type' => 0], 'unique');
  314. /** @var StoreProductServices $services */
  315. $services = app()->make(StoreProductServices::class);
  316. //减掉普通商品sku的库存加销量
  317. $res = false !== $services->decProductStock($num, $product_id, (string)$productUnique, $store_id);
  318. }
  319. return $res;
  320. }
  321. /**
  322. * 加库存减销量
  323. * @param int $num
  324. * @param int $discountId 套餐id
  325. * @param int $discount_product_id 套餐商品id
  326. * @param int $product_id 原商品ID
  327. * @param string $unique
  328. * @return bool
  329. */
  330. public function incDiscountStock(int $num, int $discountId, int $discount_product_id, int $product_id, string $unique, int $store_id = 0)
  331. {
  332. if (!$discountId || !$discount_product_id || !$product_id) return false;
  333. $res = true;
  334. if ($unique) {
  335. /** @var StoreProductAttrValueServices $skuValueServices */
  336. $skuValueServices = app()->make(StoreProductAttrValueServices::class);
  337. //套餐商品sku
  338. $suk = $skuValueServices->value(['unique' => $unique, 'product_id' => $discount_product_id, 'type' => 5], 'suk');
  339. //平台普通商品sku unique
  340. $productUnique = $skuValueServices->value(['suk' => $suk, 'product_id' => $product_id, 'type' => 0], 'unique');
  341. /** @var StoreProductServices $services */
  342. $services = app()->make(StoreProductServices::class);
  343. //增加当前普通商品sku的库存,减去销量
  344. $res = $res && $services->incProductStock($num, $product_id, (string)$productUnique, $store_id);
  345. }
  346. return $res;
  347. }
  348. /**
  349. * 判断优惠套餐显示
  350. * @param $discount
  351. * @param $uid
  352. * @return false
  353. */
  354. public function checkDiscount($discount, $uid)
  355. {
  356. $discount = is_object($discount) ? $discount->toArray() : $discount;
  357. if ($discount['is_limit'] && $discount['limit_num'] <= 0) return false;
  358. if (isset($discount['products']) && $discount['products']) {
  359. /** @var StoreProductServices $productService */
  360. $productService = app()->make(StoreProductServices::class);
  361. /** @var StoreProductAttrServices $storeProductAttrServices */
  362. $storeProductAttrServices = app()->make(StoreProductAttrServices::class);
  363. $minPrice = 0;
  364. foreach ($discount['products'] as $key => &$item) {
  365. $stock = $productService->value(['id' => $item['product_id'], 'is_del' => 0, 'is_show' => 1], 'stock') ?? 0;
  366. try {
  367. [$productAttr, $productValue] = $storeProductAttrServices->getProductAttrDetailCache($item['id'], $uid, 0, 5, $item['product_id']);
  368. } catch(\Throwable $e) {
  369. return false;
  370. }
  371. if ($discount['type']) {
  372. if ($item['type']) {
  373. if ($stock == 0) {
  374. return false;
  375. } else {
  376. if (!array_sum(array_column($productValue, 'product_stock'))) return false;
  377. }
  378. } else {
  379. if (!array_sum(array_column($productValue, 'product_stock'))) unset($discount['products'][$key]);
  380. }
  381. } else {
  382. if ($stock == 0) {
  383. return false;
  384. } else {
  385. if (!array_sum(array_column($productValue, 'product_stock'))) return false;
  386. }
  387. }
  388. $item['productAttr'] = $productAttr;
  389. $item['productValue'] = $productValue;
  390. $minPrice += min(array_column($productValue, 'price'));
  391. }
  392. if (!count($discount['products'])) return false;
  393. $discount['min_price'] = $minPrice;
  394. $discount['products'] = array_merge($discount['products']);
  395. }
  396. return $discount;
  397. }
  398. }