| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462 |
- <?php
- namespace addons\DoubleCopy\backend\controllers;
- use addons\DoubleCopy\common\enums\DoubleCopyEnum;
- use addons\DoubleCopy\common\models\DoubleCopyPoolLevel;
- use addons\DoubleCopy\common\models\DoubleCopyPoolLog;
- use addons\DoubleCopy\common\models\DoubleCopyPoolSend;
- use addons\DoubleCopy\common\models\DoubleCopyPoolSource;
- use addons\DoubleCopy\common\models\DoubleCopyPoolUser;
- use common\enums\StatusEnum;
- use common\models\goods\Goods;
- use common\models\user\User;
- use Yii;
- use yii\helpers\Json;
- /**
- * Class PoolController
- * @package addons\DoubleCopy\backend\controllers
- */
- class PoolController extends BaseController
- {
- public $enableCsrfValidation = false;
- /**
- * @return string|\yii\web\Response
- */
- public function actionIndex()
- {
- return $this->render('index');
- }
- /**
- * @return mixed|void
- * @throws \Exception
- */
- public function actionLevel()
- {
- $retuest = Yii::$app->request;
- if ($retuest->isAjax && $retuest->isGet) {
- // 检查提交的参数
- $get = $retuest->get();
- $res = Yii::$app->services->validate->validate($get,[
- [['page','limit'], 'integer'],
- [['keyword'], 'string']
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $where = [];
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['<>', 'status', StatusEnum::DELETE];
- $order = 'level asc';
- $params = array('where' => $where, 'order' => $order, 'limit' => $get['limit']);
- $list = DoubleCopyPoolLevel::listPage($params, false, true);
- if (empty($list)) return $this->error('获取失败');
- //基础设置:当前奖励类型
- $config = Yii::$app->services->setting->addons->get($this->mall_id, DoubleCopyEnum::ADDONS_NAME, 'basic');
- $reward_currency = $config['reward_currency'] ?? 'commission';
- foreach ($list['list'] as &$item) {
- $item['reward_currency'] = $reward_currency == 'score' ? '积分' : '佣金';
- if($reward_currency == 'score'){
- $item['current_money'] = $item['current_score'];
- $item['total_money'] = $item['total_score'];
- }
- // 统计当前等级人数
- $item['bonus_num'] = DoubleCopyPoolUser::find()->where(['mall_id' => $this->mall_id, 'level' => $item['level'], 'status' => StatusEnum::ENABLED])->count();
- }
- return $this->success('获取成功', $list);
- }
- return $this->render($this->action->id);
- }
- /**
- * 删除处理
- * @Author: lun
- * @DateTime: 2022/9/16 0016 10:31
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionHandle()
- {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isGet) {
- $params = Yii::$app->request->get();
- // 检查提交的参数
- $res = Yii::$app->services->validate->validate($params,[
- [['id'], 'required'],
- [['id','status'], 'integer'],
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- if (!isset($params['status'])) unset($params['status']);
- $params['mall_id'] = $this->mall_id;
- $res = DoubleCopyPoolLevel::setData($params);
- if (!$res) return $this->error('修改失败:msg=' . DoubleCopyPoolLevel::getStaticError());
- return $this->success('修改成功');
- }
- }
- }
- /**
- * 等级编辑
- * @Author: lun
- * @DateTime: 2021/10/21 0021 11:29
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|string|void
- */
- public function actionLevelEdit()
- {
- try {
- $request = Yii::$app->request;
- if ($request->isAjax) {
- // 查看等级详情
- if ($request->isGet) {
- $id = Yii::$app->request->get('id', '');
- $data = [];
- if(!empty($id)){
- // 获取等级信息
- $field = "id,name,level,achievement,single_market,goods_setting,status,is_enable_extra_pool,extra_pool_level";
- $model = DoubleCopyPoolLevel::getOne([['id' => $id, 'mall_id' => $this->mall_id]], true,[],false,$field);
- // 解析被选中的商品
- $goods_setting = Json::decode($model['goods_setting'],true) ?? [];
- if ($goods_setting) {
- $goods_ids = array_column($goods_setting, 'goods_id');
- $goods_list = Goods::lists(['where'=>[['id'=>$goods_ids]],'select'=>'id,cover_pic,goods_name as name,price','index'=>'id']);
- foreach ($goods_setting as &$item) {
- if (empty($item['is_percent'])) $item['is_percent'] = 0;
- $item['goods'] = $goods_list[$item['goods_id']] ?? [];
- }
- }
- $model['goods_setting'] = $goods_setting;
- if (!empty($model['extra_pool_level'])) {
- $model['extra_pool_level'] = Json::decode($model['extra_pool_level']);
- } else {
- $model['extra_pool_level'] = [];
- }
- $data['detail'] = $model;
- }
- $data['weights'] = DoubleCopyPoolLevel::getLevelWeights($this->mall_id);
- return $this->success('操作成功', $data);
- }
- // 提交等级内容
- if ($request->isPost) {
- $params = Yii::$app->request->post('form');
- $res = Yii::$app->services->validate->validate($params, [
- [['level', 'name', 'achievement','goods_setting'], 'required'],
- [['id','level', 'status', 'is_enable_extra_pool'], 'integer'],
- [['achievement','single_market'], 'double'],
- [['name'], 'string'],
- [['goods_setting', 'extra_pool_level'], 'safe']
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- foreach ($params['goods_setting'] as $key => $item) {
- unset($params['goods_setting'][$key]['goods']);
- }
- $params['goods_setting'] = Json::encode($params['goods_setting']);
- $res = DoubleCopyPoolLevel::setData($params);
- if ($res === false) throw new \Exception(DoubleCopyPoolLevel::getStaticCodeError()['error']);
- return $this->success('操作成功');
- }
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage(), []);
- }
- return $this->render('level-edit');
- }
- /**
- * 分红用户
- * @Author: lun
- * @DateTime: 2022/9/16 0016 16:45
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|string|void
- */
- public function actionUser()
- {
- $retuest = Yii::$app->request;
- if ($retuest->isAjax && $retuest->isGet) {
- // 检查提交的参数
- $get = $retuest->get();
- $res = Yii::$app->services->validate->validate($get,[
- [['page','limit','user_id','mobile','level'], 'integer'],
- [['start_at','end_at'], 'safe'],
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // 获取等级
- $level_map = DoubleCopyPoolLevel::getMap($this->mall_id);
- $where = $list = [];
- $where[] = ['=', 'mall_id', $this->mall_id];
- !empty($get['user_id']) && $where[] = ['=', 'user_id', $get['user_id']];
- !empty($get['level']) && $where[] = ['=', 'level', $get['level']];
- if (!empty($get['mobile'])) {
- $user_id = User::find()->select('id')->where(['mall_id' => $this->mall_id, 'mobile' => $get['mobile'], 'status' => StatusEnum::ENABLED])->scalar() ?? 0;
- if (empty($user_id)) $this->success('获取成功',compact('list','level_map'));
- $where[] = ['=', 'user_id', $user_id];
- }
- if (!empty($get['start_at']) && !empty($get['end_at'])) {
- $where[] = ['between', 'created_at', strtotime($get['start_at']), strtotime($get['end_at'])];
- }
- $where[] = ['=', 'status', StatusEnum::ENABLED];
- $with = ['user'];
- $order = 'id desc,created_at desc';
- $params = array('where' => $where, 'order' => $order, 'with' => $with, 'limit' => $get['limit']);
- $list = DoubleCopyPoolUser::listPage($params, false, true);
- if (empty($list)) return $this->error('获取失败');
- return $this->success('获取成功', compact('list','level_map'));
- }
- return $this->render($this->action->id);
- }
- /**
- * 用户分红记录
- * @Author: lun
- * @DateTime: 2022/9/16 0016 17:45
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionLog()
- {
- $retuest = Yii::$app->request;
- if ($retuest->isAjax && $retuest->isGet) {
- // 检查提交的参数
- $get = $retuest->get();
- $res = Yii::$app->services->validate->validate($get,[
- [['page','limit','user_id'], 'integer'],
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $where = $list = [];
- $where[] = ['=', 'mall_id', $this->mall_id];
- !empty($get['user_id']) && $where[] = ['=', 'user_id', $get['user_id']];
- $where[] = ['=', 'status', StatusEnum::ENABLED];
- $order = 'created_at desc';
- $params = array('where' => $where, 'order' => $order, 'limit' => $get['limit']);
- $list = DoubleCopyPoolLog::listPage($params, false, true);
- if (empty($list)) return $this->error('获取失败');
- return $this->success('获取成功', $list);
- }
- }
- /**
- * 分红发放记录
- * @Author: lun
- * @DateTime: 2022/9/16 0016 17:47
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionSend()
- {
- $retuest = Yii::$app->request;
- if ($retuest->isAjax && $retuest->isGet) {
- // 检查提交的参数
- $get = $retuest->get();
- $res = Yii::$app->services->validate->validate($get,[
- [['page','limit','level'], 'integer'],
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $where = $list = [];
- $where[] = ['=', 'mall_id', $this->mall_id];
- !empty($get['level']) && $where[] = ['=', 'level', $get['level']];
- $where[] = ['=', 'status', StatusEnum::ENABLED];
- $order = 'created_at desc';
- $params = array('where' => $where, 'order' => $order, 'limit' => $get['limit']);
- $list = DoubleCopyPoolSend::listPage($params, false, true);
- if (empty($list)) return $this->error('获取失败');
- return $this->success('获取成功', $list);
- }
- }
- /**
- * 删除分红池用户跟分红记录
- * @Author: lun
- * @DateTime: 2023/7/19 0019 9:29
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionDelUser()
- {
- $retuest = Yii::$app->request;
- if ($retuest->isAjax && $retuest->isPost) {
- // 检查提交的参数
- $post = $retuest->post();
- $res = Yii::$app->services->validate->validate($get,[
- [['user_id'], 'integer'],
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $res = DoubleCopyPoolUser::deleteUser($this->mall_id, $post['user_id']);
- if ($res === false) return $this->error('用户删除失败');
- return $this->success('已成功删除该用户,并删除已删除该用户的分红记录');
- }
- }
- public function actionEditUser()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $postData = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($postData, [
- [['user_id', 'level'], 'required'],
- [['user_id', 'level','id'], 'integer'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- if($postData['id']){
- $wheres = [
- 'user_id' => $postData['user_id'],
- 'mall_id' => $this->mall_id,
- 'status' => StatusEnum::ENABLED,
- 'id' => $postData['id']
- ];
- $model = DoubleCopyPoolUser::findOne($wheres);
- if (empty($model)) return $this->error('修改会员不存在');
- if($model->level == $postData['level']) return $this->error('修改会员等级不能与原等级相同');
- if($model->level > $postData['level']) return $this->error('会员等级不能比原等级低');
- $postData['upgrade_level_at'] = time();
- }else{
- $wheres = [
- 'user_id' => $postData['user_id'],
- 'mall_id' => $this->mall_id,
- 'status' => StatusEnum::ENABLED,
- ];
- $model = DoubleCopyPoolUser::findOne($wheres);
- if (!empty($model)) return $this->error('该会员已存在,不能重复添加');
- }
- $postData['mall_id'] = $this->mall_id;
- $postData['status'] = StatusEnum::ENABLED;
- $postData['upgrade_status'] = DoubleCopyEnum::UPGRADE_STATUS_MANUAL;
- $res = DoubleCopyPoolUser::create($postData);
- if ($res === false) return $this->error(DoubleCopyPoolUser::getStaticError());
- return $this->success('操作成功');
- }
- }
- return $this->error();
- }
- public function actionEnableList()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $level = \Yii::$app->request->get('level');
- $where = [['mall_id' => $this->mall_id], ['=', 'status', StatusEnum::ENABLED]];
- if (!empty($level)) $where[] = ['level' => $level];
- $args['where'] = $where;
- $args['select'] = 'id,level,name';
- $args['order'] = 'level asc';
- $list['list'] = DoubleCopyPoolLevel::lists($args, true);
- return $this->success('操作成功', $list);
- }
- }
- return $this->error();
- }
- /**
- * 分红池充值金额
- * @return mixed|null
- */
- public function actionRecharge()
- {
- $trans = Yii::$app->db->beginTransaction();
- try{
- $request = Yii::$app->request;
- $post = $request->post();
- $res = Yii::$app->services->validate->validate($post,[
- [['level_id'], 'integer'],
- [['level_id','num'], 'required'],
- [['desc'], 'string'],
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $config = Yii::$app->services->setting->addons->get($this->mall_id, DoubleCopyEnum::ADDONS_NAME, 'basic');
- $reward_currency = $config['reward_currency'] ?? 'commission';
- $level = DoubleCopyPoolLevel::find()->where(['id'=>$post['level_id'],'mall_id' => $this->mall_id,'status' => StatusEnum::ENABLED])->asArray()->one();
- if(!$level) throw new \Exception('等级不存在');
- if($reward_currency == 'commission'){
- DoubleCopyPoolLevel::savePoolMoney($this->mall_id, $level['level'], [
- 'current_money' => $post['num'],
- 'total_money' => $post['num'],
- ]);
- }else{
- DoubleCopyPoolLevel::savePoolMoney($this->mall_id, $level['level'], [
- 'current_score' => $post['num'],
- 'total_score' => $post['num'],
- ]);
- }
- $res = DoubleCopyPoolSource::setData([
- 'mall_id' => $this->mall_id,
- 'level' => $level['level'],
- 'order_detail_id' => 0,
- 'money' => $post['num'],
- 'reward_currency' => $reward_currency,
- 'remark' => $post['desc'] ?? '',
- ]);
- if(!$res) throw new \Exception(DoubleCopyPoolSource::getStaticError());
- $trans->commit();
- return $this->success('操作成功');
- //ALTER TABLE `qimall_addons_double_copy_pool_source`
- //ADD COLUMN `remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '备注' AFTER `reward_currency`;
- }catch (\Exception $e){
- $trans->rollBack();
- return $this->error($e->getMessage());
- }
- }
- public function actionLevelList()
- {
- $request = Yii::$app->request;
- $data = $request->get();
- $res = Yii::$app->services->validate->validate($data,[
- [['level'], 'integer'],
- [['level'], 'required'],
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $list = DoubleCopyPoolLevel::lists([
- 'where' => [
- ['<', 'level', $data['level']],
- ['mall_id' => $this->mall_id],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
- ],
- 'select'=> 'id, level, name',
- ]);
- return $this->success('获取成功', $list);
- }
- }
|