GoodsSku.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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. ->select();
  96. }
  97. /**
  98. * 生成skuList数据(写入goods_sku_id)
  99. * @param array $newSpecList
  100. * @param array $skuList
  101. * @return array
  102. */
  103. public static function getNewSkuList(array $newSpecList, array $skuList): array
  104. {
  105. foreach ($skuList as &$skuItem) {
  106. $skuItem['specValueIds'] = static::getSpecValueIds($newSpecList, $skuItem['skuKeys']);
  107. $skuItem['goodsProps'] = static::getGoodsProps($newSpecList, $skuItem['skuKeys']);
  108. $skuItem['goods_sku_id'] = implode('_', $skuItem['specValueIds']);
  109. }
  110. return $skuList;
  111. }
  112. /**
  113. * 根据$skuKeys生成规格值id集
  114. * @param array $newSpecList
  115. * @param array $skuKeys
  116. * @return array
  117. */
  118. private static function getSpecValueIds(array $newSpecList, array $skuKeys): array
  119. {
  120. $goodsSkuIdArr = [];
  121. foreach ($skuKeys as $skuKey) {
  122. $groupItem = helper::arraySearch($newSpecList, 'key', $skuKey['groupKey']);
  123. $specValueItem = helper::arraySearch($groupItem['valueList'], 'key', $skuKey['valueKey']);
  124. $goodsSkuIdArr[] = $specValueItem['spec_value_id'];
  125. }
  126. return $goodsSkuIdArr;
  127. }
  128. /**
  129. * 根据$skuKeys生成规格属性记录
  130. * @param array $newSpecList
  131. * @param array $skuKeys
  132. * @return array
  133. */
  134. private static function getGoodsProps(array $newSpecList, array $skuKeys): array
  135. {
  136. $goodsPropsArr = [];
  137. foreach ($skuKeys as $skuKey) {
  138. $groupItem = helper::arraySearch($newSpecList, 'key', $skuKey['groupKey']);
  139. $specValueItem = helper::arraySearch($groupItem['valueList'], 'key', $skuKey['valueKey']);
  140. $goodsPropsArr[] = [
  141. 'group' => ['name' => $groupItem['spec_name'], 'id' => $groupItem['spec_id']],
  142. 'value' => ['name' => $specValueItem['spec_value'], 'id' => $specValueItem['spec_value_id']]
  143. ];
  144. }
  145. return $goodsPropsArr;
  146. }
  147. /**
  148. * 新增商品sku记录
  149. * @param int $goodsId
  150. * @param array $newSkuList
  151. * @param int $specType
  152. * @param int|null $storeId 商城ID
  153. * @return array|bool|false
  154. */
  155. public static function add(int $goodsId, int $specType = SpecTypeEnum::SINGLE, array $newSkuList = [], int $storeId = null)
  156. {
  157. // 单规格模式
  158. if ($specType === SpecTypeEnum::SINGLE) {
  159. return (new static)->save(array_merge($newSkuList, [
  160. 'goods_id' => $goodsId,
  161. 'goods_sku_id' => 0,
  162. 'store_id' => $storeId ?: self::$storeId
  163. ]));
  164. } // 多规格模式
  165. elseif ($specType === SpecTypeEnum::MULTI) {
  166. // 批量写入商品sku记录
  167. return static::increasedFroMulti($goodsId, $newSkuList, $storeId);
  168. }
  169. return false;
  170. }
  171. /**
  172. * 批量写入商品sku记录
  173. * @param int $goodsId
  174. * @param array $skuList
  175. * @param int|null $storeId 商城ID
  176. * @return array|false
  177. */
  178. private static function increasedFroMulti(int $goodsId, array $skuList, int $storeId = null)
  179. {
  180. $dataset = [];
  181. foreach ($skuList as $skuItem) {
  182. $dataset[] = array_merge($skuItem, [
  183. 'id' => null, // 此处的id必须是数据库自增
  184. 'goods_sku_id' => $skuItem['goods_sku_id'],
  185. 'goods_price' => $skuItem['goods_price'] ?: 0.01,
  186. 'line_price' => $skuItem['line_price'] ?: 0.00,
  187. 'goods_sku_no' => $skuItem['goods_sku_no'] ?: '',
  188. 'stock_num' => $skuItem['stock_num'] ?: 0,
  189. 'goods_weight' => $skuItem['goods_weight'] ?: 0,
  190. 'goods_props' => $skuItem['goodsProps'],
  191. 'spec_value_ids' => $skuItem['specValueIds'],
  192. 'goods_id' => $goodsId,
  193. 'store_id' => $storeId ?: self::$storeId
  194. ]);
  195. }
  196. return (new static)->addAll($dataset);
  197. }
  198. /**
  199. * 获取库存总数量 (根据sku列表数据)
  200. * @param array $skuList
  201. * @return float|int
  202. */
  203. public static function getStockTotal(array $skuList)
  204. {
  205. return (int)helper::getArrayColumnSum($skuList, 'stock_num');
  206. }
  207. /**
  208. * 获取商品价格高低区间 (根据sku列表数据)
  209. * @param array $skuList
  210. * @return array
  211. */
  212. public static function getGoodsPrices(array $skuList): array
  213. {
  214. $goodsPriceArr = helper::getArrayColumn($skuList, 'goods_price');
  215. return empty($goodsPriceArr) ? [0, 0] : [min($goodsPriceArr), max($goodsPriceArr)];
  216. }
  217. /**
  218. * 获取划线价格高低区间 (根据sku列表数据)
  219. * @param array $skuList
  220. * @return array
  221. */
  222. public static function getLinePrices(array $skuList): array
  223. {
  224. $linePriceArr = helper::getArrayColumn($skuList, 'line_price');
  225. return empty($linePriceArr) ? [0, 0] : [min($linePriceArr), max($linePriceArr)];
  226. }
  227. }