StoreProductAttrServices.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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. /**
  18. * Class StoreProductAttrService
  19. * @package app\services\product\sku
  20. */
  21. class StoreProductAttrServices extends BaseServices
  22. {
  23. /**
  24. * StoreProductAttrServices constructor.
  25. * @param StoreProductAttrDao $dao
  26. */
  27. public function __construct(StoreProductAttrDao $dao)
  28. {
  29. $this->dao = $dao;
  30. }
  31. /**
  32. * 保存商品规格
  33. * @param array $data
  34. * @param int $id
  35. * @param int $type
  36. * @return bool
  37. */
  38. public function saveProductAttr(array $data, int $id, int $type = 0, $is_vip = 0, $is_virtual = 0)
  39. {
  40. /** @var StoreProductAttrResultServices $storeProductAttrResultServices */
  41. $storeProductAttrResultServices = app()->make(StoreProductAttrResultServices::class);
  42. /** @var StoreProductAttrValueServices $storeProductAttrValueServices */
  43. $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
  44. /** @var StoreProductServices $storeProductService */
  45. $storeProductService = app()->make(StoreProductServices::class);
  46. $this->dao->del($id, $type);
  47. $storeProductAttrResultServices->del($id, $type);
  48. $storeProductAttrValueServices->del($id, $type);
  49. $this->dao->saveAll($data['attrGroup']);
  50. $storeProductAttrResultServices->setResult($data['result'], $id, $type);
  51. $productVipPrice = 0;
  52. if (isset($data['valueGroup']) && $data['valueGroup']) {
  53. $detailTemp = array_column($data['valueGroup'], 'vip_price');
  54. if ($detailTemp) {
  55. if ($is_vip && in_array(0, $detailTemp)) throw new AdminException(400588);
  56. $detailTemp = array_diff($detailTemp, [0]);
  57. if ($detailTemp) {
  58. $productVipPrice = min($detailTemp);
  59. }
  60. }
  61. if ($type == 0) {
  62. $storeProductService->update($id, ['vip_price' => $productVipPrice]);
  63. }
  64. }
  65. if ($is_virtual == 0 || $is_virtual == 2) {
  66. if ($is_virtual == 2 && in_array(0, array_column($data['valueGroup'], 'coupon_id'))) {
  67. throw new AdminException(400589);
  68. }
  69. return $storeProductAttrValueServices->saveAll($data['valueGroup']);
  70. } else {
  71. /** @var StoreProductVirtualServices $productVirtual */
  72. $productVirtual = app()->make(StoreProductVirtualServices::class);
  73. $cardStock = 0;
  74. foreach ($data['valueGroup'] as &$item) {
  75. $res = $storeProductAttrValueServices->save($item);
  76. if ($item['is_virtual'] && count($item['virtual_list']) && !$item['coupon_id'] && $item['disk_info'] == '') {
  77. $productVirtual->delete(['product_id' => $id, 'attr_unique' => $item['unique'], 'uid' => 0]);
  78. $sales = $productVirtual->count(['product_id' => $id, 'attr_unique' => $item['unique']]);
  79. foreach ($item['virtual_list'] as &$items) {
  80. $data = [
  81. 'product_id' => $id,
  82. 'attr_unique' => $res->unique,
  83. 'card_no' => $items['key'],
  84. 'card_pwd' => $items['value'],
  85. 'card_unique' => md5($res->unique . ',' . $items['key'] . ',' . $items['value'])
  86. ];
  87. if (!$productVirtual->count(['card_no' => $items['key'], 'card_pwd' => $items['value']])) {
  88. $productVirtual->save($data);
  89. }
  90. }
  91. $allStock = $productVirtual->count(['product_id' => $id, 'attr_unique' => $res->unique]);
  92. $storeProductAttrValueServices->update(['id' => $res['id']], ['stock' => $allStock - $sales, 'sales' => $sales]);
  93. $cardStock = $cardStock + ($allStock - $sales);
  94. }
  95. }
  96. if ($cardStock > 0) $storeProductService->update($id, ['stock' => $cardStock]);
  97. return true;
  98. }
  99. }
  100. /**
  101. * 获取商品规格
  102. * @param array $where
  103. * @return array
  104. */
  105. public function getProductAttr(array $where)
  106. {
  107. return $this->dao->getProductAttr($where);
  108. }
  109. /**
  110. * 获取商品规格详情
  111. * @param int $id
  112. * @param int $uid
  113. * @param int $type
  114. * @param int $typeId
  115. * @param int $productId
  116. * @return array
  117. */
  118. public function getProductAttrDetail(int $id, int $uid, int $type, int $typeId = 0, int $productId = 0)
  119. {
  120. $attrDetail = $this->dao->getProductAttr(['product_id' => $id, 'type' => $typeId]);
  121. /** @var StoreProductAttrValueServices $storeProductAttrValueService */
  122. $storeProductAttrValueService = app()->make(StoreProductAttrValueServices::class);
  123. /** @var StoreProductServices $storeProductService */
  124. $storeProductService = app()->make(StoreProductServices::class);
  125. $_values = $storeProductAttrValueService->getProductAttrValue(['product_id' => $id, 'type' => $typeId]);
  126. if ($productId == 0) $productId = $id;
  127. $is_vip = $storeProductService->get($productId, ['is_vip']);
  128. $vip_price = true;
  129. if (!$storeProductService->vipIsOpen(!!$is_vip['is_vip'])) $vip_price = false;
  130. $stock = $storeProductAttrValueService->getColumn(['product_id' => $productId, 'type' => 0], 'stock', 'suk');
  131. $values = [];
  132. $cartNumList = [];
  133. $activityAttr = [];
  134. if ($uid) {
  135. /** @var StoreCartServices $storeCartService */
  136. $storeCartService = app()->make(StoreCartServices::class);
  137. $cartNumList = $storeCartService->getUserCartNums(array_column($_values, 'unique'), $id, $uid);
  138. }
  139. foreach ($_values as $value) {
  140. if ($type) {
  141. if ($uid)
  142. $value['cart_num'] = $cartNumList[$value['unique']];
  143. else
  144. $value['cart_num'] = 0;
  145. if (is_null($value['cart_num'])) $value['cart_num'] = 0;
  146. }
  147. if (!$vip_price) $value['vip_price'] = 0;
  148. $value['product_stock'] = $stock[$value['suk']] ?? 0;
  149. $values[$value['suk']] = $value;
  150. if ($typeId) {
  151. $attrs = explode(',', $value['suk']);
  152. $count = count($attrs);
  153. for ($i = 0; $i < $count; $i++) {
  154. $activityAttr[$i][] = $attrs[$i];
  155. }
  156. }
  157. }
  158. foreach ($attrDetail as $k => $v) {
  159. $attr = $v['attr_values'];
  160. //活动商品只展示参与活动sku
  161. if ($typeId && $activityAttr && $a = array_merge(array_intersect($v['attr_values'], $activityAttr[$k]))) {
  162. $attrDetail[$k]['attr_values'] = $a;
  163. $attr = $a;
  164. }
  165. foreach ($attr as $kk => $vv) {
  166. $attrDetail[$k]['attr_value'][$kk]['attr'] = $vv;
  167. $attrDetail[$k]['attr_value'][$kk]['check'] = false;
  168. }
  169. }
  170. return [$attrDetail, $values];
  171. }
  172. }