<?php
namespace app\api\controller\v1;

use app\api\controller\Base;
use app\hander\HelpHander;
use think\Db;
use think\helper\Time;

class Statistics extends Base
{
    protected $middleware = [];

    protected $orgId = 4;

    // 用户结构统计
    public function userStructure(){
        // 基建中心人数
        $count1 = model('UserInfo')->getCountByCompany(1);
        // 新华人数
        $count2 = model('UserInfo')->getCountByCompany(2);
        // 其他
        $count0 = model('UserInfo')->getCountByCompany(0);

        $data = [
            [
                "name" => "中心",
                "value" => $count1,
            ],
            [
                "name" => "新华",
                "value" => $count2,
            ],
            [
                "name" => "其他",
                "value" => $count0,
            ]
        ];
        $legend = ['中心','新华','其他'];
        HelpHander::success(['legend' => $legend,'data'=>$data]);
    }

    // 用户性别结构统计
    public function userGenderStructure(){
        // 男
        $count1 = model('UserInfo')->getCountByGender(1);
        // 女
        $count2 = model('UserInfo')->getCountByGender(2);

        $data = [
            [
                "name" => "男",
                "value" => $count1,
            ],
            [
                "name" => "女",
                "value" => $count2,
            ]
        ];
        $legend = ['男','女'];
        HelpHander::success(['legend' => $legend,'data'=>$data]);
    }

    // 用户性别结构统计
    public function userGenderStructure2(){
        $count = model('UserInfo')->getCountByGender2(0);
        $count1 = model('UserInfo')->getCountByGender2(1);
        $count2 = model('UserInfo')->getCountByGender2(2);

        $legend = ['男士','女士'];
        $xdata = ['全部','中心','新华'];
        $data1 = [$count['c1'],$count1['c1'],$count2['c1']];
        $data2 = [$count['c2'],$count1['c2'],$count2['c2']];
        HelpHander::success(['legend' => $legend,'xdata'=>$xdata,'data1'=>$data1,'data2'=>$data2]);
    }

    // 用户党员结构统计
    public function userDangStructure(){
        $count = model('UserInfo')->getDangStructure(0);
        $count1 = model('UserInfo')->getDangStructure(1);
        $count2 = model('UserInfo')->getDangStructure(2);

        $legend = ['党员','非党员'];
        $xdata = ['全部','中心','新华'];
        $data1 = [$count['c1'],$count1['c1'],$count2['c1']];
        $data2 = [$count['c2'],$count1['c2'],$count2['c2']];
        HelpHander::success(['legend' => $legend,'xdata'=>$xdata,'data1'=>$data1,'data2'=>$data2]);
    }

    // 当前个状态人员统计
    public function userSubsidiary(){
        $lists = Db::name('subsidiary')->select();
        $lists = $lists?$lists:[];

        $legend = [];
        $data = [];
        foreach ($lists as $k=>$v){
            $legend[] = $v['title'];
            $data[] = $v['count'];
        }
        HelpHander::success(['legend' => $legend,'data'=>$data]);
    }

    // 人员状态统计
    public function userWorkStructure(){
        $lists = [
            [
                'id' => 1,
                'title' => '试用期'
            ],
            [
                'id' => 2,
                'title' => '正式'
            ],
            [
                'id' => 3,
                'title' => '离职'
            ],
            [
                'id' => 4,
                'title' => '退休'
            ],
        ];

        $legend = [];
        $data = [];
        foreach ($lists as $k=>$v){
            $count = model('UserInfo')->getCountByWork($v['id']);
            $legend[] = $v['title'];
            $data[] = $count;
        }
        HelpHander::success(['legend' => $legend,'data'=>$data]);
    }

