StoreGoodsController.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. <?php
  2. namespace addons\Happiness\backend\controllers;
  3. use addons\Happiness\common\enums\HappinessEnum;
  4. use addons\Happiness\common\models\HappinessStore;
  5. use addons\Happiness\common\models\HappinessGoods;
  6. use addons\Store\common\models\StoreGoodsAttr;
  7. use addons\Store\common\models\StoreGoodsCate;
  8. use addons\Store\common\models\StoreGoodsModel;
  9. use addons\Happiness\common\models\HappinessStoreGoods;
  10. use addons\Happiness\common\models\HappinessStoreGoodsAttr;
  11. use addons\Happiness\common\service\GoodsService;
  12. use common\enums\AddonsEnum;
  13. use common\enums\GoodsTypeEnum;
  14. use common\enums\StatusEnum;
  15. use common\enums\WhetherEnum;
  16. use common\globals\GlobalInvoke;
  17. use common\logic\common\AddonsLimit;
  18. use common\models\goods\Goods;
  19. use common\models\goods\GoodsMarketing;
  20. use common\models\mall\MallAddons;
  21. use common\models\goods\GoodsAssociation;
  22. use common\helpers\StringHelper;
  23. use common\models\goods\GoodsCateRelate;
  24. use Exception;
  25. use Yii;
  26. class StoreGoodsController extends BaseController
  27. {
  28. public function actionIndex()
  29. {
  30. if (!\Yii::$app->request->isAjax) {
  31. return $this->render('/store-goods/index');
  32. }
  33. if (\Yii::$app->request->isGet) {
  34. //商品列表筛选
  35. $get = Yii::$app->request->get();
  36. $rules = [
  37. [['goods_id','type'], 'integer'],
  38. [['goods_name', 'sort_field'], 'string'],
  39. [['sort_type'], 'in', 'range' => ['asc', 'desc']],
  40. [['cats'], 'each', 'rule' => ['integer']],
  41. [['start', 'end'], 'datetime', 'format' => 'php:Y-m-d H:i:s']
  42. ];
  43. $res = Yii::$app->services->validate->validate($get, $rules);
  44. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  45. $where[] = ['g.mall_id' => $this->mall_id, 'yg.status' => StatusEnum::ENABLED, 'g.goods_type' => 1];
  46. if($get['type']>0)
  47. {
  48. $where[] = ['yg.label_id' => $get['type']];
  49. }
  50. // 筛选
  51. isset($get['start']) && $where[] = ['>=', 'g.created_at', strtotime($get['start'])];
  52. isset($get['end']) && $where[] = ['<=', 'g.created_at', strtotime($get['end'])];
  53. isset($get['goods_name']) && $where[] = ['like', 'g.goods_name', trim($get['goods_name']) . '%', false];
  54. isset($get['goods_id']) && $where[] = ['=', 'g.id', $get['goods_id']];
  55. if(!empty($get['cats'])){
  56. $goods_cate_relate_list = GoodsCateRelate::lists(['where'=>[['cate_id'=>$get['cats']]]]);
  57. $goods_ids = array_column($goods_cate_relate_list,'goods_id');
  58. $where[] = ['in', 'g.id', $goods_ids];
  59. }
  60. // with
  61. $with = ['cats'];
  62. // 排序
  63. $sort = '';
  64. if (isset($get['sort_field']) && isset($get['sort_type'])) $sort = 'g.' . $get['sort_field'] . ' ' . $get['sort_type'];
  65. $sort = !empty($sort) ? $sort . ',g.sort ASC,g.id DESC' : 'g.sort ASC,g.id DESC';
  66. $params = [
  67. 'alias' => 'yg',
  68. 'where' => $where,
  69. 'with' => $with,
  70. 'limit' => 20,
  71. 'order' => $sort,
  72. 'join' => [['LEFT JOIN', Goods::tableName(). ' as g', "g.id = yg.goods_id"]],
  73. 'select' => 'yg.id,yg.label_id,yg.store_scale,yg.min_num,yg.freight_type,yg.sales_num,yg.warn_day,g.id as goods_id, g.is_on_sale as goods_sale, g.goods_name, g.price, g.cost_price, g.unit, g.cover_pic, yg.is_on_sale, g.is_attr, g.attr_groups, g.attr_groups, g.status, g.stock, g.virtual_sales, g.goods_type, g.services, g.goods_source, g.check_remark, g.sort, g.created_at, g.stock_warning',
  74. ];
  75. $page_data = HappinessGoods::listPage($params);
  76. //获取标签
  77. $goods_label= \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic.goods_label');
  78. $goods_label = empty($goods_label) ? [] : json_decode($goods_label,true);
  79. $page_data['goods_label']=$goods_label;
  80. $goods_label = array_column($goods_label,null,'level');
  81. if ($page_data === false) return $this->error(HappinessGoods::getStaticError());
  82. if (!empty($page_data['list'])) {
  83. foreach ($page_data['list'] as &$item) {
  84. $item['label_name']=$goods_label[$item['label_id']]?$goods_label[$item['label_id']]['name']:'';
  85. $item['freight_type']=isset($item['freight_type'])?json_decode($item['freight_type'],true):[];
  86. }
  87. }
  88. return $this->success('操作成功', $page_data);
  89. } else {
  90. // 商品操作
  91. $form = Yii::$app->request->post();
  92. $rules = [[['id'], 'required'], [['id'], 'integer']];
  93. switch ($form['type']) {
  94. case 'is_on_sale': //上下架
  95. $rule = [
  96. [['is_on_sale'], 'required'],
  97. [['is_on_sale'], 'in', 'range' => [WhetherEnum::ENABLED, WhetherEnum::DISABLED]],
  98. ];
  99. $rules = array_merge($rules, $rule);
  100. break;
  101. }
  102. $res = Yii::$app->services->validate->validate($form, $rules);
  103. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  104. $form['mall_id'] = $this->mall_id;
  105. $res = HappinessGoods::setData($form);
  106. if ($res === false) return $this->error(HappinessGoods::getStaticError());
  107. return $this->success();
  108. }
  109. }
  110. /**
  111. * 商品编辑
  112. * @Author bing
  113. * @DateTime 2021-08-20 14:49:55
  114. * @copyright: Copyright (c) 2020 广东七件事集团
  115. * @return void
  116. */
  117. public function actionEdit()
  118. {
  119. if (\Yii::$app->request->isAjax) {
  120. if (\Yii::$app->request->isGet) {
  121. } else {
  122. // 编辑商品
  123. $form = Yii::$app->request->post();
  124. $form['mall_id'] = $this->mall_id;
  125. if(isset($form['freight_type']))
  126. {
  127. $form['freight_type'] = json_encode($form['freight_type']);
  128. }
  129. $res = HappinessGoods::setData($form);
  130. if ($res === false) return $this->error(HappinessGoods::getStaticError());
  131. return $this->success('操作成功');
  132. }
  133. }
  134. }
  135. /**
  136. * 获取商品模式
  137. * @Author: lun
  138. * @DateTime: 2021/10/26 0026 11:47
  139. * @Copyright: copyright (c) 2021 广东七件事集团
  140. * @return mixed|string|void
  141. */
  142. public function actionGoodsModel()
  143. {
  144. // 获取组件配置
  145. $addons_setting = AddonsLimit::getGlobalSetting($this->mall_id);
  146. $addons_arr = [AddonsEnum::ADDONS_NAME_DISTRIBUTION, AddonsEnum::ADDONS_NAME_AGENT, AddonsEnum::ADDONS_NAME_AREA,AddonsEnum::ADDONS_NAME_DOUBLE_COPY,AddonsEnum::ADDONS_NAME_REBATE];
  147. if (!empty($addons_setting)) {
  148. foreach ($addons_setting as $key => $val) {
  149. if (!in_array($key, $addons_arr)) unset($addons_setting[$key]);
  150. }
  151. }
  152. //门店存在的插件
  153. if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_STORE_BOSS_AGENT)){
  154. $setting = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_STORE_BOSS_AGENT, 'basic');
  155. if (!empty($setting['is_enable'])){
  156. $addons_setting[AddonsEnum::ADDONS_NAME_STORE_BOSS_AGENT] = [
  157. 'components' => 'com-' . StringHelper::toUnderScore(AddonsEnum::ADDONS_NAME_STORE_BOSS_AGENT),
  158. 'class' => '',
  159. ];
  160. $addons_arr[] = AddonsEnum::ADDONS_NAME_STORE_BOSS_AGENT;
  161. }
  162. }
  163. if (\Yii::$app->request->isAjax) {
  164. if (\Yii::$app->request->isGet) {
  165. // 商品列表筛选
  166. $params = \Yii::$app->request->get();
  167. $res = \Yii::$app->services->validate->validate($params, [[['id'], 'required'], [['id'], 'integer']]);
  168. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  169. // 获取商品详情
  170. $detail = HappinessGoods::getOne([['id' => $params['id'], 'mall_id' => $this->mall_id]], true, ['attr']);
  171. $goods_model = StoreGoodsModel::getOne([['goods_id' => $params['id'], 'mall_id' => $this->mall_id]], true);
  172. $setting = [];
  173. if (!empty($goods_model['content'])) {
  174. $content = json_decode($goods_model['content'], true);
  175. foreach ($content as $key => $item) {
  176. if (in_array($key, $addons_arr)) {
  177. $setting[$key] = json_decode($item, true);
  178. }
  179. }
  180. }
  181. // 获取等级
  182. $level_params = ['where' => [['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]], 'select' => 'level,name'];
  183. $level_params['mall_id'] = $this->mall_id;
  184. $level = GlobalInvoke::exec(GlobalInvoke::GOODS_MODEL_LEVEL, $this->mall_id, $level_params);
  185. $commission_type_map = GoodsTypeEnum::getCommissionTypeMap($this->mall_id);
  186. unset($commission_type_map['digital']);
  187. return $this->success('操作成功', compact('detail', 'level', 'setting', 'commission_type_map'));
  188. } else {
  189. $params = Yii::$app->request->post('form');
  190. $res = Yii::$app->services->validate->validate($params, [
  191. [['goods_id', 'store_id'], 'required'],
  192. [['goods_id', 'store_id'], 'integer'],
  193. [array_keys($addons_setting), 'string']
  194. ]);
  195. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  196. // dd($params);
  197. try {
  198. $param = [
  199. 'mall_id' => $this->mall_id,
  200. 'store_id' => $params['store_id'],
  201. 'goods_id' => $params['goods_id'],
  202. 'content' => json_encode($params),
  203. ];
  204. $res = StoreGoodsModel::setData($param);
  205. if ($res === false) return $this->error(StoreGoodsModel::getStaticError());
  206. return $this->success("保存成功");
  207. } catch (\Exception $e) {
  208. return $this->error("保存失败,msg=" . $e->getMessage());
  209. }
  210. }
  211. }
  212. return $this->render('/store-goods/edit', ['addons_settings' => $addons_setting]);
  213. }
  214. /**
  215. * @name:
  216. * @msg: 商品审核操作
  217. * @param {*}
  218. * @return {*}
  219. */
  220. public function actionAudit()
  221. {
  222. if (!\Yii::$app->request->isPost) {
  223. return $this->error('请求方式错误');
  224. }
  225. $post = \Yii::$app->request->post();
  226. $rules = [
  227. [['goods_id', 'type'], 'required'],
  228. ['goods_id', 'integer'],
  229. ['type', 'in', 'range' => ['adopt', 'failed']],
  230. ['check_remark', 'required', 'when' => function ($model) {
  231. return $model->type == 'failed';
  232. }],
  233. ];
  234. $res = \Yii::$app->services->validate->validate($post, $rules);
  235. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  236. $good = HappinessGoods::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $post['goods_id']]);
  237. if (empty($good)) {
  238. return $this->error('商品不存在');
  239. }
  240. if ('adopt' == $post['type']) {
  241. $check_status = 1;
  242. } else {
  243. $check_status = 2;
  244. }
  245. $good->check_status = $check_status;
  246. $good->check_remark = $post['check_remark'];
  247. $good->updated_at = time();
  248. if (!$good->save()) {
  249. return $this->error('操作失败');
  250. }
  251. return $this->success('操作成功');
  252. }
  253. /**
  254. * @name:
  255. * @msg: 获取商品分类树形目录
  256. * @param {*}
  257. * @return {*}
  258. */
  259. public function actionTree()
  260. {
  261. if (Yii::$app->request->isAjax) {
  262. if (Yii::$app->request->isGet) {
  263. $is_add = Yii::$app->request->get('is_add', 0);
  264. $store_id = Yii::$app->request->get('store_id', '');
  265. $cond = [['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]];
  266. if (!empty($store_id)) {
  267. $cond[] = ['store_id' => $store_id];
  268. }
  269. if($is_add) $cond[] = ['>', 'store_id', 0];
  270. $list = StoreGoodsCate::getCates($cond, [], true, 'id,name,parent_id');
  271. $list = StoreGoodsCate::getTreeCate($list);
  272. return $this->success('操作成功', compact('list'));
  273. }
  274. }
  275. }
  276. public function actionMutipleOff()
  277. {
  278. // if (!\Yii::$app->request->isPost) {
  279. // return $this->error('请求方式错误');
  280. // }
  281. // $good_ids = \Yii::$app->request->post('good_ids');
  282. // if (empty($good_ids) || !is_array($good_ids)) {
  283. // return $this->error('参数错误');
  284. // }
  285. //
  286. // if (empty($good_ids)) {
  287. // return $this->success('操作成功');
  288. // }
  289. //
  290. // $res = StoreGoods::updateAll(['is_on_sale' => 0, 'updated_at' => time(), 'check_status' => HappinessEnum::CHECK_WAITING], ['mall_id' => $this->mall_id, 'id' => $good_ids]);
  291. // if (!$res) {
  292. // return $this->error('操作失败:' . StoreGoods::getStaticError());
  293. // }
  294. // return $this->success('操作成功');
  295. if (!\Yii::$app->request->isPost) {
  296. return $this->error('请求方式错误');
  297. }
  298. $good_ids = \Yii::$app->request->post('good_ids');
  299. $type = \Yii::$app->request->post('type');
  300. $where = [];
  301. $info = HappinessGoods::getOne([
  302. ['mall_id' => $this->mall_id],
  303. ['id' => $good_ids],
  304. ], true);
  305. if ($type == 1) {
  306. $where = ['is_on_sale' => 0, 'updated_at' => time()];
  307. } else if ($type == 2) {
  308. $where = ['is_on_sale' => 1, 'updated_at' => time()];
  309. }
  310. if (!empty($info) && $info['is_on_sale'] == $where['is_on_sale']) return $this->error('该商品已是当前状态');
  311. if (empty($good_ids) || !is_array($good_ids) || !$type) {
  312. return $this->error('参数错误');
  313. }
  314. if (empty($good_ids)) {
  315. return $this->success('操作成功');
  316. }
  317. $res = HappinessGoods::updateAll($where, ['mall_id' => $this->mall_id, 'id' => $good_ids]);
  318. if (!$res) {
  319. return $this->error('操作失败:' . HappinessGoods::getStaticError());
  320. }
  321. $res = HappinessStoreGoods::updateAll($where, ['mall_id' => $this->mall_id, 'goods_id' => $info['goods_id']]);
  322. $res = HappinessStoreGoodsAttr::updateAll($where, ['mall_id' => $this->mall_id, 'goods_id' => $info['goods_id']]);
  323. return $this->success('操作成功');
  324. }
  325. public function actionSearch()
  326. {
  327. if (\Yii::$app->request->isAjax) {
  328. if (\Yii::$app->request->isGet) {
  329. //商品列表筛选
  330. $get = Yii::$app->request->get();
  331. $rules = [
  332. [['keyword'], 'string']
  333. ];
  334. $res = Yii::$app->services->validate->validate($get, $rules);
  335. if ($res === false)
  336. return $this->error(Yii::$app->services->validate->getErrorMessage());
  337. $where = [['mall_id' => $this->mall['id'], 'is_on_sale' => 1,'check_status'=>1, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]];
  338. isset($get['keyword']) && $where[] = ['or', ['like', 'goods_name', $get['keyword']], ['=', 'id', $get['keyword']]];
  339. $sort = !empty($sort) ? $sort . ',sort ASC,id DESC' : 'sort ASC,id DESC';
  340. $params = array('where' => $where, 'limit' => 15, 'order' => $sort, 'group' => 'id');
  341. $page_data = HappinessGoods::listPage($params, false, true);
  342. if ($page_data === false)
  343. return $this->error(Goods::getStaticError());
  344. return $this->success('操作成功', $page_data);
  345. }
  346. }
  347. return $this->render($this->action->id);
  348. }
  349. public function actionSearchWithStore()
  350. {
  351. if (\Yii::$app->request->isAjax) {
  352. if (\Yii::$app->request->isGet) {
  353. //商品列表筛选
  354. $get = Yii::$app->request->get();
  355. $rules = [
  356. [['keyword'], 'string']
  357. ];
  358. $res = Yii::$app->services->validate->validate($get, $rules);
  359. if ($res === false)
  360. return $this->error(Yii::$app->services->validate->getErrorMessage());
  361. $where = [['mall_id' => $this->mall['id'], 'is_on_sale' => 1,'check_status'=>1, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]];
  362. isset($get['keyword']) && $where[] = ['or', ['like', 'goods_name', $get['keyword']], ['=', 'id', $get['keyword']]];
  363. $sort = !empty($sort) ? $sort . ',sort ASC,id DESC' : 'sort ASC,id DESC';
  364. $params = array('where' => $where, 'limit' => 15, 'order' => $sort, 'group' => 'id');
  365. $page_data = HappinessGoods::listPage($params, false, true);
  366. if ($page_data === false)
  367. return $this->error(Goods::getStaticError());
  368. if(!empty($page_data['list'])){
  369. $store_ids = array_column($page_data['list'],'store_id');
  370. $store_list = HappinessStore::find()->where(['id'=>$store_ids])->indexBy('id')->asArray()->all();
  371. foreach($page_data['list'] as &$item){
  372. $item['store_name'] = '';
  373. if(!empty($store_list[$item['store_id']])){
  374. $item['store_name'] = $store_list[$item['store_id']]['store_name'];
  375. }
  376. }
  377. }
  378. return $this->success('操作成功', $page_data);
  379. }
  380. }
  381. return $this->render($this->action->id);
  382. }
  383. //获取门店商品
  384. public function actionGetStoreGoods()
  385. {
  386. $request = Yii::$app->request;
  387. if ($request->isAjax && $request->isGet) {
  388. try {
  389. $get = $request->get();
  390. $res = Yii::$app->services->validate->validate($get, [
  391. [['store_id', 'goods_id', 'goods_type'], 'integer'],
  392. [['goods_name', 'start', 'end', 'type'], 'string']
  393. ]);
  394. if (!$res) throw new Exception(Yii::$app->services->validate->getErrorMessage());
  395. $list = (new GoodsService(['mall_id' => $this->mall_id]))->getGoodsList($get);
  396. return $this->success('获取成功', $list);
  397. } catch (\Exception $e) {
  398. return $this->error($e->getMessage());
  399. }
  400. }
  401. }
  402. //门店商品批量下架
  403. public function actionStoreGoodsMutiple()
  404. {
  405. $request = Yii::$app->request;
  406. if ($request->isAjax && $request->isPost) {
  407. try {
  408. $post = $request->post();
  409. $res = Yii::$app->services->validate->validate($post, [
  410. [['type', 'store_id'], 'integer'],
  411. [['good_ids'], 'safe']
  412. ]);
  413. if (!$res) throw new Exception(Yii::$app->services->validate->getErrorMessage());
  414. $res = (new GoodsService(['mall_id' => $this->mall_id]))->storeGoodsMutiple($post);
  415. return $this->success('操作成功');
  416. } catch (\Exception $e) {
  417. return $this->error($e->getMessage());
  418. }
  419. }
  420. }
  421. //查询幸福小店商品
  422. public function actionSearchGoodsData()
  423. {
  424. $request = Yii::$app->request;
  425. try {
  426. if ($request->isAjax && $request->isGet) {
  427. $get = $request->get();
  428. $res = Yii::$app->services->validate->validate($get, [
  429. [['keyword', 'store_id'], 'string'],
  430. ]);
  431. if (!$res) throw new Exception(Yii::$app->services->validate->getErrorMessage());
  432. $info = (new GoodsService(['mall_id' => $this->mall_id, 'store_id' => $get['store_id']]))->searchGoodsData($get);
  433. return $this->success('获取成功', $info);
  434. }
  435. } catch (\Exception $e) {
  436. return $this->error($e->getMessage());
  437. }
  438. }
  439. //添加门店商品
  440. public function actionEditGoods()
  441. {
  442. $request = Yii::$app->request;
  443. if ($request->isAjax && $request->isPost) {
  444. try {
  445. $form = Yii::$app->request->post();
  446. $res = Yii::$app->services->validate->validate($form, [
  447. [['goods_id', 'id', 'store_id'], 'integer'],
  448. [['attr'], 'safe']
  449. ]);
  450. if (!$res) throw new Exception(Yii::$app->services->validate->getErrorMessage());
  451. $res = (new GoodsService(['mall_id' => $this->mall_id, 'store_id' => $form['store_id']]))->addGoods($form);
  452. return $this->success('操作成功');
  453. } catch (\Exception $e) {
  454. return $this->error($e->getMessage());
  455. }
  456. }
  457. }
  458. public function actionDelGoods()
  459. {
  460. $request = Yii::$app->request;
  461. if ($request->isAjax && $request->isPost) {
  462. try {
  463. $form = $request->post();
  464. $rules = [[['id'], 'required']];
  465. $res = Yii::$app->services->validate->validate($form, $rules);
  466. if ($res === false) throw new Exception(Yii::$app->services->validate->getErrorMessage());
  467. $form['mall_id'] = $this->mall_id;
  468. $form['status'] = -1;
  469. $res=HappinessGoods::setData($form);
  470. if (!$res) throw new Exception(HappinessGoods::getStaticError());
  471. return $this->success('操作成功');
  472. } catch (\Exception $e) {
  473. $this->error($e->getMessage());
  474. }
  475. }
  476. }
  477. public function actionGoodsLabel()
  478. {
  479. $request = Yii::$app->request;
  480. if ($request->isGet) {
  481. $label = \Yii::$app->services->setting->addons->get($this->mall_id, HappinessEnum::ADDONS_NAME, 'basic.goods_label');
  482. if(isset($label))
  483. {
  484. $label_list=json_decode($label, true);
  485. $label_list=array_column($label_list, null,'level');
  486. return $this->success('', $label_list);
  487. }
  488. return $this->success('', []);
  489. }
  490. }
  491. public function actionAdd()
  492. {
  493. $request = Yii::$app->request;
  494. if ($request->isAjax && $request->isPost) {
  495. try {
  496. $form = Yii::$app->request->post();
  497. if(!empty($form['goods']))
  498. {
  499. foreach ($form['goods'] as $goods)
  500. {
  501. if(isset($goods['freight_type']))
  502. {
  503. $goods['freight_type'] = json_encode($goods['freight_type']);
  504. }
  505. $goods['mall_id'] = $this->mall_id;
  506. $isHas=HappinessGoods::find()->where(['goods_id'=>$goods['goods_id'],'mall_id'=>$this->mall_id])->one();
  507. if($isHas)
  508. {
  509. $goods['id']=$isHas->id;
  510. }
  511. $res = HappinessGoods::setData($goods);
  512. if ($res === false) throw new Exception(HappinessStoreGoods::getStaticError());
  513. }
  514. return $this->success('操作成功');
  515. }
  516. return $this->error('请选择商品');
  517. } catch (\Exception $e) {
  518. $this->error($e->getMessage());
  519. }
  520. }
  521. return $this->render($this->action->id);
  522. }
  523. public function actionGoodsSearch()
  524. {
  525. if (Yii::$app->request->isAjax) {
  526. if (Yii::$app->request->isGet) {
  527. //商品列表筛选
  528. $get = Yii::$app->request->get();
  529. $rules = [
  530. [['keyword'], 'string'],
  531. [['is_partake_goods', 'goods_id','screen_main_goods','req','reqId','price','sudoku_is_attr'], 'safe'],
  532. [['goods_type'],'safe']
  533. ];
  534. $res = Yii::$app->services->validate->validate($get, $rules);
  535. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  536. $sort = !empty($sort) ? $sort . ',sort ASC,id DESC' : 'sort ASC,id DESC';
  537. $goods_ids=HappinessGoods::find()->select('goods_id')->where(['mall_id'=>$this->mall_id,'status'=>StatusEnum::ENABLED])
  538. ->asArray()
  539. ->column();
  540. $data = Goods::getPaginationList(15, function($query) use ($get,$goods_ids) {
  541. $where = [
  542. 'and',
  543. [
  544. 'mall_id' => $this->mall_id,
  545. 'mch_id' => $this->admin->mch_id,
  546. 'is_on_sale' => 1,
  547. 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED],
  548. ],
  549. ['not in','id',$goods_ids]
  550. ];
  551. isset($get['keyword']) && $where[] = ['or', ['like', 'goods_name', $get['keyword']], ['=', 'id', $get['keyword']]];
  552. if(!empty($get['price'])){
  553. $where[]=['price'=>trim($get['price'])];
  554. }
  555. if (isset($get['sudoku_is_attr'])) { // 九宫格插件关联商品。只显示单规格
  556. $where[] = ['is_attr' => 0];
  557. }
  558. if (isset($get['goods_id']) && !empty($get['goods_id'])) {
  559. $where[] = ['!=', 'id', $get['goods_id']];
  560. }
  561. if (isset($get['goods_type']) && !empty($get['goods_type'])) {
  562. if(is_array($get['goods_type'])){
  563. $where[] = ['in', 'goods_type', $get['goods_type']];
  564. } elseif (is_string($get['goods_type'])) {
  565. $where[] = ['goods_type' => explode(',', $get['goods_type'])];
  566. }else{
  567. $where[] = ['=', 'goods_type', $get['goods_type']];
  568. }
  569. }
  570. $query->where($where);
  571. }, $sort);
  572. return $this->success('操作成功', $data);
  573. }
  574. }
  575. return $this->render($this->action->id);
  576. }
  577. }