<?php
namespace app\api\controller\v1;
use app\api\model\Chanpin;
use app\api\model\Chandi;
use app\api\model\Qy_basic;
use app\api\model\Zd_cpmc;
use app\api\model\Sym;
use app\api\model\Nszy;
use app\api\model\Trpsy;
use OpenApi\Annotations as OA;
use app\BaseController;
use thans\jwt\facade\JWTAuth;
//use think\Db;
use think\facade\Request as re;
use think\Request;
use thans\jwt\JWT;
use app\validate\Chandi as ChandiValidate;
use think\exception\ValidateException;
use rsa\Rsa;
//use think\Facade\Db;

class Traceability extends BaseController
{
    use ResponseJson;
    protected $request;
    protected $middleware = [
        'jwt' => ['except' => []],
//        'SelectProductionInfo','TraceabilityPrint','TraceabilityPrint2','ShowInformation'
    ];

    public function __construct(Request $request)
    {
        $this->request = $request;
    }


    //溯源码打印界面,选择产品批次号接口
    public function SelectProductionInfo(){

        $data = $this->request->post();
        $query=$data['query'];
        $qydm=$data['qydm'];

        if($query){
            $result['rows']=Chanpin::
                where([
                    ['t_chanpin.qydm', '=', $qydm],
                    ['t_zd_cpmc.cpmc', 'like', '%'.$query .'%']
                ])
                ->field("t_chanpin.*,t_qy_basic.qymc,t_zd_cpmc.cpgg,t_zd_cpmc.cpmc")
                ->leftJoin('t_qy_basic','t_chanpin.qydm=t_qy_basic.qydm')
                ->leftJoin('t_zd_cpmc','t_chanpin.cpbh=t_zd_cpmc.cpbh and t_chanpin.qydm=t_zd_cpmc.qydm')
                ->select();
            $result['total']=Chanpin::
            where([
                ['t_chanpin.qydm', '=', $qydm],
                ['t_zd_cpmc.cpmc', 'like', '%'.$query .'%']
            ])
                ->field("t_chanpin.*,t_qy_basic.qymc,t_zd_cpmc.cpgg,t_zd_cpmc.cpmc")
                ->leftJoin('t_qy_basic','t_chanpin.qydm=t_qy_basic.qydm')
                ->leftJoin('t_zd_cpmc','t_chanpin.cpbh=t_zd_cpmc.cpbh and t_chanpin.qydm=t_zd_cpmc.qydm')
                ->count();
        }

        else{
            $result['rows']=Db::name('chanpin')
                ->where('t_chanpin.qydm', '=', $qydm)
                ->field("t_chanpin.*,t_qy_basic.qymc,t_zd_cpmc.cpgg,t_zd_cpmc.cpmc")
                ->leftJoin('t_zd_cpmc','t_chanpin.cpbh=t_zd_cpmc.cpbh and t_chanpin.qydm=t_zd_cpmc.qydm')
                ->leftJoin('t_qy_basic','t_chanpin.qydm=t_qy_basic.qydm')
                ->select();
            $result['total']=Db::name('chanpin')->where('t_chanpin.qydm', '=', $qydm)->count();
        }

//        $publicEncrypt = $rsa->publicEncrypt(json_encode($result)); //公钥加密

            if($result){
                return $this->jsonSuccessData($result);
            }
            else{
                return $this->jsonData(-1,"无查询结果");
            }
    }



    /*溯源码打印功能,先输入信息,后打码*/
    public function TraceabilityPrint(){
        //加密
//        require_once(dirname(__FILE__)."/Rsa.php");
//        $rsa = new Rsa();

        $data = $this->request->post();
        $nums = $data['nums'];
        $cpmc = $data['cpmc'];
        $pch = $data['pch'];
        $qydm= $data['qydm'];

        $bh = Qy_basic::where('t_qy_basic.qydm','=',$qydm)->field('t_qy_basic.bh,t_qy_basic.qymc,t_qy_basic.fzrtel')->find();
        $result = Chanpin::where([
            ['t_chanpin.qydm', '=', $qydm],
            ['t_chanpin.pch', '=',$pch]
        ])
            ->field('t_chanpin.chandi,t_chanpin.rq')
            ->find();
        $chandi = $result['chandi'];
        $ret = Chandi::where([
            ['t_chandi.qydm', '=', $qydm],
            ['t_chandi.chandi', '=',$chandi]
        ])
            ->field('t_chandi.*')
            ->find();
        $rets = Zd_cpmc::where([
            ['t_zd_cpmc.qydm', '=', $qydm],
            ['t_zd_cpmc.cpmc', '=',$cpmc]
        ])
            ->find();


        for($i=0;$i<$nums;$i++){

            $numbers=rand(10000,19999);
            $symMes['qydm'] = $qydm;
            $symMes['cpbh'] = $rets['cpbh'];
            $symMes['cdbh'] = $ret['cdbh'];
            $symMes['pch'] = $pch;
            $symMes['date'] = date("Y-m-d");
            $symMes['sym']=$bh['bh'].reter(substr($data['pch'],0,2)).
                reter(substr($data['pch'],2,2)).
                reter(substr($data['pch'],4,2)).
                substr($data['pch'],6,2).substr($numbers,0,5);
            $Infomation[$i]['sym'] = $symMes['sym'];
            $symres = Sym::create($symMes);
        }

//        dump($symres);die();
        /*以上部分完成溯源码的生成*/
        /*下面开始完成根据$symres['bh']查找该条溯源码的信息*/

        $infomations = array();
        for($i=0;$i<$nums;$i++) {
            $infomations[$i]['qymc'] = $bh['qymc'];
            $infomations[$i]['fzrtel'] = $bh['fzrtel'];
            $infomations[$i]['sym'] = $Infomation[$i]['sym'];
            $infomations[$i]['date'] = $result['rq'];
            $infomations[$i]['cpmc'] = $cpmc;
            $infomations[$i]['chandi'] = $ret['chandi'];
            $infomations[$i]['cpgg'] = $rets['cpgg'];
            $infomations[$i]['url'] = 'http://www.aielab.net/ah/S?'.$Infomation[$i]['sym'];
        }

//        $infomations['qymc'] = $bh['qymc'];
//        $infomations['fzrtel'] = $bh['fzrtel'];
//        $infomations['sym'] = $symres['sym'];
//        $infomations['date'] = $result['rq'];
//        $infomations['cpmc'] = $cpmc;

//        $publicEncrypt = $rsa->publicEncrypt(json_encode($infomations)); //公钥加密

        if($infomations){
            return $this->jsonSuccessData($infomations);
        }else{
            return $this->jsonData(-1,'无查询结果');
        }

    }



