StoreGoodsController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <?php
  2. namespace addons\Mch\backend\controllers;
  3. use addons\Mch\common\enums\MchEnum;
  4. use addons\Mch\common\models\Mch;
  5. use addons\Mch\common\models\MchGoodsModel;
  6. use common\enums\AddonsEnum;
  7. use common\enums\GoodsTypeEnum;
  8. use common\enums\StatusEnum;
  9. use common\enums\WhetherEnum;
  10. use common\globals\GlobalInvoke;
  11. use common\logic\common\AddonsLimit;
  12. use common\models\goods\FreightRules;
  13. use common\models\goods\Goods;
  14. use common\models\goods\GoodsAttr;
  15. use common\models\goods\GoodsCate;
  16. use common\models\goods\GoodsLabel;
  17. use common\models\goods\GoodsMarketing;
  18. use common\models\goods\GoodsService;
  19. use common\models\user\UserLevel;
  20. use Yii;
  21. class StoreGoodsController extends BaseController
  22. {
  23. public function actionIndex()
  24. {
  25. if (!\Yii::$app->request->isAjax) {
  26. return $this->render('/store-goods/index');
  27. }
  28. if (\Yii::$app->request->isGet) {
  29. //商品列表筛选
  30. $get = Yii::$app->request->get();
  31. $rules = [
  32. [['goods_id'], 'integer'],
  33. [['goods_name', 'sort_field', 'keyword'], 'string'],
  34. [['type'], 'in', 'range' => ['check_adopt', 'check_waiting', 'check_failed']],
  35. [['sort_type'], 'in', 'range' => ['asc', 'desc']],
  36. [['cats'], 'each', 'rule' => ['integer']],
  37. [['start', 'end'], 'datetime', 'format' => 'php:Y-m-d H:i:s'],
  38. ];
  39. $res = Yii::$app->services->validate->validate($get, $rules);
  40. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  41. $where[] = ['g.mall_id' => $this->mall_id, 'g.status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  42. $where[] = ['!=', 'g.mch_id', 0];
  43. // 筛选
  44. isset($get['start']) && $where[] = ['>=', 'g.created_at', strtotime($get['start'])];
  45. isset($get['end']) && $where[] = ['<=', 'g.created_at', strtotime($get['end'])];
  46. isset($get['goods_name']) && $where[] = ['like', 'g.goods_name', '%' . trim($get['goods_name']) . '%', false];
  47. isset($get['cats']) && $where[] = ['in', 'gcr.cate_id', $get['cats']];
  48. isset($get['goods_id']) && $where[] = ['=', 'g.id', $get['goods_id']];
  49. if (isset($get['type'])) {
  50. switch ($get['type']) {
  51. case 'check_adopt':
  52. $where[] = ['g.check_status' => 1];
  53. break;
  54. case 'check_waiting':
  55. $where[] = ['g.check_status' => 0];
  56. break;
  57. case 'check_failed':
  58. $where[] = ['g.check_status' => 2];
  59. break;
  60. }
  61. }
  62. if (!empty($get['keyword'])) {
  63. $mch_info = Mch::lists([
  64. 'where' => [
  65. ['mall_id' => $this->mall_id],
  66. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
  67. ['or', ['like', 'store_name', $get['keyword']], ['like', 'shop_mobile', $get['keyword']]],
  68. ],
  69. ]);
  70. if (!empty($mch_info)) {
  71. $mch_ids = array_column($mch_info, 'id');
  72. $where[] = ['g.mch_id' => $mch_ids];
  73. } else {
  74. $where[] = ['g.mch_id' => []];
  75. }
  76. }
  77. //连表
  78. $joins = array(['LEFT JOIN', '{{%goods_cate_relate}} gcr', 'gcr.goods_id=g.id']);
  79. // with
  80. $with = ['cats', 'attr', 'mch'];
  81. // 排序
  82. $sort = '';
  83. if (isset($get['sort_field']) && isset($get['sort_type'])) $sort = 'g.' . $get['sort_field'] . ' ' . $get['sort_type'];
  84. $sort = !empty($sort) ? $sort . ',g.sort ASC,g.id DESC' : 'g.sort ASC,g.id DESC';
  85. $params = array('alias' => 'g', 'where' => $where, 'join' => $joins, 'with' => $with, 'limit' => 20, 'order' => $sort, 'group' => 'g.id');
  86. $page_data = Goods::listPage($params, false, true);
  87. if ($page_data === false) return $this->error(Goods::getStaticError());
  88. $page_data["export_list"] = Goods::fieldsList();
  89. $goods_marketing_arr = [];
  90. if (!empty($page_data['list'])) {
  91. $goods_id_arr = array_column($page_data['list'], 'id');
  92. $mch_ids = array_column($page_data['list'], 'mch_id');
  93. $goods_marketing_list = GoodsMarketing::lists(['where' => [['mall_id' => $this->mall_id], ['goods_id' => $goods_id_arr], ['status' => StatusEnum::ENABLED]], 'select' => 'goods_id,marketing_type']);
  94. foreach ($goods_marketing_list as $val) {
  95. $goods_marketing_arr[$val['goods_id']][] = $val['marketing_type'];
  96. }
  97. $stores = Mch::find()
  98. ->select('id,store_name')
  99. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $mch_ids])
  100. ->asArray()
  101. ->indexBy('id')
  102. ->all();
  103. foreach ($page_data['list'] as &$item) {
  104. $addons_name = '';
  105. $store = $stores[$item['mch_id']] ?? [];
  106. $item['store_name'] = $store['store_name'] ?? '';
  107. if (isset($goods_marketing_arr[$item['id']])) {
  108. foreach ($goods_marketing_arr[$item['id']] as $value) {
  109. if (is_string(AddonsEnum::getAddonsNameMap($value))) $addons_name .= AddonsEnum::getAddonsNameMap($value) . ',';
  110. }
  111. $addons_name = trim($addons_name, ',');
  112. }
  113. $item['addons_name'] = $addons_name;
  114. }
  115. }
  116. return $this->success('操作成功', $page_data);
  117. } else {
  118. // 商品操作
  119. $form = Yii::$app->request->post();
  120. $rules = [[['id'], 'required'], [['id'], 'integer']];
  121. switch ($form['type']) {
  122. case 'sort': //排序
  123. $rules[] = [['sort'], 'required'];
  124. break;
  125. case 'destroy': //删除
  126. $form['status'] = -1;
  127. $rules[] = [['status'], 'required'];
  128. break;
  129. case 'goods_name': //修改商品名称
  130. $rules[] = [['goods_name'], 'required'];
  131. break;
  132. case 'is_on_sale': //上下架
  133. $rules[] = [['is_on_sale'], 'required'];
  134. $rules[] = [['is_on_sale'], 'in', 'range' => [WhetherEnum::ENABLED, WhetherEnum::DISABLED]];
  135. break;
  136. }
  137. $res = Yii::$app->services->validate->validate($form, $rules);
  138. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  139. $form['mall_id'] = $this->mall['id'];
  140. $goods_id = $form['id'];
  141. unset($form['id']);
  142. $res = Goods::goodsHandle($goods_id, $form);
  143. if ($res === false) return $this->error(Goods::getStaticError());
  144. //修改商品的规格
  145. if ($form['status']) {
  146. $attr_goods = GoodsAttr::find()->where(['goods_id' => $goods_id])->all();
  147. foreach ($attr_goods as $model) {
  148. $model->status = $form['status'];
  149. $model->updated_at = time();
  150. $model->update(false);
  151. }
  152. }
  153. return $this->success();
  154. }
  155. }
  156. /**
  157. * 商品编辑
  158. * @Author bing
  159. * @DateTime 2021-08-20 14:49:55
  160. * @copyright: Copyright (c) 2020 广东七件事集团
  161. * @return void
  162. */
  163. public function actionEdit()
  164. {
  165. if (\Yii::$app->request->isAjax) {
  166. if (\Yii::$app->request->isGet) {
  167. // 反显商品信息
  168. $get = \Yii::$app->request->get();
  169. $res = Yii::$app->services->validate->validate($get, [[['id'], 'integer']]);
  170. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  171. $detail = isset($get['id']) ? Goods::getOne([[
  172. 'id' => $get['id'],
  173. 'mall_id' => $this->mall_id,
  174. 'status' => StatusEnum::ENABLED,
  175. ]], true, ['cats', 'attr', 'extra']) : [];
  176. $detail['freight_type'] = !empty($detail['freight_type']) ? explode(',', $detail['freight_type']) : [];
  177. $detail['services'] = !empty($detail['services']) ? explode(',', $detail['services']) : [];
  178. $detail['labels'] = !empty($detail['labels']) ? explode(',', $detail['labels']) : [];
  179. $detail['attr_groups'] = json_decode($detail['attr_groups'], true) ?? [];
  180. $detail['bannar_pic'] = json_decode($detail['bannar_pic'], true) ?? [];
  181. $detail['attr_groups_format'] = json_decode($detail['attr_groups_format'], true) ?? [];
  182. $detail['area_limit'] = json_decode($detail['area_limit'], true) ?? [];
  183. $detail['cats'] = $detail['cats'] ?? [];
  184. $detail['attr'] = GoodsAttr::formatFormAttr($detail['attr'] ?? [], $detail['attr_groups_format']);
  185. $detail['goods_weight'] = isset($detail['attr'][0]['weight']) ? $detail['attr'][0]['weight'] : 0;
  186. if (isset($detail['extra'])) {
  187. $extra = &$detail['extra'];
  188. $extra['score_give_setting'] = json_decode($extra['score_give_setting'], true) ?? [];
  189. $extra['score_deduct_setting'] = json_decode($extra['score_deduct_setting'], true) ?? [];
  190. $extra['currency_deduct_setting'] = json_decode($extra['currency_deduct_setting'], true) ?? [];
  191. $extra['member_price_setting'] = json_decode($extra['member_price_setting'], true) ?? [];
  192. $extra['member_buy_limit_setting'] = json_decode($extra['member_buy_limit_setting'], true) ?? [];
  193. }
  194. unset($detail['attr_groups_format']);
  195. // 用户等级
  196. $levels = UserLevel::getUserLevel([['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED]], [], true, 'id,level,name');
  197. $levels = array_merge([['level' => 0, 'name' => '普通用户']], $levels);
  198. // 运费模板
  199. // $freight_rules = [];
  200. // 商品类型
  201. // 获取商品标签
  202. $cond = [['mall_id' => $this->mall_id, 'mch_id' => $detail['mch_id']]];
  203. $freight_rules = FreightRules::getFreightRules($cond, [], true, '*');
  204. $labels = GoodsLabel::getLabels($cond, [], true, 'id,title,sub_title');
  205. $services = GoodsService::getServices($cond, [], true, 'id,name');
  206. $all_freight_rules = [];
  207. if (!empty($freight_rules)) {
  208. foreach ($freight_rules as &$item) {
  209. $item['rule'] = json_decode($item['rule'], true);
  210. $all_freight_rules[$item['id']] = $item;
  211. unset($item['rule']);
  212. }
  213. }
  214. return $this->success('操作成功', compact('detail', 'levels', 'freight_rules', 'labels', 'services', 'all_freight_rules'));
  215. } else {
  216. // 编辑商品
  217. $form = json_decode(Yii::$app->request->post('form'), true);
  218. $form['mall_id'] = $this->mall_id;
  219. $res = Goods::setData($form);
  220. if ($res === false) return $this->error(Goods::getStaticError());
  221. return $this->success('操作成功');
  222. }
  223. }
  224. // 获取组件配置
  225. $addons_setting = AddonsLimit::getGlobalSetting($this->mall_id);
  226. $addons_arr = [AddonsEnum::ADDONS_NAME_DISTRIBUTION, AddonsEnum::ADDONS_NAME_AGENT, AddonsEnum::ADDONS_NAME_AREA];
  227. if(!empty($addons_setting)){
  228. foreach ($addons_setting as $key => $val) {
  229. if (!in_array($key, $addons_arr)) unset($addons_setting[$key]);
  230. }
  231. }else{
  232. $addons_setting = [];
  233. }
  234. // 公共调用
  235. $install_addons = GlobalInvoke::exec(GlobalInvoke::GOODS_DETAIL_EDIT, $this->mall_id, [
  236. 'type' => 'show_mch_component',
  237. 'mall_id' => $this->mall_id,
  238. ]);
  239. foreach ($install_addons as $key => $val) {
  240. if (!is_array($val)) {
  241. unset($install_addons[$key]);
  242. }
  243. $component = $val['component'] ?? '';
  244. if ($component != 'com-higo') {
  245. unset($install_addons[$key]);
  246. }
  247. }
  248. // dd($addons_setting);
  249. return $this->render('/store-goods/edit', ['addons_settings' => $addons_setting, 'components' => $install_addons, ]);
  250. }
  251. /**
  252. * 获取商品模式
  253. * @Author: lun
  254. * @DateTime: 2021/10/26 0026 11:47
  255. * @Copyright: copyright (c) 2021 广东七件事集团
  256. * @return mixed|string|void
  257. */
  258. public function actionGoodsModel()
  259. {
  260. // 获取组件配置
  261. $addons_setting = AddonsLimit::getGlobalSetting($this->mall_id);
  262. $addons_arr = [AddonsEnum::ADDONS_NAME_DISTRIBUTION, AddonsEnum::ADDONS_NAME_AGENT, AddonsEnum::ADDONS_NAME_AREA, AddonsEnum::ADDONS_NAME_BOSS];
  263. if(!empty($addons_setting)){
  264. foreach ($addons_setting as $key => $val) {
  265. if (!in_array($key, $addons_arr)) unset($addons_setting[$key]);
  266. }
  267. }else{
  268. $addons_setting = [];
  269. }
  270. if (\Yii::$app->request->isAjax) {
  271. if (\Yii::$app->request->isGet) {
  272. // 商品列表筛选
  273. $params = \Yii::$app->request->get();
  274. $res = \Yii::$app->services->validate->validate($params, [[['id'], 'required'], [['id'], 'integer']]);
  275. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  276. // 获取商品详情
  277. $detail = Goods::getOne([['id' => $params['id'], 'mall_id' => $this->mall_id]], true, ['attr']);
  278. $goods_model = MchGoodsModel::getOne([['goods_id' => $params['id'], 'mall_id' => $this->mall_id]], true);
  279. $setting = [];
  280. if (!empty($goods_model['content'])) {
  281. $content = json_decode($goods_model['content'], true);
  282. foreach ($content as $key => $item) {
  283. if (in_array($key, $addons_arr)) {
  284. $setting[$key] = json_decode($item, true);
  285. }
  286. }
  287. }
  288. // 获取等级
  289. $level_params = ['where' => [['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]], 'select' => 'level,name'];
  290. $level_params['mall_id'] = $this->mall_id;
  291. $level = GlobalInvoke::exec(GlobalInvoke::GOODS_MODEL_LEVEL, $this->mall_id, $level_params);
  292. $commission_type_map = GoodsTypeEnum::getCommissionTypeMap($this->mall_id);
  293. unset($commission_type_map['digital']);
  294. return $this->success('操作成功', compact('detail', 'level', 'setting', 'commission_type_map'));
  295. } else {
  296. $params = Yii::$app->request->post('form');
  297. $res = Yii::$app->services->validate->validate($params, [
  298. [['goods_id', 'mch_id'], 'required'],
  299. [['goods_id', 'mch_id'], 'integer'],
  300. [array_keys($addons_setting), 'string']
  301. ]);
  302. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  303. // dd($params);
  304. try {
  305. $param = [
  306. 'mall_id' => $this->mall_id,
  307. 'mch_id' => $params['mch_id'],
  308. 'goods_id' => $params['goods_id'],
  309. 'content' => json_encode($params),
  310. ];
  311. $res = MchGoodsModel::setData($param);
  312. if ($res === false) return $this->error(MchGoodsModel::getStaticError());
  313. return $this->success("保存成功");
  314. } catch (\Exception $e) {
  315. return $this->error("保存失败,msg=" . $e->getMessage());
  316. }
  317. }
  318. }
  319. return $this->render('/store-goods/edit', ['addons_settings' => $addons_setting]);
  320. }
  321. /**
  322. * @name:
  323. * @msg: 商品审核操作
  324. * @param {*}
  325. * @return {*}
  326. */
  327. public function actionAudit()
  328. {
  329. if (!\Yii::$app->request->isPost) {
  330. return $this->error('请求方式错误');
  331. }
  332. $post = \Yii::$app->request->post();
  333. $rules = [
  334. [['goods_id', 'type'], 'required'],
  335. ['goods_id', 'integer'],
  336. ['type', 'in', 'range' => ['adopt', 'failed']],
  337. ['check_remark', 'required', 'when' => function ($model) {
  338. return $model->type == 'failed';
  339. }],
  340. ];
  341. $res = \Yii::$app->services->validate->validate($post, $rules);
  342. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  343. $good = Goods::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $post['goods_id']]);
  344. if (empty($good)) {
  345. return $this->error('商品不存在');
  346. }
  347. if ('adopt' == $post['type']) {
  348. $check_status = 1;
  349. } else {
  350. $check_status = 2;
  351. }
  352. $good->check_status = $check_status;
  353. $good->check_remark = $post['check_remark'];
  354. $good->updated_at = time();
  355. if (!$good->save()) {
  356. return $this->error('操作失败');
  357. }
  358. return $this->success('操作成功');
  359. }
  360. /**
  361. * @name:
  362. * @msg: 获取商品分类树形目录
  363. * @param {*}
  364. * @return {*}
  365. */
  366. public function actionTree()
  367. {
  368. if (Yii::$app->request->isAjax) {
  369. if (Yii::$app->request->isGet) {
  370. $cond = [['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]];
  371. $list = GoodsCate::getCates($cond, [], true, 'id,name,parent_id');
  372. $list = GoodsCate::getTreeCate($list);
  373. return $this->success('操作成功', compact('list'));
  374. }
  375. }
  376. }
  377. public function actionMutipleOff()
  378. {
  379. if (!\Yii::$app->request->isPost) {
  380. return $this->error('请求方式错误');
  381. }
  382. $good_ids = \Yii::$app->request->post('good_ids');
  383. if (empty($good_ids) || !is_array($good_ids)) {
  384. return $this->error('参数错误');
  385. }
  386. if (empty($good_ids)) {
  387. return $this->success('操作成功');
  388. }
  389. $res = Goods::updateAll(['is_on_sale' => 0, 'updated_at' => time(), 'check_status' => MchEnum::CHECK_WAITING], ['mall_id' => $this->mall_id, 'id' => $good_ids]);
  390. if (!$res) {
  391. return $this->error('操作失败:' . Goods::getStaticError());
  392. }
  393. return $this->success('操作成功');
  394. }
  395. }