<?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\Production as ProductValidate;
use app\api\model\Chanpin;
use rsa\Rsa;

class Production extends BaseController{
    use ResponseJson;
    protected $request;
    protected $middleware = [
        'jwt' 	=> ['except' 	=> []],
//        'login','AddProductionInfo','DelProductionInfo','DoEditProductionInfo','QueryProductionInfo'
    ];
    public function __construct(Request $request)
    {
        $this->request = $request;
    }


    /*产品生产增加*/
    public function AddProductionInfo(){
        $data = $this->request->post();
        $qydm=$data['qydm'];
        $rq=$data['rq'];

        $jd=Chanpin::where([['qydm','=',$qydm], ['rq','=',$rq]])->field('pch')->order('pch','desc')->select();
        $pcqz = substr($rq,2,2).substr($rq,5,2).substr($rq,8,2);
        if(count($jd)){
            $data["pch"]=$jd[0]['pch']+1;
        }else{
            $data["pch"]=$pcqz.'01';
        }
        $result=Chanpin::create($data);
        if($result){
            return $this->jsonData(0,"信息添加成功");
        }else{
            return $this->jsonData(-1,"信息添加失败");
        }
    }


    /*产品生产删除*/
    public function DelProductionInfo(){
        $data = $this->request->post();
        $result = Chanpin::where('bh',$data['bh'])->delete();
        if($result){
            return $this->jsonData(0,"信息删除成功");
        }else{
            return $this->jsonData(-1,"信息删除失败");
        }
    }


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

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

        if ($result) {
            return $this->jsonData(0, "信息修改成功");
        } else {
            return $this->jsonData(-1, "信息修改失败");
        }
    }


    /*生产产品查询*/
    public function QueryProductionInfo(){

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

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

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


}