<?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++) {
                    echo "正在查询第" . ($index + 1) . "人({$datas[$index][3]})的失业险\r\n";
                    $res = $dataapi->queryUnemploymentInsuranceByIdAndTime($datas[$index][3],'202201','202301');
                    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);
                        }else{
                            $res_str = "2022年共缴费0个月";
                        }
                        echo "第". ($index + 1) . "失业险整理完毕，姓名：{$datas[$index][1]}，".$res_str."\r\n";
                        $sheet->setCellValue('F' . ($index+1), $res_str);
                    }
                    sleep(2);
                }
                $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++) {
                    echo "正在查询第" . ($index+1) . "人({$datas[$index][3]})的养老险\r\n";
                    $res = $dataapi->queryOldAgeSecurityInsuranceByIdAndTime($datas[$index][3],'202201','202212');
                    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);
                        }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 . "/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;
            default:
                return false;

        }
    }

}