StoreProductAttrServices.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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\product\sku;
  12. use app\dao\product\sku\StoreProductAttrDao;
  13. use app\services\BaseServices;
  14. use app\services\order\StoreCartServices;
  15. use app\services\product\product\StoreProductServices;
  16. use crmeb\exceptions\AdminException;
  17. use crmeb\traits\OptionTrait;
  18. /**
  19. * Class StoreProductAttrService
  20. * @package app\services\product\sku
  21. * @mixin StoreProductAttrDao
  22. */
  23. class StoreProductAttrServices extends BaseServices
  24. {
  25. use OptionTrait;
  26. /**
  27. * StoreProductAttrServices constructor.
  28. * @param StoreProductAttrDao $dao
  29. */
  30. public function __construct(StoreProductAttrDao $dao)
  31. {
  32. $this->dao = $dao;
  33. }
  34. /**
  35. * 生成规格唯一值
  36. * @param int $id
  37. * @param string $sku
  38. * @return false|string
  39. */
  40. public function createAttrUnique(int $id, string $sku)
  41. {
  42. return substr(md5($id . $sku . uniqid(true)), 12, 8);
  43. }
  44. /**
  45. * 根据根据前端规格数据获取sku
  46. * @param array $value
  47. * @param string
  48. */
  49. public function getSku(array $value)
  50. {
  51. $sku = '';
  52. if ($value) {
  53. $detail = [];
  54. $count = count($value['detail'] ?? []);
  55. for ($i=1; $i<=$count ; $i++) {
  56. $detail[] = trim($value['value' . $i]);
  57. }
  58. $sku = implode(',', $detail);
  59. }
  60. return $sku;
  61. }
  62. /**
  63. * 添加商品属性数据判断
  64. * @param array $attrList
  65. * @param array $valueList
  66. * @param int $productId
  67. * @param int $type
  68. * @param int $is_vip
  69. * @param int $validate
  70. * @return array
  71. */
  72. public function validateProductAttr(array $attrList, array $valueList, int $productId, int $type = 0, int $is_vip = 0, int $validate = 1)
  73. {
  74. $result = ['attr' => $attrList, 'value' => $valueList];
  75. $attrValueList = [];
  76. $attrNameList = [];
  77. foreach ($attrList as $index => $attr) {
  78. if (!isset($attr['value'])) {
  79. throw new AdminException('请输入规则名称!');
  80. }
  81. $attr['value'] = trim($attr['value']);
  82. if (!isset($attr['value'])) {
  83. throw new AdminException('请输入规则名称!!');
  84. }
  85. if (!isset($attr['detail']) || !count($attr['detail'])) {
  86. throw new AdminException('请输入属性名称!');
  87. }
  88. foreach ($attr['detail'] as $k => $attrValue) {
  89. $attrValue = trim($attrValue);
  90. if (empty($attrValue)) {
  91. throw new AdminException('请输入正确的属性');
  92. }
  93. $attr['detail'][$k] = $attrValue;
  94. $attrValueList[] = $attrValue;
  95. $attr['detail'][$k] = $attrValue;
  96. }
  97. $attrNameList[] = $attr['value'];
  98. $attrList[$index] = $attr;
  99. }
  100. $attrCount = count($attrList);
  101. foreach ($valueList as $index => $value) {
  102. if (!isset($value['detail']) || count($value['detail']) != $attrCount) {
  103. throw new AdminException('请填写正确的商品信息');
  104. }
  105. if (!isset($value['price']) || !is_numeric($value['price']) || floatval($value['price']) != $value['price']) {
  106. throw new AdminException('请填写正确的商品价格');
  107. }
  108. if ($type == 4) {
  109. if (!isset($value['integral']) || !is_numeric($value['integral']) || floatval($value['integral']) != $value['integral']) {
  110. throw new AdminException('请填写正确的商品兑换积分');
  111. }
  112. if (isset($value['price']) && $value['price'] <= 0 && isset($value['integral']) && $value['integral'] <= 0) {
  113. throw new AdminException('积分商品兑换积分和价格不能同时为空');
  114. }
  115. }
  116. if (!isset($value['stock']) || !is_numeric($value['stock']) || intval($value['stock']) != $value['stock']) {
  117. throw new AdminException('请填写正确的商品库存');
  118. }
  119. //供应商结算价 不用成本价
  120. if (!($value['settle_price'] ?? 0) && (!isset($value['cost']) || !is_numeric($value['cost']) || floatval($value['cost']) != $value['cost'])) {
  121. throw new AdminException('请填写正确的商品成本价格');
  122. }
  123. if ($validate && (!isset($value['pic']) || empty($value['pic']))) {
  124. throw new AdminException('请上传商品规格图片');
  125. }
  126. if ($is_vip && (!isset($value['vip_price']) || !$value['vip_price'])) {
  127. throw new AdminException('会员价格不能为0');
  128. }
  129. foreach ($value['detail'] as $attrName => $attrValue) {
  130. //如果attrName 存在空格 则这个规格key 会出现两次
  131. unset($valueList[$index]['detail'][$attrName]);
  132. $attrName = trim($attrName);
  133. $attrValue = trim($attrValue);
  134. if (!in_array($attrName, $attrNameList, true)) {
  135. throw new AdminException($attrName . '规则不存在');
  136. }
  137. if (!in_array($attrValue, $attrValueList, true)) {
  138. throw new AdminException($attrName . '属性不存在');
  139. }
  140. if (empty($attrName)) {
  141. throw new AdminException('请输入正确的属性');
  142. }
  143. $valueList[$index]['detail'][$attrName] = $attrValue;
  144. }
  145. }
  146. $attrGroup = [];
  147. $valueGroup = [];
  148. foreach ($attrList as $k => $value) {
  149. $attrGroup[] = [
  150. 'product_id' => $productId,
  151. 'attr_name' => $value['value'],
  152. 'attr_values' => $value['detail'],
  153. 'type' => $type
  154. ];
  155. }
  156. /** @var StoreProductAttrValueServices $storeProductAttrValueServices */
  157. $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  158. $skuArray = $storeProductAttrValueServices->getSkuArray(['product_id' => $productId, 'type' => $type], 'unique', 'suk');
  159. foreach ($valueList as $k => $value) {
  160. // sort($value['detail'], SORT_STRING);
  161. $sku = implode(',', $value['detail']);
  162. $valueGroup[$sku] = [
  163. 'product_id' => $productId,
  164. 'suk' => $this->getSku($value),
  165. 'price' => $value['price'],
  166. 'integral' => isset($value['integral']) ? $value['integral'] : 0,
  167. 'settle_price' => $value['settle_price'] ?? 0.00,//供应商结算价
  168. 'cost' => ($value['settle_price'] ?? 0.00) ?: $value['cost'],//供应端:去除成本价
  169. 'ot_price' => $value['ot_price'],
  170. 'stock' => $value['stock'],
  171. 'unique' => $skuArray[$sku] ?? $this->createAttrUnique($productId, $sku),
  172. 'image' => $value['pic'],
  173. 'bar_code' => $value['bar_code'] ?? '',
  174. 'weight' => $value['weight'] ?? 0,
  175. 'volume' => $value['volume'] ?? 0,
  176. 'brokerage' => $value['brokerage'] ?? 0,
  177. 'brokerage_two' => $value['brokerage_two'] ?? 0,
  178. 'type' => $type,
  179. 'quota' => $value['quota'] ?? 0,
  180. 'quota_show' => $value['quota'] ?? 0,
  181. 'vip_price' => $value['vip_price'] ?? 0,
  182. 'award_price' => $value['award_price'] ?? 0,
  183. 'code' => $value['code'] ?? '',
  184. 'product_type' => $value['product_type'] ?? 0,
  185. 'virtual_list' => $value['virtual_list'] ?? [],
  186. 'disk_info' => $value['disk_info'] ?? '',
  187. 'write_times' => $value['write_times'] ?? 1,
  188. 'write_valid' => $value['write_valid'] ?? 1,
  189. 'write_days' => $value['write_days'] ?? $value['days'] ?? 0,
  190. 'write_start' => ($value['section_time'][0] ?? '') ? strtotime($value['section_time'][0] ?? '') : 0,
  191. 'write_end' => ($value['section_time'][1] ?? '') ? strtotime($value['section_time'][1] ?? '') : 0,
  192. ];
  193. }
  194. if (!count($attrGroup) || !count($valueGroup)) {
  195. throw new AdminException('请设置至少一个属性!');
  196. }
  197. return compact('result', 'attrGroup', 'valueGroup');
  198. }
  199. /**
  200. * 保存商品规格
  201. * @param array $data
  202. * @param int $id
  203. * @param int $type
  204. * @return bool|mixed|\think\Collection
  205. * @throws \think\db\exception\DataNotFoundException
  206. * @throws \think\db\exception\DbException
  207. * @throws \think\db\exception\ModelNotFoundException
  208. */
  209. public function saveProductAttr(array $data, int $id, int $type = 0)
  210. {
  211. $this->setAttr($data['attrGroup'], $id, $type);
  212. /** @var StoreProductAttrResultServices $storeProductAttrResultServices */
  213. $storeProductAttrResultServices = app()->make(StoreProductAttrResultServices::class);
  214. $storeProductAttrResultServices->setResult($data['result'], $id, $type);
  215. /** @var StoreProductAttrValueServices $storeProductAttrValueServices */
  216. $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  217. $valueGroup = $data['valueGroup'] ?? [];
  218. $updateSuks = array_column($valueGroup, 'suk');
  219. $oldSuks = [];
  220. $oldAttrValue = $storeProductAttrValueServices->getSkuArray(['product_id' => $id, 'type' => $type], '*', 'suk');
  221. if ($oldAttrValue) $oldSuks = array_column($oldAttrValue, 'suk');
  222. $delSuks = array_merge(array_diff($oldSuks, $updateSuks));
  223. $dataAll = [];
  224. $res1 = $res2 = $res3 = true;
  225. foreach ($valueGroup as $item) {
  226. if ($oldSuks && in_array($item['suk'], $oldSuks) && isset($oldAttrValue[$item['suk']])) {
  227. $attrId = $oldAttrValue[$item['suk']]['id'];
  228. unset($item['suk'], $item['unique']);
  229. $item['virtual_list'] =json_encode($item['virtual_list']);
  230. $res1 = $res1 && $storeProductAttrValueServices->update($attrId, $item);
  231. } else {
  232. $dataAll[] = $item;
  233. }
  234. }
  235. if ($delSuks) {
  236. $res2 = $storeProductAttrValueServices->del($id, $type, $delSuks);
  237. }
  238. if ($dataAll) {
  239. $res3 = $storeProductAttrValueServices->saveAll($dataAll);
  240. }
  241. if ($res1 && $res2 && $res3) {
  242. // $unique = array_column($valueGroup, 'unique');
  243. // $storeProductAttrValueServices->updateSumStock($unique ?? []);
  244. return $valueGroup;
  245. } else {
  246. throw new AdminException('商品规格信息保存失败');
  247. }
  248. }
  249. /**
  250. * 获取商品规格
  251. * @param array $where
  252. * @return array
  253. */
  254. public function getProductAttr(array $where)
  255. {
  256. return $this->dao->getProductAttr($where);
  257. }
  258. /**
  259. * 获取商品规格详情
  260. * @param int $id
  261. * @param int $uid
  262. * @param int $cartNum //是否查询购物车数量
  263. * @param int $type //活动类型 attr_value表
  264. * @param int $productId
  265. * @param array $productInfo
  266. * @param int $discount //限时折扣
  267. * @return array
  268. * @throws \think\db\exception\DataNotFoundException
  269. * @throws \think\db\exception\DbException
  270. * @throws \think\db\exception\ModelNotFoundException
  271. */
  272. public function getProductAttrDetailCache(int $id, int $uid, int $cartNum = 0, int $type = 0, int $productId = 0, array $productInfo = [], int $discount = -1)
  273. {
  274. $attrDetail = $this->dao->cacheTag()->remember('attr_' . $id . '_' . $type . '_' . $productId, function () use ($productId, $id, $type) {
  275. $attrDetail = $this->dao->getProductAttr(['product_id' => $id, 'type' => $type]);
  276. if (!$attrDetail && $type > 0 && $productId) {//活动商品未获取到规格信息
  277. $attrDetail = $this->dao->getProductAttr(['product_id' => $productId, 'type' => 0]);
  278. }
  279. return $attrDetail;
  280. }, 600);
  281. /** @var StoreProductAttrValueServices $storeProductAttrValueService */
  282. $storeProductAttrValueService = app()->make(StoreProductAttrValueServices::class);
  283. $_values = $this->dao->cacheTag()->remember('attr_value_' . $id . '_' . $type, function () use ($storeProductAttrValueService, $id, $type) {
  284. return $storeProductAttrValueService->getProductAttrValue(['product_id' => $id, 'type' => $type]);
  285. }, 600);
  286. if ($productId == 0) {
  287. $productId = $id;
  288. }
  289. /** @var StoreProductServices $storeProductService */
  290. $storeProductService = app()->make(StoreProductServices::class);
  291. $vip_price = true;
  292. if (!$storeProductService->vipIsOpen(!!($productInfo['is_vip'] ?? 0))) $vip_price = false;
  293. $cartNumList = [];
  294. $activityAttr = [];
  295. if ($cartNum) {
  296. /** @var StoreCartServices $storeCartService */
  297. $storeCartService = app()->make(StoreCartServices::class);
  298. $unique = array_column($_values, 'unique');
  299. $cartNumList = $storeCartService->cacheTag('Cart_Nums_' . $uid)->remember(md5(json_encode($unique)),
  300. function () use ($storeCartService, $unique, $id, $uid) {
  301. return $storeCartService->getUserCartNums($unique, $id, $uid);
  302. }
  303. , 600);
  304. }
  305. $values = [];
  306. $field = $type ? 'stock,price' : 'stock';
  307. $storeProducts = $this->dao->cacheTag()->remember('attr_sku_' . $productId . '_' . $type, function () use ($storeProductAttrValueService, $productId, $field) {
  308. return $storeProductAttrValueService->getSkuArray(['product_id' => $productId, 'type' => 0], $field, 'suk');
  309. });
  310. foreach ($_values as $value) {
  311. if ($cartNum) {
  312. $value['cart_num'] = $cartNumList[$value['unique']] ?? 0;
  313. }
  314. if (!$vip_price) $value['vip_price'] = 0;
  315. $value['product_stock'] = $storeProducts[$value['suk']]['stock'] ?? 0;
  316. if ($discount != -1) $value['price'] = bcmul((string)$value['price'], (string)bcdiv((string)$discount, '100', 2), 2);
  317. if ($type) {
  318. $value['product_price'] = $storeProducts[$value['suk']]['price'] ?? 0;
  319. $attrs = explode(',', $value['suk']);
  320. $count = count($attrs);
  321. for ($i = 0; $i < $count; $i++) {
  322. $activityAttr[$i][] = $attrs[$i];
  323. }
  324. }
  325. $values[$value['suk']] = $value;
  326. }
  327. foreach ($attrDetail as $k => $v) {
  328. $attr = $v['attr_values'];
  329. //活动商品只展示参与活动sku
  330. if ($type && $activityAttr && $a = array_merge(array_intersect($v['attr_values'], $activityAttr[$k]))) {
  331. $attrDetail[$k]['attr_values'] = $a;
  332. $attr = $a;
  333. }
  334. foreach ($attr as $kk => $vv) {
  335. $attrDetail[$k]['attr_value'][$kk]['attr'] = $vv;
  336. $attrDetail[$k]['attr_value'][$kk]['check'] = false;
  337. }
  338. }
  339. return [$attrDetail, $values];
  340. }
  341. /**
  342. * 获取商品规格详情
  343. * @param int $id
  344. * @param int $uid
  345. * @param int $cartNum //是否查询购物车数量
  346. * @param int $type //活动类型 attr_value表
  347. * @param int $productId
  348. * @param array $productInfo
  349. * @param int $discount //限时折扣
  350. * @return array
  351. * @throws \think\db\exception\DataNotFoundException
  352. * @throws \think\db\exception\DbException
  353. * @throws \think\db\exception\ModelNotFoundException
  354. */
  355. public function getProductAttrDetail(int $id, int $uid, int $cartNum = 0, int $type = 0, int $productId = 0, array $productInfo = [], int $discount = -1)
  356. {
  357. $attrDetail = $this->dao->getProductAttr(['product_id' => $id, 'type' => $type]);
  358. if (!$attrDetail && $type > 0 && $productId) {//活动商品未获取到规格信息
  359. $attrDetail = $this->dao->getProductAttr(['product_id' => $productId, 'type' => 0]);
  360. }
  361. /** @var StoreProductAttrValueServices $storeProductAttrValueService */
  362. $storeProductAttrValueService = app()->make(StoreProductAttrValueServices::class);
  363. $_values = $storeProductAttrValueService->getProductAttrValue(['product_id' => $id, 'type' => $type]);
  364. if ($productId == 0) $productId = $id;
  365. /** @var StoreProductServices $storeProductService */
  366. $storeProductService = app()->make(StoreProductServices::class);
  367. if (!$productInfo) {
  368. $productInfo = $storeProductService->get($productId, ['is_vip']);
  369. }
  370. $vip_price = true;
  371. if (!$storeProductService->vipIsOpen(!!$productInfo['is_vip'])) $vip_price = false;
  372. $cartNumList = [];
  373. $activityAttr = [];
  374. if ($cartNum) {
  375. /** @var StoreCartServices $storeCartService */
  376. $storeCartService = app()->make(StoreCartServices::class);
  377. //真实用户
  378. if ($uid) {
  379. $cartNumList = $storeCartService->getUserCartNums(array_column($_values, 'unique'), $id, $uid);
  380. } else {
  381. //虚拟用户
  382. $touristUid = $this->getItem('touristUid');
  383. if ($touristUid) {
  384. $cartNumList = $storeCartService->getUserCartNums(array_column($_values, 'unique'), $id, $touristUid, 'tourist_uid');
  385. }
  386. }
  387. }
  388. $values = [];
  389. $field = $type ? 'stock,price' : 'stock';
  390. $storeProducts = $storeProductAttrValueService->getSkuArray(['product_id' => $productId, 'type' => 0], $field, 'suk');
  391. foreach ($_values as $value) {
  392. if ($cartNum) {
  393. $value['cart_num'] = $uid || $touristUid ? ($cartNumList[$value['unique']] ?? 0) : 0;
  394. }
  395. if (!$vip_price) $value['vip_price'] = 0;
  396. $value['product_stock'] = $storeProducts[$value['suk']]['stock'] ?? 0;
  397. if ($discount != -1) $value['price'] = bcmul((string)$value['price'], (string)bcdiv((string)$discount, '100', 2), 2);
  398. if ($type) {
  399. $value['product_price'] = $storeProducts[$value['suk']]['price'] ?? 0;
  400. $attrs = explode(',', $value['suk']);
  401. $count = count($attrs);
  402. for ($i = 0; $i < $count; $i++) {
  403. $activityAttr[$i][] = $attrs[$i];
  404. }
  405. }
  406. $values[$value['suk']] = $value;
  407. }
  408. foreach ($attrDetail as $k => $v) {
  409. $attr = $v['attr_values'];
  410. //活动商品只展示参与活动sku
  411. if ($type && $activityAttr && $a = array_merge(array_intersect($v['attr_values'], $activityAttr[$k]))) {
  412. $attrDetail[$k]['attr_values'] = $a;
  413. $attr = $a;
  414. }
  415. foreach ($attr as $kk => $vv) {
  416. $attrDetail[$k]['attr_value'][$kk]['attr'] = $vv;
  417. $attrDetail[$k]['attr_value'][$kk]['check'] = false;
  418. }
  419. }
  420. return [$attrDetail, $values];
  421. }
  422. /**
  423. * 删除一条数据
  424. * @param int $id
  425. * @param int $type
  426. */
  427. public function del(int $id, int $type)
  428. {
  429. $this->dao->del($id, $type);
  430. }
  431. /**
  432. * 设置规格
  433. * @param array $data
  434. * @param int $id
  435. * @param int $type
  436. * @return bool
  437. * @throws \Exception
  438. */
  439. public function setAttr(array $data, int $id, int $type)
  440. {
  441. if ($data) {
  442. $this->dao->del($id, $type);
  443. $res = $this->dao->saveAll($data);
  444. if (!$res) throw new AdminException('规格保存失败');
  445. }
  446. return true;
  447. }
  448. }