| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- <?php
- namespace addons\Mch\backend\controllers;
- use addons\Mch\common\enums\MchEnum;
- use addons\Mch\common\models\Mch;
- use addons\Mch\common\models\MchGoodsModel;
- use common\enums\AddonsEnum;
- use common\enums\GoodsTypeEnum;
- use common\enums\StatusEnum;
- use common\enums\WhetherEnum;
- use common\globals\GlobalInvoke;
- use common\logic\common\AddonsLimit;
- use common\models\goods\FreightRules;
- use common\models\goods\Goods;
- use common\models\goods\GoodsAttr;
- use common\models\goods\GoodsCate;
- use common\models\goods\GoodsLabel;
- use common\models\goods\GoodsMarketing;
- use common\models\goods\GoodsService;
- use common\models\user\UserLevel;
- use Yii;
- class StoreGoodsController extends BaseController
- {
- public function actionIndex()
- {
- if (!\Yii::$app->request->isAjax) {
- return $this->render('/store-goods/index');
- }
- if (\Yii::$app->request->isGet) {
- //商品列表筛选
- $get = Yii::$app->request->get();
- $rules = [
- [['goods_id'], 'integer'],
- [['goods_name', 'sort_field', 'keyword'], 'string'],
- [['type'], 'in', 'range' => ['check_adopt', 'check_waiting', 'check_failed']],
- [['sort_type'], 'in', 'range' => ['asc', 'desc']],
- [['cats'], 'each', 'rule' => ['integer']],
- [['start', 'end'], 'datetime', 'format' => 'php:Y-m-d H:i:s'],
- ];
- $res = Yii::$app->services->validate->validate($get, $rules);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $where[] = ['g.mall_id' => $this->mall_id, 'g.status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
- $where[] = ['!=', 'g.mch_id', 0];
- // 筛选
- isset($get['start']) && $where[] = ['>=', 'g.created_at', strtotime($get['start'])];
- isset($get['end']) && $where[] = ['<=', 'g.created_at', strtotime($get['end'])];
- isset($get['goods_name']) && $where[] = ['like', 'g.goods_name', '%' . trim($get['goods_name']) . '%', false];
- isset($get['cats']) && $where[] = ['in', 'gcr.cate_id', $get['cats']];
- isset($get['goods_id']) && $where[] = ['=', 'g.id', $get['goods_id']];
- if (isset($get['type'])) {
- switch ($get['type']) {
- case 'check_adopt':
- $where[] = ['g.check_status' => 1];
- break;
- case 'check_waiting':
- $where[] = ['g.check_status' => 0];
- break;
- case 'check_failed':
- $where[] = ['g.check_status' => 2];
- break;
- }
- }
- if (!empty($get['keyword'])) {
- $mch_info = Mch::lists([
- 'where' => [
- ['mall_id' => $this->mall_id],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
- ['or', ['like', 'store_name', $get['keyword']], ['like', 'shop_mobile', $get['keyword']]],
- ],
- ]);
- if (!empty($mch_info)) {
- $mch_ids = array_column($mch_info, 'id');
- $where[] = ['g.mch_id' => $mch_ids];
- } else {
- $where[] = ['g.mch_id' => []];
- }
- }
- //连表
- $joins = array(['LEFT JOIN', '{{%goods_cate_relate}} gcr', 'gcr.goods_id=g.id']);
- // with
- $with = ['cats', 'attr', 'mch'];
- // 排序
- $sort = '';
- if (isset($get['sort_field']) && isset($get['sort_type'])) $sort = 'g.' . $get['sort_field'] . ' ' . $get['sort_type'];
- $sort = !empty($sort) ? $sort . ',g.sort ASC,g.id DESC' : 'g.sort ASC,g.id DESC';
- $params = array('alias' => 'g', 'where' => $where, 'join' => $joins, 'with' => $with, 'limit' => 20, 'order' => $sort, 'group' => 'g.id');
- $page_data = Goods::listPage($params, false, true);
- if ($page_data === false) return $this->error(Goods::getStaticError());
- $page_data["export_list"] = Goods::fieldsList();
- $goods_marketing_arr = [];
- if (!empty($page_data['list'])) {
- $goods_id_arr = array_column($page_data['list'], 'id');
- $mch_ids = array_column($page_data['list'], 'mch_id');
- $goods_marketing_list = GoodsMarketing::lists(['where' => [['mall_id' => $this->mall_id], ['goods_id' => $goods_id_arr], ['status' => StatusEnum::ENABLED]], 'select' => 'goods_id,marketing_type']);
- foreach ($goods_marketing_list as $val) {
- $goods_marketing_arr[$val['goods_id']][] = $val['marketing_type'];
- }
- $stores = Mch::find()
- ->select('id,store_name')
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $mch_ids])
- ->asArray()
- ->indexBy('id')
- ->all();
- foreach ($page_data['list'] as &$item) {
- $addons_name = '';
- $store = $stores[$item['mch_id']] ?? [];
- $item['store_name'] = $store['store_name'] ?? '';
- if (isset($goods_marketing_arr[$item['id']])) {
- foreach ($goods_marketing_arr[$item['id']] as $value) {
- if (is_string(AddonsEnum::getAddonsNameMap($value))) $addons_name .= AddonsEnum::getAddonsNameMap($value) . ',';
- }
- $addons_name = trim($addons_name, ',');
- }
- $item['addons_name'] = $addons_name;
- }
- }
- return $this->success('操作成功', $page_data);
- } else {
- // 商品操作
- $form = Yii::$app->request->post();
- $rules = [[['id'], 'required'], [['id'], 'integer']];
- switch ($form['type']) {
- case 'sort': //排序
- $rules[] = [['sort'], 'required'];
- break;
- case 'destroy': //删除
- $form['status'] = -1;
- $rules[] = [['status'], 'required'];
- break;
- case 'goods_name': //修改商品名称
- $rules[] = [['goods_name'], 'required'];
- break;
- case 'is_on_sale': //上下架
- $rules[] = [['is_on_sale'], 'required'];
- $rules[] = [['is_on_sale'], 'in', 'range' => [WhetherEnum::ENABLED, WhetherEnum::DISABLED]];
- break;
- }
- $res = Yii::$app->services->validate->validate($form, $rules);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $form['mall_id'] = $this->mall['id'];
- $goods_id = $form['id'];
- unset($form['id']);
- $res = Goods::goodsHandle($goods_id, $form);
- if ($res === false) return $this->error(Goods::getStaticError());
- //修改商品的规格
- if ($form['status']) {
- $attr_goods = GoodsAttr::find()->where(['goods_id' => $goods_id])->all();
- foreach ($attr_goods as $model) {
- $model->status = $form['status'];
- $model->updated_at = time();
- $model->update(false);
- }
- }
- return $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']]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $detail = isset($get['id']) ? Goods::getOne([[
- 'id' => $get['id'],
- 'mall_id' => $this->mall_id,
- 'status' => StatusEnum::ENABLED,
- ]], true, ['cats', 'attr', 'extra']) : [];
- $detail['freight_type'] = !empty($detail['freight_type']) ? explode(',', $detail['freight_type']) : [];
- $detail['services'] = !empty($detail['services']) ? explode(',', $detail['services']) : [];
- $detail['labels'] = !empty($detail['labels']) ? explode(',', $detail['labels']) : [];
- $detail['attr_groups'] = json_decode($detail['attr_groups'], true) ?? [];
- $detail['bannar_pic'] = json_decode($detail['bannar_pic'], true) ?? [];
- $detail['attr_groups_format'] = json_decode($detail['attr_groups_format'], true) ?? [];
- $detail['area_limit'] = json_decode($detail['area_limit'], true) ?? [];
- $detail['cats'] = $detail['cats'] ?? [];
- $detail['attr'] = GoodsAttr::formatFormAttr($detail['attr'] ?? [], $detail['attr_groups_format']);
- $detail['goods_weight'] = isset($detail['attr'][0]['weight']) ? $detail['attr'][0]['weight'] : 0;
- if (isset($detail['extra'])) {
- $extra = &$detail['extra'];
- $extra['score_give_setting'] = json_decode($extra['score_give_setting'], true) ?? [];
- $extra['score_deduct_setting'] = json_decode($extra['score_deduct_setting'], true) ?? [];
- $extra['currency_deduct_setting'] = json_decode($extra['currency_deduct_setting'], true) ?? [];
- $extra['member_price_setting'] = json_decode($extra['member_price_setting'], true) ?? [];
- $extra['member_buy_limit_setting'] = json_decode($extra['member_buy_limit_setting'], true) ?? [];
- }
- unset($detail['attr_groups_format']);
- // 用户等级
- $levels = UserLevel::getUserLevel([['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED]], [], true, 'id,level,name');
- $levels = array_merge([['level' => 0, 'name' => '普通用户']], $levels);
- // 运费模板
- // $freight_rules = [];
- // 商品类型
- // 获取商品标签
- $cond = [['mall_id' => $this->mall_id, 'mch_id' => $detail['mch_id']]];
- $freight_rules = FreightRules::getFreightRules($cond, [], true, '*');
- $labels = GoodsLabel::getLabels($cond, [], true, 'id,title,sub_title');
- $services = GoodsService::getServices($cond, [], true, 'id,name');
- $all_freight_rules = [];
- if (!empty($freight_rules)) {
- foreach ($freight_rules as &$item) {
- $item['rule'] = json_decode($item['rule'], true);
- $all_freight_rules[$item['id']] = $item;
- unset($item['rule']);
- }
- }
- return $this->success('操作成功', compact('detail', 'levels', 'freight_rules', 'labels', 'services', 'all_freight_rules'));
- } else {
- // 编辑商品
- $form = json_decode(Yii::$app->request->post('form'), true);
- $form['mall_id'] = $this->mall_id;
- $res = Goods::setData($form);
- if ($res === false) return $this->error(Goods::getStaticError());
- return $this->success('操作成功');
- }
- }
- // 获取组件配置
- $addons_setting = AddonsLimit::getGlobalSetting($this->mall_id);
- $addons_arr = [AddonsEnum::ADDONS_NAME_DISTRIBUTION, AddonsEnum::ADDONS_NAME_AGENT, AddonsEnum::ADDONS_NAME_AREA];
- if(!empty($addons_setting)){
- foreach ($addons_setting as $key => $val) {
- if (!in_array($key, $addons_arr)) unset($addons_setting[$key]);
- }
- }else{
- $addons_setting = [];
- }
- // 公共调用
- $install_addons = GlobalInvoke::exec(GlobalInvoke::GOODS_DETAIL_EDIT, $this->mall_id, [
- 'type' => 'show_mch_component',
- 'mall_id' => $this->mall_id,
- ]);
- foreach ($install_addons as $key => $val) {
- if (!is_array($val)) {
- unset($install_addons[$key]);
- }
- $component = $val['component'] ?? '';
- if ($component != 'com-higo') {
- unset($install_addons[$key]);
- }
- }
- // dd($addons_setting);
- return $this->render('/store-goods/edit', ['addons_settings' => $addons_setting, 'components' => $install_addons, ]);
- }
- /**
- * 获取商品模式
- * @Author: lun
- * @DateTime: 2021/10/26 0026 11:47
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|string|void
- */
- public function actionGoodsModel()
- {
- // 获取组件配置
- $addons_setting = AddonsLimit::getGlobalSetting($this->mall_id);
- $addons_arr = [AddonsEnum::ADDONS_NAME_DISTRIBUTION, AddonsEnum::ADDONS_NAME_AGENT, AddonsEnum::ADDONS_NAME_AREA, AddonsEnum::ADDONS_NAME_BOSS];
- if(!empty($addons_setting)){
- foreach ($addons_setting as $key => $val) {
- if (!in_array($key, $addons_arr)) unset($addons_setting[$key]);
- }
- }else{
- $addons_setting = [];
- }
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- // 商品列表筛选
- $params = \Yii::$app->request->get();
- $res = \Yii::$app->services->validate->validate($params, [[['id'], 'required'], [['id'], 'integer']]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // 获取商品详情
- $detail = Goods::getOne([['id' => $params['id'], 'mall_id' => $this->mall_id]], true, ['attr']);
- $goods_model = MchGoodsModel::getOne([['goods_id' => $params['id'], 'mall_id' => $this->mall_id]], true);
- $setting = [];
- if (!empty($goods_model['content'])) {
- $content = json_decode($goods_model['content'], true);
- foreach ($content as $key => $item) {
- if (in_array($key, $addons_arr)) {
- $setting[$key] = json_decode($item, true);
- }
- }
- }
- // 获取等级
- $level_params = ['where' => [['=', 'mall_id', $this->mall_id], ['=', 'status', StatusEnum::ENABLED]], 'select' => 'level,name'];
- $level_params['mall_id'] = $this->mall_id;
- $level = GlobalInvoke::exec(GlobalInvoke::GOODS_MODEL_LEVEL, $this->mall_id, $level_params);
- $commission_type_map = GoodsTypeEnum::getCommissionTypeMap($this->mall_id);
- unset($commission_type_map['digital']);
-
- return $this->success('操作成功', compact('detail', 'level', 'setting', 'commission_type_map'));
- } else {
- $params = Yii::$app->request->post('form');
- $res = Yii::$app->services->validate->validate($params, [
- [['goods_id', 'mch_id'], 'required'],
- [['goods_id', 'mch_id'], 'integer'],
- [array_keys($addons_setting), 'string']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // dd($params);
- try {
- $param = [
- 'mall_id' => $this->mall_id,
- 'mch_id' => $params['mch_id'],
- 'goods_id' => $params['goods_id'],
- 'content' => json_encode($params),
- ];
- $res = MchGoodsModel::setData($param);
- if ($res === false) return $this->error(MchGoodsModel::getStaticError());
- return $this->success("保存成功");
- } catch (\Exception $e) {
- return $this->error("保存失败,msg=" . $e->getMessage());
- }
- }
- }
- return $this->render('/store-goods/edit', ['addons_settings' => $addons_setting]);
- }
- /**
- * @name:
- * @msg: 商品审核操作
- * @param {*}
- * @return {*}
- */
- public function actionAudit()
- {
- if (!\Yii::$app->request->isPost) {
- return $this->error('请求方式错误');
- }
- $post = \Yii::$app->request->post();
- $rules = [
- [['goods_id', 'type'], 'required'],
- ['goods_id', 'integer'],
- ['type', 'in', 'range' => ['adopt', 'failed']],
- ['check_remark', 'required', 'when' => function ($model) {
- return $model->type == 'failed';
- }],
- ];
- $res = \Yii::$app->services->validate->validate($post, $rules);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $good = Goods::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $post['goods_id']]);
- if (empty($good)) {
- return $this->error('商品不存在');
- }
- if ('adopt' == $post['type']) {
- $check_status = 1;
- } else {
- $check_status = 2;
- }
- $good->check_status = $check_status;
- $good->check_remark = $post['check_remark'];
- $good->updated_at = time();
- if (!$good->save()) {
- return $this->error('操作失败');
- }
- return $this->success('操作成功');
- }
- /**
- * @name:
- * @msg: 获取商品分类树形目录
- * @param {*}
- * @return {*}
- */
- public function actionTree()
- {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isGet) {
- $cond = [['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]];
- $list = GoodsCate::getCates($cond, [], true, 'id,name,parent_id');
- $list = GoodsCate::getTreeCate($list);
- return $this->success('操作成功', compact('list'));
- }
- }
- }
- public function actionMutipleOff()
- {
- if (!\Yii::$app->request->isPost) {
- return $this->error('请求方式错误');
- }
- $good_ids = \Yii::$app->request->post('good_ids');
- if (empty($good_ids) || !is_array($good_ids)) {
- return $this->error('参数错误');
- }
- if (empty($good_ids)) {
- return $this->success('操作成功');
- }
- $res = Goods::updateAll(['is_on_sale' => 0, 'updated_at' => time(), 'check_status' => MchEnum::CHECK_WAITING], ['mall_id' => $this->mall_id, 'id' => $good_ids]);
- if (!$res) {
- return $this->error('操作失败:' . Goods::getStaticError());
- }
- return $this->success('操作成功');
- }
- }
|