<?php


namespace app\controller\api\user;

//use app\common\repositories\user\UserRepository;
//use crmeb\basic\BaseController;
//use think\App;
use think\Exception;
use think\facade\Db;
use think\facade\Log;

class Volunteer //extends BaseController
{
//    public $repository;
//
//    /**
//     * Volunteer constructor.
//     * @param App $app
//     * @param UserRepository $repository
//     */
//    public function __construct(App $app, UserRepository $repository)
//    {
//        parent::__construct($app);
//        $this->repository = $repository;
//    }

    /**
     * 志愿者升级
     * */
    public function doUpgrade($uid=0){
        set_time_limit(0);
        try {
            //志愿者条件
            $volunteer_achievement = systemConfig("volunteer_achievement");//volunteer_achievement:用户本人满足养老金要求可升级为志愿者
            //一星条件
            $a_volunteer_achievement = systemConfig("a_volunteer_achievement");//团队养老金要求
            $a_volunteer_number = systemConfig("a_volunteer_number");//团队人数要求
            //二星条件
            $b_volunteer_achievement = systemConfig("b_volunteer_achievement");//团队养老金要求
            $b_volunteer_number = systemConfig("b_volunteer_number");//团队人数要求
            //三星条件
            $c_volunteer_achievement = systemConfig("c_volunteer_achievement");//团队养老金要求
            $c_volunteer_number = systemConfig("c_volunteer_number");//团队人数要求
            //董事条件
            $d_volunteer_achievement = systemConfig("d_volunteer_achievement");//团队养老金要求
            $d_volunteer_number = systemConfig("d_volunteer_number");//团队人数要求

            //用户列表
            if($uid > 0){
                $userList = Db::name("User") -> where("uid",$uid)-> field("uid")-> select();
            }else{
                $userList = Db::name("User")-> field("uid")-> select();
            }
            foreach($userList as $key => $value){
                $exist = Db::name("Volunteer") -> where(["uid"=>$value['uid']])->find();
                if(!$exist || $exist['grade'] < 1){
                    continue;
                }
                $volunteer = $this -> volunteer($value['uid']);
                //本人养老金小于志愿者要求
                if($volunteer['self_yanglao'] < $volunteer_achievement){
                    continue;
                }
                //满足志愿者要求
                if($volunteer['self_yanglao'] >= $volunteer_achievement){
                    //志愿者
                    if($volunteer['team'] < $a_volunteer_number || $volunteer['yanglao'] < $a_volunteer_achievement){
                        $this -> grade($value['uid'],1,$volunteer);
                    }
                    //一星
                    if ($volunteer['team'] >= $a_volunteer_number && $volunteer['yanglao'] >= $a_volunteer_achievement//满足一星条件
                        &&
                        ($volunteer['team'] < $b_volunteer_number || $volunteer['yanglao'] < $b_volunteer_achievement)//未满足二星条件
                    ){
                        $this -> grade($value['uid'],2,$volunteer);
                    }
                    //二星
                    if ($volunteer['team'] >= $b_volunteer_number && $volunteer['yanglao'] >= $b_volunteer_achievement//满足二星条件
                        &&
                        ($volunteer['team'] < $c_volunteer_number || $volunteer['yanglao'] < $c_volunteer_achievement)//未满足三星条件
                    ){
                        $this -> grade($value['uid'],3,$volunteer);
                    }
                    //三星
                    if ($volunteer['team'] >= $c_volunteer_number && $volunteer['yanglao'] >= $c_volunteer_achievement//满足三星条件
                        &&
                        ($volunteer['team'] < $d_volunteer_number || $volunteer['yanglao'] < $d_volunteer_achievement)//未满足董事条件
                    ){
                        $this -> grade($value['uid'],4,$volunteer);
                    }

                    //董事
                    if($volunteer['team'] >= $d_volunteer_number && $volunteer['yanglao'] >= $d_volunteer_achievement){
                        $this -> grade($value['uid'],5,$volunteer);
                    }
                }
                //团队各等级数量统计
                $this -> statistics($value['uid']);

            }
        }catch (Exception $e){
            Log::info('志愿者升级：'.$e->getMessage());
        }

    }
    //志愿者信息
    public function volunteer($uid){
        if(!$uid){
            return false;
        }
        //本人养老金
        $data['self_yanglao'] = $this -> getYanglao($uid);
        //团队人数
        $data['team'] = $this -> volunteer_number($uid);
        //团队养老金
        $data['yanglao'] = $this -> volunteer_achievement($uid);
        //直推人数
        $data['extension'] = Db::name("user_identity_log")->where(["uid"=>$uid])->count();
        //直推商户数量
        $data['shop'] = Db::name("user_identity_log")->where(["uid"=>$uid,'identity'=>4])->whereNotIn("identity","0")->count();
        return $data;
    }
    //团队人数
    public function volunteer_number($uid){
        $number = 0;
        $number1=Db::name("user_identity_log")->where(["uid"=>$uid])->field("link_id") -> select();
        $number += count($number1);
        foreach ($number1 as $key => $value){
            $number += Db::name("user_identity_log")->where(["uid"=>$value['link_id']])->field("link_id") -> count();
        }
        return $number;
    }
    //团队养老金
    public function volunteer_achievement($uid){
        $yanglao = 0;
        $number1=Db::name("user_identity_log")->where(["uid"=>$uid])->field("link_id") -> select();
        foreach ($number1 as $key => $value){
            $yanglao += $this -> getYanglao($value['link_id']);
            $number2 = Db::name("user_identity_log")->where(["uid"=>$value['link_id']])->field("link_id") -> select();
            foreach ($number2 as $k => $v){
                $yanglao += $this -> getYanglao($v['link_id']);
            }
            unset($number2);
        }
        return $yanglao;
    }
    //用户养老金
    public function getYanglao($uid){

        $BeginDate = date('Y-m-01', strtotime(date("Y-m-d")));
        $endDate = date('Y-m-d', strtotime("$BeginDate +1 month -1 day"));
        return Db::name("UserBill")
            -> where("`uid` = $uid and `commission_type` = 5 and `status` = 1 and `pm` = 1 and `create_time` >= $BeginDate and `create_time` <= $endDate")
            -> sum("number");
    }
    //升级
    public function grade($uid,$grade,$volunteer){
        $exist = Db::name("Volunteer") -> where(["uid"=>$uid])->find();
        if($exist){
            Db::name("Volunteer")
                -> where(["uid"=>$uid])
                -> update([
                    "grade" => $grade,
                    "team" => $volunteer['team'],
                    "yanglao" => $volunteer['yanglao'],
                    "extension" => $volunteer['extension'],
                    "shop" => $volunteer['shop'],
                    "time" => time()
                ]);
            return true;
        }
        Db::name("Volunteer")
            -> insert([
                    "uid" => $uid,
                    "grade" => $grade,
                    "team" => $volunteer['team'],
                    "yanglao" => $volunteer['yanglao'],
                    "extension" => $volunteer['extension'],
                    "shop" => $volunteer['shop'],
                    "time" => time()
                ]
            );
        return true;
    }
    //下级各等级数量统计
    public function statistics($uid){
        $data = [
            'v' => 0,
            'v1' => 0,
            'v2' => 0,
            'v3' => 0,
            'v4' => 0
        ];
        $fns = function($uid) use (&$fns,&$data){

            $identity = Db::name("user_identity_log")
                -> alias("u")
                -> join("Volunteer v","u.link_id = v.uid")
                -> where(["u.uid"=>$uid])
                -> field("u.link_id,v.grade")
                -> select();

            if($identity){
                foreach ($identity as $key => $value){
                    if($value['grade'] == 1){
                        $data['v']++;
                    }
                    if($value['grade'] == 2){
                        $data['v1']++;
                    }
                    if($value['grade'] == 3){
                        $data['v2']++;
                    }
                    if($value['grade'] == 4){
                        $data['v3']++;
                    }
                    if($value['grade'] == 5){
                        $data['v4']++;
                    }
                    $fns($value['link_id']);
                }
            }
        };
        $fns($uid);
        Db::name("Volunteer")
            -> where(["uid"=>$uid])
            -> update([
                "v" => $data['v'],
                "v1" => $data['v1'],
                "v2" => $data['v2'],
                "v3" => $data['v3'],
                "v4" => $data['v4'],
                "time" => time()
            ]);
        return $data;
    }






}