<?php

namespace catchAdmin\yunying\controller;

use catcher\base\CatchRequest as Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catchAdmin\yunying\model\KeyPersonnel as keyPerSonnelModel;
use catcher\Utils;
use catcher\library\excel\Excel;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\facade\Env;
use think\facade\Db;
class KeyPersonnel extends CatchController
{
    protected $keyPerSonnelModel;
    
    public function __construct(KeyPerSonnelModel $keyPerSonnelModel)
    {
        $this->keyPerSonnelModel = $keyPerSonnelModel;
    }
    
    /**
     * 列表
     * @time 2022年10月25日 11:27
     * @param Request $request 
     */
    public function index(Request $request) : \think\Response
    {
        return CatchResponse::paginate($this->keyPerSonnelModel->getList());
    }
    
    /**
     * 保存信息
     * @time 2022年10月25日 11:27
     * @param Request $request 
     */
    public function save(Request $request) : \think\Response
    {
        // var_dump($request->post());
        return CatchResponse::success($this->keyPerSonnelModel->storeBy($request->post()));
    }
    
    /**
     * 读取
     * @time 2022年10月25日 11:27
     * @param $id 
     */
    public function read($id) : \think\Response
    {
        return CatchResponse::success($this->keyPerSonnelModel->findBy($id));
    }
    
    /**
     * 更新
     * @time 2022年10月25日 11:27
     * @param Request $request 
     * @param $id
     */
    public function update(Request $request, $id) : \think\Response
    {
        return CatchResponse::success($this->keyPerSonnelModel->updateBy($id, $request->post()));
    }
    
    /**
     * 删除
     * @time 2022年10月25日 11:27
     * @param $id
     */
    public function delete($id) : \think\Response
    {
        return CatchResponse::success($this->keyPerSonnelModel->deleteBy($id));
    }

    /**
     * 导入车辆
     *
     * @time 2022年02月15日
     * @param Excel $excel
     * @param DeviceExport $deviceExport
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @return \think\response\Json
     */
    public function importExcel(Request $request)
    {
        $url = $request->post('url');
        if (!$url) {
            return CatchResponse::fail('请上传文件');
        }
        $creator_id = $request->post('creator_id');
        //解析地址
        $parse_url = parse_url($url)['path'];
        //载入excel表格 
        $objPHPExcel = IOFactory::load(public_path() . $parse_url);
        //获取表名,一维数组,值是表名。如:array('sheet1', 'sheet2', 'sheet3')
        // $nameArr = $objPHPExcel->getSheetNames();
        //获取表的数量
        $sheetCount = $objPHPExcel->getSheetCount();
        $fail = 0; //失败条数
        $success = 0; //成功条数
        $total = 0; //总共设备数
        $data = []; //设备数据
        //循环读取每一张表
        for ($index = 0; $index < $sheetCount; $index++) {
            //设置当前要读取的表
            $sheet = $objPHPExcel->getSheet($index);   //excel中的第一张sheet
            // var_dump($sheet);exit;
            $highestRow  = $sheet->getHighestRow();       // 取得总行数
            // var_dump($highestRow);
            if ($highestRow <= 1) {
                continue;
            }
            $card_type_id = Db::table("sys_dict_type")->where('code', 'CARD_TYPE_OPTION')->value('id');

            $total += $highestRow - 1;
            for ($j = 2; $j <= $highestRow; $j++) {
                $arr = array(); //每条设备信息
                $arr['name'] = strtoupper(trim($sheet->getCell("A" . $j)->getFormattedValue())); 
                $arr['gender'] = strtoupper(trim($sheet->getCell("B" . $j)->getFormattedValue())); 
                $card_type_text = strtoupper(trim($sheet->getCell("C" . $j)->getFormattedValue())); 
                $arr['card_type'] = Db::table("sys_dict_data")->where('type_id', $card_type_id)->whereLike('value','%'.$card_type_text.'%')->cache(60)->value('code'); 
                $arr['card_number'] = strtoupper(trim($sheet->getCell("D" . $j)->getFormattedValue())); 
                $arr['rfid_sn'] = strtoupper(trim($sheet->getCell("E" . $j)->getFormattedValue())); 
                $arr['address'] = strtoupper(trim($sheet->getCell("F" . $j)->getFormattedValue())); 
                $arr['guardians_name'] = strtoupper(trim($sheet->getCell("G" . $j)->getFormattedValue())); 
                $arr['guardians_gender'] = strtoupper(trim($sheet->getCell("H" . $j)->getFormattedValue())); 
                $arr['guardians_phone'] = strtoupper(trim($sheet->getCell("I" . $j)->getFormattedValue())); 
                $arr['guardians_address'] = strtoupper(trim($sheet->getCell("J" . $j)->getFormattedValue())); 
                

                array_push($data,$arr);
            }
        }
        array_unique($data, SORT_REGULAR);
        // // var_dump($data);return CatchResponse::success();
        $success = $this->keyPerSonnelModel->limit(100)->insertAll($data);
        if ($success) {

            return CatchResponse::success('共' . $total . '条数据,成功' . $success . '条,失败' . ($total-$success) . '条');

        }
        return CatchResponse::success(['error' => true, 'msg' => '导入失败']);
    }
}