GoodsSku.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 (int)helper::getArrayColumnSum($skuList, 'stock_num');
  31. }
  32. /**
  33. * 获取商品价格高低区间 (根据sku列表数据)
  34. * @param array $skuList
  35. * @return array
  36. */
  37. public static function getGoodsPrices(array $skuList): array
  38. {
  39. $goodsPriceArr = helper::getArrayColumn($skuList, 'goods_price');
  40. return empty($goodsPriceArr) ? [0, 0] : [min($goodsPriceArr), max($goodsPriceArr)];
  41. }
  42. /**
  43. * 获取划线价格高低区间 (根据sku列表数据)
  44. * @param array $skuList
  45. * @return array
  46. */
  47. public static function getLinePrices(array $skuList): array
  48. {
  49. $linePriceArr = helper::getArrayColumn($skuList, 'line_price');
  50. return empty($linePriceArr) ? [0, 0] : [min($linePriceArr), max($linePriceArr)];
  51. }
  52. /**
  53. * 生成skuList数据(写入goods_sku_id)
  54. * @param array $newSpecList
  55. * @param array $skuList
  56. * @return array
  57. */
  58. public static function getNewSkuList(array $newSpecList, array $skuList): array
  59. {
  60. foreach ($skuList as &$skuItem) {
  61. $skuItem['specValueIds'] = static::getSpecValueIds($newSpecList, $skuItem['skuKeys']);
  62. $skuItem['goodsProps'] = static::getGoodsProps($newSpecList, $skuItem['skuKeys']);
  63. $skuItem['goods_sku_id'] = implode('_', $skuItem['specValueIds']);
  64. }
  65. return $skuList;
  66. }
  67. /**
  68. * 根据$skuKeys生成规格值id集
  69. * @param array $newSpecList
  70. * @param array $skuKeys
  71. * @return array
  72. */
  73. private static function getSpecValueIds(array $newSpecList, array $skuKeys): array
  74. {
  75. $goodsSkuIdArr = [];
  76. foreach ($skuKeys as $skuKey) {
  77. $groupItem = helper::arraySearch($newSpecList, 'key', $skuKey['groupKey']);
  78. $specValueItem = helper::arraySearch($groupItem['valueList'], 'key', $skuKey['valueKey']);
  79. $goodsSkuIdArr[] = $specValueItem['spec_value_id'];
  80. }
  81. return $goodsSkuIdArr;
  82. }
  83. /**
  84. * 根据$skuKeys生成规格属性记录
  85. * @param array $newSpecList
  86. * @param array $skuKeys
  87. * @return array
  88. */
  89. private static function getGoodsProps(array $newSpecList, array $skuKeys): array
  90. {
  91. $goodsPropsArr = [];
  92. foreach ($skuKeys as $skuKey) {
  93. $groupItem = helper::arraySearch($newSpecList, 'key', $skuKey['groupKey']);
  94. $specValueItem = helper::arraySearch($groupItem['valueList'], 'key', $skuKey['valueKey']);
  95. $goodsPropsArr[] = [
  96. 'group' => ['name' => $groupItem['spec_name'], 'id' => $groupItem['spec_id']],
  97. 'value' => ['name' => $specValueItem['spec_value'], 'id' => $specValueItem['spec_value_id']]
  98. ];
  99. }
  100. return $goodsPropsArr;
  101. }
  102. /**
  103. * 新增商品sku记录
  104. * @param int $goodsId
  105. * @param array $newSkuList
  106. * @param int $specType
  107. * @return array|bool|false
  108. */
  109. public static function add(int $goodsId, int $specType = SpecTypeEnum::SINGLE, array $newSkuList = [])
  110. {
  111. // 单规格模式
  112. if ($specType === SpecTypeEnum::SINGLE) {
  113. return (new static)->save(array_merge($newSkuList, [
  114. 'goods_id' => $goodsId,
  115. 'goods_sku_id' => 0,
  116. 'store_id' => self::$storeId
  117. ]));
  118. } // 多规格模式
  119. elseif ($specType === SpecTypeEnum::MULTI) {
  120. // 批量写入商品sku记录
  121. return static::increasedFroMulti($goodsId, $newSkuList);
  122. }
  123. return false;
  124. }
  125. /**
  126. * 更新商品sku记录
  127. * @param int $goodsId
  128. * @param int $specType
  129. * @param array $skuList
  130. * @return array|bool|false
  131. */
  132. public static function edit(int $goodsId, int $specType = SpecTypeEnum::SINGLE, array $skuList = [])
  133. {
  134. // 删除所有的sku记录
  135. static::deleteAll(['goods_id' => $goodsId]);
  136. // 新增商品sku记录
  137. return static::add($goodsId, $specType, $skuList);
  138. }
  139. /**
  140. * 批量写入商品sku记录
  141. * @param int $goodsId
  142. * @param array $skuList
  143. * @return array|false
  144. */
  145. public static function increasedFroMulti(int $goodsId, array $skuList)
  146. {
  147. $dataset = [];
  148. foreach ($skuList as $skuItem) {
  149. $dataset[] = array_merge($skuItem, [
  150. 'goods_sku_id' => $skuItem['goods_sku_id'],
  151. 'line_price' => $skuItem['line_price'] ?: 0.00,
  152. 'goods_sku_no' => $skuItem['goods_sku_no'] ?: '',
  153. 'stock_num' => $skuItem['stock_num'] ?: 0,
  154. 'goods_weight' => $skuItem['goods_weight'] ?: 0,
  155. 'goods_props' => $skuItem['goodsProps'],
  156. 'spec_value_ids' => $skuItem['specValueIds'],
  157. 'goods_id' => $goodsId,
  158. 'store_id' => self::$storeId
  159. ]);
  160. }
  161. return (new static)->addAll($dataset);
  162. }
  163. }