<?php
/**
 * Created by PhpStorm.
 * User: sicilon_IT
 * Date: 2020/1/5
 * Time: 9:49
 */
namespace app\index\controller;
use think\Controller;
use think\Request;
use app\index\logic\paylogic;
use think\Log;

class Fileoper extends Controller{
    public function upQuestionsWrite()
    {
        // 获取表单上传文件
        $file = request()->file('file');
        $domainpath='https://app.tjzhxx.com/index.php/Uploads/';


        if(empty($file)) {
            $this->error('请选择上传文件');
        }
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move(ROOT_PATH.'public'.DS.'Uploads');

        //如果不清楚文件上传的具体键名，可以直接打印$info来查看
        //获取文件（文件名），$info->getFilename()  ***********不同之处，笔记笔记哦
        //获取文件（日期/文件名），$info->getSaveName()  **********不同之处，笔记笔记哦
        $filename = $info->getSaveName();  //在测试的时候也可以直接打印文件名称来查看
        log::info($filename);
        if($filename){
            //$this->success('文件上传成功！');
            $imageUrl=$domainpath.$filename;
            $r_upload['code']='200';
            $r_upload['resultData']=$imageUrl;
            //不转义反斜杠
            return json_encode($r_upload,320);
        }else{
            // 上传失败获取错误信息
            $this->error($file->getError());
        }
    }

    /*
     * 20210502
     * 导入支侍文件
     * 20210509edit
     * 存入日志
     * 20210509 再次修改，应将uplog主表存入放在子过程中
     * @todo 上传前获取文件名，以防止重复导入
     */

    public function uploadexcelinputdata()
    {
        // 获取表单上传文件
        $file = request()->file('file');
        $companyinfo=request()->post();
        $cid=$companyinfo['companyid'];

        //log::info($cid);
        $domainpath='https://app.tjzhxx.com/index.php/Uploads/';

        //echo $file['name'];
       // echo $file->getFilename();
        $inputfilename=$_FILES['file']['name'];

       // $upinfo['upfilename']=$file['name'];

        if(empty($file)) {
            $this->error('请选择上传文件');
        }

        //验证重复上传
        $l_p=new paylogic();
        $upfile['upfilename']=$inputfilename;
        $isfile=$l_p->getinfobyfname($upfile);
        if(!empty($isfile)){
         //   $this->error('文件已上传过');
            $r_upload['code']=0;
            $r_upload['errmsg']='文件已上传过';
            return json_encode($r_upload,320);


        }
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move(ROOT_PATH.'public'.DS.'Uploads');

        //如果不清楚文件上传的具体键名，可以直接打印$info来查看
        //获取文件（文件名），$info->getFilename()  ***********不同之处，笔记笔记哦
        //获取文件（日期/文件名），$info->getSaveName()  **********不同之处，笔记笔记哦
        $filename = $info->getSaveName();  //在测试的时候也可以直接打印文件名称来查看
        //log::info($filename);
        if($filename){
            //$this->success('文件上传成功！');
            $icount=$this->testexcelinputdb($filename,$inputfilename,$cid);
            $imageUrl=$domainpath.$filename;
            $r_upload['code']='200';
            $r_upload['resultData']['url']=$imageUrl;
            $r_upload['resultData']['inputcount']=$icount;

            return json_encode($r_upload,320);
        }else{
            // 上传失败获取错误信息
            $this->error($file->getError());
        }
    }

    /*
     * 202100502
     * edit
     * 20210509
     * 增加返回总金额
     */
    private  function testexcelinputdb($infilename,$inputfilename,$cid){
        vendor('PHPExcel.PHPExcel');
        //$phpExcel = new \PHPExcel();
        $objRender = \PHPExcel_IOFactory::createReader('Excel2007');
        $filename = '.'.'/'.'Uploads'.'/'.$infilename;
        $ExcelObj = $objRender->load($filename);
        $ExcelObj->setActiveSheetIndex(0);

        $l_p=new paylogic();
        $plist=array();

        $i=2;
        $input_arr=array();
        $sum_input=0;
        $sum_record=0;
        $upinfo=array();
        $upinfo['upfilename']=$inputfilename;
        $upinfo['updatetime']=date('Y-m-d H:i:s');
        $upinfo['companyid']=$cid;
        //log::info($upinfo);
       // $inputmoney=$ExcelObj->getActiveSheet()->getCell("H".$i)->getValue();
        $inputsfzid=$ExcelObj->getActiveSheet()->getCell("F".$i)->getValue();
       // log::info($inputsfzid);
        while($inputsfzid>''){
            $paydetail=array();

            $paydetail['paymoney']=$ExcelObj->getActiveSheet()->getCell("H".$i)->getValue();
            $paydetail['sfzid']=$ExcelObj->getActiveSheet()->getCell("F".$i)->getValue();
            $paydetail['wname']=$ExcelObj->getActiveSheet()->getCell("G".$i)->getValue();
            $paydetail['payclass']=$ExcelObj->getActiveSheet()->getCell("E".$i)->getValue();
            $daynumber=$ExcelObj->getActiveSheet()->getCell("D".$i)->getValue();
            $paydetail['paydate']=gmdate('Y-m-d',($daynumber-25569)*1440*60);
            $paydetail['indate']=date('Y-m-d');
            //$l_p->savepaydetail($paydetail);
            $sum_input+=$paydetail['paymoney'];
            $sum_record+=1;
            array_push($plist,$paydetail);

            $i++;
           // $inputmoney=$ExcelObj->getActiveSheet()->getCell("H".$i)->getValue();
            $inputsfzid=$ExcelObj->getActiveSheet()->getCell("F".$i)->getValue();
            //log::info($paydetail);
        };
        $upinfo['upcount']=$sum_record;
        $upinfo['upsummoney']=$sum_input;
        $rid=$l_p->savenewuplog($upinfo);
        foreach($plist as &$row){
            $row['uplogid']=$rid;
        }
        $icount=$l_p->savepayredetaillist($plist);
        return $icount;
       // echo count($plist);
       // var_dump($plist);
    }


    /*
     * 20210509
     * 请求所有的导入记录
     */
    public function queryalluplog(){

        //预处理奖励数据
        $l_p=new paylogic();

        $r_list=$l_p->getalluplog();

        if (empty($r_list)){
            $res_r['code']=0;
            $res_r['errmsg']='无导入数据';
        }else{
            $res_r['code']=200;
            $res_r['resultData']=$r_list;
        }
        return json_encode($res_r,320);
    }

    /*
 * 20230214
 * 请求公司的所有的导入记录
 */
    public function queryalluplogbycid(){
        $queryinfo = request() -> param();
        //预处理奖励数据
        $l_p=new paylogic();

        $r_list=$l_p->getuplogbycid($queryinfo);

        if (empty($r_list)){
            $res_r['code']=0;
            $res_r['errmsg']='无导入数据';
        }else{
            $res_r['code']=200;
            $res_r['resultData']=$r_list;
        }
        return json_encode($res_r,320);
    }


}