<?php
/*
 * @Descripttion:
 * @version:
 * @Author: kangkang
 * @Date: 2020-10-13 19:52:37
 * @LastEditors: kangkang
 * @LastEditTime: 2020-11-16 16:59:56
 */

namespace app\manager\controller;

use app\manager\model\Manager;
use app\manager\model\ProductOrder;
use app\manager\model\User;
use enum\user\User as UserEnum;
use fastphp\base\Controller;

class SetUser extends Controller
{
    /**
     * @description: 获取管理员列表
     * @param {*}
     * @return {*}
     */
    public function getManagerList()
    {
        $manager_model = new Manager;
        $list = $manager_model->getAllList();
        \result($list, '', 30000);
    }
    /**
     * @description: 给客户经理自动分配用户
     * @param {*}
     * @return {*}
     */
    public function autoSet()
    {
        $manager_list = json_decode(file_get_contents("php://input"), true)['manager_list'];
        if (empty($manager_list)) {
            die;
        }
        $manager_model = new Manager;
        $cost_user_id = [];
        $manager_list = $manager_model->getAllList(['id' => ['in', $manager_list]]);
        $product_order_model = new ProductOrder;
        $user_model = new User;
        $userid = [];
        //获取到上个月今天为止所有消费客户  --- 改为全部
        $user_cost = $product_order_model->getCost();
        $count = count($user_cost);
        $manager_count = count($manager_list);
        for ($i = 0; $i < ($count / 2); $i++) {
            //根据管理员人数来进行等量分配，第一个用户和最后一个用户相互搭配、依次分给客户经理
            $jingli_key = $manager_list[$i % $manager_count]['id'];
            $userid[$jingli_key][] = $user_cost[$i]['UserId'];
            $userid[$jingli_key][] = $user_cost[($count - $i - 1)]['UserId'];
            $cost_user_id[] = $user_cost[$i]['UserId'];
            $cost_user_id[] = $user_cost[($count - $i - 1)]['UserId'];
        }
        //获取所有为处理的用户 然后再进行随机分配
        $user_not_cost = $user_model->getList(['Id' => ['NOT IN', $cost_user_id],'agent_id'=>0], 'Id');
        $user_not_cost_id = [];
        for ($i = 0; $i < count($user_not_cost); $i++) {
            //根据管理员人数来进行等量分配，依次分给客户经理
            $jingli_key = $manager_list[$i % $manager_count]['id'];
            $user_not_cost_id[$jingli_key][] = $user_not_cost[$i]['Id'];
        }
        //处理消费过的用户
        foreach ($manager_list as $manager) {
            $user_model->updateOne(['id' => ['in', $userid[$manager['id']]],'agent_id'=>0], ['ManagerId' => $manager['id'], 'ManagerName' => $manager['RealName']]);
            $user_model->updateOne(['id' => ['in', $user_not_cost_id[$manager['id']]],'agent_id'=>0], ['ManagerId' => $manager['id'], 'ManagerName' => $manager['RealName']]);
        }

        \result();
    }

    /**
     * @description: 获取客户经理拥有的用户
     * @param {*}
     * @return {*}
     */
    public function getManagerUser()
    {
        if (!isset($_GET['manager_id'])) {
            die;
        }

        $page = 0;
        $search_info = '';
        $last_month = date("Y-m-d H:i:s", strtotime("-1 month"));
        $new_month = date("Y-m-d H:i:s", time());
        $manager_id = $_GET['manager_id'];
        $where = [];

        if (isset($_GET['page'])) {
            $page = ($_GET['page'] - 1) * 50;
        }
        if (!empty($_GET['search_info'])) {
            $search_info = $_GET['search_info'];
            $where['LoginCode'] = $search_info;
        }

        $where['ManagerId'] = $manager_id;

        $user_model = new User;
        $product_order_model = new ProductOrder;

        $manager_user = $user_model->getListPage($where, 'Id,LoginCode,CreateTime,ManagerName', 'id desc', "$page,50");
        $user_id_list = [];
        $manager_user_list = [];
        foreach ($manager_user as $key => $val) {
            $user_id_list[] = $val['Id'];
            $val['cost'] = 0;
            $manager_user_list[$val['Id']] = $val;
        }

        $user_cost = $product_order_model->getMonthCost($user_id_list);
        foreach ($user_cost as $k => $v) {
            $manager_user_list[$v['UserId']]['cost'] = $v['money'];
        }

        $data = [
            'list' => array_merge($manager_user_list),
            'count' => $user_model->getCount(['ManagerId' => $manager_id])['count'],
        ];
        \result($data);
    }

    /**
     * @description: 获取所有管理员有多少用户
     * @param {*}
     * @return {*}
     */
    public function getAllManagerUserCount()
    {
        $screen = [];   
        $user_model = new User;
        $screen_result = [];
        if (isset($_GET['screen'])) {
            $screen = $_GET['screen'];
            $product_order_model = new ProductOrder;
            //获取所有符合条件的数据
            $order_list = $product_order_model->getScreenMonthCost($screen);
            $user_id_list = [];
            foreach($order_list as $k=>$v){
                $user_id_list[] = $v['UserId'];
            }
            $where = [
                'follow_status' => $screen['follow_status'],
                'id' => ['in',$user_id_list]
            ];
            //获取结果
            $screen_result = $user_model->getScreenUserCount($where);
        }

        $data = [];
        $list = $user_model->getAllManagerUserCount();
        
        $result_list = [];
        $manager_jingli_list = [];
        //将managerid作为key 方便后边判断赋值
        foreach($list as $info){
            $info['screen_num'] = 0;
            $result_list[$info['ManagerId']] = $info;
            $temp['value'] = $info['ManagerId'];
            $temp['label'] = $info['ManagerName'];
            $manager_jingli_list[] = $temp;
        }
        if(!empty($screen_result)){
            //为筛选结果赋值
            foreach($screen_result as $info){
                if(isset($result_list[$info['ManagerId']])){
                    $result_list[$info['ManagerId']]['screen_num'] = $info['count_num'];
                }
            }
        }

        $follow_status = UserEnum::getFormatList(UserEnum::$FollowStatus);
        $data = [
            'list' => array_merge($result_list),
            'follow_status' => $follow_status,
            'manager_jingli_list' => $manager_jingli_list
        ];
        \result($data);
    }

    /**
     * @description: 执行外分 将指定客户经理的部分用户分配出去
     * @param {*}
     * @return {*}
     */
    public function waifen()
    {
        $screen = [];   
        if (isset($_GET['screen'])) {
            $user_model = new User;
            $manager_model = new Manager;
            $screen = $_GET['screen'];
            $manager_id = $screen['jingli_id'];
            $manager_main = $_GET['manager_id'];
            $manager_name = $manager_model->getOne(['id'=>$manager_id])['RealName'];
            $num = $_GET['num'];
            $product_order_model = new ProductOrder;
            //获取所有符合条件的数据
            $order_list = $product_order_model->getScreenMonthCost($screen);
            $user_id_list = [];
            foreach($order_list as $k=>$v){
                $user_id_list[] = $v['UserId'];
            }
            $where = [
                'follow_status' => $screen['follow_status'],
                'Id' => ['in',$user_id_list],
                'ManagerId' => $manager_main
            ];
            $data = [
                'ManagerId' => $manager_id,
                'ManagerName' => $manager_name
            ];
            // dump($where);
            // dump($data);
            //获取结果
            $result = $user_model->setManager($where,$data,$num);
            if($result){
                \result();
            } else {
                die;
            }
        }
    }
}
