Parcourir la source

Merge branch 'zhongqi-addGoods'

shish il y a 1 an
Parent
commit
a5bf9c5782

+ 26 - 7
app-hd/controllers/CategoryController.php

@@ -118,15 +118,34 @@ class CategoryController extends BaseController
         util::success($data);
     }
 
-    //分类排序 ssh 2020.3.11
+    //分类排序
     public function actionSort()
     {
-        $id = Yii::$app->request->get('id', 0);
-        $inTurn = Yii::$app->request->get('inTurn', 0);
-        $category = CategoryService::getById($id);
-        CategoryService::valid($category, $this->mainId);
-        CategoryService::updateById($id, ['inTurn' => $inTurn]);
-        util::complete();
+        $post = Yii::$app->request->post();
+        $items = $post['items'];
+
+        //验证商品
+        // 提取所有商品ID
+        $categoryIds = array_map(function($item) {
+            return intval($item['id']);
+        }, $items);
+
+        // 批量获取商品信息
+        $goodsInfos = CategoryClass::getByIds($categoryIds);
+
+        // 批量验证商品
+        foreach($goodsInfos as $info) {
+            CategoryService::valid($info, $this->mainId);
+        }
+
+        // 更新商品排序
+        foreach($items as $item) {
+            $id = intval($item['id']);
+            $inTurn = intval($item['inTurn']);
+            CategoryClass::updateById($id, ['inTurn' => $inTurn]);
+        }
+
+        util::complete('排序成功');
     }
 
     //获取分类 ssh 2019.12.2

+ 68 - 35
app-hd/controllers/GoodsController.php

@@ -4,11 +4,11 @@ namespace hd\controllers;
 
 use biz\shop\classes\ShopExtClass;
 use bizGhs\order\classes\OrderItemClass;
+use bizHd\goods\classes\GoodsCategoryClass;
 use bizHd\goods\classes\GoodsClass;
 use bizHd\goods\services\CategoryService;
 use bizHd\goods\services\GoodsCategoryService;
 use bizHd\goods\services\GoodsSettingService;
-use bizHd\saas\services\FestRiseService;
 use common\components\printUtil;
 use Yii;
 use common\components\util;