    // 人员统计
    public function userTypeStructure(){
        $lists = [
            [
                'id' => 1,
                'title' => '管理人员'
            ],
            [
                'id' => 2,
                'title' => '专业技术人员'
            ],
            [
                'id' => 3,
                'title' => '工勤技能人员'
            ]
        ];

        $legend = [];
        $data = [];
        foreach ($lists as $k=>$v){
            $count = model('UserInfo')->getCountByType($v['id']);
            $legend[] = $v['title'];
            $data[] = $count;
        }
        HelpHander::success(['legend' => $legend,'data'=>$data]);
    }

    // 收入支出统计
    public function financeStructure($type = 1){ // 1=当天 2=当月 3=当年
        // 收入
        $income = model('FinanceLog')->financeStructure($this->orgId,$type,1);
        // 支出
        $expenditure = model('FinanceLog')->financeStructure($this->orgId,$type,2);

        $data = [
            [
                "name" => "收入",
                "value" => $income,
            ],
            [
                "name" => "支出",
                "value" => $expenditure,
            ]
        ];
        $yy = $income - $expenditure; // 盈余
        $legend = ['收入','支出'];
        HelpHander::success(['legend' => $legend,'yy'=>$yy,'data'=>$data]);
    }

    // 工资支出统计  $type 1=中心编制 2=新华聘用 3=中心劳务 4=新华劳务 5=中心养老
    public function financeSalaryStructure($type = 1){ // 1=当月 2=当年
        // 中心在编
        $expenditure1 = model('SalaryRecord')->financeSalaryStructure($type,1);
        // 新华聘用
        $expenditure2 = model('SalaryRecord')->financeSalaryStructure($type,2);
        // 中心劳务
        $expenditure3 = model('SalaryRecord')->financeSalaryStructure($type,3);
        // 新华劳务
        $expenditure4 = model('SalaryRecord')->financeSalaryStructure($type,4);
        // 中心养老
        $expenditure5 = model('SalaryRecord')->financeSalaryStructure($type,5);

        $data = [
            [
                "name" => "中心在编",
                "value" => $expenditure1,
            ],
            [
                "name" => "中心养老",
                "value" => $expenditure5,
            ],
            [
                "name" => "新华聘用",
                "value" => $expenditure2,
            ],
            [
                "name" => "新华劳务",
                "value" => $expenditure4,
            ],
            [
                "name" => "中心劳务",
                "value" => $expenditure3,
            ],
        ];
        $total = $expenditure1 + $expenditure2 + $expenditure3 + $expenditure4 + $expenditure5;
        $legend = ['中心在编','中心养老','中心劳务','新华聘用','新华劳务'];
        HelpHander::success(['legend' => $legend,'total'=>$total,'data'=>$data]);
    }

    // 经营性房产统计
    public function house(){
        $lists = [
            [
                'id' => 0,
                'title' => '未租赁'
            ],
            [
                'id' => 1,
                'title' => '已租赁'
            ]
        ];

        $counts = model('House')->getCountByLessee();

        $legend = [];
        $data = [];
        foreach ($lists as $k=>$v){
            $legend[] = $v['title'];
            if($v['id'] == 0){
                $data[] = [
                    'name' => $v['title'],
                    'value' => $counts['count2']
                ];
            }else{
                $data[] = [
                    'name' => $v['title'],
                    'value' => $counts['count1']
                ];
            }
        }

        HelpHander::success(['legend' => $legend,'total'=>$counts['total'],'data'=>$data]);
    }

    // 经营性房产合同金额统计
    public function houseContractMoney(){
        $count = model('House')->getHouseContractMoney();

        $legend = ['应收合同金额','实收金额'];
        $xdata = ['经营性房产'];
        $data1 = [$count['c1']];
        $data2 = [$count['c2']];
        HelpHander::success(['legend' => $legend,'xdata'=>$xdata,'data1'=>$data1,'data2'=>$data2]);
    }

