<?php
namespace Work\Lib;

use Dever;
Class Cron
{
	#工单日统计
    public function index_api(){
        $title = '工单每日统计';
        $data = Dever::db('work/branch')->state(array('type'=>1));
        foreach ($data as $k => $v) {
        	$group_id = explode(',',$v['group']);
        	$admin = Dever::db('manage/admin')->find($group_id[1]);
            $content = $this->email_api($v,$admin);
            if ($content) {
                $this->send($admin['email'], $admin['username'], $title, $content);
            }
             
        }
    }
    public function test_api(){
        $title = '工单每日统计';
        $data = Dever::db('work/branch')->state(array('type'=>1));
        foreach ($data as $k => $v) {
            $group_id = explode(',',$v['group']);
            $admin = Dever::db('manage/admin')->find($group_id[1]);
            $content = $this->email_api($v,$admin);
            if ($content) {
                $this->send('1849506057@qq.com', $admin['username'], $title, $content);
            }
        }
    }
	#发送邮件
    public function email_api($v,$fsadmin)
    {
    	$start=mktime(0,0,0,date('m'),date('d'),date('Y'))-86400;
        $end = $start+86400-1;
        $config = Dever::db('work/info')->config['config_status'];
        $dbranch = $v;
        $question = Dever::db('work/question')->state(array('branch_id'=>$dbranch['group_id']));
        $question = array_keys($question);

        $data['info']['app'] = Dever::db('work/info')->getCount(array('question'=>$question,'start'=>$start,'end'=>$end));
        // echo Dever::sql();
        $data['info']['xu'] = Dever::db('work/info')->getCount(array('group'=>$dbranch['group_id'],'start'=>$start,'end'=>$end,'status'=>2));
        $admin = explode(',',$dbranch['group']);
        if ($dbranch['coor']) {
            $coor = explode(',',$dbranch['coor']);
            if (isset($coor) && $coor[0] && $coor[0] >0) {
                $people = $dbranch['coor'].','.$admin[1];
            } else {
                 $people = $admin[1];
            } 
        } else {
            $people = $admin[1];
        }
        $data['info']['yi'] = Dever::db('work/info')->getCount(array('fpeople'=>$people,'start'=>$start,'end'=>$end,'status'=>'3,4'));
        if ($data['info']['app'] == 0 && $data['info']['xu'] == 0 && $data['info']['yi'] ==0 ) {
            return false;
        }
        $data['question'] = Dever::db('work/question')->getAll(array('branch_id'=>$dbranch['group_id']));
        $question = implode(',',$question);
        if ($question) {
            $data['app'] = Dever::db('work/info')->fetchAll('select * from churen_work_info where (`group` in ('.$dbranch['group_id'].') or fpeople in ('.$people.') or question in ('.$question.')) and cdate>='.$start.' and cdate <='.$end.' and state = 1');
            foreach ($data['app'] as $k1 => $v1) {
                $data['app'][$k1]['cdate'] = date('Y-m-d H:i:s',$v1['cdate']);
                if ($v1['zdate']) {
                    $data['app'][$k1]['zhipai'] = date('Y-m-d H:i:s',$v1['zdate']).'<br/>'.date('Y-m-d H:i:s',$v1['chdate']);
                } else {
                    $data['app'][$k1]['zhipai'] = date('Y-m-d H:i:s',$v1['chdate']);
                }
                
                // $data['app'][$k1]['chu'] = date('Y-m-d H:i:s',$v1['chdate']);
                $cate = Dever::db('work/q_cate')->find($v1['cate_id']);
                $data['app'][$k1]['cate_name'] = $cate['name'];
                $priority = Dever::db('work/priority')->find($v1['priority_id']);
                $data['app'][$k1]['priority_name'] = $priority['name'];
                $data['app'][$k1]['status']= Dever::status($config,$v1['status']);
            }
        }
        
        $msg = array();
        $msg['admin_email'] = $fsadmin['email'];
        $msg['admin_username'] = $fsadmin['username'];
        $msg['app'] = $data['info']['app'];
        $msg['xu'] = $data['info']['xu'];
        $msg['yi'] = $data['info']['yi'];
        $res = Dever::log($msg, 'work');
        return Dever::render('email', $data);
    }
    
    #代理商统计发送邮件
    public function send($email, $username, $title, $content)
    {
        $config = Dever::config('base', 'project')->email;

        Dever::apply('src/PHPMailer', 'email');
        Dever::apply('src/Exception', 'email');
        Dever::apply('src/SMTP', 'email');
        $mail = new \PHPMailer\PHPMailer\PHPMailer();

        $mail->isSMTP();
        //$mail->SMTPDebug = 2;
        $mail->CharSet = 'UTF-8';
        $mail->Host = $config['smtp'];
        $mail->Port = 465;
        $mail->SMTPSecure = 'ssl';
        $mail->SMTPAuth = true;
        $mail->Username = $config['username'];
        $mail->Password = $config['password'];
        $mail->setFrom($config['from'][0], '工单系统');
        $mail->addAddress($email, $username);
        $mail->Subject = "=?utf-8?B?" . base64_encode($title) . "?=";
        $mail->Body = $content;
        $mail->isHTML(true);
        //$mail->addAttachment($file);
        if (!$mail->send()) {
            Dever::alert("Mailer Error: " . $mail->ErrorInfo);
        } else {
            return 'ok';
        }
    }
    #同步info表中的product
    public function product_api()
    {
        $data = Dever::db('work/info')->state();
        if ($data) {
            foreach ($data as $k => $v) {
                if (!$v['product'] || $v['product']<=0) {
                    $cate = explode(',',$v['cate_id']);
                    if (isset($cate[1]) && $cate[1]) {
                        $product = Dever::db('work/question')->find($cate[1]);
                        if ($product) {
                            $where['where_id'] = $v['id'];
                            $where['product'] = $product['product_id'];
                            Dever::db('work/info')->update($where);
                        }
                    }
                }
            }
        }
    }

    public function updateTime_api()
    {
        $data = Dever::db('work/info')->state();
        foreach ($data as $k => $v) {
            if (!$v['fpeople'] && ($v['status'] == 3 || $v['status'] == 4)) {
                $app = Dever::db('work/appoint')->getAdmin(array('info_id'=>$v['id'],'admin'=>$v['audit_admin']));
                if ($app && $app['admin']) {
                    $update['fpeople'] = $app['admin'];
                } else {
                    if ($v['branch_id']) {
                        $branch = explode(',',$v['branch_id']);
                        if (isset($branch[1]) && $branch[1]) {
                            $update['fpeople'] = $branch[1];
                        }
                    } else {
                        $update['fpeople'] = $v['audit_admin'];
                    }
                }
                $update['where_id'] = $v['id'];
                Dever::db('work/info')->update($update);
            }
        }
    }
    public function user_api()
    {
        $num = Dever::input('num', 1);
        $start = Dever::input('start', date('Y-m-d', strtotime('-'.$num.' day')));
        $end = Dever::input('end', date('Y-m-d'));
        
        $start = Dever::maketime($start . ' 00:00:00');
        $end = Dever::maketime($end . ' 23:59:59');
        $day = intval(($end - $start)/86400);

        for($i=0; $i<=$day; $i++) {
            $where['start'] = $start + 86400*$i;
            $where['end'] = $start + 86400*$i + 86399;
            $where['priority_id'] = '';
            $cate = Dever::db('work/q_cate')->state();
            foreach ($cate as $k =>$v) {
                $where['cate'] = $v['id'];
                unset($where['priority_id']);
                $num = Dever::db('work/info')->getNum($where);
                if ($num <= 0) {
                    continue;
                }
                $data = array();
                $data['cate'] = $where['cate'];
                $data['day'] = $where['start'];
                $info = Dever::db('work/info_stat')->find($data);

                $data['question'] = $num;
                $where['priority_id'] = 4;
                $data['super'] = Dever::db('work/info')->getNum($where);
                $where['priority_id'] = 3;
                $data['urgent'] = Dever::db('work/info')->getNum($where);
                $where['priority_id'] = 2;
                $data['yb'] = Dever::db('work/info')->getNum($where);
                $where['priority_id'] = 1;
                $data['pt'] = Dever::db('work/info')->getNum($where);
                // echo dever::sql();
                // print_r($data);
                if (!$info) {
                    Dever::db('work/info_stat')->insert($data);
                } else {
                    $data['where_id'] = $info['id'];
                    Dever::db('work/info_stat')->update($data);
                }
            }
            
        }
        return 'ok';
    }
    #修改info表中的问题分类
    public function updateCate_api()
    {
        $data = Dever::db('work/info')->state();
        foreach ($data as $k => $v) {
            if (!$v['cate'] || $v['cate']<=0) {
                if ($v['cate_id']) {
                    $cate_id = explode(',',$v['cate_id']);
                    $where['where_id'] = $v['id'];
                    $where['cate'] = $cate_id[0];
                    Dever::db('work/info')->update($where);
                }
            }
        }
    }
    #同步info_log中的指派人（app_admin_id）
    public function updateInfoLog_api()
    {
        $data = Dever::db('work/info_log')->state();
        foreach ($data as $k => $v) {
            if ($v['appoint_id']) {
                $app = Dever::db('work/appoint')->find($v['appoint_id']);
                if ($app) {
                    $where['where_id'] = $v['id'];
                    $where['app_admin_id'] = $app['admin'];
                    $where['clear'] = true;
                    Dever::db('work/info_log')->update($where);
                }
            }
        }
    }
}