| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917 |
- <?php
- namespace addons\Store\client\controllers;
- use addons\Coupon\common\models\AddonsCoupon;
- use addons\Store\common\enums\StoreEnum;
- use addons\Store\common\models\StoreCoupon;
- use addons\Store\common\models\StoreCouponGiveItemSetting;
- use addons\Store\common\models\StoreFreightRules;
- use addons\Store\common\models\StoreGoods;
- use addons\Store\common\models\StoreGoodsAttr;
- use addons\Store\common\models\StoreGoodsCateRelate;
- use addons\Store\common\models\StoreGoodsModel;
- use addons\Store\common\models\StoreSettings;
- use common\components\CsvExport;
- use common\enums\AddonsEnum;
- use common\enums\DownloadEnum;
- use common\enums\GoodsTypeEnum;
- use common\enums\import\GoodsImportEnum;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use common\enums\WhetherEnum;
- use common\globals\GlobalInvoke;
- use common\helpers\csv\CsvExportHelper;
- use common\helpers\TaskHelper;
- use common\logic\common\AddonsLimit;
- use common\models\common\ImportFailLog;
- use common\models\common\ImportLog;
- use common\models\common\Region;
- use common\models\goods\Goods;
- use common\models\goods\GoodsCateRelate;
- use common\models\mall\Mall;
- use common\models\mall\MallAddons;
- use common\models\user\UserLevel;
- use common\models\goods\GoodsAssociation;
- use PhpAmqpLib\Message\AMQPMessage;
- use common\helpers\StringHelper;
- use Yii;
- use addons\Store\common\models\Store;
- use addons\Store\common\services\SyncMallGoodsService;
- class GoodsController extends BaseController
- {
- public $enableCsrfValidation = false;
- /**
- * Renders the index view for the module
- * @return string
- */
- public function actionIndex()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- //商品列表筛选
- $get = Yii::$app->request->get();
- $rules = [
- [['goods_id', 'goods_type'], 'integer'],
- [['goods_name', 'sort_field'], 'string'],
- [['type'], 'in', 'range' => ['all', 'checking', 'onsale', 'offline', 'sell-out', 'stock-lack']],
- [['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.store_id' => $this->store_id, 'g.status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]];
- // 筛选
- 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];
- if(!empty($get['cats'])){
- $goods_cate_relate_list = StoreGoodsCateRelate::lists(['where'=>[['cate_id'=>$get['cats']]]]);
- $goods_ids = array_column($goods_cate_relate_list,'goods_id');
- $where[] = ['in', 'g.id', $goods_ids];
- }
- isset($get['goods_id']) && $where[] = ['=', 'g.id', $get['goods_id']];
- !empty($get['goods_type']) && $where[] = ['=', 'g.goods_type', $get['goods_type']];
- if (isset($get['type'])) {
- switch ($get['type']) {
- case 'checking':
- $where[] = ['=', 'g.check_status', 0];
- break;
- case 'onsale':
- $where[] = ['=', 'g.is_on_sale', WhetherEnum::ENABLED];
- $where[] = ['=', 'g.check_status', 1];
- $where[] = ['>', 'g.stock', 0];
- break;
- case 'offline':
- $where[] = ['=', 'g.is_on_sale', WhetherEnum::DISABLED];
- break;
- case 'sell-out':
- $where[] = ['=', 'g.stock', 0];
- break;
- case 'stock-lack':
- $where[] = new \yii\db\Expression('`stock` <= `stock_warning`');
- break;
- }
- }
- // with
- $with = ['cats', 'attr'];
- // 排序
- $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, 'with' => $with, 'limit' => 20, 'order' => $sort);
- $page_data = StoreGoods::listPage($params, false, true);
- if ($page_data === false) return $this->error(Goods::getStaticError());
- $platform_goods_cate = StoreGoods::getPlatformGoodsCate($this->mall_id);
- $page_data["export_list"] = Goods::fieldsList();
- if (!empty($page_data['list'])) {
- foreach ($page_data['list'] as &$item) {
- $addons_name = '';
- $item['addons_name'] = $addons_name;
- $item['show_sync_goods'] = 0;
- $cate_ids = array_column($item['cats'], 'id') ?? [];
- in_array($platform_goods_cate['id'], $cate_ids) && $item['show_sync_goods'] = 1;
- $item['option_limit'] = 0;
- if ($item['supplier_type'] == StoreEnum::SELF_MALL_WUZHOU) $item['option_limit'] = 1;
- }
- }
- // 是否需要设置支付成功后跳转
- $page_data['pay_success_jump'] = StatusEnum::DISABLED;
- if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_PAY_SUCCESS_JUMP)) {
- $pay_jump_config = \Yii::$app->services->setting->addons->get($this->mall_id,
- AddonsEnum::ADDONS_NAME_PAY_SUCCESS_JUMP, 'basic');
- if (!empty($pay_jump_config['is_enable'])) {
- $page_data['pay_success_jump'] = intval($pay_jump_config['is_enable_jump']);
- }
- }
- // 是否显示同步多商户商品按钮
- $is_sync_mchgoods_2store = isset(Yii::$app->params['is_sync_mchgoods_2store']) ? Yii::$app->params['is_sync_mchgoods_2store'] : false;
- $page_data['synchronize_goods_dialog'] = StatusEnum::DISABLED;
- if ($is_sync_mchgoods_2store && MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_MCH)) {
- $page_data['synchronize_goods_dialog'] = StatusEnum::ENABLED;
- }
- 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 = StoreGoods::goodsHandle($goods_id, $form);
- if ($res === false) return $this->error(StoreGoods::getStaticError());
- //修改商品的规格
- if ($form['status']) {
- $attr_goods = StoreGoodsAttr::find()->where(['goods_id' => $goods_id])->all();
- foreach ($attr_goods as $model) {
- $model->status = $form['status'];
- $model->updated_at = time();
- $model->update(false);
- }
- }
- if ($form['status'] == -1) {
- // 五洲供应链商品删除
- SyncMallGoodsService::deleteGoods($goods_id, $this->mall_id);
- }
- return $this->success();
- }
- }
- return $this->render($this->action->id, ['pay_success_jump_component' =>
- MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_PAY_SUCCESS_JUMP)]);
- }
- /**
- * 商品编辑
- * @Author bing
- * @DateTime 2021-08-20 14:49:55
- * @return mixed
- * @copyright: Copyright (c) 2020 广东七件事集团
- */
- public function actionEdit()
- {
- $is_reserve_type = $this->reserveGoodsType();
- 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']) ? StoreGoods::getOne([['id' => $get['id'], 'mall_id' => $this->mall_id,
- 'store_id' => $this->store_id]], true, ['cats', 'attr', 'extra', 'reserve', 'reserve.skus', 'reserve.skus.times', 'reserve.skus.times.timeSpec']) : [];
- $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'] = !empty($detail['attr_groups']) ? json_decode($detail['attr_groups'], true) : [];
- $detail['bannar_pic'] = !empty($detail['bannar_pic']) ? json_decode($detail['bannar_pic'], true) : [];
- $detail['attr_groups_format'] = !empty($detail['attr_groups_format']) ? json_decode($detail['attr_groups_format'], true) : [];
- $detail['area_limit'] = !empty($detail['area_limit']) ? json_decode($detail['area_limit'], true) : [];
- $detail['cats'] = $detail['cats'] ?? [];
- $detail['attr'] = StoreGoodsAttr::formatFormAttr($detail['attr'] ?? [], $detail['attr_groups_format']);
- $detail['goods_weight'] = isset($detail['attr'][0]['weight']) ? $detail['attr'][0]['weight'] : 0;
- $detail['package_data'] = !empty($detail['package_data']) ? json_decode($detail['package_data'], true) : [];
- $default_goods_style = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'decoration');
- $detail['show_style'] = !empty($detail['show_style']) ? $detail['show_style'] : ($default_goods_style['default_goods_style'] ?? 1);
- if (isset($detail['extra'])) {
- $extra = &$detail['extra'];
- $extra['score_give_setting'] = !empty($extra['score_give_setting']) ? json_decode($extra['score_give_setting'], true) : [];
- $extra['score_deduct_setting'] = !empty($extra['score_deduct_setting']) ? json_decode($extra['score_deduct_setting'], true) : [];
- $extra['currency_deduct_setting'] = !empty($extra['currency_deduct_setting']) ? json_decode($extra['currency_deduct_setting'], true) : [];
- $extra['member_price_setting'] = !empty($extra['member_price_setting']) ? json_decode($extra['member_price_setting'], true) : [];
- $extra['member_buy_limit_setting'] = !empty($extra['member_buy_limit_setting']) ? json_decode($extra['member_buy_limit_setting'], true) : [];
- }
- if (!isset($extra['score_give_setting']['settlement'])) $extra['score_give_setting']['settlement'] = 1;
- if(!isset($extra['score_give_setting']['condition']))$extra['score_give_setting']['condition'] = 0;
- $platform_score_give_setting = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'scoreGive.score_give_setting');
- $extra['platform_score_give_setting'] = !empty($platform_score_give_setting) ? json_decode($platform_score_give_setting, true) : [];
- // 预约商品字段处理
- if (!empty($detail['reserve'])) {
- $reserve = &$detail['reserve'];
- !empty($reserve['reserve_date']) && $reserve['reserve_date'] = json_decode($reserve['reserve_date'], true);
- !empty($reserve['reserve_time_slot']) && $reserve['reserve_time_slot'] = json_decode($reserve['reserve_time_slot'], true);
- !empty($reserve['reserve_time_slot_price']) && $reserve['reserve_time_slot_price'] = json_decode($reserve['reserve_time_slot_price'], true);
- !empty($reserve['service_type']) && $reserve['service_type'] = json_decode($reserve['service_type'], true);
- !empty($reserve['reserve_week']) && $reserve['reserve_week'] = explode(',',$reserve['reserve_week']);
- if (!empty($reserve['skus'])) {
- foreach($reserve['skus'] as &$sku){
- $signId = $sku['sign_id'];
- $sku['attr_list'] = $detail['attr_groups_format'][$signId] ?? [];
- !empty($sku['reserve_date']) && $sku['reserve_date'] = json_decode($sku['reserve_date'], true);
- !empty($sku['reserve_week']) && $sku['reserve_week'] = explode(',',$sku['reserve_week']);
- !empty($sku['reserve_time_slot']) && $sku['reserve_time_slot'] = json_decode($sku['reserve_time_slot'], true);
- !empty($sku['reserve_time_slot_price']) && $sku['reserve_time_slot_price'] = json_decode($sku['reserve_time_slot_price'], true);
- }
- }
- $reserve['cancel_rule'] = $reserve['cancel_rule'] ? json_decode($reserve['cancel_rule'], true) : [];
- $reserve['appointments_number'] = !empty($reserve['appointments_number'])?$reserve['appointments_number']:'';
- }
- // 用户等级
- // 用户等级
- $levels = UserLevel::getUserLevel([['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED]], [], true, 'id,level,name');
- $levels = array_merge([['level' => 0, 'name' => '普通用户']], $levels);
- // 运费模板
- $cond = [['mall_id' => $this->mall_id, 'store_id' => $this->store_id]];
- $freight_rules = StoreFreightRules::getFreightRules($cond, [], true, 'id,name');
- // 商品类型
- $goods_type = GoodsTypeEnum::getGoodsType();
- // 判断是否需要移除预约商品
- if ($is_reserve_type == StatusEnum::DELETE) {
- unset($goods_type[GoodsTypeEnum::GoodsTypeReserved]);
- }
- unset($detail['attr_groups_format']);
- $where = [
- ['mall_id' => $this->mall_id],
- ['store_id' => $this->store_id],
- ['item_id' => $get['id']],
- ['item_type' => 'goods'],
- ['status' => StatusEnum::ENABLED]
- ];
- $store_coupon_item_setting = StoreCouponGiveItemSetting::getOne($where, true, ['detail' => function ($query) {
- $query->where(['status' => StatusEnum::ENABLED]);
- }]);
- $detail['coupon_is_enable'] = $store_coupon_item_setting['is_enable'] ?? 0;
- if (!empty($store_coupon_item_setting['detail'])) {
- $coupon_ids = $store_coupon_ids = [];
- foreach ($store_coupon_item_setting['detail'] as $item) {
- $item['from_type'] == 1 ? $coupon_ids[] = $item['coupon_id'] : $store_coupon_ids[] = $item['coupon_id'];
- }
- $select = 'id,name,type,discount,sub_price';
- $coupon_arr[2] = StoreCoupon::lists(['where' => [['id' => $store_coupon_ids]], 'select' => $select, 'index' => 'id']);
- $is_install = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_COUPON);
- if ($is_install) $coupon_arr[1] = AddonsCoupon::lists(['where' => [['id' => $coupon_ids]], 'select' => $select, 'index' => 'id']);
- foreach ($store_coupon_item_setting['detail'] as &$item) {
- $item['type_text'] = $item['from_type'] == 1 ? '平台优惠券' : '门店优惠券';
- $item['discount_method'] = $item['name'] = $item['total_count'] = '';
- if (!empty($coupon_arr[$item['from_type']])) {
- $coupons = $coupon_arr[$item['from_type']];
- $res = StoreCoupon::formatData($coupons[$item['coupon_id']]);
- $item = array_merge($item, $res);
- }
- }
- }
- $goods_association = GoodsAssociation::getOne([['goods_id'=>$detail['id']], ['store_id' => $this->store_id], ['mall_id' => $this->mall_id],['status'=>StatusEnum::ENABLED]],true);
- $extra1_goods_list = [];
- $extra2_goods_list = [];
- $extra1_goods_id = [];
- $extra2_goods_id = [];
- if($goods_association){
- $extra1_goods_id = json_decode($goods_association['extra1_goods_id'],true);
- $extra2_goods_id = json_decode($goods_association['extra2_goods_id'],true);
- $extra1_goods_list = StoreGoods::lists(['where'=>[['id'=>$extra1_goods_id]],'select'=>'id as goods_id,price,status,stock as goods_stock,goods_name,cover_pic,store_id'],true);
- $extra2_goods_list = StoreGoods::lists(['where'=>[['id'=>$extra2_goods_id]],'select'=>'id as goods_id,price,status,stock as goods_stock,goods_name,cover_pic,store_id'],true);
- }
- $detail['extra1_goods_id'] = $extra1_goods_id;
- $detail['extra2_goods_id'] = $extra2_goods_id;
- $detail['coupon_selection'] = $store_coupon_item_setting['detail'] ?? [];
- $detail['store_id'] = $detail['store_id'] ?? $this->store_id;
- $score_give_is_store_alone = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'scoreGive.score_give_is_store_alone');
- $score_give_is_store_alone = $score_give_is_store_alone ?? 0;
- $storeDetail = Store::getOne([['id' => $this->store_id], ['mall_id' => $this->mall_id]], true);
- $detail['store_freight_type'] = json_decode($storeDetail['freight_type'],true);
- if (!empty($get['id']) && !empty($detail['store_freight_type'])) {
- foreach ($detail['freight_type'] as $key => $itemFreight) { // 因为商品优惠券使用了&$item 所以这里不能在重复使用修改为$itemFreight
- if ($itemFreight == 1 && $detail['store_freight_type']['express_delivery'] == 0) { // 快递发送
- $detail['store_freight_type']['express_delivery'] = 1;
- } else if ($itemFreight == 2 && $detail['store_freight_type']['self_mention'] == 0) { // 自提
- $detail['store_freight_type']['self_mention'] = 1;
- } else if ($itemFreight == 3 && $detail['store_freight_type']['merchant_distribution'] == 0) { // 同城配送
- $detail['store_freight_type']['merchant_distribution'] = 1;
- }
- }
- }
- return $this->success('操作成功', compact('detail', 'levels', 'goods_type', 'freight_rules','extra1_goods_list','extra2_goods_list','score_give_is_store_alone'));
- } else {
- // 编辑商品
- $form = json_decode(Yii::$app->request->post('form'), true);
- $form['mall_id'] = $this->mall_id;
- $form['store_id'] = $this->store_id;
- $form['supplier_type'] = StoreEnum::SUPPLIER_SELF;
- $form['supplier_id'] = $this->store_id;
- $form['supplier_goods_id'] = 0;
- if (empty($form['check_status'])) {
- $form['check_status'] = 0;
- $form['is_on_sale'] = 0;
- } else {
- if ($form['check_status'] == 2) {
- $form['check_status'] = 0;
- }
- }
- if ($form['goods_type'] == 1 && empty($form['freight_type'])) {
- return $this->error('请选择物流方式');
- }
- $basic = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'basic');
- if (empty($basic['goods_auth'])) {
- $form['check_status'] = 1;
- } else {
- $form['check_status'] = 0; // 开启了审核,保存都要重新审核
- }
- $res = StoreGoods::setData($form);
- if ($res === false) {
- return $this->error(StoreGoods::getStaticError());
- }
- return $this->success('操作成功');
- }
- }
- // 公共调用
- $install_addons = GlobalInvoke::exec(GlobalInvoke::GOODS_DETAIL_EDIT, $this->mall_id, [
- 'type' => 'show_store_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]);
- }
- }
- return $this->render($this->action->id, ['components' => $install_addons, 'is_reserve_type' => $is_reserve_type, 'store_id' => $this->store_id]);
- }
- public function actionSearch()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- //商品列表筛选
- $get = Yii::$app->request->get();
- $rules = [
- [['keyword'], 'string'],
- [['store_id', 'goods_id'], 'safe']
- ];
- $res = Yii::$app->services->validate->validate($get, $rules);
- if ($res === false)
- return $this->error(Yii::$app->services->validate->getErrorMessage());
- $where = [['mall_id' => $this->mall_id, 'store_id' => $this->store_id, 'is_on_sale' => 1,'check_status'=>1, 'status' => [StatusEnum::ENABLED]]];
- if (isset($get['goods_id']) && !empty($get['goods_id'])) {
- $where[] = ['!=', 'id', $get['goods_id']];
- }
- isset($get['keyword']) && $where[] = ['or', ['like', 'goods_name', $get['keyword']], ['=', 'id', $get['keyword']]];
- $sort = !empty($sort) ? $sort . ',sort ASC,id DESC' : 'sort ASC,id DESC';
- $params = array('where' => $where, 'limit' => 15, 'order' => $sort, 'group' => 'id');
- $page_data = StoreGoods::listPage($params, false, true);
- if ($page_data === false)
- return $this->error(StoreGoods::getStaticError());
- return $this->success('操作成功', $page_data);
- }
- }
- return $this->render($this->action->id);
- }
- /**
- * @return void
- */
- public function actionImportStoreGoods()
- {
- set_time_limit(0);
- if (\Yii::$app->request->isAjax) {
- $params = \Yii::$app->request->post();
- $res = \Yii::$app->services->validate->validate($params, [
- [['current_num'], 'required'],
- [['file'], 'file', 'extensions' => ['csv']],
- [['file_path'], 'string'],
- [['sync'], 'safe'],
- ]);
- if ($res === false)
- return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- $params['store_id'] = $this->store_id;
- if (!empty($params['sync'])) {
- $res = StoreGoods::importGoodsSync($params);
- } else {
- $res = StoreGoods::importGoods($params);
- }
- if ($res === false)
- return $this->error(StoreGoods::getStaticError());
- if (empty($res['fail_counts']))
- return $this->success('操作成功', $res);
- if (!empty($res['fail_counts']))
- return $this->error('导入失败' . $res['fail_counts'] . '条');
- } else {
- return $this->render($this->action->id);
- }
- }
- /**
- * 商品导出
- * @return void
- */
- public function actionExportExcel()
- {
- if (\Yii::$app->request->isPost) {
- $post = \Yii::$app->request->post();
- $post['mall_id'] = $this->mall_id;
- $post['store_id'] = $this->store_id;
- $filename = '商品列表-' . date('YmdHis') . '_' . rand(10000, 99999);
- $res = CsvExportHelper::exportDownLoadCenter($this->mall_id, $filename, DownloadEnum::TYPE_GOODS, StoreGoods::class, 'exportHandle', $post,0,$this->store_id);
- if ($res === false)
- return $this->error('加入导出任务失败' . CsvExportHelper::getStaticError());
- return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
- }
- }
- /**
- * 预约商品
- * @return int -1两个插件都没有安装,0仅限预约商品,1仅限预约服务,2商品/服务
- */
- protected function reserveGoodsType()
- {
- // 是否有安装家政服务
- $is_install_home_making = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_HOME_MAKING);
- // 是否有安装预约服务
- $is_install_reserve_service = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_RESERVATION_SERVICE);
- $diff = $is_install_home_making <=> $is_install_reserve_service;
- if ($diff < 0) { // 如果没有安装预约服务
- return 1;
- } else if ($diff == 0 && $is_install_home_making == 0) { // 如果两个都没有安装
- return -1;
- } else if ($diff == 0) { // 两个都有安装
- return 2;
- } else { // 如果仅安装了家政服务
- return 0;
- }
- }
- /**
- * 选品中心
- * @Author bing
- * @DateTime 2022-04-26 18:05:56
- * @return void
- * @copyright: Copyright (c) 2022 广东七件事集团
- */
- public function actionSupply()
- {
- return $this->render($this->action->id);
- }
- public function actionGoodsModel()
- {
- $basic = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'basic');
- $addons_setting = AddonsLimit::getGlobalSetting($this->mall_id);
- $addons_arr = [];
- if (!empty($basic['store_agent_status'])) $addons_arr[] = AddonsEnum::ADDONS_NAME_AGENT;
- if (!empty($basic['store_area_status'])) $addons_arr[] = AddonsEnum::ADDONS_NAME_AREA;
- if (!empty($basic['store_distribution_status'])) $addons_arr[] = AddonsEnum::ADDONS_NAME_DISTRIBUTION;
- if(!empty($basic['double_copy_status'])) $addons_arr[] = AddonsEnum::ADDONS_NAME_DOUBLE_COPY;
- if(!empty($basic['store_rebate_status'])) $addons_arr[] = AddonsEnum::ADDONS_NAME_REBATE;
- if ($addons_setting) {
- foreach ($addons_setting as $key => $val) {
- if (!in_array($key, $addons_arr)) unset($addons_setting[$key]);
- }
- } else {
- $addons_setting = [];
- }
- //门店存在的插件
- if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_STORE_BOSS_AGENT)){
- $setting = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_STORE_BOSS_AGENT, 'basic');
- if (!empty($setting['is_enable'])){
- $addons_setting[AddonsEnum::ADDONS_NAME_STORE_BOSS_AGENT] = [
- 'components' => 'com-' . StringHelper::toUnderScore(AddonsEnum::ADDONS_NAME_STORE_BOSS_AGENT),
- 'class' => '',
- ];
- $addons_arr[] = AddonsEnum::ADDONS_NAME_STORE_BOSS_AGENT;
- }
- }
- 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 = StoreGoods::getOne([['id' => $params['id'], 'mall_id' => $this->mall_id, 'store_id' => $this->store_id]], true, ['attr']);
- $goods_model = StoreGoodsModel::getOne([['goods_id' => $params['id'], 'mall_id' => $this->mall_id, 'store_id' => $this->store_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']);
- // 商品支持的插件分佣币种
- // $addons_commission_type = GlobalInvoke::exec(GlobalInvoke::INVOKE_COMMISSION_TYPE, $this->mall_id, ['mall_id' => $this->mall_id]);
- // $addons_commission_type = [];
- // foreach ($addons_commission_type as $type) {
- // $commission_type_map = array_merge($commission_type_map, array_column($type, 'label', 'name'));
- // }
- return $this->success('操作成功', compact('detail', 'level', 'setting', 'commission_type_map'));
- } else {
- $store_setting = StoreSettings::findOne(['store_id'=>$this->store_id,'mall_id'=>$this->mall_id, 'status' => StatusEnum::ENABLED]);
- if(!empty($store_setting)){
- if($store_setting['store_settle_type']!=3 && $store_setting['store_settle_type']!=4){
- if(empty($store_setting['is_allow_editing'])){
- return $this->error('分佣由平台承担,门店无权编辑');
- }
- }
- }else{
- if($basic['settle_type']!=3 && $basic['settle_type']!=4){
- return $this->error('分佣由平台承担,门店无权编辑');
- }
- }
- $params = Yii::$app->request->post('form');
- $res = Yii::$app->services->validate->validate($params, [
- [['goods_id'], 'required'],
- [['goods_id'], 'integer'],
- [array_keys($addons_setting), 'string']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- try {
- $param = [
- 'mall_id' => $this->mall_id,
- 'store_id' => $this->store_id,
- 'goods_id' => $params['goods_id'],
- 'content' => json_encode($params),
- ];
- $res = StoreGoodsModel::setData($param);
- if ($res === false) return $this->error(StoreGoodsModel::getStaticError());
- return $this->success("保存成功");
- } catch (\Exception $e) {
- return $this->error("保存失败,msg=" . $e->getMessage());
- }
- }
- }
- return $this->render($this->action->id, ['addons_setting' => $addons_setting]);
- }
- 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 = StoreGoods::updateAll(
- [
- 'is_on_sale' => 0,
- 'updated_at' => time(),
- 'check_status' => StoreEnum::CHECK_WAITING
- ],
- [
- 'mall_id' => $this->mall_id,
- 'store_id' => $this->store_id,
- 'id' => $good_ids
- ]
- );
- if (!$res) {
- return $this->error('操作失败:' . StoreGoods::getStaticError());
- }
- return $this->success('操作成功');
- }
- public function actionMutipleOn()
- {
- 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 = StoreGoods::updateAll(
- [
- 'is_on_sale' => 1,
- 'updated_at' => time(),
- ],
- [
- 'mall_id' => $this->mall_id,
- 'store_id' => $this->store_id,
- 'id' => $good_ids
- ]
- );
- if (!$res) {
- return $this->error('操作失败:' . StoreGoods::getStaticError());
- }
- return $this->success('操作成功');
- }
- public function actionGetList()
- {
- $where=[];
- $where[]=['=','store_id',$this->store_id];
- $where[]=['=','mall_id',$this->mall_id];
- $where[]=['=','status',1];
- $with = ['detail'];
- $order = "id desc";
- $params = [
- 'select' => '*',
- 'where' => $where,
- 'order' => 'id asc',
- 'page' => 1,
- 'limit' => 10,
- ];
- $goodsLists = StoreGoods::listPage($params);
- return $this->success('返回成功', $goodsLists);
- }
- public function actionIsRecommend()
- {
- if (\Yii::$app->request->isAjax) {
- $params = Yii::$app->request->post();
- $rules = [[['id'], 'required'], [['id'], 'integer']];
- $rules[] = [['is_recommend'], 'required'];
- $rules[] = [['is_recommend'], 'in', 'range' => [0, 1]];
- $res = Yii::$app->services->validate->validate($params, $rules);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- // $params['store_id'] = $this->admin->store_id;
- try {
- $recommend_nums = \Yii::$app->services->setting->addons->get($params['mall_id'], StoreEnum::ADDONS_NAME, 'basic.recommend_nums');
- if (empty($recommend_nums)) throw new \Exception('允许推荐商品数为0');
- if ($params['is_recommend'] == 1) {
- $counts = StoreGoods::find()->where(['store_id' => $params['store_id'], 'mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED, 'is_recommend' => 1])->count();
- if ($counts >= $recommend_nums) throw new \Exception('允许推荐商品数为' . $recommend_nums);
- $goods = StoreGoods::findOne(['id'=>$params['id']]);
- if(empty($goods)) throw new \Exception('商品不存在');
- // if(empty($goods->is_on_sale)) throw new \Exception('商品未上架');
- // if($goods->check_status!=1) throw new \Exception('只有审核通过的商品,才允许设置成推荐商品');
- }
- $goods_id = $params['id'];
- unset($params['id']);
- $res = StoreGoods::goodsHandle($goods_id, $params);
- if ($res === false) throw new \Exception(StoreGoods::getStaticError());
- return $this->success();
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- }
- }
- public function actionSyncGoodsList()
- {
- if (\Yii::$app->request->isAjax && \Yii::$app->request->isGet) {
- $params = Yii::$app->request->get();
- $mall_id = $params['mall_id'];
- $goods_id = $params['goods_id'];
- $name = $params['name'];
- $cats = $params['cats'];
- $where = [
- ['g.is_on_sale' => StatusEnum::ENABLED],
- ['g.status' => StatusEnum::ENABLED],
- ['g.check_status' => StatusEnum::ENABLED],
- ];
- $mall_list = Mall::getEnableMallList();
- $mall_list = array_column($mall_list, null, 'id');
- $mall_ids = array_column($mall_list, 'id') ?? [];
- if(!empty($mall_ids) && $mall_id > 0){
- $mall_ids = $mall_id;
- $where[] = ['g.mall_id' => $mall_ids];
- if(!empty($cats)){
- $goods_cate_relate = GoodsCateRelate::lists([
- 'where' => [
- ['cate_id' => $cats]
- ],
- 'select' => 'goods_id'
- ]);
- $where[] = ['g.id' => array_column($goods_cate_relate, 'goods_id') ?? []];
- }
- }
- if(!empty($goods_id)){
- $search_goods = Goods::find()->where(['or', ['id' => $goods_id], ['like', 'goods_name', $goods_id]])
- ->andWhere(['is_on_sale' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED, 'check_status' => StatusEnum::ENABLED])
- ->select('id')
- ->asArray()
- ->all();
- $where[] = ['g.id' => array_column($search_goods, 'id') ?? []];
- }
- $mch_where = [
- ['mall_id' => $mall_ids], ['status' => StatusEnum::ENABLED], ['check_status' => StatusEnum::ENABLED]
- ];
- /** @var \addons\Mch\common\models\Mch $mch_class */
- $mch_class = \addons\Mch\common\models\Mch::class;
- if(!empty($name)){
- $search_mch_ids = $mch_class::find()->where(['or', ['like', 'shop_owner', $name], ['like', 'store_name', $name]])
- ->select('id')->asArray()->all();
- $mch_where[] = ['id' => array_column($search_mch_ids, 'id') ?? []];
- }
- $mch_list = $mch_class::lists([
- 'where' => $mch_where,
- 'select' => 'id,shop_owner,store_name',
- 'index' => 'id'
- ]);
- $mch_ids = array_column($mch_list, 'id') ?? [];
- $where[] = ['g.mch_id' => $mch_ids];
- $joins = array(['LEFT JOIN', '{{%goods_cate_relate}} gcr', 'gcr.goods_id=g.id']);
- $goods_list = Goods::listPage([
- 'where' => $where,
- 'select' => 'g.id,g.mall_id,g.mch_id,g.goods_name,g.price',
- 'with' => [
- 'cats'
- ],
- 'join' => $joins,
- 'alias' => 'g',
- 'limit' => $params['limit'] ?? 10,
- 'order' => 'g.mall_id DESC'
- ]);
- if($goods_list === false) return $this->error(Goods::getStaticError());
- foreach($goods_list['list'] as &$goods){
- $goods['shop_owner'] = isset($mch_list[$goods['mch_id']]) ? $mch_list[$goods['mch_id']]['shop_owner'] : '-';
- $goods['store_name'] = isset($mch_list[$goods['mch_id']]) ? $mch_list[$goods['mch_id']]['store_name'] : '-';
- $goods['mall_name'] = isset($mall_list[$goods['mall_id']]) ? $mall_list[$goods['mall_id']]['name'] : '-';
- }
- unset($goods);
- return $this->success('请求成功',$goods_list);
- }
- }
- public function actionSyncGoodsStore()
- {
- try{
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['selected_goods_ids'], 'required'],
- [['selected_goods_ids'], 'safe']
- ]);
- if ($res === false) throw new \Exception(Yii::$app->services->validate->getErrorMessage());
- $params['store_id'] = $this->store_id;
- $params['mall_id'] = $this->mall_id;
- TaskHelper::pushTaskQueue(RabbitMqEnum::EXCHANGE_TASK,RabbitMqEnum::TASK_QUEUE,$params,StoreGoods::class,'syncMchGoods',function(AMQPMessage $message){
- // 生产错误
- Yii::error('发送异步任务失败');
- });
- return $this->success('同步成功');
- }catch(\Exception $e){
- return $this->error($e->getMessage());
- }
- }
- public function actionGetMallList()
- {
- if(\Yii::$app->request->isAjax && \Yii::$app->request->isGet){
- $mall_list = Mall::getEnableMallList();
- array_unshift($mall_list, ['id' => '', 'name' => '全部商城']);
- return $this->success('数据请求成功', $mall_list);
- }
- }
- /**
- * 获取地区
- * @return mixed|null
- */
- public function actionRegions()
- {
- $params = Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'integer'],
- [['id'], 'required'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $regions = Region::find()
- ->select('id,name,lng,lat')
- ->asArray()
- ->where(['parent_id' => $params['id']])
- ->indexBy('id')
- ->all();
- return $this->success('获取成功', $regions);
- }
- /**
- * @Description: 根据地区id获取第一层子级地区信息列表
- * @Author: zhngsan
- * @DateTime 2023-02-08 11:22:11
- * @return mixed
- */
- public function actionGetRegions()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $region_id = \Yii::$app->request->get('region_id');
- if (empty($region_id)) {
- return $this->error('参数错误');
- }
- $region = Region::findOne($region_id);
- if (empty($region)) {
- return $this->error('地区信息未找到');
- }
- $region_list = Region::find()
- ->select('id,name,lng,lat')
- ->where(['parent_id' => $region->id])
- ->asArray()
- ->all();
- return $this->success('success', $region_list);
- }
- /**
- * 日志记录
- * @return mixed|null
- */
- public function actionGetGoodsImportLog()
- {
- $params = \Yii::$app->request->get();
- $params['where'] = [
- ['mall_id' => $this->mall_id],
- ['store_id' => $this->store_id],
- ['source' => StoreGoods::class]
- ];
- $params['order'] = 'id desc';
- $data = ImportLog::listPage($params, false, true);
- if (!empty($data['list'])) {
- foreach ($data['list'] as &$v) {
- $v['url'] = '';
- if ($v['fail_counts'])
- $v['url'] = \Yii::$app->urlManager->createUrl(['store/client/goods/export-fail', 'import_log_id' => $v['id']]);
- }
- }
- return $this->success('操作成功', $data);
- }
- /**
- * 导出失败日志
- */
- public function actionExportFail()
- {
- ImportFailLog::exportFailLog(\Yii::$app->request->get());
- }
- /**
- * 会员批量导入模板下载
- */
- public function actionGoodsBatchSendModel()
- {
- $csv = new CsvExport();
- $file_name = '商品批量导入模板-' . date('YmdHis', time());
- $head_list = array_column(GoodsImportEnum::HEAD_LIST, 'field_name');
- return $csv->export([], $head_list, $file_name);
- }
- }
|