    // 周转房房产统计
    public function houseTurn(){
        $lists = [
            [
                'id' => 0,
                'title' => '未入住'
            ],
            [
                'id' => 1,
                'title' => '已入住'
            ]
        ];

        $legend = [];
        $data = [];
        $total = 0;
        foreach ($lists as $k=>$v){
            $count = model('HouseTurn')->getCountByLessee($v['id']);
            $legend[] = $v['title'];
            $data[] = [
                'name' => $v['title'],
                'value' => $count
            ];
            $total += $count;
        }
        HelpHander::success(['legend' => $legend,'total'=>$total,'data'=>$data]);
    }

    public function houseTurnArea(){
        // 70及以下
        $count1 = model('HouseTurn')->getArea(1);
        // 70-90
        $count2 = model('HouseTurn')->getArea(2);
        // 90-110
        $count3 = model('HouseTurn')->getArea(3);
        // 110以上
        $count4 = model('HouseTurn')->getArea(4);

        $data = [
            [
                "name" => "70及以下",
                "value" => $count1,
            ],
            [
                "name" => "70-90",
                "value" => $count2,
            ],
            [
                "name" => "90-110",
                "value" => $count3,
            ],
            [
                "name" => "110以上",
                "value" => $count4,
            ]
        ];
        $legend = ['70及以下','70-90','90-110','110以上'];
        HelpHander::success(['legend' => $legend,'data'=>$data]);
    }

    public function contract(){
        $lists = [
            [
                'id' => 0,
                'title' => '未办结'
            ],
            [
                'id' => 1,
                'title' => '已办结'
            ]
        ];

        $legend = [];
        $data = [];
        foreach ($lists as $k=>$v){
            $count = model('Contract')->getCountByTransferred($v['id']);
            $legend[] = $v['title'];
            $data[] = [
                'name' => $v['title'],
                'value' => $count
            ];
        }
        HelpHander::success(['legend' => $legend,'data'=>$data]);
    }

    public function contractMoney(){
        $cur = date('Y-m');
        $curTime = strtotime($cur.'-01');
        $month = [];
        for ($i=5;$i>0;$i--){
            $date = strtotime("-".$i." month",$curTime);
            $month[] = date('Y-m',$date);
        }
        $month[] = $cur;

        $counts = [];
        foreach ($month as $k=>$v){
            $money = model('Contract')->getContractMoneyByMonth($v);
            $counts[] = $money;
        }
        $data = [
            "month" => $month,
            "counts" => $counts
        ];
        ajax_return_ok($data);
    }

    // 资产数量
    public function assetItem(){
        $lists = [
            [
                'id' => 1,
                'title' => '中心'
            ],
            [
                'id' => 2,
                'title' => '新华'
            ]
        ];

        $legend = [];
        $data = [];
        foreach ($lists as $k=>$v){
            $count = model('AssetItems')->getCountByCompany($v['id']);
            $legend[] = $v['title'];
            $data[] = [
                'name' => $v['title'],
                'value' => $count
            ];
        }
        HelpHander::success(['legend' => $legend,'data'=>$data]);
    }

    // 资产价值
    public function assetItemPrice(){
        $lists = [
            [
                'id' => 1,
                'title' => '中心'
            ],
            [
                'id' => 2,
                'title' => '新华'
            ]
        ];

        $legend = [];
        $data = [];
        foreach ($lists as $k=>$v){
            $count = model('AssetItems')->getMoneyByCompany($v['id']);
            $legend[] = $v['title'];
            $data[] = [
                'name' => $v['title'],
                'value' => $count
            ];
        }
        HelpHander::success(['legend' => $legend,'data'=>$data]);
    }

