| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473 |
- <?php
- namespace addons\Area\backend\controllers;
- use addons\Area\common\enums\AreaEnum;
- use addons\Area\common\models\AreaAgent;
- use addons\Area\common\models\AreaApply;
- use addons\Area\common\models\AreaOrder;
- use addons\Area\common\models\AreaCommissionLog;
- use addons\Area\common\models\AreaRegional;
- use common\enums\StatusEnum;
- use common\enums\UserWalletEnum;
- use common\helpers\DateHelper;
- use common\models\goods\Goods;
- use common\models\user\Town;
- use common\models\user\User;
- use services\common\UserWalletService;
- use Yii;
- use yii\helpers\Json;
- /**
- * 默认控制器
- *
- * Class DefaultController
- * @package addons\Area\backend\controllers
- */
- class DefaultController extends BaseController
- {
- public $enableCsrfValidation = false;
- public function actionIndex()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $params = \Yii::$app->request->get();
- $mall_id = $this->mall_id;
- $where[] = ['mall_id' => $mall_id];
- $where[] = ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
- $user_ids = [];
- if (!empty($params['level'])) $where[] = ['level' => $params['level']];
- if (!empty($params['start'])) $where[] = ['>=', 'created_at', strtotime($params['start'] . ' 00:00:00')];
- if (!empty($params['end'])) $where[] = ['<=', 'created_at', strtotime($params['end'] . ' 23:59:59')];
- if (!empty($params['keyword'])) {
- $is_search = 1;
- $user_list = User::lists(['where' => [['mall_id' => $this->mall_id], [
- 'or',
- ['=', 'id', $params['keyword']],
- ['like', 'nickname', $params['keyword'] . '%', false],
- ['like', 'mobile', $params['keyword'] . '%', false]
- ]], 'select' => 'id']);
- $user_ids = array_column($user_list, 'id');
- }
- if(!empty($params['province'])) {
- $where[] =['IN', 'id', (array)AreaAgent::find()->where(['mall_id' => $mall_id])
- ->andWhere(['OR',['province_id' => $params['province']],['city_id' => $params['province']], ['district_id' => $params['province']]])
- ->select('id')
- ->asArray()
- ->column()];
- }
- if (!empty($is_search)) $where[] = ['user_id' => $user_ids];
- $params['limit'] = 10;
- $params['where'] = $where;
- $params['order'] = 'id DESC';
- $params['select'] = 'id,user_id,realname,mobile,level,address,regional_id,province_id,city_id,district_id,town_id,total_commission,created_at,community_id, is_enable_dividend';
- $params['with'] = ['user' => function ($query) use ($mall_id) {
- $query->where(['mall_id' => $mall_id])->select('id,nickname,avatar_url,mobile,parent_id')->with(['parent']);
- }, 'goods' => function($query) {
- $query->with(['goods']);
- }];
- $pageData = AreaAgent::listPage($params, false);
- if ($pageData === false) return $this->error(AreaAgent::getStaticError());
- $pageData['level_map'] = AreaEnum::getLevelMap(null, $this->mall_id);
- $config = \Yii::$app->services->setting->addons->get($this->mall_id, AreaEnum::ADDONS_NAME, 'basic');
- $pageData['config']['is_enable_only_dividend'] = $config['is_enable_only_dividend'] ?? StatusEnum::DISABLED;
- $pageData['config']['only_dividend_goods_type'] = $config['only_dividend_goods_type'] ?? StatusEnum::DISABLED;
- return $this->success('操作成功', $pageData);
- }
- }
- return $this->render('index', []);
- }
- /**
- * @desc:审核列表
- * @Author: hua
- * @Date: 2022/1/21
- * @Time: 14:28
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|string|void
- */
- public function actionApplyList()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $params = \Yii::$app->request->get();
- $mall_id = $this->mall_id;
- $where[] = ['mall_id' => $mall_id];
- $where[] = ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
- $user_ids = [];
- if (isset($params['check_status']) && $params['check_status'] != -1 && $params['check_status'] !== '') $where[] = ['check_status' => $params['check_status']];
- if (!empty($params['month'])) {
- $where[] = ['>=', 'created_at', strtotime($params['month'])];
- $where[] = ['<=', 'created_at', DateHelper::endOfMonth($params['month'])];
- }
- if (!empty($params['keyword'])) {
- $is_search = 1;
- $user_list = User::lists(['where' => [['mall_id' => $this->mall_id], [
- 'or',
- ['=', 'id', $params['keyword']],
- ['like', 'nickname', $params['keyword']],
- ]], 'select' => 'id']);
- $user_ids = array_column($user_list, 'id');
- }
- if (!empty($is_search)) $where[] = ['user_id' => $user_ids];
- $params['limit'] = 10;
- $params['where'] = $where;
- $params['order'] = 'id DESC';
- $params['with'] = ['user' => function ($query) use ($mall_id) {
- $query->where(['mall_id' => $mall_id])->select('id,nickname,avatar_url,mobile');
- }];
- $pageData = AreaApply::listPage($params, false);
- if ($pageData === false) return $this->error(AreaAgent::getStaticError());
- $pageData['level_map'] = AreaEnum::getLevelMap(null, $this->mall_id);
- $pageData['check_status_map'] = AreaEnum::getCheckStatusMap();
- return $this->success('操作成功', $pageData);
- }
- }
- return $this->render('apply-list', []);
- }
- /**
- * @desc:审核
- * @Author: hua
- * @Date: 2022/1/21
- * @Time: 14:40
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionApply()
- {
- $params = \Yii::$app->request->post();
- $params['mall_id'] = $this->mall_id;
- $res = AreaApply::apply($params);
- if ($res === false) return $this->error(AreaApply::getStaticError());
- return $this->success('操作成功');
- }
- /**
- * @desc:基础配置
- * @Author: hua
- * @Date: 2022/1/21
- * @Time: 14:40
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|string|void
- */
- public function actionSetting()
- {
- if (\Yii::$app->request->isAjax) {
- try {
- if (\Yii::$app->request->isPost) {
- $params = \Yii::$app->request->post();
- $params['mall_id'] = $this->mall_id;
- \Yii::$app->services->setting->addons->set($this->mall_id, AreaEnum::ADDONS_NAME, ['basic' => $params]);
- return $this->success('操作成功');
- }
- if (\Yii::$app->request->isGet) {
- $list = \Yii::$app->services->setting->addons->get($this->mall_id, AreaEnum::ADDONS_NAME, 'basic');
- if(!isset($list['is_show_at_center'])) $list['is_show_at_center'] = 1;
- if(!isset($list['equal_reward_is_equal'])) $list['equal_reward_is_equal'] = 0;
- return $this->success('操作成功', ['list' => $list, 'addons_ext_config' => $this->getAddonsConfig()]);
- }
- }catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- return $this->render('setting', []);
- }
- /**
- * @desc:保存区域管理
- * @Author: hua
- * @Date: 2022/1/19
- * @Time: 17:05
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionEdit()
- {
- try {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $params = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['level'], 'required'],
- [['id', 'user_id', 'level', 'regional_id', 'province_id', 'city_id', 'district_id', 'town_id',
- 'community_id', 'is_enable_dividend'], 'integer'],
- [['realname', 'mobile'], 'string'],
- [['goods'], 'safe'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- $params['status'] = StatusEnum::ENABLED;
- if (!empty($params['goods'])) {
- $params['goods'] = Json::decode($params['goods']);
- } else {
- $params['goods'] = [];
- }
- $configs = \Yii::$app->services->setting->addons->get($params['mall_id'], AreaEnum::ADDONS_NAME, 'basic');
- if ($configs['is_buy_area'] == 1 && $params['level'] == 3) throw new \Exception('已开启区代申请条件,无法进行添加、编辑区代操作');
- $res = AreaAgent::setData($params);
- if ($res === false) return $this->error(AreaAgent::getStaticError());
- return $this->success('操作成功');
- }
- }
- }catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- /**
- * @desc:删除区域代理
- * @Author: hua
- * @Date: 2022/1/21
- * @Time: 9:33
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionDelete()
- {
- try {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $params = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['id'], 'required'],
- [['id'], 'integer']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- //获取当前代理数据
- $check = AreaAgent::getOne([
- ['mall_id' => $this->mall_id],
- ['id' => $params['id']]
- ]);
- $configs = \Yii::$app->services->setting->addons->get($this->mall_id, AreaEnum::ADDONS_NAME, 'basic');
- if ($configs['is_buy_area'] == 1 && $check['level'] == 3) return $this->error('已开启区代申请条件,无法删除区代身份');
- $params['mall_id'] = $this->mall_id;
- $params['status'] = StatusEnum::DELETE;
- $res = AreaAgent::updateAll(['status' => StatusEnum::DELETE], ['id' => $params['id'], 'mall_id' => $this->mall_id]);
- if ($res === false) return $this->error(AreaAgent::getStaticError());
- if ($check['level'] == 3) {
- $order_data = AreaOrder::getOne([
- ['mall_id' => $this->mall_id],
- ['user_id' => $check['user_id']],
- ['area_id' => $check['district_id']],
- ['status' => StatusEnum::ENABLED],
- ['is_pay' => StatusEnum::ENABLED],
- ]);
- //如果删除的区代存在购买数据,则退回购买金额
- if (!empty($order_data)) {
- $insert_wallet[] = [
- 'mall_id' => $order_data->mall_id,
- 'user_id' => $order_data->user_id,
- 'is_frozen' => false,
- 'wallet_type' => 'balance',
- 'money' => $order_data->price, //转入金额
- 'desc' => '代理身份被删除,退回金额',
- 'source_table' => 'addons_area_order',
- 'source_table_id' => $order_data->id,//结算周期id
- 'from_type' => UserWalletEnum::REFUND,
- 'capital_type' => UserWalletEnum::AREA_BONUS_REFUND,
- // 'not_add_total' => true,
- ];
- $order_data->status = StatusEnum::DELETE;
- $order_data->save();
- //维护申请记录数据
- AreaApply::updateAll(['is_refund' => 1, 'refund_time' => time(), 'refund_remark' => '代理身份被删除', 'check_status' => 2], ['mall_id' => $this->mall_id, 'order_id' => $order_data['id']]);
- if (!empty($insert_wallet)) {
- UserWalletService::batchHandleByQueue($insert_wallet);
- }
- }
- }
- return $this->success('操作成功');
- }
- }
- }catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- /**
- * @desc:添加区域代理,搜索用户,不用验证权限
- * @Author: hua
- * @Date: 2022/1/19
- * @Time: 16:09
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionSearchUser()
- {
- if (\Yii::$app->request->isAjax) {
- $keyword = \Yii::$app->request->get('keyword', '');
- $list['list'] = User::searchUser(['keyword' => $keyword, 'mall_id' => $this->mall_id]);
- return $this->success('操作成功', $list);
- }
- return $this->error();
- }
- /**
- * @desc:添加区域代理,获取省市区,不需要验证权限
- * @Author: hua
- * @Date: 2022/1/19
- * @Time: 16:09
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionDistrict()
- {
- $params = \Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($params, [
- [['level'], 'required'],
- [['level'], 'integer'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $list = AreaAgent::getDistrictList($params);
- return $this->success('操作成功', ['district' => $list]);
- }
- /**
- * @desc:添加区域代理,获取镇列表,不用验证权限
- * @Author: hua
- * @Date: 2022/1/19
- * @Time: 16:10
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionGetTownList()
- {
- $params = \Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($params, [
- [['district_id'], 'required'],
- [['district_id'], 'integer'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $district_id = $params['district_id'];
- $list = Town::lists(['where' => [['district_id' => $district_id]], 'select' => 'id,district_id,name']);
- return $this->success('操作成功', ['list' => $list]);
- }
- /**
- * @return mixed|string|null
- */
- public function actionCommission()
- {
- if (Yii::$app->request->isAjax && Yii::$app->request->isGet) {
- $params = \Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($params, [
- [['user_id', 'level', 'status', 'limit', 'page'], 'integer'],
- [['goods_name', 'date_start', 'date_end', 'member'], 'string'],
- ], [
- 'user_id' => '用户ID',
- 'area_level' => '区域代理等级',
- 'status' => '佣金状态',
- 'member' => '会员昵称/手机号',
- 'goods_name' => '商品名称',
- 'date_start' => '开始时间',
- 'date_end' => '结束时间'
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // 查询
- $where = [
- ['mall_id' => $this->mall_id],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
- ];
- // 分佣状态
- if (isset($params['status']) && $params['status'] !== '' && in_array($params['status'], [0, 1])) {
- $where[] = ['commission_status' => $params['status']];
- }
- // 时间区间
- if (!empty($params['date_start']) && !empty($params['date_end'])) {
- $s = strtotime($params['date_start']);
- $e = strtotime($params['date_end']);
- $where[] = ['between', 'created_at', $s, $e];
- } elseif (!empty($params['date_start'])) {
- $s = strtotime($params['date_start']);
- $where[] = ['<=', 'created_at', $s];
- } elseif (!empty($params['date_end'])) {
- $e = strtotime($params['date_end']);
- $where[] = ['>=', 'created_at', $e];
- }
- // 商品
- if (!empty($params['goods_name'])) {
- $gids = Goods::find()->where(['mall_id' => $this->mall_id])
- ->andWhere(['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
- ->andWhere(['like', 'goods_name', $params['goods_name']])
- ->select('id')
- ->column();
- if (!empty($gids)) {
- $where[] = ['goods_id' => $gids];
- } else {
- return $this->success('获取成功');
- }
- }
- $uids = [];
- if (!empty($params['user_id'])) {
- $uids[] = $params['user_id'];
- }
- if (!empty($params['member'])) {
- $s_uids = User::find()->where(['mall_id' => $this->mall_id])
- ->andWhere(['status' => StatusEnum::ENABLED])
- ->andWhere(['or', ['like', 'nickname', $params['member']],
- ['like', 'username', $params['member']],
- ['like', 'mobile', $params['member']]])
- ->select('id')->column();
- if (!empty($s_uids)) {
- array_push($uids, ...$s_uids);
- } else {
- return $this->success('获取成功');
- }
- }
- // 区域代理等级
- if (!empty($params['level'])) {
- // $a_uids = AreaAgent::find()
- // ->where(['mall_id' => $this->mall_id])
- // ->andWhere(['status' => StatusEnum::ENABLED])
- // ->andWhere(['level' => $params['area_level']])
- // ->select('user_id')
- // ->column();
- // if (!empty($a_uids)) array_push($uids, ...$a_uids);
- $where[] = ['area_level' => $params['level']];
- }
- if (!empty($uids)) $where[] = ['user_id' => $uids];
- $ret = AreaCommissionLog::listPage([
- 'where' => $where,
- 'order' => 'id desc',
- 'with' => ['user', 'order', 'goods'],
- 'limit' => $params['limit'] ?? 10
- ]);
- if (!empty(AreaCommissionLog::getStaticError())) return $this->error(AreaCommissionLog::getStaticError());
- $ret['level_map'] = AreaEnum::getLevelMap(null, $this->mall_id);
- return $this->success('获取成功', $ret);
- }
- return $this->render('commission');
- }
- /**
- * @return array|mixed
- */
- protected function getAddonsConfig()
- {
- return Yii::$app->params['addons_ext_config'][AreaEnum::ADDONS_NAME] ?? [];
- }
- public function actionLevelList()
- {
- $ret = AreaEnum::getLevelMap(null, $this->mall_id);
- // if (!empty($ret[0])) unset($ret[0]);
- $this->success('获取成功', $ret);
- }
- }
|