Просмотр исходного кода

添加商品时产品的管理

shish 2 лет назад
Родитель
Сommit
e5943a2db2
1 измененных файлов с 122 добавлено и 3 удалено
  1. 122 3
      biz-ghs/product/classes/ProductClass.php

+ 122 - 3
biz-ghs/product/classes/ProductClass.php

@@ -1656,7 +1656,9 @@ class ProductClass extends BaseClass
             }
             $upData['hasCpId'] = 1;
         } else {
-            util::fail('请选择产品');
+            if ($shop->itemToClass == 1) {
+                util::fail('请选择产品');
+            }
         }
         /***************产品模块*****************/
 
@@ -2155,7 +2157,53 @@ class ProductClass extends BaseClass
                 util::fail('零售价要大于批发价');
             }
             $data['property'] = 1;
-            $respond = self::addBaseData($data, true);
+            $respond = self::addBaseData($data);
+            $respondId = $respond->id ?? 0;
+
+            /*****************产品模块*********************/
+            $ptCpIds = [];
+            if (isset($data['cpInfo']) && !empty($data['cpInfo'])) {
+                $cpIds = array_column($data['cpInfo'], 'cpId');
+                $cpInfoList = CpClass::getByIds($cpIds, null, 'id');
+                $ptCpIds = array_column($cpInfoList, 'ptCpId');
+                foreach ($data['cpInfo'] as $cpKey => $cpVal) {
+                    $cpId = $cpVal['cpId'] ?? 0;
+                    if (isset($cpInfoList[$cpId]) == false) {
+                        util::fail('添加了无效产品');
+                    }
+                    $currentCp = $cpInfoList[$cpId];
+                    if (isset($currentCp['mainId']) == false || $currentCp['mainId'] != $mainId) {
+                        util::fail('不是你的产品哦');
+                    }
+                    $cpName = $currentCp['name'] ?? '';
+                    $ptCpId = $currentCp['ptCpId'] ?? 0;
+                    $cpPy = $currentCp['py'] ?? '';
+                    $name = $update['name'] ?? '';
+                    $py = stringUtil::py($name);
+                    $hasCp = CpItemClass::getByCondition(['cpId' => $cpId, 'productId' => $respondId], true);
+                    if (empty($hasCp)) {
+                        $addCp = [
+                            'cpId' => $cpId,
+                            'ptCpId' => $ptCpId,
+                            'ptItemId' => $ptItemId,
+                            'productId' => $respondId,
+                            'name' => $name,
+                            'py' => $py,
+                            'cpName' => $cpName,
+                            'cpPy' => $cpPy,
+                            'mainId' => $mainId,
+                        ];
+                        CpItemClass::add($addCp);
+                    }
+                }
+                $respond->hasCpId = 1;
+                $respond->save();
+            } else {
+                if ($currentShop->itemToClass == 1) {
+                    util::fail('请选择产品');
+                }
+            }
+            /*****************产品模块*********************/
 
             if ($stock > 0) {
                 //有库存数量,增加盘盈记录 2021.7.9 lqh
@@ -2256,7 +2304,78 @@ class ProductClass extends BaseClass
                     if (!empty($chainHas)) {
                         continue;
                     }
-                    self::addBaseData($data);
+                    $chainProduct = self::addBaseData($data);
+                    $chainProductId = $chainProduct->id ?? 0;
+
+                    /****************产品模块********************/
+                    if (!empty($ptCpIds)) {
+                        $ptCpClassList = PtCpClassClass::getAllByCondition(['delStatus' => 0], null, '*', 'id');
+                        $thisCpClassList = CpClassClass::getAllByCondition(['mainId' => $thisMainId, 'delStatus' => 0], null, '*', 'id');
+                        $thisCpDefaultClassId = 0;
+                        foreach ($thisCpClassList as $thisCpClass) {
+                            if ($thisCpClass['isDefault'] == 1) {
+                                $thisCpDefaultClassId = $thisCpClass['id'] ?? 0;
+                            }
+                        }
+                        if (empty($thisCpDefaultClassId)) {
+                            util::fail('没有找到产品的默认分类哦');
+                        }
+                        $ptCpList = PtCpClass::getAllByCondition(['id' => ['in', $ptCpIds]], null, '*', 'id');
+                        $thisCpList = CpClass::getAllByCondition(['mainId' => $thisMainId, 'ptCpId' => ['in', $ptCpIds]], null, '*', 'ptCpId', true);
+                        foreach ($ptCpList as $ptCpId => $ptCpInfo) {
+                            $ptCpClassId = $ptCpInfo['classId'] ?? 0;
+                            $ptCpClassName = '';
+                            if (isset($ptCpClassList[$ptCpClassId])) {
+                                $ptCpClassName = $ptCpClassList[$ptCpClassId]['name'] ?? '';
+                            }
+                            $currentCpClassId = $thisCpDefaultClassId;
+                            if (!empty($ptCpClassName)) {
+                                foreach ($thisCpClassList as $thisCpClass) {
+                                    if ($thisCpClass['name'] == $ptCpClassName) {
+                                        $currentCpClassId = $thisCpClass['id'] ?? 0;
+                                    }
+                                }
+                            }
+                            if (isset($thisCpList[$ptCpId])) {
+                                $currentCp = $thisCpList[$ptCpId];
+                            } else {
+                                unset($ptCpInfo['id']);
+                                unset($ptCpInfo['addTime']);
+                                unset($ptCpInfo['updateTime']);
+                                $ptCpInfo['mainId'] = $thisMainId;
+                                $ptCpInfo['classId'] = $currentCpClassId;
+                                $ptCpInfo['ptCpId'] = $ptCpId;
+                                $currentCp = CpClass::add($ptCpInfo, true);
+                            }
+                            $currentCpId = $currentCp->id ?? 0;
+                            $cpName = $currentCp->name ?? '';
+                            $ptCpId = $currentCp->ptCpId ?? 0;
+                            $cpPy = $currentCp->py ?? '';
+                            $name = $chainProduct->name ?? '';
+                            $py = stringUtil::py($name);
+                            $hasCp = CpItemClass::getByCondition(['cpId' => $currentCpId, 'productId' => $chainProductId], true);
+                            if (empty($hasCp)) {
+                                $addCp = [
+                                    'cpId' => $currentCpId,
+                                    'ptCpId' => $ptCpId,
+                                    'ptItemId' => $ptItemId,
+                                    'productId' => $chainProductId,
+                                    'name' => $name,
+                                    'py' => $py,
+                                    'cpName' => $cpName,
+                                    'cpPy' => $cpPy,
+                                    'mainId' => $mainId,
+                                ];
+                                CpItemClass::add($addCp);
+                            }
+                        }
+                    } else {
+                        if ($shop->itemToClass == 1) {
+                            util::fail('请选择产品哈');
+                        }
+                    }
+                    /****************产品模块********************/
+
                 }
             }
             $transaction->commit();