0], [['cost_price', 'original_price', 'price'], 'number', 'max' => 9999999], [['sort', 'virtual_sales', 'freight_id'], 'default', 'value' => 100], [['confine_count', 'use_attr'], 'default', 'value' => 0], [['name', 'price', 'cover_pic', 'type', 'plugin_sign', 'banner_urls', 'model', 'cx_mch_id'], 'required'], ]; } public function attributeLabels() { return [ 'name' => '商品名称', 'subtitle' => '副标题', 'original_price' => '原价', 'cost_price' => '成本价', 'detail' => '商品详情,图文', 'cover_pic' => '商品缩略图', 'pic_urls' => '商品轮播图', 'video_url' => '商品视频', 'unit' => '单位', 'type' => '商品类型:0=实体商品 ', 'status' => '上架状态:0=下架,1=上架', 'price' => '售价', 'use_attr' => '是否使用规格:0=不使用,1=使用', 'attr' => '商品规格', 'goods_stock' => '商品库存', 'virtual_sales' => '已出售量', 'confine_count' => '购物数量限制,0=不限制', 'freight_id' => '运费模板ID', 'plugin_sign' => '商品标示用于区分商品属于什么模块', 'sort' => '排序', ]; } public function save() { if (!$this->validate()) { return $this->getModelError(); } $t = \Yii::$app->db->beginTransaction(); if ($this->model->isNewRecord) { $this->model->cx_mch_id = $this->cx_mch_id; $this->model->plugin_sign = $this->plugin_sign; } $this->model->price = $this->price; $this->model->goods_stock = empty($this->goods_stock) ? 9999 : $this->goods_stock; // 商品库存 $this->model->virtual_sales = $this->virtual_sales; $this->model->confine_count = $this->confine_count; $this->model->freight_id = $this->freight_id; $this->model->sort = $this->sort; $this->model->status = 1; $this->handleAttrGroups(); if ($this->attrGroups) { $this->model->attr_groups = json_encode($this->attrGroups, JSON_UNESCAPED_UNICODE); $this->use_attr = 1; $this->model->use_attr = $this->use_attr; // $this->model->attr_groups = 1; //商品规格组 用不到看表 0 不需要规格组 } $this->model->date = strtotime($this->date); //保存商品基础信息 $res = $this->saveGoodsHub(); //保存商品基础信息 $res1 = $this->saveGoodsCat(); $res2 = $this->saveGoodsAttr(); if ($res2['code'] != 0) { $t->rollBack(); return $res; } if ($res['code'] != 0 || $res1['code'] != 0) { $t->rollBack(); return $res; } if (!$this->model->save()) { $t->rollBack(); return $this->getModelError($this->model); } $t->commit(); return $this->apiReturnSuccess('保存成功'); } //扩展其他 protected function saveExtra() { return $this->apiReturnSuccess(); } private function saveGoodsHub() { $goods_hub_id = $this->model->isNewRecord ? 0 : $this->model->goods_hub_id; $goods_hub = GoodsHub::findOne(['id' => $goods_hub_id, 'is_delete' => 0, 'cx_mch_id' => $this->cx_mch_id]); if ($goods_hub == null) { $goods_hub = new GoodsHub(); $goods_hub->cx_mch_id = $this->cx_mch_id; } $goods_hub->name = $this->name; $goods_hub->subtitle = $this->subtitle; $goods_hub->original_price = $this->original_price ? $this->original_price : $this->price; $goods_hub->cost_price = $this->cost_price ? $this->cost_price : $this->price; $goods_hub->detail = $this->detail; $goods_hub->cover_pic = $this->cover_pic; $goods_hub->signing_head_img = $this->signing_head_img; $goods_hub->signing_foot_img = $this->signing_foot_img; $goods_hub->banner_urls = $this->banner_urls; $video_banner_urls = is_string($this->video_banner_urls) ? $this->video_banner_urls : json_encode($this->video_banner_urls); $goods_hub->video_banner_urls = $video_banner_urls; $pic_urls = is_string($this->pic_urls) ? $this->pic_urls : json_encode($this->pic_urls); $goods_hub->pic_urls = $pic_urls; $video_urls = is_string($this->video_url) ? $this->video_url : json_encode($this->video_url); $goods_hub->video_url = $video_urls; $goods_hub->unit = $this->unit; $goods_hub->type = $this->type; if (!$goods_hub->save()) { return $this->getModelError($goods_hub); } $this->model->goods_hub_id = $goods_hub->id; return $this->apiReturnSuccess(); } //保存商品规格 private function saveGoodsAttr() { if ((int)$this->use_attr === 0) { $this->model->use_attr = 0; // 未使用规格就添加默认规格 $res = $this->setDefaultAttr(); if ($res['code'] != 0) return $res; $attrPicList = []; } else { $this->handleAttr(); // 多规格数据处理 $this->newAttrs = $this->attr; $attrPicList = array_column($this->attrGroups[0]['attr_list'], 'cover_pic', 'attr_id'); } $oldAttr = GoodsAttr::find() ->where(['is_delete' => 0, 'goods_id' => $this->model->id]) ->select('id') ->asArray() ->all(); // 是否为新增 if (!$this->model->isNewRecord) { GoodsAttr::updateAll(['is_delete' => 1,], ['goods_id' => $this->model->id, 'is_delete' => 0]); } // 旧规格ID $oldAttrIds = []; $newAttrIds = []; foreach ($oldAttr as $oldItem) { $oldAttrIds[] = $oldItem['id']; } $goodsStock = 0; foreach ($this->newAttrs as $newAttr) { $goodsStock += $newAttr['stock']; // 记录规格ID数组 $signIds = ''; foreach ($newAttr['attr_list'] as $aLItem) { $signIds .= $signIds ? ':' . (int)$aLItem['attr_id'] : (int)$aLItem['attr_id']; } // TODO 待修改 // 判断规格是需要新增还是更新 $goodsAttr = null; if ($this->model->id) { $goodsAttr = GoodsAttr::findOne([ 'id' => isset($newAttr['id']) ? $newAttr['id'] : 0, 'goods_id' => $this->model->id ]); if ($goodsAttr == null && count($this->newAttrs) == 1) { $goodsAttr = GoodsAttr::findOne([ 'sign_id' => $signIds, 'goods_id' => $this->model->id ]); } } if ($goodsAttr) { $goodsAttr->is_delete = 0; } else { $goodsAttr = new GoodsAttr(); } if ($newAttr['stock'] > 9999999) { return $this->apiReturnError('商品总库存不能大于9999999'); } $goodsAttr->goods_id = $this->model->id; $goodsAttr->sign_id = $signIds; $goodsAttr->stock = 0; $goodsAttr->price = $newAttr['price']; $goodsAttr->serial_no = $newAttr['serial_no']; $goodsAttr->weight = $newAttr['weight'] ?: 0; $key = strstr($signIds, ':', true) ?: $signIds; $goodsAttr->cover_pic = !empty($newAttr['cover_pic']) ? $newAttr['cover_pic'] : $this->cover_pic; $res = $goodsAttr->save(); if (!$res) { return $this->getModelError($goodsAttr); } $newAttrIds[] = $goodsAttr->id; } if ($goodsStock > 9999999) { return $this->apiReturnError('商品总库存不能大于9999999'); } $this->model->goods_stock = 0; $res = $this->model->save(); if (!$res) { return $this->getModelError($this->model); } return $this->apiReturnSuccess(); } private function handleAttrGroups() { $this->attrGroups = $this->addAttrGroupsId($this->attrGroups); } private function handleAttr() { foreach ($this->attr as &$item) { foreach ($item['attr_list'] as &$alItem) { $alItem['attr_group_id'] = $this->newAttrGroupList[$alItem['attr_group_name']]; $alItem['attr_id'] = $this->newAttrList[$alItem['attr_name']]; } unset($alItem); } unset($item); } private $newAttrGroupList = []; private $newAttrList = []; private $signArr = []; private function addAttrGroupsId($list, &$id = 1) { $newId = 1; foreach ($list as $key => $item) { if (isset($item['attr_list'])) { $this->newAttrGroupList[$item['attr_group_name']] = $newId; $list[$key]['attr_group_id'] = $newId++; $newItemList = $this->addAttrGroupsId($item['attr_list'], $id); $list[$key]['attr_list'] = $newItemList; } else { if (isset($this->signArr[$item['attr_name']])) { $this->newAttrList[$item['attr_name']] = $this->signArr[$item['attr_name']]; $list[$key]['attr_id'] = $this->signArr[$item['attr_name']]; } else { $this->signArr[$item['attr_name']] = $id; $this->newAttrList[$item['attr_name']] = $id; $list[$key]['attr_id'] = $id++; } } } return $list; } /** * 添加默认规格 */ private function setDefaultAttr() { $attrList = [ [ 'attr_group_name' => '规格', 'attr_group_id' => 1, 'attr_id' => 1, 'attr_name' => '默认', ] ]; $count = 1; $attrGroups = []; foreach ($attrList as &$item) { $item['attr_group_id'] = $count; $count++; $item['attr_id'] = $count; $count++; $newItem = [ 'attr_group_id' => $item['attr_group_id'], 'attr_group_name' => $item['attr_group_name'], 'attr_list' => [ [ 'attr_id' => $item['attr_id'], 'attr_name' => $item['attr_name'] ] ] ]; $attrGroups[] = $newItem; } unset($item); // 未使用规格 就添加一条默认规格 $newAttrs = [ [ 'attr_list' => $attrList, 'stock' => $this->goods_stock, 'price' => $this->price, 'serial_no' => $this->goods_no ? $this->goods_no : '', 'weight' => $this->goods_weight ? $this->goods_weight : 0, 'cover_pic' => $this->cover_pic, ] ]; // 未使用规格情况下,要把上一次的规格ID 存回去,不然规格记录会重复添加 if (count($this->attr) === 1 && isset($this->attr[0]['id'])) { $newAttrs[0]['id'] = $this->attr[0]['id']; } $this->model->attr_groups = (new Serializer)->encode($attrGroups); $res = $this->model->save(); if (!$res) { return $this->getModelError($this->model); } $this->newAttrs = $newAttrs; return $this->apiReturnSuccess(); } //保存商品分类 protected function saveGoodsCat() { $type = Cat::TYPE_GOODS; $goods_hub_id = $this->model->goods_hub_id; $goods_cat = GoodsCat::findOne(['goods_hub_id' => $goods_hub_id, 'is_delete' => 0]); if ($goods_cat == null) { $goods_cat = new GoodsCat(); } $goods_cat->goods_hub_id = $goods_hub_id; $goods_cat->cat_id = $this->cat_id; $goods_cat->cat1_id = $this->cat1_id; $goods_cat->cat2_id = $this->cat2_id; $goods_cat->cat3_id = $this->cat3_id; $goods_cat->cat4_id = $this->cat4_id; $goods_cat->is_delete = 0; $res = $goods_cat->save(); if (!$res) { return $this->getModelError($goods_cat); } return $this->apiReturnSuccess(); } }