| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- <?php
- namespace addons\Happiness\backend\controllers;
- use addons\Happiness\common\enums\HappinessEnum;
- use addons\Happiness\common\logic\weight\PoolLogLogic;
- use addons\Happiness\common\models\HappinessWeightPoolJob;
- use addons\Happiness\common\models\HappinessWeightPoolOrderLog;
- use addons\Happiness\common\models\HappinessWeightPoolLog;
- use Yii;
- use common\enums\StatusEnum;
- use common\models\user\User;
- use common\models\user\UserLevel;
- use Exception;
- /**
- * 分红控制器
- *
- * Class PoolController
- * @package addons\Happiness\backend\controllers
- */
- class StoreWeightController extends BaseController
- {
- /**
- * 首页 分红池首页
- *
- * @return string
- */
- public function actionIndex()
- {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isGet) {
- $get = Yii::$app->request->get();
- $where = $list = [];
- $where[] = ['=', 'status', StatusEnum::ENABLED];
- $where[] = ['=', 'mall_id', $this->mall_id];
- if (!empty($get['order_no'])) {
- $where[] = ['order_no'=>$get['order_no']];
- }
- if (!empty($get['user_id'])) {
- $where[] = ['user_id'=>$get['user_id']];
- }
- if (!empty($get['date'])) {
- $where[]=['between', 'created_at', strtotime($get[0]), strtotime($get[1])];
- }
- if (!empty($get['change_type'])) {
- if($get['change_type']=='add')
- {
- $where[] = ['>','amount',0];
- }
- else{
- $where[] = ['<','amount',0];
- }
-
- }
- if (isset($get['keyword']) && !empty($get['keyword'])) {
- $user_list = User::lists([
- 'where' => [
- ['mall_id' => $this->mall_id],
- ['or', ['like', 'nickname', $get['keyword']], ['=', 'mobile', $get['keyword']]]
- ],
- 'select' => 'id'
- ]);
- $where[] = ['user_id' => array_column($user_list, 'id') ?? []];
- }
- $with = [
- 'user' => function ($query) {
- return $query->select('id, nickname, avatar_url, mobile');
- }
- ];
- $order = 'created_at desc';
- $params = array('where' => $where, 'order' => $order, 'limit' => $get['limit'] ?? 15,'with'=>$with);
- $list = HappinessWeightPoolOrderLog::listPage($params, false, true);
- if ($list === false) return $this->error(HappinessWeightPoolOrderLog::getStaticError());
- foreach ($list['list'] as &$val) {
- $date_limit = explode('-', $val['date_limit']);
- $val['date_limit'] = date('Y-m-d', strtotime($date_limit[0])) . '至' . date('Y-m-d', strtotime($date_limit[1]));
- }
- unset($val);
-
- $total_amount=HappinessWeightPoolOrderLog::find()->where(['mall_id' => $this->mall_id,'status'=>1])->sum('amount');//实际已结算的订单分红总额
- $total_settle_amount=HappinessWeightPoolOrderLog::find()->where(['mall_id' => $this->mall_id,'settlement_status' => 1,'status'=>1])->sum('amount');//实际已结算的订单分红总额
- $total_send_amount=HappinessWeightPoolLog::find()->where(['mall_id' => $this->mall_id,'status'=>1])->sum('amount');//实际用户获得的分红
- $now_amount=HappinessWeightPoolOrderLog::find()->where(['mall_id' => $this->mall_id, 'settlement_status' => 0,'status'=>1])->sum('amount');//当前未结算的分红,就是分红池的额度
- $list['orderStatistics'] = [
- 'total_amount' =>$total_amount??0,
- 'total_settle_amount' => $total_settle_amount??0,
- 'total_send_amount' => $total_send_amount??0,
- 'now_amount' => $now_amount??0
- ];
- return $this->success('获取成功', $list);
- }
- }
- return $this->render($this->action->id);
-
- }
-
- public function actionRecharge()
- {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isPost) {
- $params = Yii::$app->request->post();
- $rules = [
- [['price'], 'required'],
- [['price'], 'number'],
- [['remark'], 'string'],
- ];
- $res = Yii::$app->services->validate->validate($params, $rules);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
-
- try {
- $params['remark'] = !empty($params['remark']) ? trim($params['remark']) : '管理员调整,' . ($params['price'] > 0 ? '增加' : '减少') . strval(abs($params['price']));
- $before_amount = HappinessWeightPoolOrderLog::find()->where(['mall_id' => $this->mall_id, 'settlement_status' => 0])->sum('amount')??0;
- if(($before_amount+$params['price'])<0)
- {
- throw new Exception('奖励池金额不足'.abs($params['price']));
- }
- if (HappinessWeightPoolOrderLog::addData([
- 'mall_id' => $this->mall_id,
- 'date' => date('YmdHis'),
- 'amount' => $params['price'],
- 'user_id'=>0,
- 'price'=>0,
- 'pay_price'=>0,
- 'order_id' => 0,
- 'order_no' => '',
- 'before_amount' => $before_amount,
- 'after_amount' => bcadd($before_amount, $params['price'], 2),
- 'desc'=>$params['remark']
- ]) === false) {
- throw new \Exception(HappinessWeightPoolOrderLog::getStaticError());
- }
- return $this->success('调整成功');
- } catch (Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- return $this->error('非法的请求');
- }
- }
- /**
- * 首页 结算周期
- *
- * @return string
- */
- public function actionList()
- {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isGet) {
- $get = Yii::$app->request->get();
- $where = $list = [];
- $where[] = ['=', 'status', StatusEnum::ENABLED];
- $where[] = ['=', 'mall_id', $this->mall_id];
- if (isset($get['start_at']) && !empty($get['start_at'])) {
- $where[] = ['>=', 'created_at', strtotime($get['start_at'])];
- }
- if (isset($get['end_at']) && !empty($get['end_at'])) {
- $where[] = ['<=', 'created_at', strtotime($get['end_at'])];
- }
- $order = 'created_at desc';
- $params = array('where' => $where, 'order' => $order, 'limit' => $get['limit'] ?? 15);
- $list = HappinessWeightPoolJob::listPage($params, false, true);
- if ($list === false) return $this->error(HappinessWeightPoolJob::getStaticError());
- foreach ($list['list'] as &$val) {
- $date_limit = explode('-', $val['date_limit']);
- $val['date_limit'] = date('Y-m-d', strtotime($date_limit[0])) . '至' . date('Y-m-d', strtotime($date_limit[1]));
- }
- unset($val);
- return $this->success('获取成功', compact('list'));
- }
- }
- return $this->render($this->action->id);
- }
- /**
- * 结算明细
- */
- public function actionDetail()
- {
- if (Yii::$app->request->isAjax) {
- $get = Yii::$app->request->get();
- $where = $list = [];
- $where[] = ['=', 'status', StatusEnum::ENABLED];
- $where[] = ['=', 'mall_id', $this->mall_id];
- $where[] = ['=', 'job_id', $get['job_id']];
- if (isset($get['user_id']) && !empty($get['user_id'])) {
- $where[] = ['user_id' => $get['user_id']];
- }
- $user_where= [];
- if(isset($get['level'])&& !empty($get['level']))
- {
- $user_where[]= ['level' => $get['level']];
-
- }
- if (isset($get['keyword']) && !empty($get['keyword'])) {
- $user_where[]=[
- 'or', ['like', 'nickname', $get['keyword']], ['like', 'mobile', $get['keyword']]];
- }
-
- if (!empty($user_where)) {
- $user_where=array_merge(['mall_id' => $this->mall_id],$user_where);
- $user_list = User::lists([
- 'where' => $user_where,
- 'select' => 'id'
- ]);
- $where[] = ['user_id' => array_column($user_list, 'id') ?? []];
- }
- $with = [
- 'user' => function ($query) {
- return $query->select('id, nickname, avatar_url, mobile');
- }
- ];
- $order = 'created_at desc';
- $params = array('where' => $where, 'order' => $order, 'with' => $with, 'limit' => $get['limit'] ?? 15);
- $list = HappinessWeightPoolLog::listPage($params, false, true);
- $levels =UserLevel::getListIndexByLevelColumn($this->mall_id);
- if ($list === false) return $this->error(HappinessWeightPoolLog::getStaticError());
- return $this->success('获取成功', compact('list','levels'));
- }
- return $this->render($this->action->id);
-
- }
-
- //手动执行
- public function actionDoRun()
- {
- try {
- $logic = new PoolLogLogic();
- $res = $logic->executeNoAuto($this->mall_id);
- if($res == true) {
- return $this->success('已经执行');
- }
-
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
-
-
- public function actionOrderList()
- {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isGet) {
- $get = Yii::$app->request->get();
- $where = $list = [];
- $where[] = ['=', 'status', StatusEnum::ENABLED];
- $where[] = ['=', 'mall_id', $this->mall_id];
- if (!empty($get['order_no'])) {
- $where[] = ['order_no'=>$get['order_no']];
- }
- if (!empty($get['user_id'])) {
- $where[] = ['user_id'=>$get['user_id']];
- }
- if (!empty($get['date'])) {
- $where[]=['between', 'created_at', strtotime($get[0]), strtotime($get[1])];
- }
- if (isset($get['keyword']) && !empty($get['keyword'])) {
- $user_list = User::lists([
- 'where' => [
- ['mall_id' => $this->mall_id],
- ['or', ['like', 'nickname', $get['keyword']], ['like', 'mobile', $get['keyword']]],
-
- ],
- 'select' => 'id'
- ]);
- $where[] = ['user_id' => array_column($user_list, 'id') ?? []];
- }
- $with = [
- 'user' => function ($query) {
- return $query->select('id, nickname, avatar_url, mobile');
- }
- ];
- $order = 'created_at desc';
- $params = array('where' => $where, 'order' => $order, 'limit' => $get['limit'] ?? 15,'with'=>$with);
- $list = HappinessWeightPoolOrderLog::listPage($params, false, true);
- if ($list === false) return $this->error(HappinessWeightPoolOrderLog::getStaticError());
- foreach ($list['list'] as &$val) {
- $date_limit = explode('-', $val['date_limit']);
- $val['date_limit'] = date('Y-m-d', strtotime($date_limit[0])) . '至' . date('Y-m-d', strtotime($date_limit[1]));
- }
- return $this->success('获取成功', $list);
- }
- }
- return $this->render($this->action->id);
- }
-
-
- }
|