GoodsPackage.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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\GoodsPackage as GoodsPackageModel;
  15. use app\common\enum\goods\SpecType as SpecTypeEnum;
  16. /**
  17. * 商品Package模型
  18. * Class GoodsSku
  19. * @package app\store\model
  20. */
  21. class GoodsPackage extends GoodsPackageModel
  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. $dataset = [];
  131. foreach ($newSkuList as $skuItem) {
  132. $dataset[] = array_merge($skuItem, [
  133. 'goods_id' => $goodsId,
  134. 'store_id' => self::$storeId,
  135. 'rel_goods_id' => $skuItem['rel_goods_id'],
  136. 'rel_goods_sku_id' => $skuItem['rel_goods_sku_id']
  137. ]);
  138. }
  139. return (new static)->addAll($dataset);
  140. } // 多规格模式
  141. elseif ($specType === SpecTypeEnum::MULTI) {
  142. // 批量写入商品sku记录
  143. // return static::increasedFroMulti($goodsId, $newSkuList);
  144. }
  145. return false;
  146. }
  147. /**
  148. * 更新商品sku记录
  149. * @param int $goodsId
  150. * @param int $specType
  151. * @param array $skuList
  152. * @return array|bool|false
  153. */
  154. public static function edit(int $goodsId, int $specType = SpecTypeEnum::SINGLE, array $skuList = [])
  155. {
  156. // 调用批量更改门店商品sku
  157. // (new ShopGoods())->updateAllShopGoodsSku($goodsId, $skuList);
  158. // 删除所有的sku记录
  159. static::deleteAll(['goods_id' => $goodsId]);
  160. // 新增商品sku记录
  161. return static::add($goodsId, $specType, $skuList);
  162. }
  163. // /**
  164. // * 批量写入商品sku记录
  165. // * @param int $goodsId
  166. // * @param array $skuList
  167. // * @return array|false
  168. // */
  169. // public static function increasedFroMulti(int $goodsId, array $skuList)
  170. // {
  171. // $dataset = [];
  172. // foreach ($skuList as $skuItem) {
  173. // $dataset[] = array_merge($skuItem, [
  174. // 'goods_sku_id' => $skuItem['goods_sku_id'],
  175. // 'line_price' => $skuItem['line_price'] ?: 0.00,
  176. // 'goods_sku_no' => $skuItem['goods_sku_no'] ?: '',
  177. // 'stock_num' => $skuItem['stock_num'] ?: 0,
  178. // 'alarm_stock_num' => $skuItem['alarm_stock_num'] ?: 0,
  179. // 'goods_weight' => $skuItem['goods_weight'] ?: 0,
  180. // 'goods_gross_weight' => $skuItem['goods_gross_weight'] ?: 0,
  181. // 'goods_props' => $skuItem['goodsProps'],
  182. // 'spec_value_ids' => $skuItem['specValueIds'],
  183. // 'goods_id' => $goodsId,
  184. // 'store_id' => self::$storeId
  185. // ]);
  186. // }
  187. // return (new static)->addAll($dataset);
  188. // }
  189. // /**
  190. // * @param $prices
  191. // * @return bool
  192. // * @throws \Exception
  193. // */
  194. // public static function updateClearingPrice($prices){
  195. // if (!count($prices))return true;
  196. // $user = new self();
  197. // $user->saveAll($prices);
  198. // return true;
  199. // }
  200. // /**
  201. // * @param $prices
  202. // * @return bool
  203. // * @throws \Exception
  204. // */
  205. // public static function updatePlatformRate($prices){
  206. // if (!count($prices))return true;
  207. // $user = new self();
  208. // $user->saveAll($prices);
  209. // return true;
  210. // }
  211. public function goods(){
  212. return $this->hasOne(Goods::class,'goods_id','rel_goods_id')->field('goods_id,goods_name,goods_no');
  213. }
  214. /**
  215. * 关联商品图片表
  216. * @return \think\model\relation\HasMany
  217. */
  218. public function images()
  219. {
  220. return $this->hasMany('GoodsImage','goods_id','rel_goods_id')->order(['id']);
  221. }
  222. /**
  223. * 关联商品规格表
  224. * @return \think\model\relation\HasOne
  225. */
  226. public function skuList()
  227. {
  228. return $this->hasOne('GoodsSku','goods_sku_id','rel_goods_sku_id');
  229. //return $this->hasMany('GoodsSku','goods_sku_id','rel_goods_sku_id')->order(['id' => 'asc']);
  230. }
  231. }