    /*溯源码打印功能,先生成溯源码,后补充信息*/
    public function TraceabilityPrint2(){

        $data = $this->request->post();
        $qydm = $data['qydm'];

        $bh = Qy_basic::where('qydm','=',$qydm)->field('bh')->find();
        $randomnums = generate_word();
        $numbers=rand(10000,19999);
        $symMes['sym']=$bh['bh'].$randomnums.substr($numbers,0,5);
        $symMes['date'] = date("Y-m-d");
        $symMes['qydm'] = $qydm;
        $symres = Sym::create($symMes);

//        $publicEncrypt = $rsa->publicEncrypt(json_encode($symres)); //公钥加密

        if($symres){
            return $this->jsonSuccessData($symres);
        }else{
            return $this->jsonData(-1,'无查询结果');
        }
    }



    /*扫码显示界面的信息*/
    public function ShowInformation(){
        //加密
//        require_once(dirname(__FILE__)."/Rsa.php");
//        $rsa = new Rsa();

        $data = $this->request->post();
        $qydm = $data['qydm'];

        $symresult  =  Sym::where('t_sym.sym','=',$data['sym'])->field("t_sym.pch,t_sym.cdbh,t_sym.cpbh,t_sym.qydm")->find();
//        dump($symresult);die();
        $information  =  Qy_basic::where('t_qy_basic.qydm','=',$qydm)->field("t_qy_basic.dwjj,t_qy_basic.qymc,t_qy_basic.fzrtel")->find();
        $productresult  =  Zd_cpmc::where([
            ['t_zd_cpmc.cpbh','=',$symresult['cpbh']],
            ['t_zd_cpmc.qydm','=',$qydm]
        ])
            ->field("t_zd_cpmc.cpmc,t_zd_cpmc.picname,t_zd_cpmc.bzq,t_zd_cpmc.ms,t_zd_cpmc.cpgg")
            ->find();
        $chanpinresult  = Chanpin::where([
            ['t_chanpin.pch','=',$symresult['pch']],
            ['t_chanpin.qydm','=',$qydm]
        ])
            ->field("t_chanpin.rq,t_chanpin.chandi")
            ->find();
        $nszyresult  = Nszy::where([
            ['t_nszy.cdbh','=',$symresult['cdbh']],
            ['t_nszy.qydm','=',$qydm]
        ])
            ->field("t_nszy.nszy,t_nszy.trpmc,t_nszy.zyl,t_nszy.zyldw,t_nszy.rq,t_nszy.zyr")
            ->select();
        $trpresult  = Trpsy::where([
            ['t_trpsy.cdbh','=',$symresult['cdbh']],
            ['t_trpsy.qydm','=',$qydm]
        ])
            ->field("t_trpsy.trpm,t_trpsy.trpgg,t_trpsy.cgrq,t_trpsy.cgr")
            ->select();

        $information['cpmc'] = $productresult['cpmc'];
        $information['bzq'] = $productresult['bzq'];
        $information['ms'] = $productresult['ms'];
        $information['cpgg'] = $productresult['cpgg'];
        $information['picname'] = "http://qklapi.ahaia.net/Uploads/".$qydm."/".$productresult['picname'];
        $information['rq'] = $chanpinresult['rq'];
        $information['chandi'] = $chanpinresult['chandi'];
        $information['nszy'] = $nszyresult;
        $information['trpsy'] = $trpresult;

//        $publicEncrypt = $rsa->publicEncrypt(json_encode($information)); //公钥加密

        if($symresult['pch']&&$symresult['cpbh']!=null) {
            return $this->jsonSuccessData($information);
        }else if($symresult==null) {
            return $this->jsonData(-1, "该溯源码不存在");
        }

    }

}