| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- <?php
- namespace addons\Agent\api\modules\v1\controllers;
- use addons\Agent\common\enums\AgentEnum;
- use addons\Agent\common\models\Agent;
- use addons\Agent\common\models\AgentApply;
- use addons\Agent\common\models\AgentLevel;
- use common\enums\GlobalFuncEnum;
- use common\enums\OrderStatusEnum;
- use common\enums\StatusEnum;
- use common\globals\GlobalInvoke;
- use common\models\common\CapitalLog;
- use common\models\common\GlobalVarMap;
- use common\models\order\Order;
- use common\models\order\OrderDetail;
- use common\models\user\User;
- use common\models\user\UserPartitionRelationship;
- use api\controllers\OnAuthController;
- use Yii;
- /**
- * 默认控制器
- *
- * Class DefaultController
- * @package addons\Agent\api\modules\v1\controllers
- */
- class DefaultController extends OnAuthController
- {
- public $modelClass = '';
- public $from_type = ['Agent'];
- /**
- * 不用进行登录验证的方法
- *
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = ['index'];
- /**
- * 首页
- *
- * @return string
- */
- public function actionIndex()
- {
- return 'Hello world';
- }
- /**
- * @desc:渠道详情
- * @Author: hua
- * @Date: 2021/11/4
- * @Time: 16:47
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionInfo()
- {
- $data['user_id'] = $this->user_id;
- $data['nickname'] = empty($this->user->nickname) ? '' : $this->user->nickname;
- $data['avatar_url'] = empty($this->user->avatar_url) ? '' : $this->user->avatar_url;
- $agent = Agent::getOne([['user_id' => $this->user_id],['status'=>[StatusEnum::DISABLED,StatusEnum::ENABLED]]], true, ['agentLevel']);
- $data['is_agent'] = 0;
- $data['level_name'] = '';
- $data['frozen_price'] = 0;
- $data['total_price'] = 0;
- $data['total_income'] = 0;
- $data['yesterday_price'] = 0;
- $agent_apply=AgentApply::getOne([['user_id' => $this->user_id],['status'=>[StatusEnum::DISABLED,StatusEnum::ENABLED],'check_status'=>StatusEnum::ENABLED]], true);
- if(!empty($agent_apply)) $data['is_agent'] = 1;
- if ($agent) {
- $data['is_agent'] = 1;
- $data['level_name'] = $agent['agentLevel']['name'] ?? '默认等级';
- $wallet_type = 'commission';
- $yesterday = date('Y-m-d', strtotime('-1 day'));
- $start_time = "{$yesterday} 00:00:00";
- $end_time = "{$yesterday} 23:59:59";
- CapitalLog::$capitalType = "{$wallet_type}_frozen";
- $where = ['user_id' => $this->user_id, 'from_type' => AgentEnum::ADDONS_NAME, 'status' => CapitalLog::STATUS_WAIT_SEND];
- $query = CapitalLog::find();
- $frozen_price = $query->where($where)->sum('change_num');
- $yesterday_frozen_price = $query->where($where)
- ->andWhere(['<=', 'created_at', $end_time])
- ->andWhere(['>=', 'created_at', $start_time])
- ->sum('change_num');
- if (empty($yesterday_frozen_price)) $yesterday_frozen_price = 0;
- CapitalLog::$capitalType = "{$wallet_type}";
- $where = ['user_id' => $this->user_id, 'from_type' => AgentEnum::ADDONS_NAME];
- $query = CapitalLog::find();
- $total_price = $query->where($where)->sum('change_num');
- $yesterday_total_price = $query->where($where)
- ->andWhere(['<=', 'created_at', $end_time])
- ->andWhere(['>=', 'created_at', $start_time])
- ->sum('change_num');
- if (empty($yesterday_total_price)) $yesterday_total_price = 0;
- $data['total_price'] = empty($total_price) ? 0 : $total_price;
- $data['frozen_price'] = empty($frozen_price) ? 0 : $frozen_price;
- $data['total_income'] = bcadd($data['total_price'],$data['frozen_price'],2);
- $data['yesterday_price'] = bcadd($yesterday_total_price,$yesterday_frozen_price,2);
-
- }
- $level_list = [];
- // $children_id_arr = UserRelationship::getChildIdArr($this->user_id);
- $children_id_arr = UserPartitionRelationship::getChildIdArr($this->user_id);
- if (!empty($children_id_arr)) {
- $params = [];
- $params['where'] = [['user_id' => $children_id_arr],['status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]];
- $params['group'] = 'level';
- $params['with'] = ['agentLevel'];
- $params['select'] = 'count("user_id") as counts,level';
- $list = Agent::lists($params);
- foreach ($list as $val) {
- $level_list[] = [
- 'name' => $val['agentLevel']['name'] ?? '',
- 'level' => $val['agentLevel']['level'] ?? '',
- 'total' => $val['total'] ?? 0,
- ];
- }
- }
- $data['level_list'] = $level_list;
- //more_setting
- $more_setting = \Yii::$app->services->setting->addons->get($this->mall_id, AgentEnum::ADDONS_NAME, 'more');
- $data['is_enable_team_buy_detail'] = $more_setting['is_enable_team_buy_detail'] ?? 0;
- return $this->success('操作成功', $data);
- }
- /**
- * @desc:奖励列表
- * @Author: hua
- * @Date: 2021/11/4
- * @Time: 17:00
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionLogList()
- {
- $params = \Yii::$app->request->get();
- // $where[] = ['mall_id' => $this->mall_id];
- // $where[] = ['user_id' => $this->user_id];
- // $where[] = ['from_type' => AgentEnum::ADDONS_NAME];
- // $params['order'] = 'id desc';
- // $params['where'] = $where;
- // $params['select'] = 'id,change_num,source_table,source_table_id,created_at,from_type,capital_type,order_no,contributor_money,contributor_id,contributor_name';
- // $wallet_type = 'commission';
- // CapitalLog::$capitalType = "{$wallet_type}";
- // $list = CapitalLog::listPage($params);
- // if (!empty($list)) {
- // $contributor_ids = array_column($list['list'],'contributor_id');
- // $user_list = User::lists(['where'=>[['id'=>$contributor_ids]],'select'=>'id,nickname,avatar_url','index'=>'id']);
- // foreach ($list['list'] as &$item) {
- // $item['user_id'] = $item['contributor_id'];
- // $item['goods_price'] = $item['contributor_money'];
- // $item['goods_name'] = $item['contributor_name'];
- // $item['nickname'] = '';
- // $item['avatar_url'] = '';
- // if(isset($user_list[$item['contributor_id']])){
- // $item['nickname'] = $user_list[$item['contributor_id']]['nickname'];
- // $item['avatar_url'] = $user_list[$item['contributor_id']]['avatar_url'];
- // }
- // }
- // }
- //
- // $wallet_type = 'commission';
- // $params['mall_id'] = $this->mall_id;
- // $params['user_id'] = $this->user_id;
- $wallet_type = 'commission';
- if(!empty($params['list_type'])&&$params['list_type'] ==1){//未结算
- $wallet_type = 'commission_frozen';
- $params['status'] = StatusEnum::DISABLED;
- }
- $params['mall_id'] = $this->mall_id;
- $params['user_id'] = $this->user_id;
- $list = CapitalLog::getCapitalLogList($params, $wallet_type, AgentEnum::ADDONS_NAME);
- if (!empty(CapitalLog::getStaticError())) return $this->error(CapitalLog::getStaticError());
- if (!empty($list['list'])) {
- $commission_map = GlobalVarMap::getEnumMapByType('FromTypeMap');
- $capital_type_map = GlobalVarMap::getEnumMapByType('CapitalTypeMap');
- foreach ($list['list'] as &$item) {
- $item['source_text'] = ($commission_map[$item['from_type']] ?? '').($capital_type_map[$item['capital_type']]
- ?'-'.$capital_type_map[$item['capital_type']]:'');
- }
- }
- return $this->success('操作成功', $list);
- }
- /**
- * @desc:等级列表
- * @Author: hua
- * @Date: 2021/11/5
- * @Time: 16:20
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionLevelList(){
- $params = [];
- $params['where'] = [['mall_id'=>$this->mall_id],['status'=>StatusEnum::ENABLED]];
- $params['select'] = 'id,name,explain';
- $params['order'] = 'level asc';
- $list = AgentLevel::lists($params);
- return $this->success('操作成功', $list);
- }
-
- /**
- * Notes:渠道分红申请
- * User: LD
- * Date: 2024/4/11
- * Time: 14:15
- * @return mixed|void
- */
- public function actionApply(){
- $mall_id = $this->mall_id;
- if (\Yii::$app->request->isPost) {
- $params = \Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['name', 'mobile'], 'required'],
- [['remark'], 'safe'],
- ['mobile', 'match', 'pattern' => '/^1\d{10}$/', 'message' => '请输入正确的手机号码'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $mall_id;
- $params['user_id'] = $this->user_id;
- $Agent = Agent::getOne([['user_id' => $this->user_id], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]], ['mall_id' => $this->mall_id]], true);
- if (!empty($Agent)) return $this->error('您已是渠道分红团队');
- $agent_apply = AgentApply::getOne([['user_id' => $this->user_id], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]], ['mall_id' => $this->mall_id]], true, [], 'id desc');
- if (!empty($agent_apply) && empty($agent_apply['check_status'])) return $this->error('您已经提交申请,请等待客服审核');
- $setting = \Yii::$app->services->setting->addons->get($this->mall_id, AgentEnum::ADDONS_NAME, 'more');
- if (empty($setting['is_check'])) $params['check_status'] = 1; //不需要审核
- $res = AgentApply::setData($params);
- if ($res === false) return $this->error(AgentApply::getStaticError());
- return $this->success('操作成功');
- }
- if (\Yii::$app->request->isGet) {
- $params['mall_id'] = $mall_id;
- $params['user_id'] = $this->user_id;
- $agent_apply = AgentApply::getOne([['user_id' => $this->user_id],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
- ['mall_id' => $this->mall_id]], true, [], 'id desc','id,user_id,check_status,check_remark');
- if(empty($agent_apply)){$agent_apply['check_msg']='您不是渠道分红';$agent_apply['check_status']=StatusEnum::DELETE;}
- if(!empty($agent_apply)){
- if($agent_apply['check_status'] ==0) $agent_apply['check_msg']='您已提交申请,请等待审核';
- if($agent_apply['check_status'] ==2) $agent_apply['check_msg']=$agent_apply['check_remark']??'';
- }
- //获取分红配置
- $agent_apply['setting'] = \Yii::$app->services->setting->addons->get($this->mall_id, AgentEnum::ADDONS_NAME, 'more');
- return $this->success('操作成功',$agent_apply);
- }
- }
- /**
- * @desc:团队购买明细
- * @return mixed|null
- */
- public function actionTeamBuyList()
- {
- $more_setting = \Yii::$app->services->setting->addons->get($this->mall_id, AgentEnum::ADDONS_NAME, 'more');
- $is_enable_team_buy_detail = $more_setting['is_enable_team_buy_detail'] ?? 0;
- $order_list = [];
- $order_status = OrderStatusEnum::ACCOMPLISH;
- if($is_enable_team_buy_detail){
- $compute_type = $more_setting['compute_type'] ?? 1;
- $child_ids = UserPartitionRelationship::getChildIdArr($this->user_id);
- if(!empty($child_ids)){
- $where = [
- ['mall_id'=>$this->mall_id],
- ['user_id'=>$child_ids],
- ['status'=>StatusEnum::ENABLED],
- ['pay_status' => StatusEnum::ENABLED]
- ];
- if ($compute_type == 1) {
- $order_status = [OrderStatusEnum::PAY,OrderStatusEnum::SHIPMENTS,OrderStatusEnum::SING,OrderStatusEnum::ACCOMPLISH];
- }
- $where[] = ['order_status' => $order_status];
- $select = 'id,order_no,user_id,pay_time';
- $params['order'] = 'pay_time desc';
- $params['select'] = $select;
- $params['where'] = $where;
- $params['with'] = ['user' => function($query){
- $query->select('id,nickname,mobile,avatar_url');
- }];
- $params['page'] = \Yii::$app->request->get('page') ?? 1;
- $params['limit'] = \Yii::$app->request->get('limit') ?? 10;
- $order_list = Order::listPage($params);
- $order_ids = array_column($order_list['list'],'id');
- $order_goods_num = OrderDetail::find()->where(['order_id'=>$order_ids,'mall_id'=>$this->mall_id])->groupBy('order_id')->select('order_id,sum(num) as goods_num')->indexBy('order_id')->asArray()->all();
- foreach($order_list['list'] as $key =>$item){
- $item['goods_num'] = $order_goods_num[$item['id']]['goods_num'] ?? 0;
- $item['pay_time'] = date('Y-m-d H:i:s',$item['pay_time']);
- $order_list['list'][$key] = $item;
- }
- //昨日订单商品总数:
- $yesterday_order_goods_num = OrderDetail::find()->where(['mall_id'=>$this->mall_id,'order_status'=> $order_status,'user_id' => $child_ids])->andWhere(['between','updated_at',strtotime(date('Y-m-d',strtotime("-1 day"))),strtotime(date('Y-m-d',strtotime("-1 day"))) + 86400])->sum('num') ?? 0;
- //累计订单总数:200
- $total_order_goods_num = OrderDetail::find()->where(['mall_id'=>$this->mall_id,'order_status'=> $order_status,'user_id' => $child_ids])->sum('num') ?? 0;
- }
- $order_list['yesterday_order_num'] = $yesterday_order_goods_num ?? 0;
- $order_list['total_order_num'] = $total_order_goods_num ?? 0;
- }
- return $this->success('操作成功',$order_list);
- }
- }
|