| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- <?php
- namespace addons\CloudStockTwo\backend\controllers;
- use addons\Agent\common\enums\AgentEnum;
- use addons\Agent\common\models\AgentLevel;
- use addons\CloudStockTwo\common\models\CloudStockTwoAgent;
- use addons\CloudStockTwo\common\models\CloudStockTwoAgentQuota;
- use addons\CloudStockTwo\common\models\CloudStockTwoLevel;
- use addons\CloudStockTwo\common\models\CloudStockTwoSms;
- use common\enums\AddonsEnum;
- use common\enums\StatusEnum;
- use common\models\goods\Goods;
- use common\models\user\User;
- use Yii;
- use common\controllers\AddonsController;
- /**
- * 默认控制器
- *
- * Class DefaultController
- * @package addons\CloudStockTwo\backend\controllers
- */
- class LevelController extends BaseController
- {
- public $enableCsrfValidation = false;
- public function actionIndex()
- {
- if (\Yii::$app->request->isAjax) {
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['in', 'status', [StatusEnum::DISABLED, StatusEnum::ENABLED]];
- $params = [
- 'where' => $where, 'order' => 'level desc'
- ];
- $page_data = CloudStockTwoLevel::listPage($params, false);
- if (!empty($page_data['list'])) {
- foreach ($page_data['list'] as &$item) {
- $item['give_quota'] = json_decode($item['give_quota'], true);
- $item['give_quota_count'] = count($item['give_quota']) > 0 ? 1 : 0;
- }
- }
- return $this->success('操作成功', $page_data);
- }
- return $this->render('index', [
- ]);
- }
- public function actionEdit()
- {
- try {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isGet) {
- $id = Yii::$app->request->get('id', '');
- $setting = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK_TWO, 'basic');
- $data = CloudStockTwoLevel::getOne([['id' => $id], ['mall_id' => $this->mall_id], ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]]], true);
- $configs['setting'] = $setting;
- if (!empty($data)) {
- $level_list = CloudStockTwoLevel::lists(['where'=>[['mall_id'=>$this->mall_id],['status'=>StatusEnum::ENABLED]],'select'=>'level,name','index'=>'level']);
- $data['checked_condition_values'] = json_decode($data['checked_condition_values'], true);
- $data['checked_condition_keys'] = json_decode($data['checked_condition_keys'], true);
- $data['agent_setting_info'] = json_decode($data['agent_setting_info'],true) ?? [];
- $data['commission'] = json_decode($data['commission'], true);
- if(!$data['commission']){
- $data['commission'] = [
- 'is_percent_team'=>0,
- 'is_percent_equal'=>0,
- 'agent_team_prize'=>0,
- 'agent_equal_prize'=>0,
- 'agent_over_prize'=>0,
- ];
- }
- $data['score'] = json_decode($data['score'], true);
- if(!$data['score']){
- $data['score'] = [
- 'is_percent_team'=>0,
- 'is_percent_equal'=>0,
- 'agent_team_prize'=>0,
- 'agent_equal_prize'=>0,
- 'agent_over_prize'=>0,
- ];
- }
- $data['give_quota'] = json_decode($data['give_quota'], true);
- if(!empty($data['give_quota'] )&&is_array($data['give_quota'] )){
- foreach ($data['give_quota'] as $key=>&$item){
- if(isset($level_list[$item['level']])&&$data['level']>$item['level']){
- $item['name'] = $level_list[$item['level']]['name'];
- $item['is_use'] = 1;
- }else{
- $item['is_use'] = 0;
- }
- }
- }
- if(!empty($data['checked_condition_values'])){
- if(isset($data['checked_condition_values'][0]['value'])){
- $goods_ids = array_column($data['checked_condition_values'][0]['value'],'goods_id');
- $goods_list = Goods::lists(['where'=>[['id'=>$goods_ids]],'select'=>'id,cover_pic,goods_name,price,stock','index'=>'id']);
- foreach ($data['checked_condition_values'][0]['value'] as&$item){
- if(isset($goods_list[$item['goods_id']])){
- $item['cover_pic'] = $goods_list[$item['goods_id']]['cover_pic'];
- $item['goods_name'] = $goods_list[$item['goods_id']]['goods_name'];
- $item['price'] = $goods_list[$item['goods_id']]['price'];
- $item['stock'] = $goods_list[$item['goods_id']]['stock'];
- }
- }
- }
- if(isset($data['checked_condition_values'][1]['value'])){
- $level_arr = array_column($data['checked_condition_values'][1]['value'],'level');
- $level_list = CloudStockTwoLevel::lists(['where'=>[['mall_id'=>$this->mall_id],['level'=>$level_arr],['status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]],'index'=>'level']);
- foreach ($data['checked_condition_values'][1]['value'] as &$val){
- if(isset($level_list[$val['level']])){
- $val['name'] = $level_list[$val['level']]['name'];
- }
- }
- }
- if(isset($data['checked_condition_values'][2]['value'])){
- $level_arr = array_column($data['checked_condition_values'][2]['value'],'level');
- $level_list = CloudStockTwoLevel::lists(['where'=>[['mall_id'=>$this->mall_id],['level'=>$level_arr],['status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]],'index'=>'level']);
- foreach ($data['checked_condition_values'][2]['value'] as &$val){
- if(isset($level_list[$val['level']])){
- $val['name'] = $level_list[$val['level']]['name'];
- }
- }
- }
- if(isset($data['checked_condition_values'][4]['value'])){
- $level_arr = array_column($data['checked_condition_values'][4]['value'],'level');
- $level_list = CloudStockTwoLevel::lists(['where'=>[['mall_id'=>$this->mall_id],['level'=>$level_arr],['status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]],'index'=>'level']);
- foreach ($data['checked_condition_values'][4]['value'] as &$val){
- if(isset($level_list[$val['level']])){
- $val['name'] = $level_list[$val['level']]['name'];
- }
- }
- }
- }
- $data['fill_min'] =$data['fill_min']??0;
- $data['is_open_fill_min'] =$data['is_open_fill_min']??0;
- }
- return $this->success('操作成功', $data);
- }
- if ($request->isPost) {
- $params = Yii::$app->request->post();
- // $res = Yii::$app->services->validate->validate($params, [
- // [['level', 'is_auto_upgrade', 'upgrade_type_condition', 'condition_type', 'upgrade_type_goods', 'buy_goods_type', 'goods_type', 'status'], 'integer'],
- // [['name'], 'string', 'max' => 45],
- // [['explain'], 'string', 'max' => 5000],
- // [['id', 'checked_condition_values', 'checked_condition_keys', 'score', 'commission', 'goods_ids', 'goods_list'], 'safe']
- // ]);
- // if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- $res = CloudStockTwoLevel::setData($params);
- if ($res === false) return $this->error(CloudStockTwoLevel::getStaticError());
- return $this->success('操作成功');
- }
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage(), []);
- }
- return $this->render('edit');
- }
- public function actionUpdateStatus()
- {
- $params = Yii::$app->request->post();
- if (!in_array($params['status'], [StatusEnum::ENABLED, StatusEnum::DISABLED])) {
- return $this->error('参数不对');
- }
- $params['mall_id'] = $this->mall_id;
- $res = CloudStockTwoLevel::setData($params);
- if ($res === false) return $this->error(CloudStockTwoLevel::getStaticError());
- return $this->success('操作成功');
- }
- public function actionDel()
- {
- $params = Yii::$app->request->post();
- $params['mall_id'] = $this->mall_id;
- $params['status'] = StatusEnum::DELETE;
- $res = CloudStockTwoLevel::setData($params);
- if ($res === false) return $this->error(CloudStockTwoLevel::getStaticError());
- return $this->success('操作成功');
- }
- //不用验证权限
- public function actionGetCanChooseLevelList()
- {
- $params = Yii::$app->request->get();
- $level = isset($params['level']) ? $params['level'] : null;
- $can_choose_list = CloudStockTwoLevel::find()->select('level')->where([
- 'mall_id' => $this->mall_id,
- 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED],
- ])->column();
- $new_list = [];
- for ($i = 1; $i <= 10; $i++) {
- $can_choose = 1;
- //已经存在的等级为false
- if (in_array($i, $can_choose_list)) $can_choose = 0;
- //有指定等级,比指定等级小的等级为false
- if ($level && $i < $level) $can_choose = 0;
- //有指定等级,指定等级为true
- if ($level && $i == $level) $can_choose = 1;
- $new_list[] = [
- 'name' => "等级" . $i,
- 'level' => $i,
- 'is_can_choose' => $can_choose,
- ];
- }
- return $this->success('操作成功', $new_list);
- }
- //不用验证权限
- public function actionGetLessLevelList()
- {
- $params = Yii::$app->request->get();
- $level = isset($params['level']) ? $params['level'] : 10;
- $use_list = CloudStockTwoLevel::find()->select('level')->where([
- 'mall_id' => $this->mall_id,
- 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED],
- ])->column();
- $new_list = [];
- $list = CloudStockTwoLevel::lists(
- [
- 'where' => [
- ['mall_id' => $this->mall_id,],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
- ],
- 'select' => 'name,level',
- 'index' => 'level'
- ]
- );
- for ($i = 1; $i <= 10; $i++) {
- if (in_array($i, $use_list) && $i <= $level) {
- $is_use = 1;
- } else {
- $is_use = 0;
- }
- $new_list[] = [
- 'name' => $list[$i]['name'] ?? '等级不存在',
- 'level' => $i,
- 'is_use' => $is_use,
- ];
- }
- return $this->success('操作成功', $new_list);
- }
- public function actionAgentQuota()
- {
- if (\Yii::$app->request->isAjax) {
- $get = Yii::$app->request->get();
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['=', 'status', StatusEnum::ENABLED];
- if (!empty($get['keyword'])) {
- $user_list = User::find()->where(['or',
- ['id' => $get['keyword']],
- ['like', 'mobile', $get['keyword']],
- ['like', 'nickname', $get['keyword']]])->select('id')->asArray()->all();
- $user_id_arr = array_column($user_list, 'id');
- $where[] = ['user_id' => $user_id_arr];
- }
- if (!empty($get['level'])) $where[] = ['level' => $get['level']];
- $mall_id = $this->mall_id;
- $params = [
- 'where' => $where,
- 'with' => ['stockLevel'=>function($query)use($mall_id){
- $query->where(['status'=>[StatusEnum::DISABLED,StatusEnum::ENABLED],'mall_id'=>$mall_id]);
- }, 'user']
- ];
- $page_data = CloudStockTwoAgent::listPage($params, false);
- if (!empty($page_data['list'])) {
- foreach ($page_data['list'] as &$item) {
- $item['agent_quota_list'] = CloudStockTwoAgentQuota::lists([
- 'where' => [
- ['user_id' => $item['user_id']],
- ['status'=>StatusEnum::ENABLED]
- ],
- 'with' => [
- 'stockLevel'=>function($query)use($mall_id){
- $query->where(['status'=>[StatusEnum::DISABLED,StatusEnum::ENABLED],'mall_id'=>$mall_id]);
- }
- ]
- ], true);
- }
- }
- return $this->success('操作成功', $page_data);
- }
- return $this->render('agent-quota');
- }
- public function actionGetLevelList()
- {
- $data = CloudStockTwoLevel::lists(['where' => [['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED]]], true);
- return $this->success('操作成功', $data);
- }
- }
|