CateController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. namespace addons\Store\client\controllers;
  3. use addons\Store\common\models\StoreGoodsCate;
  4. use addons\Store\common\models\StoreGoodsCateRelate;
  5. use common\enums\StatusEnum;
  6. use common\models\goods\GoodsCate;
  7. use Yii;
  8. /**
  9. * 商品控制器
  10. * @Author bing
  11. * @DateTime 2021-08-16 13:12:44
  12. * @copyright: Copyright (c) 2020 广东七件事集团
  13. */
  14. class CateController extends BaseController
  15. {
  16. public $enableCsrfValidation = false;
  17. /**
  18. * 商品分类
  19. * @Author bing
  20. * @DateTime 2021-08-20 14:49:55
  21. * @copyright: Copyright (c) 2020 广东七件事集团
  22. * @return void
  23. */
  24. public function actionIndex(){
  25. if (Yii::$app->request->isAjax) {
  26. $keyword = Yii::$app->request->get('keyword','');
  27. $where = [['mall_id' => $this->mall['id'],'parent_id' => 0,'store_id' => $this->store_id,'status' => [StatusEnum::ENABLED,StatusEnum::DISABLED]]];
  28. $keyword && $where[] = ['like','name',$keyword.'%',false];
  29. $order = 'sort asc,created_at desc';
  30. $with = ['child','child.child'];
  31. $params = compact('where','order','with');
  32. $list = StoreGoodsCate::lists($params);
  33. if($list === false) return $this->error(StoreGoodsCate::getStaticError());
  34. return $this->success('ok',compact('list'));
  35. }
  36. return $this->render($this->action->id);
  37. }
  38. /**
  39. * 商品分类
  40. * @Author bing
  41. * @DateTime 2021-08-20 14:49:55
  42. * @copyright: Copyright (c) 2020 广东七件事集团
  43. * @return void
  44. */
  45. public function actionSortEdit(){
  46. if (Yii::$app->request->isAjax) {
  47. $post = Yii::$app->request->post();
  48. $res = StoreGoodsCate::sort($post);
  49. if($res === false) return $this->error(GoodsCate::getStaticError());
  50. return $this->success();
  51. }
  52. }
  53. /**
  54. * 商品分类转移
  55. * @Author bing
  56. * @DateTime 2021-08-20 14:49:55
  57. * @copyright: Copyright (c) 2020 广东七件事集团
  58. * @return void
  59. */
  60. public function actionTransfer(){
  61. if (Yii::$app->request->isAjax) {
  62. if(Yii::$app->request->isGet){
  63. $id = Yii::$app->request->get('id',0);
  64. $keyword = Yii::$app->request->get('keyword','');
  65. $where = [['mall_id' => $this->mall_id,'parent_id' =>$id,'store_id' => $this->store_id,'status' => [StatusEnum::ENABLED]]];
  66. $keyword && $where[] = ['like','name',$keyword.'%',false];
  67. $with = ['parent', 'child', 'parent.parent'];
  68. $params = compact('where','order','with');
  69. $page_data = StoreGoodsCate::listPage($params,false);
  70. $list = $page_data['list'];
  71. $parent = $list[0]['parent'] ? $list[0]['parent'] : null;
  72. $grandParent = $parent['parent'] ? $parent['parent'] : null;
  73. if($list === false) return $this->error(StoreGoodsCate::getStaticError());
  74. return $this->success('ok',compact('list','parent','grandParent'));
  75. }else{
  76. // 分类转移
  77. $post = Yii::$app->request->post();
  78. $res = $this->checkRequireParam($post,['before','after']);
  79. if($post['before'] <= 0 || $post['after'] <= 0) return $this->error('请选择转移前和转移后的分类');
  80. $res = StoreGoodsCateRelate::changeGoodsCate($post['before'],$post['after']);
  81. if($res === false) return $this->error(StoreGoodsCate::getStaticError());
  82. return $this->success('转移成功,一共转移' . $res . '个商品');
  83. }
  84. }
  85. }
  86. /**
  87. * 商品样式设置
  88. * @Author bing
  89. * @DateTime 2021-08-23 09:55:11
  90. * @copyright: Copyright (c) 2020 广东七件事集团
  91. * @return void
  92. */
  93. public function actionStyle(){
  94. if (Yii::$app->request->isAjax) {
  95. $default = [
  96. 'cat_style' => 3,
  97. 'recommend_count' => 3,
  98. 'cat_goods_count' => 1,
  99. 'cat_goods_cols' => 1
  100. ];
  101. if(Yii::$app->request->isGet){
  102. Yii::$app->services->setting->mall->get();
  103. $cate_setting = Yii::$app->services->setting->mall->get($this->mall_id,'cate_setting',null,true);
  104. $setting = array_merge($default,$cate_setting);
  105. $this->success('ok',compact('setting'));
  106. }else{
  107. $post = Yii::$app->request->post();
  108. $res = Yii::$app->services->validate->validate($post,[
  109. [['cat_style'], 'required'],
  110. [['recommend_count','cat_goods_count','cat_goods_cols'], 'integer'],
  111. ]);
  112. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  113. $cate_setting = array_merge($default,$post);
  114. $res = Yii::$app->services->setting->mall->set($this->mall_id,compact('cate_setting'));
  115. $this->success('保存成功');
  116. }
  117. }
  118. }
  119. /**
  120. * 商品分类
  121. * @Author bing
  122. * @DateTime 2021-08-20 14:49:55
  123. * @copyright: Copyright (c) 2020 广东七件事集团
  124. * @return void
  125. */
  126. public function actionEdit(){
  127. if (Yii::$app->request->isAjax) {
  128. if (Yii::$app->request->isGet) {
  129. // 数据获取
  130. $get = Yii::$app->request->get();
  131. $res = Yii::$app->services->validate->validate($get, [[['id'], 'integer'],[['keyword'], 'string']]);
  132. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  133. $detail = [];
  134. // 传了id参数并且无分类搜索则获取分类详细信息
  135. if(isset($get['id']) && empty($get['keyword'])){
  136. $detail = StoreGoodsCate::getOne([['id' => $get['id'], 'mall_id' => $this->mall_id,
  137. 'store_id' => $this->store_id]], true,['parent.parent']);
  138. if(empty($detail)) return $this->error('不存在此分类');
  139. $detail['parents'] = [];
  140. if(isset($detail['parent'])) $detail['parents'][] = $detail['parent'];
  141. if(isset($detail['parent']['parent'])) $detail['parents'][] = $detail['parent']['parent'];
  142. }
  143. $cond = [['mall_id' => $this->mall_id, 'store_id' => $this->store_id]];
  144. isset($get['keyword']) && $cond[] = ['like', 'name', $get['keyword'] . '%', false];
  145. $cates = StoreGoodsCate::getCates($cond,[],true,'id,name,parent_id');
  146. $cates = StoreGoodsCate::getTreeCate($cates);
  147. return $this->success('操作成功',compact('detail','cates'));
  148. } else {
  149. // 编辑分类
  150. $form = Yii::$app->request->post('form');
  151. $res = Yii::$app->services->validate->validate($form, [
  152. [['id'], 'integer'],
  153. [['name','sort','pic','big_pic_url','advert_pic','advert_url','parent_id','status','is_show','advert_open_type','advert_params'],'safe'],
  154. ]);
  155. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  156. $form['parent_id'] = empty($form['parent_id']) ? 0 : $form['parent_id'];
  157. $form['mall_id'] = (int)$this->mall_id;
  158. $form['store_id'] = $this->store_id;
  159. $res = StoreGoodsCate::setData($form);
  160. if ($res == false) return $this->error(GoodsCate::getStaticError());
  161. return $this->success('操作成功');
  162. }
  163. }
  164. return $this->render($this->action->id);
  165. }
  166. public function actionTree(){
  167. if (Yii::$app->request->isAjax) {
  168. if (Yii::$app->request->isGet) {
  169. $cond = [['mall_id' => $this->mall_id, 'store_id' => $this->store_id]];
  170. $list = StoreGoodsCate::getCates($cond,[],true,'id,name,parent_id');
  171. $list = StoreGoodsCate::getTreeCate($list);
  172. return $this->success('操作成功',compact('list'));
  173. }
  174. }
  175. }
  176. /**
  177. * @desc:删除商品分类
  178. * @Author: hua
  179. * @Date: 2021/11/4
  180. * @Time: 14:20
  181. * @Copyright: copyright (c) 2021 广东七件事集团
  182. * @return mixed|void
  183. */
  184. public function actionDelete(){
  185. $params = Yii::$app->request->post();
  186. $res = Yii::$app->services->validate->validate($params, [
  187. [['id'], 'integer'],
  188. ]);
  189. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  190. $params['status'] = StatusEnum::DELETE;
  191. $params['mall_id'] = $this->mall_id;
  192. $res = StoreGoodsCateRelate::findOne(['cate_id'=>$params['id']]);
  193. if ($res === false) return $this->error('该分类关联了商品,不能删除!');
  194. $res = StoreGoodsCate::findOne(['parent_id'=>$params['id'],'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]);
  195. if (!empty($res)) return $this->error('该分类还有子分类,不能删除!');
  196. $res = StoreGoodsCate::setData($params);
  197. if ($res === false) return $this->error('删除失败!');
  198. return $this->success('操作成功',[]);
  199. }
  200. public function actionMallTree(){
  201. if (Yii::$app->request->isAjax) {
  202. if (Yii::$app->request->isGet) {
  203. $params = Yii::$app->request->get();
  204. $cond = [['mall_id' =>$params['mall_id']]];
  205. $list = GoodsCate::getCates($cond,[],true,'id,name,parent_id');
  206. $list = GoodsCate::getTreeCate($list);
  207. return $this->success('操作成功',compact('list'));
  208. }
  209. }
  210. }
  211. public function actionMallCateTree(){
  212. if (Yii::$app->request->isAjax) {
  213. if (Yii::$app->request->isGet) {
  214. $params = Yii::$app->request->get();
  215. $cond = [['mall_id' => $this->mall_id]];
  216. $list = GoodsCate::getCates($cond,[],true,'id,name,parent_id');
  217. $list = GoodsCate::getTreeCate($list);
  218. return $this->success('操作成功',compact('list'));
  219. }
  220. }
  221. }
  222. public function actionImportCate()
  223. {
  224. $trans = Yii::$app->db->beginTransaction();
  225. try {
  226. if (!Yii::$app->request->isAjax || !Yii::$app->request->isPost) throw new \Exception("请求不合法");
  227. $cates = Yii::$app->request->post('cates', []);
  228. if (empty($cates)) throw new \Exception("导入分类列表不能为空");
  229. $cate_id_arr = array_unique(array_merge(...$cates));
  230. // 查询分类信息
  231. $mall_cate_list = GoodsCate::lists(['where' => [['mall_id' => $this->mall_id], ['id' => $cate_id_arr]], 'index' => 'id']);
  232. foreach ($cates as $rows) {
  233. $parent_id = 0;
  234. foreach ($rows as $cate_id) {
  235. if (!isset($mall_cate_list[$cate_id])) continue;
  236. $mall_cate = $mall_cate_list[$cate_id];
  237. $model = StoreGoodsCate::insertDataForMallCateID([
  238. 'mall_id' => $this->mall_id,
  239. 'store_id' => $this->store_id,
  240. 'parent_id' => $parent_id,
  241. 'name' => $mall_cate['name'],
  242. 'pic' => $mall_cate['pic'],
  243. 'big_pic' => $mall_cate['big_pic'],
  244. 'advert_pic' => $mall_cate['advert_pic'],
  245. 'advert_url' => $mall_cate['advert_url'],
  246. 'advert_open_type' => $mall_cate['advert_open_type'],
  247. 'advert_params' => $mall_cate['advert_params'],
  248. 'sort' => $mall_cate['sort'],
  249. 'mall_goods_cate_id' => $cate_id
  250. ]);
  251. if (!empty($model)) $parent_id = $model->id;
  252. }
  253. }
  254. $trans->commit();
  255. return $this->success("导入成功");
  256. }catch (\Exception $e){
  257. $trans->rollBack();
  258. return $this->error($e->getMessage());
  259. }
  260. }
  261. public function actionSyncCate()
  262. {
  263. $trans = Yii::$app->db->beginTransaction();
  264. try {
  265. if (!Yii::$app->request->isAjax || !Yii::$app->request->isPost) throw new \Exception("请求不合法");
  266. // 查询导入的分类信息
  267. $cate_list = StoreGoodsCate::lists(['where' => [['mall_id' => $this->mall_id], ['<>', 'mall_goods_cate_id', 0], ['<>', 'status', StatusEnum::DELETE]]]);
  268. // 查询分类信息
  269. $cate_id_arr = array_column($cate_list, 'mall_goods_cate_id');
  270. $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']);
  271. $cate_list = StoreGoodsCate::getTreeCate($cate_list);
  272. foreach ($cate_list as $cate) {
  273. $this->SyncCateTree($cate, $mall_cate_list);
  274. }
  275. $trans->commit();
  276. return $this->success("更新商品分类成功");
  277. }catch (\Exception $e){
  278. $trans->rollBack();
  279. return $this->error($e->getMessage());
  280. }
  281. }
  282. public function SyncCateTree($cate = [], $mall_cate_list = [], $key = 'children')
  283. {
  284. if (empty($cate)) return;
  285. if (isset($mall_cate_list[$cate['mall_goods_cate_id']])){
  286. $mall_cate = $mall_cate_list[$cate['mall_goods_cate_id']];
  287. $elementsToCompare = ['name', 'pic', 'big_pic', 'sort'];
  288. if (array_intersect_key($cate, array_flip($elementsToCompare)) !== array_intersect_key($mall_cate, array_flip($elementsToCompare))) {
  289. // 修改分类信息
  290. StoreGoodsCate::setData([
  291. 'id' => $cate['id'],
  292. 'mall_id' => $this->mall_id,
  293. 'store_id' => $this->store_id,
  294. 'name' => $mall_cate['name'],
  295. 'pic' => $mall_cate['pic'],
  296. 'big_pic' => $mall_cate['big_pic'],
  297. 'sort' => $mall_cate['sort']
  298. ]);
  299. }
  300. // 如果主商城有新增的子分类
  301. $mall_child_arr = StoreGoodsCate::getChildrenCate($mall_cate_list, $cate['mall_goods_cate_id']);
  302. if (!empty($mall_child_arr)) {
  303. $child_id_arr = !empty($cate[$key]) ? array_column($cate[$key], 'mall_goods_cate_id') : [];
  304. $mall_child_id_arr = array_column($mall_child_arr, 'id');
  305. foreach ($mall_child_id_arr as $temp_mall_cate_id) {
  306. if (!in_array($temp_mall_cate_id, $child_id_arr) && isset($mall_cate_list[$temp_mall_cate_id])) {
  307. $temp_mall_cate = $mall_cate_list[$temp_mall_cate_id];
  308. StoreGoodsCate::insertDataForMallCateID([
  309. 'mall_id' => $this->mall_id,
  310. 'store_id' => $this->store_id,
  311. 'parent_id' => $cate['id'],
  312. 'name' => $temp_mall_cate['name'],
  313. 'pic' => $temp_mall_cate['pic'],
  314. 'big_pic' => $temp_mall_cate['big_pic'],
  315. 'advert_pic' => $temp_mall_cate['advert_pic'],
  316. 'advert_url' => $temp_mall_cate['advert_url'],
  317. 'advert_open_type' => $temp_mall_cate['advert_open_type'],
  318. 'advert_params' => $temp_mall_cate['advert_params'],
  319. 'sort' => $temp_mall_cate['sort'],
  320. 'mall_goods_cate_id' => $temp_mall_cate_id
  321. ]);
  322. }
  323. }
  324. }
  325. if (!empty($cate['children'])) {
  326. foreach ($cate['children'] as $child) {
  327. $this->SyncCateTree($child, $mall_cate_list);
  328. }
  329. }
  330. }else {
  331. // 分类不存在则删除门店商品分类及下级分类
  332. $child_cate_id_arr = StoreGoodsCate::getChildCateIDByTree($cate);
  333. $delete_id_arr = array_merge([$cate['id']], $child_cate_id_arr);
  334. StoreGoodsCate::deleteAll(['id' => $delete_id_arr]);
  335. }
  336. }
  337. }