@@ -57,7 +57,14 @@ class GoodsController extends BaseController
     //添加商品 ssh 2019.12.7
     public function actionAdd()
     {
-        $data = Yii::$app->request->post();
+        $form = new \hd\models\goods\AddForm();
+        $form->load(Yii::$app->request->post(), '');
+        if (!$form->validateForm()) {
+            return;
+        }
+        
+        //$data = Yii::$app->request->post();
+        $data = $form->attributes;
         $data['sjId'] = $this->sjId;
         $data['mainId'] = $this->mainId;
         $data['shopId'] = $this->shopId ?? 0;
@@ -106,11 +113,12 @@ class GoodsController extends BaseController
     {
         $get = Yii::$app->request->get();
         $status = isset($get['status']) && is_numeric($get['status']) ? $get['status'] : -1;
-        $cId = isset($get['cId']) && !empty($get['cId']) ? $get['cId'] : 0;
+        $cId = isset($get['cId']) ? intval($get['cId']) : 0;
         $name = isset($get['name']) && !empty($get['name']) ? $get['name'] : '';
         $flowerNum = isset($get['flowerNum']) && $get['flowerNum'] > 0 ? $get['flowerNum'] : 0;
         $flower = isset($get['flower']) ? $get['flower'] : '';
         $hasStock = isset($get['hasStock']) ? $get['hasStock'] : 0;
+
         $where = [];
         if ($status != -1) {
             $where['status'] = $status;
@@ -156,7 +164,7 @@ class GoodsController extends BaseController
     //获取商品详情 ssh 2019.12.7
     public function actionDetail()
     {
-        $id = Yii::$app->request->get('id');
+        $id = intval(Yii::$app->request->get('id'));
         $goods = GoodsClass::getGoodsInfo($id);
         GoodsClass::valid($goods, $this->mainId);
         util::success($goods);
@@ -165,9 +173,15 @@ class GoodsController extends BaseController
     //更新商品 ssh 2019.12.7
     public function actionUpdate()
     {
-        $data = Yii::$app->request->post();
-        $id = isset($data['id']) ? $data['id'] : 0;
-        $info = GoodsService::getById($id);
+        $form = new \hd\models\goods\UpdateForm();
+        $form->load(Yii::$app->request->post(), '');
+        if (!$form->validateForm()) {
+            return;
+        }
+        
+        $data = $form->attributes;
+        $id = $data['id'];
+        $info = GoodsClass::getById($id);
         GoodsService::valid($info, $this->mainId);
         $data['sjId'] = $this->sjId;
         $data['mainId'] = $this->mainId;
@@ -187,30 +201,43 @@ class GoodsController extends BaseController
             $transaction->commit();
             util::complete();
         } catch (\Exception $e) {
+            // 记录错误日志
+            Yii::error('商品更新失败:' . $e->getMessage(), 'goods.update'); //. "\n" . $e->getTraceAsString()
             $transaction->rollBack();
             util::fail();
         }
     }
 
-    //商品排序 ssh 2020.3.11
+    //商品排序
     public function actionSort()
     {
-        $id = Yii::$app->request->get('id', 0);
-        $cId = Yii::$app->request->get('cId', 0);
-        $inTurn = Yii::$app->request->get('inTurn', 0);
+        $post = Yii::$app->request->post();
+        $items = $post['items'];
+        $cId = intval($post['cId']);
+
         //验证商品
-        $info = GoodsService::getById($id);
-        GoodsService::valid($info, $this->mainId);
-        if ($cId != 0) {
-            //验证分类
-            $category = CategoryService::getById($cId);
-            CategoryService::valid($category, $this->mainId);
-            GoodsCategoryService::updateByCondition(['cId' => $cId, 'gId' => $id], ['inTurn' => $inTurn]);
-        } else {
-            GoodsService::updateById($id, ['inTurn' => $inTurn]);
-            GoodsCategoryService::updateByCondition(['gId' => $id], ['inTurn' => $inTurn]);
+        // 提取所有商品ID
+        $categoryIds = array_map(function($item) {
+            return intval($item['id']);
+        }, $items);
+
+        // 批量获取商品信息
+        $goodsInfos = GoodsClass::getByIds($categoryIds);
+        // 批量验证商品
+        foreach($goodsInfos as $info) {
+            GoodsService::valid($info, $this->mainId);
+        }
+
+        $category = CategoryService::getById($cId);
+        CategoryService::valid($category, $this->mainId);
+        // 更新商品排序
+        foreach($items as $item) {
+            $id = intval($item['id']);
+            $inTurn = intval($item['inTurn']);
+            GoodsCategoryClass::updateByCondition(['cId' => $cId, 'gId' => $id], ['inTurn' => $inTurn]);
         }
-        util::complete('操作成功');
+
+        util::complete('排序完成');
     }
 
     //删除商品 ssh 2019.12.7
@@ -261,21 +288,27 @@ class GoodsController extends BaseController
     //更新涨价 ssh 2019.12.9
     public function actionUpdateRise()
     {
-        $post = Yii::$app->request->post();
+        $form = new \hd\models\goods\UpdateRiseForm();
+        $form->load(Yii::$app->request->post(), '');
+        if (!$form->validateForm()) {
+            return;
+        }
+        
+        $post = $form->attributes;
         $festIdList = isset($post['festIdList']) && !empty($post['festIdList']) ? $post['festIdList'] : [];
         unset($post['festIdList']);
-        $riseSwitch = isset($post['riseSwitch']) && is_numeric($post['riseSwitch']) ? $post['riseSwitch'] : 1;
-        if ($riseSwitch == 1) {
-            if (empty($festIdList)) {
-                util::fail('请选择节日');
-            }
-            FestRiseService::deleteByCondition(['mainId' => $this->mainId]);
-            $add = [];
-            foreach ($festIdList as $festId) {
-                $add[] = ['mainId' => $this->mainId, 'festId' => $festId];
-            }
-            FestRiseService::batchAdd($add);
-        }
+        // $riseSwitch = isset($post['riseSwitch']) && is_numeric($post['riseSwitch']) ? $post['riseSwitch'] : 1;
+        // if ($riseSwitch == 1) {
+        //     if (empty($festIdList)) {
+        //         util::fail('请选择节日');
+        //     }
+        //     FestRiseService::deleteByCondition(['mainId' => $this->mainId]);
+        //     $add = [];
+        //     foreach ($festIdList as $festId) {
+        //         $add[] = ['mainId' => $this->mainId, 'festId' => $festId];
+        //     }
+        //     FestRiseService::batchAdd($add);
+        // }
         GoodsSettingService::updateByCondition(['mainId' => $this->mainId], $post);
         util::complete('提交成功');
     }

+ 1 - 1
app-hd/controllers/OrderController.php

@@ -512,7 +512,7 @@ class OrderController extends BaseController
         $post['hdId'] = $hdId;
         $post['hdName'] = $hdName;
 
-        $post['customId'] = $customId;
+        //$post['customId'] = $customId;
         $customName = $custom->name ?? '';
         $post['customName'] = $customName;
         $post['customNamePy'] = stringUtil::py($customName);

+ 0 - 1
app-hd/models/.gitkeep

@@ -1 +0,0 @@
-*

+ 27 - 0
app-hd/models/BaseForm.php

@@ -0,0 +1,27 @@
+<?php
+namespace hd\models;
+
+use yii\base\Model;
+use common\components\util;
+
+/**
+ * 管理员表单基类
+ */
+class BaseForm extends Model
+{
+    /**
+     * 验证表单数据并返回结果
+     *
+     * @return bool 验证是否通过
+     */
+    public function validateForm()
+    {
+        if (!$this->validate()) {
+            $errors = $this->getFirstErrors();
+            $message = reset($errors);
+            util::fail($message);
+            return false;
+        }
+        return true;
+    }
+}

+ 171 - 0
app-hd/models/goods/AddForm.php

@@ -0,0 +1,171 @@
+<?php
+
+namespace hd\models\goods;
+
+use hd\models\BaseForm;
+
+/**
+ * 商品添加表单模型
+ */
+class AddForm extends BaseForm
+{
+    /**
+     * 商品名称
+     * @var string
+     */
+    public $name;
+
+    /**
+     * 商品单价
+     * @var float
+     */
+    public $price;
+
+    /**
+     * 价格类型
+     * @var int
+     */
+    public $priceType;
+
+    /**
+     * 历史销量
+     * @var int
+     */
+    public $sold;
+
+    /**
+     * 花朵数量
+     * @var int
+     */
+    public $flowerNum;
+
+    /**
+     * 商品图片
+     * @var array
+     */
+    public $shopImg;
+
+    /**
+     * 商品封面图
+     * @var string
+     */
+    public $cover;
+
+    /**
+     * 商品库存
+     * @var int
+     */
+    public $stock;
+
+    /**
+     * 商品分类ID
+     * @var array
+     */
+    public $catIds;
+
+    /**
+     * 库存状态
+     * @var int
+     */
+    public $stockSet;
+
+    /**
+     * 商品状态
+     * @var int
+     */
+    public $status;
+
+    /**
+     * 是否需要发货
+     * @var int
+     */
+    public $needSend;
+
+    /**
+     * 运费类型
+     * @var int
+     */
+    public $freightType;
+
+    /**
+     * 自动涨价
+     * @var int
+     */
+    public $autoRise;
+
+
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            // 必填字段验证
+            [['name'], 'required', 'message' => '请填写商品名称'],
+            [['priceType'], 'required', 'message' => '请选择价格类型'],
+            [['catIds'], 'required', 'message' => '请选择商品分类'],
+
+            // 默认值设置
+            ['price', 'default', 'value' => 0],
+            ['flowerNum', 'default', 'value' => 0],
+            ['sold', 'default', 'value' => 0],
+            ['catIds', 'default', 'value' => []],
+            
+            // 数据类型验证
+            [['name', 'cover'], 'string'],
+            [['flowerNum', 'stock'], 'integer'],
+            ['priceType', 'in', 'range' => [0, 1]],
+            ['price', 'number', 'min' => 0, 'message' => '商品价格必须大于或等于0'],
+            ['flowerNum', 'number', 'min' => 0, 'message' => '花朵数量必须大于或等于0'],
+            ['sold', 'integer', 'min' => 0, 'message' => '已售数量必须大于或等于0'],
+            [['stockSet'], 'in', 'range' => [0, 1]],
+            [['needSend'], 'in', 'range' => [0, 1]],
+            [['freightType'], 'in', 'range' => [0, 1]],
+            [['status'], 'in', 'range' => [0, 1]],
+            [['autoRise'], 'in', 'range' => [0, 1]],
+
+            // 添加条件验证
+//            ['price', 'required', 'message' => '请填写商品价格', 'when' => function($model) {
+//                 return isset($_POST['priceType']) && $_POST['priceType'] == '1';
+//            }],
+            
+            // 数组类型验证
+            [['shopImg'], 'validateShopImg'],
+            
+            // 其他业务规则验证
+            [['flowerNum'], 'number', 'min' => 0, 'message' => '花朵数量必须大于或等于0'],
+        ];
+    }
+
+    /**
+     * 验证商品图片
+     * @param string $attribute 属性名
+     * @param array $params 参数
+     */
+    public function validateShopImg($attribute, $params)
+    {
+        if (!empty($this->$attribute) && !is_array($this->$attribute)) {
+            $this->addError($attribute, '商品图片格式不正确');
+        }
+    }
+
+    /**
+     * 获取属性标签
+     * @return array
+     */
+    public function attributeLabels()
+    {
+        return [
+            'name' => '商品名称',
+            'price' => '商品价格',
+            'kindId' => '商品种类ID',
+            'kindName' => '商品种类名称',
+            'flowerNum' => '花朵数量',
+            'shopImg' => '商品图片',
+            'cover' => '商品封面图',
+            'stock' => '商品库存',
+            'spu' => '商品编码',
+            'autoRise' => '自动涨价',
+        ];
+    }
+} 

+ 199 - 0
app-hd/models/goods/UpdateForm.php

@@ -0,0 +1,199 @@
+<?php
+
+namespace hd\models\goods;
+
+use hd\models\BaseForm;
+
+/**
+ * 商品更新表单模型
+ */
+class UpdateForm extends BaseForm
+{
+    /**
+     * 商品ID
+     * @var int
+     */
+    public $id;
+
+    /**
+     * 商品名称
+     * @var string
+     */
+    public $name;
+
+    /**
+     * 商品价格
+     * @var float
+     */
+    public $price;
+
+    /**
+     * 价格类型
+     * @var int
+     */
+    public $priceType;
+
+    /**
+     * 已售数量
+     * @var int
+     */
+    public $sold;
+
+    /**
+     * 花朵数量
+     * @var int
+     */
+    public $flowerNum;
+
+    /**
+     * 自动涨价
+     * @var int
+     */
+    public $autoRise;
+
+    /**
+     * 商品图片
+     * @var array
+     */
+    public $shopImg;
+
+    /**
+     * 商品封面图
+     * @var string
+     */
+    public $cover;
+
+    /**
+     * 库存状态
+     * @var int
+     */
+    public $stockSet;
+
+    /**
+     * 商品库存
+     * @var int
+     */
+    public $stock;
+
+    /**
+     * 商品分类ID列表
+     * @var array
+     */
+    public $categoryIdList;
+
+    /**
+     * 商品状态
+     * @var int
+     */
+    public $status;
+
+    /**
+     * 删除状态
+     * @var int
+     */
+    public $delStatus;
+
+    /**
+     * 商品子项数量
+     * @var int
+     */
+    public $setItemNum;
+
+    /**
+     * 是否需要发货
+     * @var int
+     */
+    public $needSend;
+
+    /**
+     * 运费类型
+     * @var int
+     */
+    public $freightType;
+
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            // 必填字段验证
+            [['id'], 'required', 'message' => '商品ID不能为空'],
+            [['name'], 'required', 'message' => '请填写商品名称'],
+            [['categoryIdList'], 'required', 'message' => '请选择商品分类'],
+
+            // 数据类型验证
+            [['id', 'status', 'delStatus', 'setItemNum'], 'integer'],
+            [['name', 'cover'], 'string'],
+            [['price'], 'number', 'min' => 0, 'message' => '商品价格必须大于或等于0'],
+            [['sold'], 'integer', 'min' => 0, 'message' => '已售数量必须大于或等于0'],
+            [['flowerNum'], 'integer', 'min' => 0, 'message' => '花朵数量必须大于或等于0'],
+            [['autoRise'], 'in', 'range' => [0, 1]],
+            [['stockSet'], 'in', 'range' => [0, 1]],
+            [['stock'], 'integer', 'min' => 0, 'message' => '库存不能小于0'],
+            [['priceType'], 'in', 'range' => [0, 1]],
+            [['delStatus'], 'in', 'range' => [0, 1]],
+            [['needSend'], 'in', 'range' => [0, 1]],
+            [['freightType'], 'in', 'range' => [0, 1]],
+            [['status'], 'in', 'range' => [0, 1]],
+
+            // 数组类型验证
+            [['shopImg'], 'validateShopImg'],
+            [['categoryIdList'], 'validateCategoryIdList'],
+
+            // 默认值设置
+            ['flowerNum', 'default', 'value' => 0],
+            ['status', 'default', 'value' => 1],
+            ['delStatus', 'default', 'value' => 0],
+            ['categoryIdList', 'default', 'value' => []],
+            ['stock', 'default', 'value' => 0],
+        ];
+    }
+
+    /**
+     * 验证商品图片
+     * @param string $attribute 属性名
+     * @param array $params 参数
+     */
+    public function validateShopImg($attribute, $params)
+    {
+        if (!empty($this->$attribute) && !is_array($this->$attribute)) {
+            $this->addError($attribute, '商品图片格式不正确');
+        }
+    }
+
+    /**
+     * 验证分类ID列表
+     * @param string $attribute 属性名
+     * @param array $params 参数
+     */
+    public function validateCategoryIdList($attribute, $params)
+    {
+        if (!empty($this->$attribute) && !is_array($this->$attribute)) {
+            $this->addError($attribute, '商品分类格式不正确');
+        }
+    }
+
+    /**
+     * 获取属性标签
+     * @return array
+     */
+    public function attributeLabels()
+    {
+        return [
+            'id' => '商品ID',
+            'name' => '商品名称',
+            'price' => '商品价格',
+            'priceType' => '价格类型',
+            'flowerNum' => '花朵数量',
+            'shopImg' => '商品图片',
+            'cover' => '商品封面图',
+            'stock' => '商品库存',
+            'categoryIdList' => '商品分类',
+            'status' => '商品状态',
+            'delStatus' => '删除状态',
+            'setItemNum' => '商品子项数量',
+            'itemList' => '商品子项列表',
+        ];
+    }
+}

