<?php
namespace app\job;

use app\common\api\BatchApi;
use app\common\api\TalentLogApi;
use app\common\model\TalentChecklog;
use app\common\state\ProjectState;
use think\queue\Job;
use think\facade\Log;
use think\facade\Db;
use app\common\api\ChuanglanSmsApi;
// 给类文件的命名空间起个别名
use PhpOffice\PhpSpreadsheet\Spreadsheet;
//Xlsx类  保存文件功能类
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use app\admin\api\DataApi;

class Jjhc {

    public function fire(Job $job, $data) {
        if ($this->deal($data)) {
            Log::info(json_encode($data));
            $job->delete();
            return true;
        }
        Log::error(json_encode($data));
        if ($job->attempts() >= 3) {
            $job->delete();
            return false;
        }
        $job->release(10); //10秒后重试
    }

    /**
     * 处理业务逻辑
     * @param type $data
     * @return bool
     */
    public function deal($data): bool {
        switch ($data['type']){
            case '1':
                $path = dirname(dirname(dirname(__FILE__)));
                $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($path . "/test.xlsx");
                $sheet = $spreadsheet->getSheet(0);
                $datas = $sheet->toArray();
                $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
                $dataapi = new DataApi();
                for ($index = 0; $index <= count($datas); $index++) {
                    $cards_list = [];
                    echo "正在查询第" . ($index + 1) . "人({$datas[$index][1]})的出生日期\r\n";
                    $info = Db::table('new_talent_info ti')->leftJoin('un_enterprise ue','ti.enterprise_id = ue.id')->where("ue.type = 2 and ti.name = '{$datas[$index][1]}'")->find();
                    echo "正在查询第" . ($index + 1) . "人({$datas[$index][1]}----{$info['birthday']})的证件号\r\n";
                    $cards = $dataapi->IDNumber($datas[$index][1],date("Ymd",strtotime($info['birthday'])));
                    if($cards['code'] == '01' && count($cards['data']) >= 1){
                        foreach ($cards['data'] as $card){
                            array_push($cards_list,$card['aac147']);
                        }
                    }
                    array_push($cards_list,$datas[$index][3]);
                    array_push($cards_list,substr($datas[$index][3],4));
                    $cards_list = array_unique($cards_list);
                    foreach ($cards_list as $v){
                        echo "正在查询第" . ($index + 1) . "人({$v})的失业险\r\n";
                        $res = $dataapi->queryUnemploymentInsuranceByIdAndTime($v,'202201','202301');
                        Db::table('data_check_log')->save(['realname' => $datas[$index][1],'card' => $card['aac147'],'params' => json_encode(['card' => $v,'start' => '202201','end' => '202301']), 'result' => json_encode($res),'createTime' => date("Y-m-d H:i:s",time())]);
                        if($res){
                            $i = 0;
                            $months = [];
                            if(count($res['data']) > 0){
                                foreach ($res['data'] as $k => $v){
                                    if(strpos($v['AAB069'],"晋华") !== false || strpos($v['AAB069'],"渠梁") !== false){
                                        $i++;
                                        array_push($months,$v['AAE003']);
                                    }
                                }
                                sort($months);
                                $res_str = "2022年共缴费{$i}个月，分别是：".implode('、',$months);
                                echo "第". ($index + 1) . "失业险整理完毕，姓名：{$datas[$index][1]}，".$res_str."\r\n";
                                $sheet->setCellValue('F' . ($index+1), $res_str);
                                break;
                            }
                        }
                        echo "第". ($index + 1) . "失业险整理完毕，姓名：{$datas[$index][1]}，2022年共缴费0个月\r\n";
                        $sheet->setCellValue('F' . ($index+1), "2022年共缴费0个月");
                        sleep(3);
                    }
                }
                $writer->save($path . "/test.xlsx");
                return false;
                break;
            case '2':
                return false;
                $path = dirname(dirname(dirname(__FILE__)));
                $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($path . "/test.xlsx");
                $sheet = $spreadsheet->getSheet(0);
                $datas = $sheet->toArray();
                $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
                $dataapi = new DataApi();
                for ($index = 0; $index <= count($datas); $index++) {
                    $cards_list = [];
                    echo "正在查询第" . ($index + 1) . "人({$datas[$index][1]})的出生日期\r\n";
                    $info = Db::table('new_talent_info ti')->leftJoin('un_enterprise ue','ti.enterprise_id = ue.id')->where("ue.type = 2 and ti.name = '{$datas[$index][1]}'")->find();
                    echo "正在查询第" . ($index + 1) . "人({$datas[$index][1]}----{$info['birthday']})的证件号\r\n";
                    $cards = $dataapi->IDNumber($datas[$index][1],date("Ymd",strtotime($info['birthday'])));
                    if($cards['code'] == '01' && count($cards['data']) >= 1){
                        foreach ($cards['data'] as $card){
                            array_push($cards_list,$card['aac147']);
                        }
                    }
                    array_push($cards_list,$datas[$index][3]);
                    array_push($cards_list,substr($datas[$index][3],4));
                    $cards_list = array_unique($cards_list);
                    foreach ($cards_list as $v){
                        echo "正在查询第" . ($index + 1) . "人({$v})的养老险\r\n";
                        $res = $dataapi->queryOldAgeSecurityInsuranceByIdAndTime($v,'202201','202212');
                        Db::table('data_check_log')->save(['realname' => $datas[$index][1],'card' => $card['aac147'],'params' => json_encode(['card' => $v,'start' => '202201','end' => '202212']), 'result' => json_encode($res),'createTime' => date("Y-m-d H:i:s",time())]);
                        if($res){
                            $i = 0;
                            $months = [];
                            if(count($res['data']) > 0){
                                foreach ($res['data'] as $k => $v){
                                    if(strpos($v['AAB004'],"晋华") !== false || strpos($v['AAB004'],"渠梁") !== false){
                                        $i++;
                                        array_push($months,$v['AAE003']);
                                    }
                                }
                                sort($months);
                                $res_str = "2022年共缴费{$i}个月，分别是：".implode('、',$months);
                                echo "第". ($index + 1) . "养老险整理完毕，姓名：{$datas[$index][1]}，".$res_str."\r\n";
                                $sheet->setCellValue('E' . ($index+1), $res_str);
                                break;
                            }
                        }
                        echo "第". ($index + 1) . "养老险整理完毕，姓名：{$datas[$index][1]}，2022年共缴费0个月\r\n";
                        $sheet->setCellValue('E' . ($index+1), "2022年共缴费0个月");
                        sleep(3);
                    }
                }
                $writer->save($path . "/test.xlsx");
                return false;
                break;
            case '3':
                $path = dirname(dirname(dirname(__FILE__)));
                $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($path . "/test1.xlsx");
                $sheet = $spreadsheet->getSheet(0);
                $datas = $sheet->toArray();
                $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
                $dataapi = new DataApi();
                for ($index = 0; $index < count($datas); $index++) {
                    echo "正在查询第" . ($index+1) . "人({$datas[$index][3]})的医保\r\n";
                    $res = $dataapi->queryJfhbxxByYears($datas[$index][3],$datas[$index][1],'2022');
                    if($res){
                        $i = 0;
                        $months = [];
                        if(count($res['datalist']) > 0){
                            foreach ($res['datalist'] as $k => $v){
                                if( $v['aae140'] == '310'){
                                    $i++;
                                    array_push($months,$v['zmny00']);
                                }
                            }
                            sort($months);
                            $res_str = "2022年共缴费{$i}个月，分别是：".implode('、',$months);
                        }else{
                            $res_str = "2022年共缴费0个月";
                        }
                        echo "第". ($index + 1) . "医保整理完毕，姓名：{$datas[$index][1]}，".$res_str."\r\n";
                        $sheet->setCellValue('E' . ($index+1), $res_str);
                    }
                    sleep(2);
                }
                $writer->save($path . "/test1.xlsx");
                return false;
                break;
            case '4':
                $path = dirname(dirname(dirname(__FILE__)));
                $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($path . "/test.xlsx");
                $sheet = $spreadsheet->getSheet(0);
                $datas = $sheet->toArray();
                $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
                $dataapi = new DataApi();
                for ($index = 0; $index < count($datas); $index++) {
                    echo "正在查询第" . ($index + 1) . "人({$datas[$index][1]})的失信被执行情况\r\n";
                    $res = $dataapi->untrustworthyPersonnel($datas[$index][2]);
                    echo "第". ($index + 1) . "人查询，姓名：{$datas[$index][1]}，查询结果：{$res['message']}\r\n";
                    $sheet->setCellValue('D' . ($index+1), $res['message']);
                }
                $writer->save($path . "/test.xlsx");
                return false;
                break;
            default:
                return false;

        }
    }

}