    public function contractPay(){
        $year = date('Y');
        $month = [];
        for ($i = 1;$i <= 12;$i++) {
            if($i >= 10){
                $month[] = $year.'-'.$i;
            }else{
                $month[] = $year.'-0'.$i;
            }
        }

        $money1 = [];
        $money2 = [];
        foreach ($month as $k=>$v){
            $start = $v.'-01 00:00:00';
            $end = date("Y-m-d H:i:s",strtotime("+1 month",strtotime($start)));
            $m1 = Db::name('contract_pay')
                ->where('ispay',1)
                ->where('status',1)
                ->where('create_time','>=',$start)
                ->where('create_time','<',$end)
                ->sum('money');
            $money1[] = [
                'month' => $v,
                'money' => $m1
            ];

            $m2 = Db::name('contract_pay')
                ->where('ispay',2)
                ->where('status',1)
                ->where('create_time','>=',$start)
                ->where('create_time','<',$end)
                ->sum('money');
            $money2[] = [
                'month' => $v,
                'money' => $m2
            ];
        }
        $data = [
            "month" => $month,
            "money1" => $money1,
            "money2" => $money2,
        ];
        HelpHander::success($data);
    }

    public function car(){
        list($start, $end) = Time::month();
        $start = date('Y-m-d H:i:s',$start);
        $end = date('Y-m-d H:i:s',$end);

        $carids = Db::name('car_record')
            ->where('status',1)
            ->where('start_time','>=',$start)
            ->where('start_time','<=',$end)
            ->group('car_id')
            ->distinct(true)
            ->column('car_id');
        if(!$carids){
            return ['total'=>0,'carcount' => 0, 'cars' => []];
        }

        $total = Db::name('car_record')
            ->where('status',1)
            ->where('start_time','>=',$start)
            ->where('start_time','<=',$end)
            ->count();

        $map[] = ['id','in',$carids];
        $cars = Db::name('car')
            ->where($map)
            ->field('id,title,sn')
            ->order('id asc')
            ->select();
        foreach ($cars as $k=>$v){
            $cars[$k]['count'] = Db::name('car_record')
                ->where('status',1)
                ->where('car_id',1)
                ->where('start_time','>=',$start)
                ->where('start_time','<=',$end)
                ->count();
        }

        return ['total'=>$total,'carcount' => count($carids), 'cars' => $cars?$cars:[]];
    }

    // 本月用印 official_seal ,用印审批id是90
    public function seal(){
        $approval_id = 90;
        list($start, $end) = Time::month();
        $start = date('Y-m-d H:i:s',$start);
        $end = date('Y-m-d H:i:s',$end);

        $applys = Db::name('apply')
            ->where('status',2)
            ->where('del',0)
            ->where('finish_time','>=',$start)
            ->where('finish_time','<=',$end)
            ->where('approval_id',$approval_id)
            ->field('id,form_json')
            ->select();

        $seals = [];
        foreach ($applys as $k=>$v){
            $lists[$k]['seallist'] = [];
            $formJson = json_decode($v['form_json'],true);
            foreach ($formJson as $kk=>$vv){
                if($vv['componentName'] == 'ddofficialsealfield'){
                    $seallist = json_decode($vv['values'],true);
                    $seallist = $seallist?$seallist:[];

                    $slist = [];
                    foreach ($seallist as $key=>$val){
                        $slist[] = $val['title'];
                        if(!in_array($val['title'],$seals)){
                            $seals[] = $val['title'];
                        }
                    }
                    $lists[$k]['seallist'] = $slist;
                }
            }
        }

        $sealss = [];
        foreach ($seals as $key=>$val){
            $count = 0;
            foreach ($applys as $k=>$v){
                if(in_array($val,$v['seallist'])){
                    $count++;
                }
            }
            $sealss[] = [
                'title' => $val,
                'count' => $count
            ];
        }

        $data = ['total'=>count($seals),'count' => count($applys), 'sealss' => $sealss];
        HelpHander::success($data);
    }