+ 99 - 0
app-hd/models/goods/UpdateRiseForm.php

@@ -0,0 +1,99 @@
+<?php
+
+namespace hd\models\goods;
+
+use hd\models\BaseForm;
+
+/**
+ * 商品涨价设置表单模型
+ */
+class UpdateRiseForm extends BaseForm
+{
+    /**
+     * 节日ID列表
+     * @var array
+     */
+    public $festIdList;
+
+    /**
+     * 涨价开关
+     * @var int
+     */
+    public $riseSwitch;
+
+    /**
+     * 涨价类型 (0=金额, 1=百分比)
+     * @var int
+     */
+    public $riseType;
+
+    /**
+     * 涨价金额或百分比
+     * @var float
+     */
+    public $riseAmount;
+
+    /**
+     * {@inheritdoc}
+     */
+    public function rules()
+    {
+        return [
+            // 默认值设置
+            ['festIdList', 'default', 'value' => []],
+            ['riseSwitch', 'default', 'value' => 1],
+            
+            // 数据类型验证
+            // ['festIdList', 'validateFestIdList'],
+            ['riseSwitch', 'in', 'range' => [0, 1]],
+            ['riseType', 'in', 'range' => [0, 1]],
+            ['riseAmount', 'number', 'min' => 0],
+            
+            // 条件验证 - 当涨价开关打开时,必须选择节日
+            // ['festIdList', 'required', 'when' => function($model) {
+            //     return $model->riseSwitch == 1;
+            // }, 'message' => '请选择节日'],
+            
+            // 条件验证 - 当涨价开关打开时,必须设置涨价金额
+            ['riseAmount', 'required', 'when' => function($model) {
+                return $model->riseSwitch == 1;
+            }, 'message' => '请设置涨价金额'],
+        ];
+    }
+
+    /**
+     * 验证节日ID列表
+     * @param string $attribute 属性名
+     * @param array $params 参数
+     */
+    public function validateFestIdList($attribute, $params)
+    {
+        if (!empty($this->$attribute)) {
+            if (!is_array($this->$attribute)) {
+                $this->addError($attribute, '节日列表格式不正确');
+                return;
+            }
+            
+            foreach ($this->$attribute as $festId) {
+                if (!is_numeric($festId) || $festId <= 0) {
+                    $this->addError($attribute, '节日ID必须为正整数');
+                    return;
+                }
+            }
+        }
+    }
+
+    /**
+     * 获取属性标签
+     * @return array
+     */
+    public function attributeLabels()
+    {
+        return [
+            'festIdList' => '节日列表',
+            'riseSwitch' => '涨价开关',
+            'riseType' => '涨价类型',
+            'riseAmount' => '涨价金额/百分比',
+        ];
+    }
+}

