addError($attr, "{$attr}数据格式错误。"); } }], [['goods_id'], 'required'], [['cat_id', 'goods_id', 'integral', 'goods_count', 'sales', 'sort', 'user_num', 'goods_num'], 'integer'], [['integral', 'goods_count'], 'integer', 'min' => 1], [['name'], 'string', 'max' => 250], [['name', 'detail'], 'trim'], [['detail'], 'string'] ]; } public function save() { if (!$this->validate()) { return ['code' => 1, 'msg' => $this->getErrorSummary(false)[0]]; } if (empty($this->detail)) { return [ 'code' => 1, 'msg' => '请填写商品详情' ]; } if (empty($this->goods_id)) { return [ 'code' => 1, 'msg' => '请选择商品' ]; } if (empty($this->attr)) { return [ 'code' => 1, 'msg' => '商品规格数据为空' ]; } if ($this->goods_count > $this->goods_num) { return [ 'code' => 1, 'msg' => '积分商品规格库存不能大于本商品规格库存' ]; } if (intval($this->integral) < 1) { return [ 'code' => 1, 'msg' => '设置积分非法' ]; } // 检测是否有重复商品 $exist_goods = SaasIntegralGoods::find()->where(['store_id' => $this->store_id, 'goods_id' => $this->goods_id, 'is_delete' => 0])->asArray()->all(); if (!empty($exist_goods) && is_array($exist_goods)) { $ids = array_column($this->attr, 'attr_id'); $exist_goods_attr = []; foreach ($exist_goods as $goods) { $exist_goods_attr[] = json_decode($goods['attr'], true); foreach ($exist_goods_attr as $value) { $exist_goods_id = array_column($value, 'attr_id'); if ($exist_goods_id == $ids) { return [ 'code' => 1, 'msg' => '已添加同规格商品' ]; } } } } $t = \Yii::$app->db->beginTransaction(); $integralGoods = new SaasIntegralGoods(); $integralGoods->store_id = $this->store_id; $integralGoods->attr = json_encode($this->attr); $integralGoods->goods_id = $this->goods_id; $integralGoods->name = empty($this->name) ? Goods::findOne($this->goods_id)->name : $this->name; $integralGoods->goods_count = $this->goods_count; $integralGoods->integral = $this->integral; $integralGoods->user_num = $this->user_num; $integralGoods->sales = $this->sales; $integralGoods->cat_id = $this->cat_id; $integralGoods->sort = $this->sort; $integralGoods->detail = $this->detail; $integralGoods->is_delete = 0; if (!$integralGoods->save()) { $t->rollBack(); return [ 'code' => 1, 'msg' => $integralGoods->errors[0] ]; } $ids = array_column(json_decode($integralGoods->attr, true), 'attr_id'); // 修改库存 $goods_attr_res = Goods::findOne($integralGoods->goods_id); $goods_attr = json_decode($goods_attr_res->attr, true); foreach ($goods_attr as $key => $value) { $goods_attr_id = array_column($value['attr_list'], 'attr_id'); if ($ids == $goods_attr_id) { $goods_attr[$key]['num'] -= $integralGoods->goods_count; if ($goods_attr[$key]['num'] < 0) { $t->rollBack(); return [ 'code' => 1, 'msg' => '添加失败' ]; } break; } } $goods_attr_res->attr = json_encode($goods_attr); if (!$goods_attr_res->save()) { $t->rollBack(); return [ 'code' => 1, 'msg' => '添加失败' ]; } $t->commit(); return [ 'code' => 0, 'msg' => '添加成功' ]; } public static function getAttrGroupByAttId($att_id) { $cache_key = 'get_attr_group_by_attr_id_' . $att_id; $attr_group = \Yii::$app->cache->get($cache_key); if ($attr_group) { return $attr_group; } $attr_group = AttrGroup::find()->alias('ag') ->where(['ag.id' => Attr::find()->select('attr_group_id')->distinct()->where(['id' => $att_id])]) ->limit(1)->one(); if (!$attr_group) { return $attr_group; } \Yii::$app->cache->set($cache_key, $attr_group, 10); return $attr_group; } }