<?php
/**
 *  绝对人气 大屏控制器
 *
 * ============================================================================
 * 版权所有 2017北京素玄科技，并保留所有权利。
 *
 * 网站地址: http://www.suxuantech.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！未经允许的情况下，您不能对本系统代码做任何修改 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * 如有修改需求，请联系素玄科技有限公司：contact@suxuantech.cn
 * ============================================================================
 * $Author: lilong<lilong@suxuantech.cn> $
 */
namespace app\crm\controller;
use think\Controller;
// use app\index\controller\Base;
use think\Queue;
use think\Request;

class Jdrq extends Controller{
    // 绝对人气推广店
    protected  $shop_code = ['JDRQ02'];
    // 网销组所在部门
    protected  $wxGroupDep = [
        'id'=>19,
        'department_name'=>'网络邀约部',
    ];
    // 今天
    protected  $today = [
        'start'=>'', 'end'  =>'',
    ];
    // 昨天
    // protected  $yestoday = [
        // 'start'=>'', 'end'  =>'',
    // ];
    // 本月一号和当天
    protected  $thisMonth = [
        'start'=>'', 'end'  =>'',
    ];
    // 本月每一天
    protected  $thisMonthAllDay = [];
    public function _initialize(){
        // 今天
        $this->today['start']=  date('Y-m-d 00:00:00');
        $this->today['end']  =  date('Y-m-d 23:59:59');
        // 昨天
        $this->yestoday['start']= date('Y-m-d 00:00:00',strtotime('-1 day'));
        $this->yestoday['end']  = date('Y-m-d 23:59:59',strtotime('-1 day'));
        // 本月
        $this->thisMonth['start']= date('Y-m-01 00:00:00');
        $this->thisMonth['end']  = date('Y-m-d 23:59:59');
        // 本月每一天
        $date = getdateArea('this_month');
        $this->thisMonthAllDay = getDateFromRange($date[0], $date[1]);
    }
    // 员工大屏页面  2018-07-26
    public function staff(){
        // $auth = controller('crm/Bigscreen', 'lib')->checkBigScreenAuth();
        // if ($auth === false) {
        //     controller('crm/Index','controller')->index();
        //     exit;
        // }
        
        $this->assign('title','绝对人气 【网销实时大数据】');
        return $this->fetch();
    }
    // 员工移动端大屏
    public function staffMobile(){
        // $auth = controller('crm/Bigscreen', 'lib')->checkBigScreenAuth();
        // if ($auth === false) {
        //     controller('crm/Index','controller')->index();
        //     exit;
        // }
        $this->assign('title','绝对人气 【网销实时大数据】');
        return $this->fetch();
    }
    // 第一部分 
    public function staffBaseData(){
        //本月
        $mdate = getdateArea('this_month');
        $mData['start'] = $this->thisMonth['start'];
        $mData['end']   = $this->thisMonth['end'];
        $mData['shop_code']= $this->shop_code;
        //今天
        $tData['start']    = $this->today['start'];
        $tData['end']      = $this->today['end'];
        $tData['shop_code']= $this->shop_code;
        // 今日电商总线索  即  推广今日录入的客资总数
        $tmkz = controller('crm/Jdrq', 'lib')->getCustomerNum($tData);
        $mmkz = controller('crm/Jdrq', 'lib')->getCustomerNum($mData);
        // 今日待联系新线索 即  今日录入的 一次都没有追踪过的客资 (待追踪)
        $dzz = controller('crm/Jdrq', 'lib')->getDzzNum($tData);
        // 电商客服在线数
        $onlinestaff = controller('crm/Jdrq', 'lib')->getOnlineStaffs($tData);
        // 本月成交冠军
        $allOrderStaff = controller('crm/Jdrq', 'lib')->getAllStaffOrders($mData);
        $maxOrderStaff = array_shift($allOrderStaff);
        // 返回数据
        $returnData['tmkz'] = $tmkz;
        $returnData['mmkz'] = $mmkz;
        $returnData['dzz']  = $dzz;
        $returnData['onlinestaff']  = $onlinestaff;
        $returnData['maxOrderStaff']= $maxOrderStaff;
        return json($returnData);
    }
    // 第二部分  1今日网销各组订单量
    public function todayWxGroupOrder(){
        //今天
        $tData['start']    = $this->today['start'];
        $tData['end']      = $this->today['end'];
        $tData['shop_code']= $this->shop_code;
        // 所有的组
        $wxGroupDep = $this->wxGroupDep;
        $allGroups = controller('crm/Jdrq', 'lib')->getAllGroups($wxGroupDep);
        $gids = '';
        foreach ($allGroups as $key => $value) {
            $gids .= $value['id'].',';
        }
        $gids = trim($gids,',');
        $tData['gids']= $gids;
        // 所有组的订单
        $allGroupOrder = controller('crm/Jdrq', 'lib')->getAllGroupOrders($tData);
        $allData = [];
        foreach ($allGroups as $key => $value) {
            $temp['sales_group_name'] = $value['group_name'];
            $temp['orderNum'] = $allGroupOrder[$value['id']]['orderNum'] ? $allGroupOrder[$value['id']]['orderNum'] : 0;
            $allData[] = $temp;
        }
        $return['names'] = array_column($allData,'sales_group_name');
        $return['datas'] = array_column($allData,'orderNum');
        return json($return);
    }
    // 第二部分  2本月网销各组订单量
    public function monthWxGroupOrder(){
        //本月
        $mdate = getdateArea('this_month');
        $mData['start'] = $this->thisMonth['start'];
        $mData['end']   = $this->thisMonth['end'];
        $mData['shop_code']= $this->shop_code;
        // 所有的组
        $wxGroupDep = $this->wxGroupDep;
        $allGroups = controller('crm/Jdrq', 'lib')->getAllGroups($wxGroupDep);
        $gids = '';
        foreach ($allGroups as $key => $value) {
            $gids .= $value['id'].',';
        }
        $gids = trim($gids,',');
        $mData['gids']= $gids;
        // 所有组的订单
        $allGroupOrder = controller('crm/Jdrq', 'lib')->getAllGroupOrders($mData);
        $allData = [];
        foreach ($allGroups as $key => $value) {
            $temp['sales_group_name'] = $value['group_name'];
            $temp['orderNum'] = $allGroupOrder[$value['id']]['orderNum'] ? $allGroupOrder[$value['id']]['orderNum'] : 0;
            $allData[] = $temp;
        }
        $return['names'] = array_column($allData,'sales_group_name');
        $return['datas'] = array_column($allData,'orderNum');
        return json($return);
    }
    // 第二部分  3本月网销各组线索转化率
    public function monthWxGroupCusRate(){
        //本月
        $mdate = getdateArea('this_month');
        $mData['start'] = $this->thisMonth['start'];
        $mData['end']   = $this->thisMonth['end'];
        $mData['shop_code']= $this->shop_code;
        // 所有的组
        $wxGroupDep = $this->wxGroupDep;
        $allGroups = controller('crm/Jdrq', 'lib')->getAllGroups($wxGroupDep);
        $gids = '';
        foreach ($allGroups as $key => $value) {
            $gids .= $value['id'].',';
        }
        $gids = trim($gids,',');
        $mData['gids']= $gids;
        // 所有组的订单
        $allGroupOrder = controller('crm/Jdrq', 'lib')->getAllGroupOrders($mData);
        // 所有组的客资
        $allGroupCus = controller('crm/Jdrq', 'lib')->getAllGroupCus($mData);
        // 返回的数组
        $return = [];
        // 循环所有组
        foreach ($allGroups as $key => $value) {
            $temp['group_id'] = $value['id'];
            $temp['group_name'] = $value['group_name'];
            $temp['cusNum']  = $allGroupCus[$value['id']]['cusNum'];
            $temp['orderNum']= $allGroupOrder[$value['id']]['orderNum'];
            $temp['cusRate'] = $temp['orderNum'] ? sprintf('%.2f',$temp['orderNum']/$temp['cusNum']*100) : '0';
            $return[] = $temp;

            $arr['group_id'] = '0';
            $arr['group_name'] = '总计';
            $arr['cusNum'] += $temp['cusNum'];
            $arr['orderNum'] += $temp['orderNum'];
        }
        $arr['cusRate'] = $arr['orderNum'] ? sprintf('%.2f',$arr['orderNum']/$arr['cusNum']*100) : '0';
        array_push($return, $arr);
        return json($return);
    }
    // 第二部分  4本月网销各组订单实收
    public function monthWxOrderFact(){
        //本月
        $mdate = getdateArea('this_month');
        $mData['start'] = $this->thisMonth['start'];
        $mData['end']   = $this->thisMonth['end'];
        $mData['shop_code']= $this->shop_code;
        // 所有的组
        $wxGroupDep = $this->wxGroupDep;
        $allGroups = controller('crm/Jdrq', 'lib')->getAllGroups($wxGroupDep);
        $gids = '';
        foreach ($allGroups as $key => $value) {
            $gids .= $value['id'].',';
        }
        $gids = trim($gids,',');
        $mData['gids']= $gids;
        // 所有组的订单
        $allGroupFactMoney = controller('crm/Jdrq', 'lib')->getAllGroupFactMoney($mData);
        // 返回的数组
        $return = [];
        // 循环所有组
        foreach ($allGroups as $key => $value) {
            $temp['group_id'] = $value['id'];
            $temp['group_name'] = $value['group_name'];
            $temp['factMoney']= sprintf('%.2f',$allGroupFactMoney[$value['id']]['factMoney']);
            $return[] = $temp;
            $arr['group_id'] = '0';
            $arr['group_name'] = '总计';
            $arr['factMoney'] += $temp['factMoney'];
            $arr['factMoney'] = sprintf('%.2f',$arr['factMoney']);
        }
        array_push($return, $arr);
        return json($return);
    }
    // 第三部分  1网销月订单量排行榜
    public function monthWxStaffOrder(){
        // 本月
        $mdate = getdateArea('this_month');
        $mData['start'] = $this->thisMonth['start'];
        $mData['end']   = $this->thisMonth['end'];
        $mData['shop_code']= $this->shop_code;
        $allStaffOrders = controller('crm/Jdrq', 'lib')->getAllStaffOrders($mData);
        $allStaffOrders = my_sort($allStaffOrders, 'orderNum');
        $return['names'] = array_column($allStaffOrders,'sales_staff');
        $return['datas'] = array_column($allStaffOrders,'orderNum');
        return json($return);
    }
    // 第三部分  2网销月线索订单率
    public function monthWxStaffCusRate(){
        // 本月
        $mdate = getdateArea('this_month');
        $mData['start'] = $this->thisMonth['start'];
        $mData['end']   = $this->thisMonth['end'];
        $mData['shop_code']= $this->shop_code;
        $allStaffOrders = controller('crm/Jdrq', 'lib')->getAllStaffOrders($mData);
        $allStaffCus = controller('crm/Jdrq', 'lib')->getAllStaffCus($mData);

        $allData = [];
        foreach ($allStaffCus as $key => $value) {
            $temp['sales_staff'] = $value['sales_staff'];
            $temp['cusNum'] = $value['cusNum'];
            $temp['orderNum'] = $allStaffOrders[$value['sales_staff']]['orderNum'];
            $temp['cusRate'] = $temp['orderNum'] ? sprintf('%.2f',$temp['orderNum']/$temp['cusNum']*100) : '0%';
            $allData[]=$temp;
        }
        $allData = my_sort($allData, 'cusRate');
        $return['names'] = array_column($allData,'sales_staff');
        $return['datas'] = array_column($allData,'cusRate');
        $return['cusNum'] = array_column($allData,'cusNum');
        $return['orderNum'] = array_column($allData,'orderNum');
        return json($return);
    }
}