| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- <?php
- namespace addons\Store\client\controllers;
- use addons\Store\common\models\StoreGoodsCate;
- use addons\Store\common\models\StoreGoodsCateRelate;
- use common\enums\StatusEnum;
- use common\models\goods\GoodsCate;
- use Yii;
- /**
- * 商品控制器
- * @Author bing
- * @DateTime 2021-08-16 13:12:44
- * @copyright: Copyright (c) 2020 广东七件事集团
- */
- class CateController extends BaseController
- {
- public $enableCsrfValidation = false;
- /**
- * 商品分类
- * @Author bing
- * @DateTime 2021-08-20 14:49:55
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @return void
- */
- public function actionIndex(){
- if (Yii::$app->request->isAjax) {
- $keyword = Yii::$app->request->get('keyword','');
- $where = [['mall_id' => $this->mall['id'],'parent_id' => 0,'store_id' => $this->store_id,'status' => [StatusEnum::ENABLED,StatusEnum::DISABLED]]];
- $keyword && $where[] = ['like','name',$keyword.'%',false];
- $order = 'sort asc,created_at desc';
- $with = ['child','child.child'];
- $params = compact('where','order','with');
- $list = StoreGoodsCate::lists($params);
- if($list === false) return $this->error(StoreGoodsCate::getStaticError());
- return $this->success('ok',compact('list'));
- }
- return $this->render($this->action->id);
- }
- /**
- * 商品分类
- * @Author bing
- * @DateTime 2021-08-20 14:49:55
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @return void
- */
- public function actionSortEdit(){
- if (Yii::$app->request->isAjax) {
- $post = Yii::$app->request->post();
- $res = StoreGoodsCate::sort($post);
- if($res === false) return $this->error(GoodsCate::getStaticError());
- return $this->success();
- }
- }
- /**
- * 商品分类转移
- * @Author bing
- * @DateTime 2021-08-20 14:49:55
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @return void
- */
- public function actionTransfer(){
- if (Yii::$app->request->isAjax) {
- if(Yii::$app->request->isGet){
- $id = Yii::$app->request->get('id',0);
- $keyword = Yii::$app->request->get('keyword','');
- $where = [['mall_id' => $this->mall_id,'parent_id' =>$id,'store_id' => $this->store_id,'status' => [StatusEnum::ENABLED]]];
- $keyword && $where[] = ['like','name',$keyword.'%',false];
- $with = ['parent', 'child', 'parent.parent'];
- $params = compact('where','order','with');
- $page_data = StoreGoodsCate::listPage($params,false);
- $list = $page_data['list'];
- $parent = $list[0]['parent'] ? $list[0]['parent'] : null;
- $grandParent = $parent['parent'] ? $parent['parent'] : null;
- if($list === false) return $this->error(StoreGoodsCate::getStaticError());
- return $this->success('ok',compact('list','parent','grandParent'));
- }else{
- // 分类转移
- $post = Yii::$app->request->post();
- $res = $this->checkRequireParam($post,['before','after']);
- if($post['before'] <= 0 || $post['after'] <= 0) return $this->error('请选择转移前和转移后的分类');
- $res = StoreGoodsCateRelate::changeGoodsCate($post['before'],$post['after']);
- if($res === false) return $this->error(StoreGoodsCate::getStaticError());
- return $this->success('转移成功,一共转移' . $res . '个商品');
- }
- }
- }
- /**
- * 商品样式设置
- * @Author bing
- * @DateTime 2021-08-23 09:55:11
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @return void
- */
- public function actionStyle(){
- if (Yii::$app->request->isAjax) {
- $default = [
- 'cat_style' => 3,
- 'recommend_count' => 3,
- 'cat_goods_count' => 1,
- 'cat_goods_cols' => 1
- ];
- if(Yii::$app->request->isGet){
- Yii::$app->services->setting->mall->get();
- $cate_setting = Yii::$app->services->setting->mall->get($this->mall_id,'cate_setting',null,true);
- $setting = array_merge($default,$cate_setting);
- $this->success('ok',compact('setting'));
- }else{
- $post = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($post,[
- [['cat_style'], 'required'],
- [['recommend_count','cat_goods_count','cat_goods_cols'], 'integer'],
- ]);
-
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $cate_setting = array_merge($default,$post);
-
- $res = Yii::$app->services->setting->mall->set($this->mall_id,compact('cate_setting'));
- $this->success('保存成功');
- }
- }
- }
- /**
- * 商品分类
- * @Author bing
- * @DateTime 2021-08-20 14:49:55
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @return void
- */
- public function actionEdit(){
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isGet) {
- // 数据获取
- $get = Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($get, [[['id'], 'integer'],[['keyword'], 'string']]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $detail = [];
- // 传了id参数并且无分类搜索则获取分类详细信息
- if(isset($get['id']) && empty($get['keyword'])){
- $detail = StoreGoodsCate::getOne([['id' => $get['id'], 'mall_id' => $this->mall_id,
- 'store_id' => $this->store_id]], true,['parent.parent']);
- if(empty($detail)) return $this->error('不存在此分类');
- $detail['parents'] = [];
- if(isset($detail['parent'])) $detail['parents'][] = $detail['parent'];
- if(isset($detail['parent']['parent'])) $detail['parents'][] = $detail['parent']['parent'];
- }
- $cond = [['mall_id' => $this->mall_id, 'store_id' => $this->store_id]];
- isset($get['keyword']) && $cond[] = ['like', 'name', $get['keyword'] . '%', false];
- $cates = StoreGoodsCate::getCates($cond,[],true,'id,name,parent_id');
- $cates = StoreGoodsCate::getTreeCate($cates);
- return $this->success('操作成功',compact('detail','cates'));
- } else {
- // 编辑分类
- $form = Yii::$app->request->post('form');
- $res = Yii::$app->services->validate->validate($form, [
- [['id'], 'integer'],
- [['name','sort','pic','big_pic_url','advert_pic','advert_url','parent_id','status','is_show','advert_open_type','advert_params'],'safe'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $form['parent_id'] = empty($form['parent_id']) ? 0 : $form['parent_id'];
- $form['mall_id'] = (int)$this->mall_id;
- $form['store_id'] = $this->store_id;
- $res = StoreGoodsCate::setData($form);
- if ($res == false) return $this->error(GoodsCate::getStaticError());
- return $this->success('操作成功');
- }
- }
- return $this->render($this->action->id);
- }
- public function actionTree(){
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isGet) {
- $cond = [['mall_id' => $this->mall_id, 'store_id' => $this->store_id]];
- $list = StoreGoodsCate::getCates($cond,[],true,'id,name,parent_id');
- $list = StoreGoodsCate::getTreeCate($list);
- return $this->success('操作成功',compact('list'));
- }
- }
- }
- /**
- * @desc:删除商品分类
- * @Author: hua
- * @Date: 2021/11/4
- * @Time: 14:20
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionDelete(){
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'integer'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['status'] = StatusEnum::DELETE;
- $params['mall_id'] = $this->mall_id;
- $res = StoreGoodsCateRelate::findOne(['cate_id'=>$params['id']]);
- if ($res === false) return $this->error('该分类关联了商品,不能删除!');
- $res = StoreGoodsCate::findOne(['parent_id'=>$params['id'],'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]);
- if (!empty($res)) return $this->error('该分类还有子分类,不能删除!');
- $res = StoreGoodsCate::setData($params);
- if ($res === false) return $this->error('删除失败!');
- return $this->success('操作成功',[]);
- }
- public function actionMallTree(){
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isGet) {
- $params = Yii::$app->request->get();
- $cond = [['mall_id' =>$params['mall_id']]];
- $list = GoodsCate::getCates($cond,[],true,'id,name,parent_id');
- $list = GoodsCate::getTreeCate($list);
- return $this->success('操作成功',compact('list'));
- }
- }
- }
- public function actionMallCateTree(){
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isGet) {
- $params = Yii::$app->request->get();
- $cond = [['mall_id' => $this->mall_id]];
- $list = GoodsCate::getCates($cond,[],true,'id,name,parent_id');
- $list = GoodsCate::getTreeCate($list);
- return $this->success('操作成功',compact('list'));
- }
- }
- }
- public function actionImportCate()
- {
- $trans = Yii::$app->db->beginTransaction();
- try {
- if (!Yii::$app->request->isAjax || !Yii::$app->request->isPost) throw new \Exception("请求不合法");
- $cates = Yii::$app->request->post('cates', []);
- if (empty($cates)) throw new \Exception("导入分类列表不能为空");
- $cate_id_arr = array_unique(array_merge(...$cates));
- // 查询分类信息
- $mall_cate_list = GoodsCate::lists(['where' => [['mall_id' => $this->mall_id], ['id' => $cate_id_arr]], 'index' => 'id']);
- foreach ($cates as $rows) {
- $parent_id = 0;
- foreach ($rows as $cate_id) {
- if (!isset($mall_cate_list[$cate_id])) continue;
- $mall_cate = $mall_cate_list[$cate_id];
- $model = StoreGoodsCate::insertDataForMallCateID([
- 'mall_id' => $this->mall_id,
- 'store_id' => $this->store_id,
- 'parent_id' => $parent_id,
- 'name' => $mall_cate['name'],
- 'pic' => $mall_cate['pic'],
- 'big_pic' => $mall_cate['big_pic'],
- 'advert_pic' => $mall_cate['advert_pic'],
- 'advert_url' => $mall_cate['advert_url'],
- 'advert_open_type' => $mall_cate['advert_open_type'],
- 'advert_params' => $mall_cate['advert_params'],
- 'sort' => $mall_cate['sort'],
- 'mall_goods_cate_id' => $cate_id
- ]);
- if (!empty($model)) $parent_id = $model->id;
- }
- }
- $trans->commit();
- return $this->success("导入成功");
- }catch (\Exception $e){
- $trans->rollBack();
- return $this->error($e->getMessage());
- }
- }
- public function actionSyncCate()
- {
- $trans = Yii::$app->db->beginTransaction();
- try {
- if (!Yii::$app->request->isAjax || !Yii::$app->request->isPost) throw new \Exception("请求不合法");
- // 查询导入的分类信息
- $cate_list = StoreGoodsCate::lists(['where' => [['mall_id' => $this->mall_id], ['<>', 'mall_goods_cate_id', 0], ['<>', 'status', StatusEnum::DELETE]]]);
- // 查询分类信息
- $cate_id_arr = array_column($cate_list, 'mall_goods_cate_id');
- $mall_cate_list = GoodsCate::lists(['where' => [['mall_id' => $this->mall_id], ['or', ['id' => $cate_id_arr], ['parent_id' => $cate_id_arr]], ['<>', 'status', StatusEnum::DELETE]], 'index' => 'id']);
- $cate_list = StoreGoodsCate::getTreeCate($cate_list);
- foreach ($cate_list as $cate) {
- $this->SyncCateTree($cate, $mall_cate_list);
- }
- $trans->commit();
- return $this->success("更新商品分类成功");
- }catch (\Exception $e){
- $trans->rollBack();
- return $this->error($e->getMessage());
- }
- }
- public function SyncCateTree($cate = [], $mall_cate_list = [], $key = 'children')
- {
- if (empty($cate)) return;
- if (isset($mall_cate_list[$cate['mall_goods_cate_id']])){
- $mall_cate = $mall_cate_list[$cate['mall_goods_cate_id']];
- $elementsToCompare = ['name', 'pic', 'big_pic', 'sort'];
- if (array_intersect_key($cate, array_flip($elementsToCompare)) !== array_intersect_key($mall_cate, array_flip($elementsToCompare))) {
- // 修改分类信息
- StoreGoodsCate::setData([
- 'id' => $cate['id'],
- 'mall_id' => $this->mall_id,
- 'store_id' => $this->store_id,
- 'name' => $mall_cate['name'],
- 'pic' => $mall_cate['pic'],
- 'big_pic' => $mall_cate['big_pic'],
- 'sort' => $mall_cate['sort']
- ]);
- }
- // 如果主商城有新增的子分类
- $mall_child_arr = StoreGoodsCate::getChildrenCate($mall_cate_list, $cate['mall_goods_cate_id']);
- if (!empty($mall_child_arr)) {
- $child_id_arr = !empty($cate[$key]) ? array_column($cate[$key], 'mall_goods_cate_id') : [];
- $mall_child_id_arr = array_column($mall_child_arr, 'id');
- foreach ($mall_child_id_arr as $temp_mall_cate_id) {
- if (!in_array($temp_mall_cate_id, $child_id_arr) && isset($mall_cate_list[$temp_mall_cate_id])) {
- $temp_mall_cate = $mall_cate_list[$temp_mall_cate_id];
- StoreGoodsCate::insertDataForMallCateID([
- 'mall_id' => $this->mall_id,
- 'store_id' => $this->store_id,
- 'parent_id' => $cate['id'],
- 'name' => $temp_mall_cate['name'],
- 'pic' => $temp_mall_cate['pic'],
- 'big_pic' => $temp_mall_cate['big_pic'],
- 'advert_pic' => $temp_mall_cate['advert_pic'],
- 'advert_url' => $temp_mall_cate['advert_url'],
- 'advert_open_type' => $temp_mall_cate['advert_open_type'],
- 'advert_params' => $temp_mall_cate['advert_params'],
- 'sort' => $temp_mall_cate['sort'],
- 'mall_goods_cate_id' => $temp_mall_cate_id
- ]);
- }
- }
- }
- if (!empty($cate['children'])) {
- foreach ($cate['children'] as $child) {
- $this->SyncCateTree($child, $mall_cate_list);
- }
- }
- }else {
- // 分类不存在则删除门店商品分类及下级分类
- $child_cate_id_arr = StoreGoodsCate::getChildCateIDByTree($cate);
- $delete_id_arr = array_merge([$cate['id']], $child_cate_id_arr);
- StoreGoodsCate::deleteAll(['id' => $delete_id_arr]);
- }
- }
- }
|