| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- <?php
- namespace addons\ScoreBonus\backend\controllers;
- use common\models\user\User;
- use Yii;
- use addons\ScoreBonus\common\enums\ScoreBonusEnums;
- use common\controllers\AddonsController;
- use addons\ScoreBonus\common\models\ScoreBonusModel;
- use addons\ScoreBonus\common\models\ScoreBonusCapitalModel;
- use addons\ScoreBonus\common\models\ScoreBonusCapitalPool;
- use addons\ScoreBonus\common\models\ScoreBonusRewardLogModel;
- use addons\ScoreBonus\common\models\ScoreBonusUserWeight;
- use addons\ScoreBonus\common\models\ScoreBonusUserWeightLog;
- use common\models\user\UserWallet;
- use common\enums\StatusEnum;
- use common\helpers\csv\CsvExportHelper;
- use common\enums\DownloadEnum;
- /**
- * 默认控制器
- *
- * Class DefaultController
- * @package addons\ScoreBonus\backend\controllers
- */
- class DefaultController extends BaseController
- {
- /**
- * 首页
- *
- * @return string
- */
- // 获取结算明细
- public function actionIndex()
- {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isGet) {
- $get = Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($get, [
- [['orderBy', 'num_text'], 'string'],
- [['status', 'user_id'], 'number'],
- [['start', 'end'], 'datetime', 'format' => 'php:Y-m-d H:i:s'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $where = [
- ['mall_id' => $this->mall_id],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
- ];
- if (isset($get['status'])) $where[] = ['status' => $get['status']];
- if (!empty($get['start'])) $where[] = ['>=', 'created_at', strtotime($get['start'])];
- if (!empty($get['end'])) $where[] = ['<=', 'created_at', strtotime($get['end'])];
- if (!empty($get['num_text'])) $where[] = ['like', 'num_text', $get['num_text']];
- $order = 'id desc';
- if (!empty($get['orderBy'])) $order = $get['orderBy'];
- // $list_params = compact('where', 'order');
- $list_params = array('where' => $where, 'order' => $order);
- $list = ScoreBonusModel::listPage($list_params, false, true);
- if (!empty($list['list'])) {
- foreach ($list['list'] as $key => $value) {
- if ($value['num'] < 10) {
- $value['num'] = '0' . $value['num'];
- }
- $list['list'][$key]['num'] = $value['date_time'].$value['num'].'期';
- }
- }
- $config = Yii::$app->services->setting->addons->get($this->mall_id, ScoreBonusEnums::ADDONS_NAME, 'basic',true);
-
- //获取结算周期枚举
- $list['bonus_circle'] = ScoreBonusEnums::getSettleCircleMap();
- $capital_pool = ScoreBonusCapitalPool::getOne([
- ['mall_id' => $this->mall_id],
- ], true);
- //本期分红池总额
- $statistics_data['bonus_price'] = $capital_pool['bonus_price'];//本期分红池总额
- //当前总积分额
- /*因为现在添加了【权重占比分配】选项。 所以这里的积分统计不能只按照【积分占比分配】来统计。*/
- if (!empty($config['settle_method'])) {
- $statistics_data['total_score'] = ScoreBonusUserWeight::find()
- ->alias('w')
- ->joinWith(['userWallet uw' => function($query) {
- $query->andWhere(['>', 'uw.score', 0]);
- }])
- ->where(['w.status' => 1, 'w.mall_id' => $this->mall_id])
- ->andWhere(['>', 'weight', 0])
- ->sum('uw.score');
- } else {
- $statistics_data['total_score'] = UserWallet::find()->where(['and', ['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED], ['>=', 'score', $config['user_score']]])->sum('score');
- }
- //已结算分红总额
- $statistics_data['total_bonus_price'] = $capital_pool['total_price'];
- $list['statistics_data'] = $statistics_data;
- $list['export_list'] = ScoreBonusModel::fieldsList();
- return $this->success('操作成功',$list);
- }
- if (\Yii::$app->request->post('flag') == 'EXPORT') {
- $post = \Yii::$app->request->post();
- $post['mall_id'] = $this->mall_id;
- $filename = '结算明细-' . date('YmdHis') . '_' . rand(10000, 99999);
- $res = CsvExportHelper::exportDownLoadCenter($this->mall_id, $filename, DownloadEnum::TYPE_SCORE_BONUS, ScoreBonusModel::class, 'exportHandle', $post);
- if ($res === false) return $this->error('加入导出任务失败' . CsvExportHelper::getStaticError());
- return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
- }
- }
- return $this->render($this->action->id);
- }
- //获取用户结算明细
- public function actionUserSettleData()
- {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isGet) {
- $get = Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($get, [
- [['orderBy'], 'string'],
- [['user_id', 'bid'], 'number'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $where = [
- ['mall_id' => $this->mall_id],
- ['bid' => $get['bid']]
- ];
- if (!empty($get['user_id'])) $where[] = ['user_id' => $get['user_id']];
- $orderBy = 'id desc';
- if (!empty($get['orderBy'])) $orderBy = $get['orderBy'];
- $list_params = array('where' => $where, 'order' => $orderBy, 'with'=>['user']);
- $list = ScoreBonusRewardLogModel::listPage($list_params, false, true);
- //获取本期数据
- $score_bonus_data = ScoreBonusModel::findOne(['mall_id' => $this->mall_id, 'id' => $get['bid']]);
- if ($score_bonus_data->num < 10) $score_bonus_data->num = '0' . $score_bonus_data->num;
- $circly_num = $score_bonus_data->date_time . $score_bonus_data->num .'期明细';
- $list['titleText'] = $circly_num;
- $list['bonus_circle'] = ScoreBonusEnums::getSettleCircleMap();
- $list['export_list'] = ScoreBonusRewardLogModel::fieldsList();
- return $this->success('操作成功',$list);
- }
- if (\Yii::$app->request->post('flag') == 'EXPORT') {
- $post = \Yii::$app->request->post();
- $post['mall_id'] = $this->mall_id;
- $score_bonus_data = ScoreBonusModel::findOne(['mall_id' => $this->mall_id, 'id' => $post['bid']]);
- if ($score_bonus_data->num < 10) $score_bonus_data->num = '0' . $score_bonus_data->num;
- $circly_num = $score_bonus_data->date_time . $score_bonus_data->num .'期用户结算明细';
- $filename = $circly_num.'-' . date('YmdHis') . '_' . rand(10000, 99999);
- $res = CsvExportHelper::exportDownLoadCenter($this->mall_id, $filename, DownloadEnum::TYPE_SCORE_BONUS, ScoreBonusRewardLogModel::class, 'exportHandle', $post);
- if ($res === false) return $this->error('加入导出任务失败' . CsvExportHelper::getStaticError());
- return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
- }
- }
- return $this->render($this->action->id);
- }
- public function actionTestExport()
- {
- $params = [
- 'flag' => 'EXPORT',
- 'fields' => [
- 'user_id',
- 'nickname',
- 'commission',
- 'score',
- 'total_score',
- 'status',
- 'created_at',
- 'bonus_circle',
- ],
- 'mall_id' => 5,
- 'bid' => 20
- ];
- $data = [
- 'params' => json_encode($params),
- 'filename' => '2024062602期用户结算明细-20240627102225_85862',
- 'type' => 66,
- 'handler_class' => ScoreBonusRewardLogModel::class,
- 'method' => 'exportHandle'
- ];
- $data = json_decode(json_encode($data));
- $res = ScoreBonusRewardLogModel::exportHandle($data);
- print_r($res);exit;
- }
- public function actionUserList()
- {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isGet) {
- $params = \Yii::$app->request->get();
- $where = [];
- $where[] = ['=', 'u.mall_id', $this->mall_id];
- $where[] = ['in', 'u.status', [StatusEnum::ENABLED, StatusEnum::DISABLED]];
- // 根据ID
- if (!empty($params['user_id'])) $where[] = ['=', 'u.id', $params['user_id']];
- $select = 'u.id,u.id as user_id,nickname,mobile';
- $order = 'user_id desc';
- $limit = $params['limit'] ?? 10;
- $params = array(
- 'alias' => 'u',
- 'select' => $select,
- 'where' => $where,
- 'order' => $order,
- 'limit' => $limit
- );
- $list = User::listPage($params, false, true);
- if ($list['list']) {
- $user_ids = array_column($list['list'], 'id');
- $user_wallet_info = UserWallet::find()
- ->select('user_id,score')
- ->where([
- 'user_id' => $user_ids,
- 'status' => StatusEnum::ENABLED
- ])
- ->indexBy('user_id')
- ->asArray()
- ->all();
- $user_weight_info = ScoreBonusUserWeight::find()
- ->select('user_id,weight,total_income')
- ->where([
- 'user_id' => $user_ids,
- 'status' => StatusEnum::ENABLED
- ])
- ->indexBy('user_id')
- ->asArray()
- ->all();
- foreach ($list['list'] as &$item) {
- $item['score'] = $user_wallet_info[$item['id']]['score'] ?? '0';
- $item['weight'] = $user_weight_info[$item['id']]['weight'] ?? '0';
- $item['total_income'] = $user_weight_info[$item['id']]['total_income'] ?? '0';
- }
- }
- return $this->success('操作成功', $list);
- }
- }
- return $this->render($this->action->id);
- }
- public function actionWeightList()
- {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isGet) {
- $get = \Yii::$app->request->get();
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['=', 'status', StatusEnum::ENABLED];
- if (!empty($get['user_id'])) {
- $where[] = ['=', 'user_id', $get['user_id']];
- }
- $params['order'] = 'id desc';
- $params['where'] = $where;
- $params['with'] = ['user'];
- $params['limit'] = 10;
- $list = ScoreBonusUserWeightLog::listPage($params, false);
- return $this->success('操作成功', $list);
- }
- }
- return $this->render($this->action->id);
- }
- }
|