    // 本月发文 ,发文审批id是89
    public function posts(){
        $approval_id = 89;
        list($start, $end) = Time::month();
        $start = date('Y-m-d H:i:s',$start);
        $end = date('Y-m-d H:i:s',$end);

        $applys = Db::name('apply')
            ->where('status',2)
            ->where('del',0)
            ->where('finish_time','>=',$start)
            ->where('finish_time','<=',$end)
            ->where('approval_id',$approval_id)
            ->field('id,form_json')
            ->select();

        $seals = [];
        foreach ($applys as $k=>$v){
            $applys[$k]['posts'] = '';
            $formJson = json_decode($v['form_json'],true);
            foreach ($formJson as $kk=>$vv){
                if($vv['componentName'] == 'ddselectfield'){
                    if(!in_array($vv['values'],$seals)){
                        $seals[] = $vv['values'];
                    }
                    $applys[$k]['posts'] = $vv['values'];
                }
            }
        }

        $sealss = [];
        foreach ($seals as $key=>$val){
            $count = 0;
            foreach ($applys as $k=>$v){
                if($val ==$v['posts']){
                    $count++;
                }
            }
            $sealss[] = [
                'title' => $val,
                'count' => $count
            ];
        }

        $data = ['total'=>count($seals),'count' => count($applys), 'posts' => $sealss];
        HelpHander::success($data);
    }

    // 工资统计
    public function salary(){
        $month = input('month','','trim');

        $data = [
            [
                'id' => 1,
                'title' => '中心在编',
                'total' => 0,
                'money' => 0
            ],
            [
                'id' => 2,
                'title' => '新华聘用',
                'total' => 0,
                'money' => 0
            ],
            [
                'id' => 3,
                'title' => '中心劳务',
                'total' => 0,
                'money' => 0
            ],
            [
                'id' => 4,
                'title' => '新华劳务',
                'total' => 0,
                'money' => 0
            ],
            [
                'id' => 5,
                'title' => '中心退休',
                'total' => 0,
                'money' => 0
            ],
        ];
        $total = 0;
        $money = 0;
        foreach ($data as $k=>$v){
            if($v['id'] == 5){ // 中心退休
                $count = Db::name('user_retired_salary')
                    ->alias('a')
                    ->join('salary_record sr','sr.id = a.salary_record_id')
                    ->where('sr.status',3)
                    ->where('sr.month',$month)
                    ->count();
                $data[$k]['total'] = $count;
                $total += $count;
                $salary = Db::name('user_retired_salary')
                    ->alias('a')
                    ->join('salary_record sr','sr.id = a.salary_record_id')
                    ->where('sr.status',3)
                    ->where('sr.month',$month)
                    ->sum('salary');
                $data[$k]['money'] = $salary;
                $money += $salary;
            }else{
                $count = Db::name('salary_record_log')
                    ->alias('a')
                    ->join('salary_record sr','sr.id = a.salary_record_id')
                    ->where('sr.status',3)
                    ->where('sr.type',$v['id'])
                    ->where('sr.month',$month)
                    ->count();
                $data[$k]['total'] = $count;
                $total += $count;
                $salary = Db::name('salary_record_log')
                    ->alias('a')
                    ->join('salary_record sr','sr.id = a.salary_record_id')
                    ->where('sr.status',3)
                    ->where('sr.month',$month)
                    ->where('sr.type',$v['id'])
                    ->sum('salary');
                $data[$k]['money'] = $salary;
                $money += $salary;
            }
        }

        $json = ['total'=>$total,'money' => $money, 'list' => $data];
        HelpHander::success($json);
    }

    public function houseMoney(){
        $cur = date('Y');
        $curTime = strtotime($cur.'-01-01');
        $month = [];
        for ($i=0;$i<12;$i++){
            $date = strtotime("+".$i." month",$curTime);
            $month[] = date('Y-m',$date);
        }
//        $month[] = $cur;

        $counts = [];
        foreach ($month as $k=>$v){
            $money = model('HouseContractPay')->getPayMoneyByMonth($v);
            $counts[] = $money;
        }
        $data = [
            "month" => $month,
            "counts" => $counts
        ];
        ajax_return_ok($data);
    }
}