GoodsSku.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2025 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\common\model;
  13. use cores\BaseModel;
  14. use app\common\library\helper;
  15. use think\model\relation\HasOne;
  16. use app\common\enum\goods\SpecType as SpecTypeEnum;
  17. /**
  18. * 商品SKU模型
  19. * Class GoodsSku
  20. * @package app\common\model
  21. */
  22. class GoodsSku extends BaseModel
  23. {
  24. // 定义表名
  25. protected $name = 'goods_sku';
  26. // 定义主键
  27. protected $pk = 'id';
  28. /**
  29. * 关联模型:规格图片
  30. * @return HasOne
  31. */
  32. public function image(): HasOne
  33. {
  34. return $this->hasOne('UploadFile', 'file_id', 'image_id');
  35. }
  36. /**
  37. * 获取器:规格值ID集
  38. * @param $value
  39. * @return array|mixed
  40. */
  41. public function getSpecValueIdsAttr($value)
  42. {
  43. return helper::jsonDecode($value);
  44. }
  45. /**
  46. * 获取器:规格属性
  47. * @param $value
  48. * @return array|mixed
  49. */
  50. public function getGoodsPropsAttr($value)
  51. {
  52. return helper::jsonDecode($value);
  53. }
  54. /**
  55. * 设置器:规格值ID集
  56. * @param $value
  57. * @return string
  58. */
  59. public function setSpecValueIdsAttr($value): string
  60. {
  61. return helper::jsonEncode($value);
  62. }
  63. /**
  64. * 设置器:规格属性
  65. * @param $value
  66. * @return string
  67. */
  68. public function setGoodsPropsAttr($value): string
  69. {
  70. return helper::jsonEncode($value);
  71. }
  72. /**
  73. * 获取sku信息详情
  74. * @param int $goodsId
  75. * @param string $goodsSkuId
  76. * @return static|array|null
  77. */
  78. public static function detail(int $goodsId, string $goodsSkuId)
  79. {
  80. return static::get(['goods_id' => $goodsId, 'goods_sku_id' => $goodsSkuId], ['image']);
  81. }
  82. /**
  83. * 获取商品SKU列表
  84. * @param int $goodsId 商品ID
  85. * @param bool $withImage 是否携带sku封面图
  86. * @return \think\Collection
  87. * @throws \think\db\exception\DataNotFoundException
  88. * @throws \think\db\exception\DbException
  89. * @throws \think\db\exception\ModelNotFoundException
  90. */
  91. public static function getSkuList(int $goodsId, bool $withImage = false): \think\Collection
  92. {
  93. return (new static)->with($withImage ? ['image'] : [])
  94. ->where('goods_id', '=', $goodsId)
  95. ->order(['id' => 'asc'])
  96. ->select();
  97. }
  98. /**
  99. * 生成skuList数据(写入goods_sku_id)
  100. * @param array $newSpecList
  101. * @param array $skuList
  102. * @return array
  103. */
  104. public static function getNewSkuList(array $newSpecList, array $skuList): array
  105. {
  106. foreach ($skuList as &$skuItem) {
  107. $skuItem['specValueIds'] = static::getSpecValueIds($newSpecList, $skuItem['skuKeys']);
  108. $skuItem['goodsProps'] = static::getGoodsProps($newSpecList, $skuItem['skuKeys']);
  109. $skuItem['goods_sku_id'] = implode('_', $skuItem['specValueIds']);
  110. }
  111. return $skuList;
  112. }
  113. /**
  114. * 根据$skuKeys生成规格值id集
  115. * @param array $newSpecList
  116. * @param array $skuKeys
  117. * @return array
  118. */
  119. private static function getSpecValueIds(array $newSpecList, array $skuKeys): array
  120. {
  121. $goodsSkuIdArr = [];
  122. foreach ($skuKeys as $skuKey) {
  123. $groupItem = helper::arraySearch($newSpecList, 'key', $skuKey['groupKey']);
  124. $specValueItem = helper::arraySearch($groupItem['valueList'], 'key', $skuKey['valueKey']);
  125. $goodsSkuIdArr[] = $specValueItem['spec_value_id'];
  126. }
  127. return $goodsSkuIdArr;
  128. }
  129. /**
  130. * 根据$skuKeys生成规格属性记录
  131. * @param array $newSpecList
  132. * @param array $skuKeys
  133. * @return array
  134. */
  135. private static function getGoodsProps(array $newSpecList, array $skuKeys): array
  136. {
  137. $goodsPropsArr = [];
  138. foreach ($skuKeys as $skuKey) {
  139. $groupItem = helper::arraySearch($newSpecList, 'key', $skuKey['groupKey']);
  140. $specValueItem = helper::arraySearch($groupItem['valueList'], 'key', $skuKey['valueKey']);
  141. $goodsPropsArr[] = [
  142. 'group' => ['name' => $groupItem['spec_name'], 'id' => $groupItem['spec_id']],
  143. 'value' => ['name' => $specValueItem['spec_value'], 'id' => $specValueItem['spec_value_id']]
  144. ];
  145. }
  146. return $goodsPropsArr;
  147. }
  148. /**
  149. * 新增商品sku记录
  150. * @param int $goodsId
  151. * @param array $newSkuList
  152. * @param int $specType
  153. * @param int|null $storeId 商城ID
  154. * @return array|bool|false
  155. */
  156. public static function add(int $goodsId, int $specType = SpecTypeEnum::SINGLE, array $newSkuList = [], int $storeId = null)
  157. {
  158. // 单规格模式
  159. if ($specType === SpecTypeEnum::SINGLE) {
  160. return (new static)->save(array_merge($newSkuList, [
  161. 'goods_id' => $goodsId,
  162. 'goods_sku_id' => 0,
  163. 'store_id' => $storeId ?: self::$storeId
  164. ]));
  165. } // 多规格模式
  166. elseif ($specType === SpecTypeEnum::MULTI) {
  167. // 批量写入商品sku记录
  168. return static::increasedFroMulti($goodsId, $newSkuList, $storeId);
  169. }
  170. return false;
  171. }
  172. /**
  173. * 批量写入商品sku记录
  174. * @param int $goodsId
  175. * @param array $skuList
  176. * @param int|null $storeId 商城ID
  177. * @return array|false
  178. */
  179. private static function increasedFroMulti(int $goodsId, array $skuList, int $storeId = null)
  180. {
  181. $dataset = [];
  182. foreach ($skuList as $skuItem) {
  183. $dataset[] = array_merge($skuItem, [
  184. 'id' => null, // 此处的id必须是数据库自增
  185. 'goods_sku_id' => $skuItem['goods_sku_id'],
  186. 'goods_price' => $skuItem['goods_price'] ?: 0.01,
  187. 'line_price' => $skuItem['line_price'] ?: 0.00,
  188. 'goods_sku_no' => $skuItem['goods_sku_no'] ?: '',
  189. 'stock_num' => $skuItem['stock_num'] ?: 0,
  190. 'goods_weight' => $skuItem['goods_weight'] ?: 0,
  191. 'goods_props' => $skuItem['goodsProps'],
  192. 'spec_value_ids' => $skuItem['specValueIds'],
  193. 'goods_id' => $goodsId,
  194. 'store_id' => $storeId ?: self::$storeId
  195. ]);
  196. }
  197. return (new static)->addAll($dataset);
  198. }
  199. /**
  200. * 获取库存总数量 (根据sku列表数据)
  201. * @param array $skuList
  202. * @return float|int
  203. */
  204. public static function getStockTotal(array $skuList)
  205. {
  206. return (int)helper::getArrayColumnSum($skuList, 'stock_num');
  207. }
  208. /**
  209. * 获取商品价格高低区间 (根据sku列表数据)
  210. * @param array $skuList
  211. * @return array
  212. */
  213. public static function getGoodsPrices(array $skuList): array
  214. {
  215. $goodsPriceArr = helper::getArrayColumn($skuList, 'goods_price');
  216. return empty($goodsPriceArr) ? [0, 0] : [min($goodsPriceArr), max($goodsPriceArr)];
  217. }
  218. /**
  219. * 获取划线价格高低区间 (根据sku列表数据)
  220. * @param array $skuList
  221. * @return array
  222. */
  223. public static function getLinePrices(array $skuList): array
  224. {
  225. $linePriceArr = helper::getArrayColumn($skuList, 'line_price');
  226. return empty($linePriceArr) ? [0, 0] : [min($linePriceArr), max($linePriceArr)];
  227. }
  228. }