<?php
namespace app\api\controller\v1;
use OpenApi\Annotations as OA;
use app\BaseController;
use thans\jwt\facade\JWTAuth;
use think\exception\ValidateException;
use think\facade\Request as re;
use think\Request;
use thans\jwt\JWT;
use app\api\model\Chandi as ChandiModel;
use app\api\model\Qy_basic;
use app\api\model\Zd_cpmc;
use app\api\model\Nszy as NszyModel;
use think\File;
use app\validate\Product as ProductValidate;
use rsa\Rsa;

class Product extends BaseController{
    use ResponseJson;
    protected $request;
    protected $middleware = [
        'jwt' 	=> ['except' 	=> [] ],
//        'login','AddProductInfo','DelProductInfo','DoEditProductInfo','QueryProductInfo'
    ];
    public function __construct(Request $request)
    {
        $this->request = $request;
    }


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

        $data = $this->request->post();
        $qydm=$data['qydm'];
//        $cpmc=$data['cpmc'];
//        try{
//            validate(ProductValidate::class)->check(
//                [
//                    'qydm'=>$qydm,
//                    'cpmc'=>$cpmc,
//                    'ms' =>$data['ms'],
//                    'cpgg'=>$data['cpgg'],
//                    'bzq'=>$data['bzq'] ,
//                ]
//            );
//        }catch (ValidateException $e){
//            return ($e->getError());
//        }
        $qy=Qy_basic::Where('qydm','=',$qydm)->find();
        if($qy){
            $jd=Zd_cpmc::field('cpbh')->where('qydm','=',$qydm)->select();
            //dump($jd);die;
            $wz=count($jd) - 1;//产品数量-1
            // dump($wz);die;
            if(count($jd)){
                $x="1".$jd[$wz]['cpbh'];//对应最后一条cpbh
                $y=intval($x)+1;
                $data["cpbh"]=substr($y,1);
            }else{
                $data["cpbh"]='001';
            }

            //dump($data);die;
            //新增记录
            $result = Zd_cpmc::create($data);

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

            if($result) {
                return $this->jsonData(0,"添加成功");
            }else {
                return $this->jsonData(-1,"添加错误");
            }
        }
        else{
            return $this->jsonData(-2,"企业未注册");
        }


    }


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

        $data = $this->request->post();
        $Zd_cpmc = new Zd_cpmc();
        $bh = $data['bh'];
        $del=$Zd_cpmc->delproduct($bh);

//        $publicEncrypt = $rsa->publicEncrypt(json_encode($data)); //公钥加密
        if ($del == '1') {
            return $this->jsonData(
                0,
                "删除成功"
            );
        } else {
            return $this->jsonData(-1, "删除失败");
        }

    }


    /*执行产品修改*/
    public function DoEditProductInfo() {
        $data = $this->request->post();
        //设置更新条件
        $condition = ['bh'=>$data['bh']];

        //更新当前记录
        $result = Zd_cpmc::update($data,$condition);

        if ($result) {
            return $this->jsonData(0,"修改成功");

        } else {
            return $this->jsonData(-1, "修改失败");
        }
    }


    /*产品查询*/
    public function QueryProductInfo(){
   

        $data = $this->request->post();
        $pageNum=$data['pageNum'];
        $pageSize=$data['pageSize'];
        $query=$data['query'];
        $qydm=$data['qydm'];
        if($query){
            $result['rows']=Zd_cpmc::where([
                ['cpmc', 'like', '%'.$query .'%'],
                ['t_zd_cpmc.qydm', '=', $qydm]
            ])
                ->field("t_zd_cpmc.*,t_qy_basic.qymc")
                ->leftjoin('t_qy_basic',' t_zd_cpmc.qydm=t_qy_basic.qydm')
                ->page($pageNum,$pageSize)
                ->select();
            $result['pages']=$pageNum;
            $result['total']=Zd_cpmc::where(
                [
                    ['cpmc', 'like', '%'.$query .'%'],
                    ['t_zd_cpmc.qydm', '=', $qydm]
                ]
            )
                ->select()
                ->count('bh');
        }
        else{
            $result['rows']=Zd_cpmc::where([
                ['t_zd_cpmc.qydm', '=', $qydm]
            ])
                ->field("t_zd_cpmc.*,t_qy_basic.qymc")
                ->leftjoin('t_qy_basic',' t_zd_cpmc.qydm=t_qy_basic.qydm')
                ->page($pageNum,$pageSize)
                ->select();
            $result['pages']=$pageNum;
            $result['total']=Zd_cpmc::where(
                [
                    ['cpmc', 'like', '%'.$query .'%'],
                    ['t_zd_cpmc.qydm', '=', $qydm]
                ]
            )
                ->select()
                ->count('bh');
        }

  

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




}