| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\models\salesman;
- use app\constants\OptionSetting;
- use app\jobs\storeSync\DiyCommon;
- use app\models\Admin;
- use app\models\Attr;
- use app\models\AttrGroup;
- use app\models\AttrLibrary;
- use app\models\Cat;
- use app\models\CloudGoodsUpdateLog;
- use app\models\FoodCat;
- use app\models\Goods;
- use app\models\GoodsBook;
- use app\models\GoodsCat;
- use app\models\GoodsFullMinus;
- use app\models\GoodsPic;
- use app\models\Option;
- use app\models\Order;
- use app\models\OrderDetail;
- use app\models\PostageRules;
- use app\models\StoreSyncExtLog;
- use app\models\Supplier;
- use yii\base\Model;
- use yii\helpers\Json;
- use app\modules\admin\models\PlatformForm;
- class StoreAdminGoodsForm extends Model
- {
- public $status = 0;//商品列表状态 /分类状态
- public $sort = 0;//时间排序
- public $model;
- public $id;//商品ID
- public $goods_name;//商品名称
- public $goods_num;//商品数量
- public $goods_price;//商品价格
- public $attr;//商品规格
- public $goods_info;//商品信息
- public $cat_name; //分类名称
- public $pic_url; //分类缩略图
- public $parent_id; //分类父级
- public $store_id;
- public $cat_id;
- public $is_food;
- public $min_price;
- public $max_price;
- public $goods_keyword;
- public function rules()
- {
- return [
- [['status', 'sort', 'goods_num', 'goods_price', 'parent_id', 'store_id', 'is_food'], 'integer'],
- [['goods_name', 'id', 'pic_url', 'cat_name', 'goods_keyword', 'cat_id'], 'string'],
- [['attr', 'goods_info'], 'array'],
- [['min_price', 'max_price'], 'number']
- ];
- }
- public function initGoods()
- {
- $this->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;
- }
- }
|