GoodsSku.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\store\model;
  13. use app\common\library\helper;
  14. use app\common\model\GoodsSku as GoodsSkuModel;
  15. use app\common\enum\goods\SpecType as SpecTypeEnum;
  16. /**
  17. * 商品规格模型
  18. * Class GoodsSku
  19. * @package app\store\model
  20. */
  21. class GoodsSku extends GoodsSkuModel
  22. {
  23. /**
  24. * 获取库存总数量 (根据sku列表数据)
  25. * @param array $skuList
  26. * @return float|int
  27. */
  28. public static function getStockTotal(array $skuList)
  29. {
  30. return helper::getArrayColumnSum($skuList, 'stock_num');
  31. }
  32. /**
  33. * 获取预警库存总数量 (根据sku列表数据)
  34. * @param array $skuList
  35. * @return float|int
  36. */
  37. public static function getAlarmStockTotal(array $skuList)
  38. {
  39. return helper::getArrayColumnSum($skuList, 'alarm_stock_num');
  40. }
  41. /**
  42. * 获取商品价格高低区间 (根据sku列表数据)
  43. * @param array $skuList
  44. * @return array
  45. */
  46. public static function getGoodsPrices(array $skuList)
  47. {
  48. $goodsPriceArr = helper::getArrayColumn($skuList, 'goods_price');
  49. return [min($goodsPriceArr), max($goodsPriceArr)];
  50. }
  51. /**
  52. * 获取划线价格高低区间 (根据sku列表数据)
  53. * @param array $skuList
  54. * @return array
  55. */
  56. public static function getLinePrices(array $skuList)
  57. {
  58. $linePriceArr = helper::getArrayColumn($skuList, 'line_price') ?: [0.00];
  59. return [min($linePriceArr), max($linePriceArr)];
  60. }
  61. /**
  62. * 生成skuList数据(写入goods_sku_id)
  63. * @param array $newSpecList
  64. * @param array $skuList
  65. * @return array
  66. */
  67. public static function getNewSkuList(array $newSpecList, array $skuList, $goodsNo, $goodsId = 0)
  68. {
  69. foreach ($skuList as $key=>&$skuItem) {
  70. $skuItem['specValueIds'] = static::getSpecValueIds($newSpecList, $skuItem['skuKeys']);
  71. $skuItem['goodsProps'] = static::getGoodsProps($newSpecList, $skuItem['skuKeys']);
  72. $skuItem['goods_sku_id'] = implode('_', $skuItem['specValueIds']);
  73. if(!isset($skuItem['goods_sku_no']) || empty($skuItem['goods_sku_no'])){//如果没有设置sku编号
  74. $skuItem['goods_sku_no'] = Goods::makeGoodsSkuNo($goodsNo, (string)($key+1));
  75. }
  76. // 获取原来的结算价
  77. if ($goodsId) {
  78. $oldSkuInfo = self::where('goods_id', $goodsId)->where('goods_sku_id', $skuItem['goods_sku_id'])->find();
  79. $skuItem['clearing_price'] = $oldSkuInfo['clearing_price'] ?? 0;
  80. $skuItem['platform_rate'] = $oldSkuInfo['platform_rate'] ?? 0;
  81. }
  82. }
  83. return $skuList;
  84. }
  85. /**
  86. * 根据$skuKeys生成规格值id集
  87. * @param array $newSpecList
  88. * @param array $skuKeys
  89. * @return array
  90. */
  91. private static function getSpecValueIds(array $newSpecList, array $skuKeys)
  92. {
  93. $goodsSkuIdArr = [];
  94. foreach ($skuKeys as $skuKey) {
  95. $specValueItem = $newSpecList[$skuKey['groupKey']]['valueList'][$skuKey['valueKey']];
  96. $goodsSkuIdArr[] = $specValueItem['spec_value_id'];
  97. }
  98. return $goodsSkuIdArr;
  99. }
  100. /**
  101. * 根据$skuKeys生成规格属性记录
  102. * @param array $newSpecList
  103. * @param array $skuKeys
  104. * @return array
  105. */
  106. private static function getGoodsProps(array $newSpecList, array $skuKeys)
  107. {
  108. $goodsPropsArr = [];
  109. foreach ($skuKeys as $skuKey) {
  110. $groupItem = $newSpecList[$skuKey['groupKey']];
  111. $specValueItem = $groupItem['valueList'][$skuKey['valueKey']];
  112. $goodsPropsArr[] = [
  113. 'group' => ['name' => $groupItem['spec_name'], 'id' => $groupItem['spec_id']],
  114. 'value' => ['name' => $specValueItem['spec_value'], 'id' => $specValueItem['spec_value_id']]
  115. ];
  116. }
  117. return $goodsPropsArr;
  118. }
  119. /**
  120. * 新增商品sku记录
  121. * @param int $goodsId
  122. * @param array $newSkuList
  123. * @param int $specType
  124. * @return array|bool|false
  125. */
  126. public static function add(int $goodsId, int $specType = SpecTypeEnum::SINGLE, array $newSkuList = [])
  127. {
  128. // 单规格模式
  129. if ($specType === SpecTypeEnum::SINGLE) {
  130. return (new static)->save(array_merge($newSkuList, [
  131. 'goods_id' => $goodsId,
  132. 'goods_sku_id' => 0,
  133. 'store_id' => self::$storeId,
  134. 'create_time'=>time(),
  135. 'update_time'=>time()
  136. ]));
  137. } // 多规格模式
  138. elseif ($specType === SpecTypeEnum::MULTI) {
  139. // 批量写入商品sku记录
  140. return static::increasedFroMulti($goodsId, $newSkuList);
  141. }
  142. return false;
  143. }
  144. /**
  145. * 更新商品sku记录
  146. * @param int $goodsId
  147. * @param int $specType
  148. * @param array $skuList
  149. * @return array|bool|false
  150. */
  151. public static function edit(int $goodsId, int $specType = SpecTypeEnum::SINGLE, array $skuList = [])
  152. {
  153. // 调用批量更改门店商品sku
  154. (new ShopGoods())->updateAllShopGoodsSku($goodsId, $skuList);
  155. // 删除所有的sku记录
  156. static::deleteAll(['goods_id' => $goodsId]);
  157. // 新增商品sku记录
  158. return static::add($goodsId, $specType, $skuList);
  159. }
  160. /**
  161. * 批量写入商品sku记录
  162. * @param int $goodsId
  163. * @param array $skuList
  164. * @return array|false
  165. */
  166. public static function increasedFroMulti(int $goodsId, array $skuList)
  167. {
  168. $dataset = [];
  169. foreach ($skuList as $skuItem) {
  170. $dataset[] = array_merge($skuItem, [
  171. 'goods_sku_id' => $skuItem['goods_sku_id'],
  172. 'line_price' => $skuItem['line_price'] ?: 0.00,
  173. 'goods_sku_no' => $skuItem['goods_sku_no'] ?: '',
  174. 'stock_num' => $skuItem['stock_num'] ?: 0,
  175. 'alarm_stock_num' => $skuItem['alarm_stock_num'] ?: 0,
  176. 'goods_weight' => $skuItem['goods_weight'] ?: 0,
  177. 'goods_gross_weight' => $skuItem['goods_gross_weight'] ?: 0,
  178. 'goods_props' => $skuItem['goodsProps'],
  179. 'spec_value_ids' => $skuItem['specValueIds'],
  180. 'goods_id' => $goodsId,
  181. 'store_id' => self::$storeId
  182. ]);
  183. }
  184. return (new static)->addAll($dataset);
  185. }
  186. /**
  187. * @param $prices
  188. * @return bool
  189. * @throws \Exception
  190. */
  191. public static function updateClearingPrice($prices){
  192. if (!count($prices))return true;
  193. $user = new self();
  194. $user->saveAll($prices);
  195. return true;
  196. }
  197. /**
  198. * @param $prices
  199. * @return bool
  200. * @throws \Exception
  201. */
  202. public static function updatePlatformRate($prices){
  203. if (!count($prices))return true;
  204. $user = new self();
  205. $user->saveAll($prices);
  206. return true;
  207. }
  208. }