+ 86 - 30
biz-hd/goods/classes/GoodsClass.php

@@ -343,8 +343,11 @@ class GoodsClass extends BaseClass
     }
 
     //组装商品基本信息 ssh 2019.12.2
-    public static function groupGoodsBaseInfo($list, $getItem = false)
+    public static function groupGoodsBaseInfo($list, $getItem = false, $sortIds= [])
     {
+        $newList = [];
+        $arrNotEmpty = is_array($sortIds) && !empty($sortIds);
+        
         //注意这里涉及商家节日涨价的问题,不要去取不同商家的商品,确有必要另外起方法!!!!!!!!!!
         $currentData = current($list);
         $mainId = $currentData['mainId'] ?? 0;
@@ -385,6 +388,14 @@ class GoodsClass extends BaseClass
             }
             $val['belongCategory'] = $belongCategory;
             $shortCover = $val['cover'] ?? '';
+            // 兼容 cover 保存的是数字(表示第几张图片)
+            if (intval($shortCover) > 0) {
+                $shopImg = isset($val['shortImgList']) ? $val['shortImgList'] : [];
+                $idx = intval($shortCover) - 1;
+                if (isset($shopImg[$idx])) {
+                    $shortCover = $shopImg[$idx];
+                }
+            }
             $val['shortCover'] = $shortCover;
             $val['cover'] = imgUtil::groupImg($shortCover);
             $val['smallCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
@@ -403,8 +414,23 @@ class GoodsClass extends BaseClass
             }
             //前端使用字段
             $val['bigCount'] = 0;
