model = Goods::find()->where(['store_id' => $this->store_id, 'is_delete' => 0]); } //商品列表 public function goodsList(){ try { $this->initGoods(); $sort = $this->sort; $status = $this->status; $store_id = $this->store_id; $model = $this->model; $cat_id = $this->cat_id; $is_food = $this->is_food; $min_price = $this->min_price; $max_price = $this->max_price; $goods_keyword = $this->goods_keyword; //商品id $up_status = Goods::find()->where(['store_id' => $store_id, 'is_delete' => 0])->andWhere(['status'=>1])->count(); $down_status = Goods::find()->where(['store_id' => $store_id, 'is_delete' => 0])->andWhere(['status'=>0])->count(); $total = Goods::find()->where(['store_id' => $store_id, 'is_delete' => 0])->count(); if ($is_food) { $model->andWhere(['<>', 'md_food_id', 0]); } else { $model->andWhere(['md_food_id' => 0]); } //商品状态 if (in_array($status, [0, 1])) { $model->andWhere(['status' => $status]); } //排序方式 switch ($sort) { case 1: $model->orderBy('price desc');//价格从高到低 break; case 2: $model->orderBy('price asc');//价格从低到高 break; case 3: $model->orderBy('goods_num desc');//库存从高到低 break; case 4: $model->orderBy('goods_num asc');//库存从低到高 break; default: $model->orderBy('created_at desc');//创建时间 break; } //商品分类 if ($cat_id) { $cat_id = explode(',', $cat_id); if ($is_food) { $model->andWhere(['cat_id' => $cat_id]); } else { $goods_ids = GoodsCat::find()->where(['cat_id' => $cat_id, 'is_delete' => 0])->select('goods_id')->column(); $model->andWhere(['id' => array_merge($goods_ids, [0])]); } } if ($goods_keyword) { $model->andWhere(['OR', ['LIKE', 'name', $goods_keyword], ['id' => $goods_keyword]]); } //价格区间 if ($min_price > 0 && $max_price > 0) { $model->andWhere(['AND', ['>=', 'price', $min_price], ['<=', 'price', $max_price]]); } $model->select('id, name, price, goods_num, virtual_sales, cover_pic, created_at, status, use_attr, attr, md_food_id'); //分页 $pagination = pagination_make($model); $list = $pagination['list']; foreach ($list as $i => &$item) { if(!empty($item['attr'])){ $item['attr'] = is_array($item['attr'])? $item['attr'] : json_decode($item['attr'], true); } // $form = new PlatformForm(); // $form->id = $item['cloud_goods_id']; // $glist = $form->goodsInfo(); // $supplier_name = ''; // if($glist['code'] == 0 && $glist['data']['count'] == 1){ // $supplier_name = $glist['data']['list'][0]['supplier']['name']; // } // if (!$supplier_name) { // continue; // } $supplier = Supplier::findOne(['cloud_supplier_id' => $item['cloud_supplier_id'], 'is_delete' => 0]); $supplier_name = ''; if ($supplier) { $supplier_name = $supplier->supplier_name; } $item['cloud_goods_supplier_name'] = $supplier_name; } return [ 'code' => 0, 'msg' => '获取成功', 'data' => [ 'sql' => $model->createCommand()->getRawSql(), 'list' => $list, 'up_status' => $up_status, 'down_status' => $down_status, 'totalCount' => $pagination['totalCount'], 'pageNo' => $pagination['pageNo'], 'total' => $total ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //商品规格库存 public function getGoodsAttr(){ try { $id = $this->id; $this->initGoods(); $model = $this->model; $goods = $model->andWhere(['id' => $id, 'use_attr' => 1])->select('id, attr')->one(); if (empty($goods->attr) || empty($goods)) { throw new \Exception("获取商品规格库存失败"); } $attr = !empty($goods->attr) ? json_decode($goods->attr, true) : []; $getAttrGroupList = $goods->getAttrGroupList(); $getAttrGroupList = json_decode(json_encode($getAttrGroupList), true); $title = null; if (!empty($getAttrGroupList)) { $title = $getAttrGroupList[0]['attr_list']; } $arr = []; if (!empty($title) && !empty($attr)) { foreach ($title as $index =>$item) { foreach ($attr as $at) { if (!empty($at)) { foreach ($at['attr_list'] as $al) { if ($item['attr_id'] === $al['attr_id']) { $arr[$index]['list'][] = $at; } } } } } } return [ 'code' => 0, 'msg' => "获取成功", 'data' => [ 'attr' => $attr, 'attr_group' => $arr, 'title' => $title ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //修改标题 public function setGoodsName(){ try { $this->initGoods(); $model = $this->model; $goods = $model->andWhere(['id' => $this->id])->select('id, name')->one(); if (empty($goods) || empty($goods->name)) { throw new \Exception("信息不存在"); } $goods->name = $this->goods_name; if (!$goods->save()) { throw new \Exception("保存失败"); } return [ 'code' => 0, 'msg' => "修改成功" ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //删除 public function goodsDel(){ try { $id = $this->id; $id = explode(',',$id); $res = Goods::updateAll(['is_delete' => 1], ['id' => $id]); if ($res) { if (count($id) === 1) { $goods = Goods::findOne($id[0]); (new DiyCommon)->JobBehaviors($goods->store_id, StoreSyncExtLog::TYPE_PRODUCT, $id); } return [ 'code' => 0, 'msg' => "删除成功" ]; } throw new \Exception("删除失败"); } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //上下架 public function setGoodsStatus(){ try { $id = $this->id; $id = explode(',',$id); if (empty($id) ) { throw new \Exception("没有传入有效数据"); } $this->initGoods(); $model = $this->model; if(!empty($this->status)){ //判断库存 $goods = $model->andWhere(['id' => $id])->select('id, status, goods_num, name, cloud_goods_id, is_wholesale')->all(); if (empty($goods)) { throw new \Exception("获取产品信息失败"); } foreach ($goods as $item) { if (empty($item->goods_num) && $item->goods_num <= 0) { throw new \Exception('商品' . $item->name . "库存不足"); } //是云仓商品 if ($item->cloud_goods_id > 0 && !intval($item->is_wholesale)) { //判断是否可以上架 供货商是否更新产品 $CloudGoodsUpdateLog = CloudGoodsUpdateLog::findOne(['cloud_goods_id' => $item->cloud_goods_id]); //存在记录并且不为批发订单 if ($CloudGoodsUpdateLog && !$item->is_wholesale) { if (!$CloudGoodsUpdateLog->is_audit) { return [ 'code' => 1, 'msg' => '供货商商品暂未审核通过' ]; } if (!$CloudGoodsUpdateLog->is_update) { return [ 'code' => 1, 'msg' => '商品信息需要更新' ]; } } } } } $status = empty($this->status) ? 0 : 1; //修改上下架状态 $res = Goods::updateAll(['status' => $status], ['id' => $id]); if ($res) { if (count($id) === 1) { $goods = Goods::findOne($id[0]); (new DiyCommon)->JobBehaviors($goods->store_id, StoreSyncExtLog::TYPE_PRODUCT, $id); } return [ 'code' => 0, 'msg' => "操作成功" ]; } throw new \Exception('信息修改失败'); } catch (\Exception $e){ return [ 'code' => 0, 'msg' => $e->getMessage() ]; } } //修改价格/数量 (规格库存价格)//未测试带规格的 public function setPrice(){ try { $this->initGoods(); $model = $this->model; $goods = $model->andWhere(['id' => $this->id])->select('id, status, goods_num ,use_attr, attr, price, cost_price')->one(); if (empty($goods)) { throw new \Exception("获取产品信息失败"); } //判断是否存在以及开启规格 if ($goods->use_attr == 1 && !empty($this->attr)) { $attr = $this->attr; $arr = []; foreach ($attr as $item) { if ($item['num'] >=0 && $item['price'] >=0) { $arr[] = $item; } else { throw new \Exception("数据错误"); } } $goods->attr = json_encode($arr); } elseif ($goods->use_attr == 0) { $this->goods_num > 0 && $goods->goods_num = $this->goods_num; $this->goods_price > 0 && $goods->price = $this->goods_price; $attr = json_decode($goods->attr, true); if (!empty($attr)) { foreach ($attr as &$item) { $item['price'] = $goods->price; $item['num'] = $goods->goods_num; } } else { list($default_attr, $default_attr_group) = $this->getDefaultAttr(); $attr = [ [ 'attr_list' => [ [ 'attr_group_name' => $default_attr_group->attr_group_name, 'attr_id' => $default_attr->id, 'attr_name' => $default_attr->attr_name, ], ], 'num' => intval($goods->goods_num) ?: 0, 'price' => $goods->price, 'cost_price' => $goods->cost_price, 'no' => '', 'is_wholesale' => 0, 'wholesale_num' => 0 ], ]; } $goods->attr = json_encode($attr); //如果库存不足,则下架处理 if ($this->goods_num == 0) { $goods->status = 0; } } if (!$goods->save()) { throw new \Exception('数据保存错误' . json_encode($goods->errors)); } return [ 'code' => 0, 'msg' => '修改成功' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * @return array */ private function getDefaultAttr() { $default_attr_name = '默认'; $default_attr_group_name = '规格'; $attr = Attr::findOne([ 'attr_name' => $default_attr_name, 'is_delete' => 0, 'is_default' => 1, ]); $attr_group = null; if (!$attr) { $attr_group = AttrGroup::findOne([ 'attr_group_name' => $default_attr_group_name, 'is_delete' => 0, ]); if (!$attr_group) { $attr_group = new AttrGroup(); $attr_group->store_id = $this->store_id; $attr_group->attr_group_name = $default_attr_group_name; $attr_group->is_delete = 0; $attr_group->save(false); } $attr = new Attr(); $attr->attr_group_id = $attr_group->id; $attr->attr_name = $default_attr_name; $attr->is_delete = 0; $attr->is_default = 1; $attr->save(false); } else { $attr_group = AttrGroup::findOne($attr->attr_group_id); } return [$attr, $attr_group]; } //修改添加商品 public function getGoodsEdit(){ try { $id = $this->id; $store_id = $this->store_id; $is_food = $this->is_food; $this->initGoods(); $model = $this->model; if ($id) { $goods = $model->where(['id' => $id])->select('id, cat_id, product_type, name, attr, use_attr, freight, price, goods_num, detail, delivery_type, pieces, forehead, cover_pic, original_price, goods_no, md_food_id')->one(); if (empty($goods)) { return [ 'code' => 1, 'msg' => '商品获取失败' ]; } $getAttrGroupList = $goods->getAttrGroupList(); $goods = $goods->toArray(); if ((int)$goods['md_food_id'] === 0) { $cat_id = GoodsCat::find() ->where(['goods_id' => $goods['id'], 'is_delete' => 0]) ->select(['cat_id'])->asArray()->all(); $cat_id = array_column($cat_id, 'cat_id'); } else { $cat_id = [$goods['cat_id']]; } $goods['cat_id'] = $cat_id; $goods['integral'] = json_decode($goods['integral'],true); $goods['goods_pic_list'] = GoodsPic::find()->select(['pic_url']) ->where(['goods_id' => $id, 'is_delete' => 0])->asArray()->all(); foreach ($goods as &$val) { $val = is_int($val) ? (string)$val : $val; } $goods['attr'] = !empty($goods['attr']) ? json_decode($goods['attr'], true) : []; //unset($goods['attr']); } else { $goods = null; } //商品规格 $AttrLibrary = AttrLibrary::find()->where(['is_delete' => 0, 'store_id' => $store_id])->select('id, name, parent_id')->asArray()->all(); $new_AttrLibrary = $this->getmenu($AttrLibrary); $send_type = Option::get(OptionSetting::STORE_SEND_TYPE, $store_id, 'store')['value']; $send_type = Option::get(OptionSetting::STORE_SEND_TYPE, $store_id, 'pay', $send_type); $send_type = $send_type ? Json::decode($send_type['value']) : []; $send_type_arr = []; foreach ((array)$send_type as $key => $send) { if ($send['value'] == 1) { $send_type_arr[$key] = $send['text']; } } //物流方式 $arr = empty($send_type_arr) ? ['express' => '快递', 'shop' => '自提'] : $send_type_arr; $goods_delivery_type = array_keys($arr); foreach ($goods_delivery_type as &$item) { switch ($item) { case "express": $value = '快递'; break; case "shop": $value = '到店自提'; break; case "delivery": $value = '同城配送'; break; default: $value = ''; break; } $item = [ 'key' => $item, 'value' => $value ]; } $goods['delivery_type'] = !empty($goods['delivery_type']) ? json_decode($goods['delivery_type'], true) : []; // unset($goods['goods_delivery_type']); // 获取运费规则 $postage = PostageRules::find()->where([ 'store_id' => $store_id, 'is_delete' => 0, 'mch_id' => 0 ])->select(['id', 'name', 'is_enable'])->asArray()->all(); //分类列表 if ((int)$goods['md_food_id'] !== 0) { $cat = FoodCat::find()->where(['is_delete' => 0, 'is_show' => 1, 'store_id' => $store_id, 'md_id' => -1]) ->select('id, name')->asArray()->all(); } else { $cat = Cat::find()->where(['is_delete' => 0, 'is_show' => 1, 'store_id' => $store_id]) ->select('id, name, parent_id')->asArray()->all(); } $cat_arr = $this->getmenu($cat); return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'info' => $goods, 'postage' => $postage, 'cat' => $cat_arr, 'goods_delivery_type' => $goods_delivery_type, 'new_AttrLibrary' => $new_AttrLibrary, 'AttrGroupList' => $getAttrGroupList ?? [], ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //获取运费规则 public function getPostageData() { try { $store_id = $this->store_id; // 获取运费规则 $postage = PostageRules::find()->where([ 'store_id' => $store_id, 'is_delete' => 0, 'mch_id' => 0 ])->select(['id', 'name', 'is_enable'])->asArray()->all(); return [ 'code' => 0, 'msg' => "获取成功", 'data' => [ 'postage' => $postage ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //商品信息保存 public function saveGoods(){ $t = \Yii::$app->db->beginTransaction(); try { $this->initGoods(); $model = $this->model; $goods_info = $this->goods_info; $store_id = $this->store_id; if (empty($goods_info)) { throw new \Exception("数据错误"); } $id = $goods_info['id']; if (!empty($id)) { $model = $model->andWhere(['id' => $id])->select('id, cat_id, product_type, attr, use_attr, price, goods_num, freight, detail, delivery_type, pieces, forehead, cover_pic, original_price, goods_no')->one(); if (empty($model)) { throw new \Exception("获取产品信息失败"); } } else { $model = new Goods(); $model->store_id = $store_id; } $model->use_attr = $goods_info['use_attr']; $model->product_type = $goods_info['product_type']; if ((int)$goods_info['md_food_id'] !== 0) { $food_cat = FoodCat::findOne($goods_info['cat_id']); if ($food_cat) { $model->cat_id = implode(',', $goods_info['cat_id']); } else { throw new \Exception("点餐分类错误"); } } $model->price = $goods_info['price']; $model->goods_num = $goods_info['goods_num']; $model->name = $goods_info['name']; // if ((int)$goods_info['use_attr'] === 0) { $model->original_price = $goods_info['original_price']; $model->goods_no = $goods_info['goods_no']; // } $model->detail = $goods_info['detail']; $model->delivery_type = json_encode($goods_info['delivery_type']); $model->pieces = $goods_info['pieces']; $model->forehead = $goods_info['forehead']; $model->cover_pic = $goods_info['cover_pic']; $model->freight = $goods_info['freight'] ?: 0; if ($model->save()) { //分类处理 // if ($goods_info['cat_id']) { // if (!empty($id)) { // GoodsCat::updateAll(['is_delete' => 1], ['goods_id' => $id, 'is_delete' => 0]); // } // // $goods_cat = new GoodsCat(); // $goods_cat->goods_id = $model->id; // $goods_cat->store_id = $this->store_id; // $goods_cat->cat_id = (int)$goods_info['cat_id']; // $goods_cat->created_at = time(); // $goods_cat->updated_at = time(); // if (!$goods_cat->save()) { // throw new \Exception(json_encode($goods_cat->errors)); // } // } // if ($goods_info['goods_pic_list']) { // if (!empty($id)) { // GoodsPic::updateAll(['is_delete' => 1], ['goods_id' => $id, 'is_delete' => 0]); // } // foreach ($goods_info['goods_pic_list'] as $item) { // if(!isset($item['pic_url']) || empty($item['pic_url'])){ // continue; // } // $goods_pic = new GoodsPic(); // $goods_pic->pic_url = $item['pic_url']; // $goods_pic->goods_id = $model->id; // if (!$goods_pic->save()) { // throw new \Exception(json_encode($goods_pic->errors)); // } // } // // } // //判断如果规格发生改变就删除满减规则 // if (!empty($goods_info['is_change_attr'])) { // $res = GoodsFullMinus::updateAll(['is_delete' => 1], ['goods_id' => $model->id]); // \Yii::error($res, "规格发生改变就删除满减规则"); // } $form = new StoreAdminAttrForm(); $form->store_id = $store_id; $res = $form->goodsAttrBook($goods_info); if ($res['code'] != 0) { throw new \Exception($res['msg']); } $t->commit(); return [ 'code' => 0, 'msg' => "保存成功" ]; } throw new \Exception(json_encode($model->errors)); } catch (\Exception $e) { $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage().$e->getLine() ]; } } //展示商品详情 public function goodsDetail(){ try { $id = $this->id; $this->initGoods(); $model = $this->model; $store_id = $this->store_id; if (empty($id)) { throw new \Exception("获取参数失败"); } $goods = $model->andWhere(['id' => $id])->select('id, detail, name, price, goods_num, virtual_sales, cat_id, attr, use_attr, md_food_id')->with('goodsPicList')->asArray()->one(); if (empty($goods)) { throw new \Exception("获取产品信息失败"); } if ((int)$goods['md_food_id'] === 0) { //分类列表 $cat = Cat::find()->where(['is_delete' => 0, 'is_show' => 1, 'store_id' => $store_id]) ->select('id, name, parent_id')->asArray()->all(); } else { $cat = FoodCat::find()->where(['is_delete' => 0, 'is_show' => 1, 'store_id' => $store_id, 'md_id' => -1]) ->select('id, name')->asArray()->all(); } $cat_arr = $this->getmenu($cat); //实际销量 $actual_num = Order::find()->where(['and', ['o.is_delete' => 0, 'o.store_id' => $store_id, 'od.goods_id' => $id]]) ->andWhere(['or', ['<>', 'o.trade_status', '-1'],['<>', 'o.trade_status', '1']]) ->alias('o')->leftJoin(['od' => OrderDetail::tableName()], 'od.order_id = o.id') ->count(); $goods['virtual_sales'] += $actual_num; //规格 $goods['attr'] = !empty($goods['attr']) ? json_decode($goods['attr'], true) : ''; return [ 'code' => 0, 'msg' => "获取成功", 'data' => [ 'goods_info' => $goods, 'cat' => $cat_arr ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //分类列表 public function catList(){ $store_id = $this->store_id; $status = $this->status; if ($this->is_food) { $up_status = FoodCat::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'is_show' => 1])->count(); $down_status = FoodCat::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'is_show' => 0])->count(); $total = FoodCat::find()->where(['store_id' => $store_id, 'is_delete' => 0])->count(); $data = FoodCat::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'is_show' => $status]) ->select('id, name, pic_url, is_show')->orderBy('sort desc')->asArray()->all();; } else { $up_status = Cat::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'is_show' => 1])->count(); $down_status = Cat::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'is_show' => 0])->count(); $total = Cat::find()->where(['store_id' => $store_id, 'is_delete' => 0])->count(); $data = Cat::find()->where(['store_id' => $store_id, 'is_delete' => 0, 'is_show' => $status]) ->select("id, parent_id, name, pic_url, is_show")->orderBy('sort desc')->asArray()->all(); } $list = $this->getmenu($data, 0, 1); return [ 'code' => 0, 'msg' => "获取成功", 'data' => [ 'list' => $list, 'up_status' => $up_status, 'down_status' => $down_status, 'total' => $total ] ]; } //分类保存 public function catAdd(){ try { $id = $this->id; $cat_name = $this->cat_name; $pic_url = $this->pic_url; $parent_id = $this->parent_id; $status = $this->status; $sort = $this->sort; $store_id = $this->store_id; if ($this->is_food) { $model = FoodCat::findOne($id)? : new FoodCat(); } else { $model = Cat::findOne($id)? : new Cat(); $model->parent_id = $parent_id; } $model->name = $cat_name; $model->store_id = $store_id; $model->pic_url = $pic_url; $model->sort = $sort; $model->is_show = $status; $model->created_at = time(); $model->updated_at = time(); if (!$model->save()) { throw new \Exception(json_encode($model->errors)); } return [ 'code' => 0, 'msg' => "保存成功" ]; } catch (\Exception $e){ return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function catInfo() { try { $id = $this->id; $store_id = $this->store_id; $model = Cat::findOne(['id' => $id, 'store_id' => $store_id]); if (!$model) { $data = null; } else { $data = [ 'name' => $model->name, 'parent_id' => $model->parent_id, 'pic_url' => $model->pic_url, 'sort' => $model->sort, 'is_show' => $model->is_show, 'parent_name' => '' ]; if ($model->parent_id) { $data['parent_name'] = Cat::findOne(['id' => $model->parent_id, 'store_id' => $store_id])->name ?? ''; } } return [ 'code' => 0, 'msg' => 'success', 'data' => $data ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //分类状态更改 public function setCatStatus(){ try { $id = $this->id; $status = $this->status; if (empty($id)) { throw new \Exception("找不到对应的数据信息"); } if ($this->is_food) { $model = FoodCat::findOne($id); } else { $model = Cat::findOne($id); } if (empty($model)) { throw new \Exception("找不到对应的数据信息"); } switch ($status) { case 0: if ($this->is_food) { $res = FoodCat::updateAll(['is_show' => 0], ['id' => $id]); } else { $res = Cat::updateAll(['is_show' => 0], ['OR', ['id' => $id], ['parent_id' => $id]]); } break; case 1: if ($this->is_food) { $res = FoodCat::updateAll(['is_show' => 0], ['id' => $id]); } else { $res = Cat::updateAll(['is_show' => 1], ['OR', ['id' => $id], ['parent_id' => $id]]); } break; case 2: if ($this->is_food) { $res = FoodCat::updateAll(['is_show' => 0], ['id' => $id]); } else { $res = Cat::updateAll(['is_delete' => 1], ['OR', ['id' => $id], ['parent_id' => $id]]); } break; } if (!$res) { throw new \Exception("失败"); } return [ 'code' => 0, 'msg' => "成功" ]; } catch (\Exception $e){ return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //递归 public function getmenu($menu, $id = 0, $show_parent = 0, $name = ""){ $arr = []; foreach ($menu as $k => $v) { if(($v['parent_id'] ?? 0) == $id){ if ($show_parent == 1) { $v['parent_name'] = $name; $v['children'] = $this -> getmenu($menu, $v['id'], $show_parent, $v['name']); }else { $v['children'] = $this -> getmenu($menu, $v['id']); } $arr[] = $v; } } return $arr; } }