<?php
namespace app\api\controller\v1;
use app\api\model\Qy_basic;
use app\api\model\Sym;
use OpenApi\Annotations as OA;
use app\BaseController;
use thans\jwt\facade\JWTAuth;
use think\facade\Request as re;
use think\Request;
use thans\jwt\JWT;
use app\api\model\Chandi as ChandiModel;
use app\validate\Chandi as ChandiValidate;
use think\exception\ValidateException;
//use rsa\Rsa;
class Chandi extends BaseController{
    use ResponseJson;
    protected $request;
    protected $middleware = [
        'jwt' 	=> ['except' 	=> ['qycount'] ],
        'check' => ['except' =>[]]
//        'AddPlaceInfo','AddPlaceInfo','DoEditPlaceInfo','DelPlaceInfo','PlaceInfo'
    ];
    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    /*产地增加*/
    public function AddPlaceInfo()
    {
        //加密
//        require_once(dirname(__FILE__)."/Rsa.php");
//        $rsa = new Rsa();

        $data = $this->request->post();
        $qydm=$data['qydm'];
//        $chandi=$data['chandi'];
//        $lng=$data['lng'];
//        $lat=$data['lat'];
//        $yt=$data['yt'];
//        if(isset($data['jldw'])){
//            try{
//                validate(ChandiValidate::class)->check(
//                    [
//                        'qydm'=>$qydm,
//                        'chandi'=>$chandi,
//                        'lng' =>$lng,
//                        'lat'=>$lat,
//                        'yt'=>$yt ,
//                        'tiliang'=>$data['tiliang'],
//                        'jldw'=>$data['jldw'],
//                    ]
//                );
//            }catch (ValidateException $e){
//                return ($e->getError());
//            }
//        }

        $qy=Qy_basic::Where('qydm','=',$qydm)->find();
        if($qy){
            $jd=ChandiModel::field('cdbh')->where('qydm','=',$qydm)->order('cdbh desc')->find();//按照产地查找产地表中的所有信息，
            if(!$jd['cdbh']&&$jd['cdbh']==''){
                $data['cdbh']='000';
            }else{
                $x='1'.$jd['cdbh'];
                $y=intval($x)+1;//十进制取整
                $data["cdbh"]=substr($y,1);//给产地编号赋值
            }
            //新增记录
            $result = ChandiModel::create($data);
//            $publicEncrypt = $rsa->publicEncrypt(json_encode($result)); //公钥加密
            if($result) {
                return $this->jsonSuccessData($result);
            }else {
                return $this->jsonData(-1,"产地上传失败");
            }
        }
        else{
            return $this->jsonData(-2,"企业未注册");
        }

    }


    /*执行产地信息修改*/
    public function DoEditPlaceInfo(){
        //加密
//        require_once(dirname(__FILE__)."/Rsa.php");
//        $rsa = new Rsa();

        //从提交表单中排除关联字段cpmc字段
        $data = $this->request->post();
        //设置更新条件
        $condition = ['bh'=>$data['bh']];
        $result = ChandiModel::update($data,$condition);

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

        if($result) {
            return $this->jsonSuccessData($result);
        }else {
            return $this->jsonData(-1,"更新失败");
        }

    }


    /*产地删除*/
    public function DelPlaceInfo()
    {
        //加密
//        require_once(dirname(__FILE__)."/Rsa.php");
//        $rsa = new Rsa();

        $data = $this->request->post();
        $map['bh']= $data['bh'];
        //删除产地信息
        $result = ChandiModel::where($map)->delete();

//        $publicEncrypt = $rsa->publicEncrypt(json_encode($result)); //公钥加密
        if($result) {
            return $this->jsonSuccessData($result);
        }else {
            return $this->jsonData(-1,"产地信息删除失败");
        }
    }


    /*产地信息查询*/
    public function PlaceInfo(){
        //加密
//        require_once(dirname(__FILE__)."/Rsa.php");
//        $rsa = new Rsa();

        $data = $this->request->post();
        $page = $data['page']; //分页的页码
        $query = $data['query']; //前台传过来的查询
        $pageSize=$data['pageSize'];
        $qydm=$data['qydm'];

        if($query) {
            $result['rows']= ChandiModel::
            where([
                ['chandi', 'like', '%' . $query . '%'],
                ['t_chandi.qydm', '=', $qydm]
            ])
                ->field("t_chandi.*,t_qy_basic.qymc")
                ->leftjoin('t_qy_basic','t_chandi.qydm=t_qy_basic.qydm')
                ->page($page,$pageSize)
                ->select();
            $result['total']=ChandiModel::
            where([
                ['chandi', 'like', '%' . $query . '%'],
                ['t_chandi.qydm', '=', $qydm]
            ])
                -> leftjoin('t_qy_basic','t_chandi.qydm=t_qy_basic.qydm')->count();

            $result['page']=$page;

        }
        else{
            $result['rows']= ChandiModel::
            where(

                't_chandi.qydm', '=', $qydm
            )
                ->field("t_chandi.*,t_qy_basic.qymc")
                ->leftjoin('t_qy_basic','t_chandi.qydm=t_qy_basic.qydm')
                ->page($page,$pageSize)
                ->select();
            $result['total']=ChandiModel::
            where('t_chandi.qydm', '=', $qydm)
                ->leftjoin('t_qy_basic','t_chandi.qydm=t_qy_basic.qydm')->count();

            $result['page']=$page;
        }
//        $publicEncrypt = $rsa->publicEncrypt(json_encode($result)); //公钥加密

        if($result['rows']){
            return $this->jsonSuccessData($result);
        }
        else{
            return $this->jsonData('-1',"查询错误");
        }
    }


    public function qycount()
    {
        $sum=0;
        $count1 =Qy_basic::where(
            'qylx','=','药材'
        )->field('qydm')->select();
//        dump(count($count1));die();

//        $sym =Sym::where('date','>','2021-06-01')->count('bh');
//
//        return $this->jsonSuccessData(
//            $count1
//        );


        for ($i = 0;$i<count($count1);$i++){

            $sym =Sym::where('qydm',$count1[$i]['qydm'])->count('bh');
            $sum=$sum+$sym;
        }
        return $this->jsonSuccessData(
            $sum
        );
//
    }



}