-            $list[$key] = $val;
+            if ($arrNotEmpty) {
+                $sortIndex = array_search($id, $sortIds);
+                // $newList[$sortIndex] = $val;
+                $val['sortWeight'] = $sortIndex !== false ? $sortIndex : count($sortIds);
+            }
+
+            $list[$key] = $val; 
         }
+
+        if ($arrNotEmpty) {
+            usort($list, function($a, $b) {
+                return $a['sortWeight'] - $b['sortWeight'];
+            });
+            //return $newList;
+            // return array_values($newList);
+        }
+
         return $list;
     }
 
@@ -543,11 +569,11 @@ class GoodsClass extends BaseClass
         return $info;
     }
 
-    //更新商品 ssh 2019.12.7
-    public static function updateGoods($info, $data, $staff)
+    //更新商品
+    public static function updateGoods($goods, $data, $staff)
     {
-        $id = $info['id'] ?? 0;
-        $preSetItemNum = $info['setItemNum'] ?? 0;
+        $id = $goods['id'] ?? 0;
+        $preSetItemNum = $goods['setItemNum'] ?? 0;
 
         $categoryIdList = isset($data['categoryIdList']) && !empty($data['categoryIdList']) ? $data['categoryIdList'] : [];
         unset($data['categoryIdList']);
@@ -570,37 +596,67 @@ class GoodsClass extends BaseClass
         if (!empty($diff)) {
             util::fail('不能使用别人的分类');
         }
-        //先删除再添加分类
+        //精确处理分类变更:分别处理删除、新增、修改三种情况
         $goodsHasCategoryIds = GoodsCategoryService::getGoodsHasCategoryIds($id);
-        CategoryClass::counters(['goodsNum' => -1], ['id' => $goodsHasCategoryIds]);
-        GoodsCategoryClass::delGoodsCategory($id);
-        $addCategory = [];
-        foreach ($categoryIdList as $categoryId) {
-            $cName = $categoryData[$categoryId]['categoryName'];
-            $addCategory[] = [
-                'gId' => $id,
-                'name' => $name,
-                'py' => $py,
-                'cId' => $categoryId,
-                'cName' => $cName,
-                'sjId' => $sjId,
-                'flower' => $flower,
-                'mainId' => $mainId,
-                'status' => $status,
-                'delStatus' => $delStatus,
-                'setItemNum' => $setItemNum,
-                'flowerNum' => $flowerNum,
-            ];
+        
+        // 1. 找出需要删除的分类:原有分类中不在新分类列表中的
+        $toDeleteCategoryIds = array_diff($goodsHasCategoryIds, $categoryIdList);
+        if (!empty($toDeleteCategoryIds)) {
+            GoodsCategoryClass::deleteByCondition(['gId' => $id, 'cId' => ['in', $toDeleteCategoryIds]]);
+            CategoryClass::counters(['goodsNum' => -1], ['id' => $toDeleteCategoryIds]);
+        }
+        
+        // 2. 找出需要新增的分类:新分类列表中不在原有分类中的
+        $toAddCategoryIds = array_diff($categoryIdList, $goodsHasCategoryIds);
+        if (!empty($toAddCategoryIds)) {
+            $addCategory = [];
+            foreach ($toAddCategoryIds as $categoryId) {
+                $cName = $categoryData[$categoryId]['categoryName'];
+                $addCategory[] = [
+                    'gId' => $id,
+                    'name' => $name,
+                    'py' => $py,
+                    'cId' => $categoryId,
+                    'cName' => $cName,
+                    'sjId' => $sjId,
+                    'flower' => $flower,
+                    'mainId' => $mainId,
+                    'status' => $status,
+                    'setItemNum' => $setItemNum,
+                ];
+            }
+            GoodsCategoryClass::batchAdd($addCategory);
+            CategoryClass::counters(['goodsNum' => 1], ['id' => $toAddCategoryIds]);
+        }
+        
+        // 3. 处理需要修改的分类:既在原有分类中也在新分类列表中的
+        $toUpdateCategoryIds = array_intersect($goodsHasCategoryIds, $categoryIdList);
+        if (!empty($toUpdateCategoryIds)) {
+            foreach ($toUpdateCategoryIds as $categoryId) {
+                $cName = $categoryData[$categoryId]['categoryName'];
+                $updateData = [
+                    'name' => $name,
+                    'py' => $py,
+                    'cName' => $cName,
+                    'status' => $status,
+                    'setItemNum' => $setItemNum,
+                ];
+                GoodsCategoryClass::updateByCondition(['gId' => $id, 'cId' => $categoryId], $updateData);
+            }
+        }
+        
+        // 兼容 cover 参数是数字
+        if (intval($data['cover']) > 0) {
+            // 如果是大于0的整数,说明是图片序号
+        } else {
+            $data['cover'] = $data['shopImg'][0] ?? '';
         }
-        GoodsCategoryClass::batchAdd($addCategory);
-        CategoryClass::counters(['goodsNum' => 1], ['id' => $categoryIdList]);
-        $data['cover'] = $data['shopImg'][0] ?? '';
         $data['shopImg'] = json_encode($data['shopImg']);
         $data['py'] = $py;
 
         //库存可以修改
         if (isset($data['stock']) && is_numeric($data['stock'])) {
-            $preStock = $info['stock'] ?? 0;
+            $preStock = $goods['stock'] ?? 0;
             $newStock = $data['stock'];
             if ($newStock != $preStock) {
                 if ($staff->super != 1) {

+ 4 - 6
biz-hd/goods/services/GoodsCategoryService.php

@@ -5,8 +5,6 @@ namespace bizHd\goods\services;
 use bizHd\base\services\BaseService;
 use bizHd\goods\classes\GoodsCategoryClass;
 use bizHd\goods\classes\GoodsClass;
-use Yii;
-use linslin\yii2\curl;
 
 class GoodsCategoryService extends BaseService
 {
@@ -17,14 +15,14 @@ class GoodsCategoryService extends BaseService
     public static function getGoodsList($where)
     {
         unset($where['delStatus']);//这个后面改进
-        $data = GoodsCategoryClass::getList('*', $where, 'status DESC,inTurn DESC');
-        $list = isset($data['list']) && !empty($data['list']) ? $data['list'] : [];
+        $data = GoodsCategoryClass::getList('*', $where, 'inTurn DESC');
+        $list = isset($data['list']) ? $data['list'] : [];
         if (empty($list)) {
             return $data;
         }
         $ids = array_column($list, 'gId');
-        $list = GoodsService::getAllByCondition(['id' => ['in', $ids]], null, '*');
-        $data['list'] = GoodsClass::groupGoodsBaseInfo($list);
+        $goodsList = GoodsClass::getAllByCondition(['id' => ['in', $ids]], null, '*');
+        $data['list'] = GoodsClass::groupGoodsBaseInfo($goodsList, false, $ids);
         return $data;
     }
 

+ 13 - 14
biz-hd/goods/services/GoodsService.php

@@ -2,24 +2,20 @@
 
 namespace bizHd\goods\services;
 
-use bizHd\admin\services\AdminService;
 use bizHd\base\services\BaseService;
-
 use bizHd\goods\classes\GoodsCategoryClass;
 use bizHd\goods\classes\GoodsClass;
 use bizHd\goods\classes\GoodsItemClass;
 use bizHd\goods\models\GoodsCategory;
-use bizHd\goods\models\Goods;
 use common\components\util;
 use Yii;
-use linslin\yii2\curl;
 
 class GoodsService extends BaseService
 {
 
     public static $baseFile = '\bizHd\goods\classes\GoodsClass';
 
-    //后台列表 ssh 2019.12.7
+    //商品列表
     public static function getGoodsList($where)
     {
         $page = Yii::$app->request->get('page', 1);
@@ -127,32 +123,32 @@ class GoodsService extends BaseService
             ->joinWith(['goods' => function($query) use ($where) {
                 // 商品表的筛选条件
                 if (isset($where['mainId'])) {
-                    $query->andWhere(['goods.mainId' => $where['mainId']]);
+                    $query->andWhere(['xhGoods.mainId' => $where['mainId']]);
                 }
                 if (isset($where['delStatus'])) {
-                    $query->andWhere(['goods.delStatus' => $where['delStatus']]);
+                    $query->andWhere(['xhGoods.delStatus' => $where['delStatus']]);
                 }
                 if (isset($where['status'])) {
-                    $query->andWhere(['goods.status' => $where['status']]);
+                    $query->andWhere(['xhGoods.status' => $where['status']]);
                 }
                 if (isset($where['name'])) {
-                    $query->andWhere(['like', 'goods.name', $where['name']]);
+                    $query->andWhere(['like', 'xhGoods.name', $where['name']]);
                 }
                 // 如果需要筛选有库存的商品
                 if (isset($where['hasStock']) && $where['hasStock'] == 1) {
-                    $query->andWhere(['>', 'goods.stock', 0]);
+                    $query->andWhere(['>', 'xhGoods.stock', 0]);
                 }
             }]);
 
         // 分类表的筛选条件
         if (isset($where['cId'])) {
-            $query->andWhere(['goodsCategory.cId' => $where['cId']]);
+            $query->andWhere(['cId' => $where['cId']]);
         }
         if (isset($where['flower'])) {
-            $query->andWhere(['goodsCategory.flower' => $where['flower']]);
+            $query->andWhere(['xhGoodsCategory.flower' => $where['flower']]);
         }
         if (isset($where['flowerNum'])) {
-            $query->andWhere(['goodsCategory.flowerNum' => $where['flowerNum']]);
+            $query->andWhere(['xhGoodsCategory.flowerNum' => $where['flowerNum']]);
         }
 
         // 计算总数
@@ -163,7 +159,7 @@ class GoodsService extends BaseService
         $query->offset($offset)->limit($pageSize);
 
         // 排序
-        $query->orderBy(['goodsCategory.inTurn' => SORT_DESC]);
+        $query->orderBy(['xhGoodsCategory.inTurn' => SORT_DESC, 'xhGoods.sold' => SORT_DESC]);
 
         // 获取数据
         $list = $query->all();
@@ -181,11 +177,14 @@ class GoodsService extends BaseService
             }
         }
 
+        $totalPage = ceil($total / $pageSize);
+        $moreData = $totalPage > $page ? 1 : 0;//是否还有更多数据
         return [
             'list' => $goodsList,
             'total' => $total,
             'page' => $page,
             'pageSize' => $pageSize,
+            'moreData' => $moreData,
             'totalPage' => ceil($total / $pageSize)
         ];
     }