<?php

namespace app\admin\controller;

use app\BaseController;
use think\App;
use think\facade\Db;use think\facade\Validate;

//品牌授权
class Keepbrand extends Base
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }
    public function create(){
    	
    	$param = $this->request->only([
    		"brand_book",
    		"gyscode",
    		"brand_id",
    		"is_del"=>0,
    		'remark',
    		'long',
    		"status",
    		"starttime"=>date("Y-m-d H:i:s"),
    		"endtime"=>''],"post","trim");
        $valud= Validate::rule([
        	"brand_book|授权书图片"=>"max:255",
        	"gyscode|供应商编号"=>"require|max:255",
        	"brand_id|品牌id"=>"require|number|gt:0|unique:brand_book,brand_id^gyscode^is_del",
        	"long|授权类型"=>"require|number|in:0,1",
        	"status|状态"=>"require|number|in:0,1",
        	"starttime|开始日期"=>"require|date|dateFormat:Y-m-d H:i:s",
        	"endtime|结束日期"=>"requireIf:long,1|date|dateFormat:Y-m-d H:i:s",
        	]);
        if($valud->check($param)==false) return error_show(1004,$valud->getError());
        $data=[];
        $data['createrid']= $this->uid;//isset($user["data"]['id']) ?  $user["data"]['id'] : "";
        $data['creater']= $this->uname;//isset($user["data"]['nickname']) ?  $user["data"]['nickname'] : "";
        if($param['long']===1){
            $data['endtime']='';
        }
        $userCommon = \app\admin\common\User::getIns();
        $companyinfo =$userCommon->handle('getCodeAndName',['code'=>$param['gyscode']]);
//        var_dump($companyinfo);
        if(!isset($companyinfo['data']) || empty($companyinfo['data'])){
        	 return error_show(1004,'供应商信息未找到');
        }
        $data['gysname'] =$companyinfo['data'][$param['gyscode']]?:"";
        $info = Db::name("brand_book")->insert(array_merge($param,$data));
        if($info){
        	return error_show(0,"新建成功");
        }else{
            return error_show(1002,"新建失败");
            }
    }
    public function list(){
        $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) :"1";
        $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) :"10";
        $where=[['a.is_del',"=",0]];
        $gyscode = isset($this->post['gyscode']) && $this->post['gyscode'] !=="" ? trim($this->post['gyscode']):"";
        if($gyscode!=""){
            $where[]=['a.gyscode',"like","%$gyscode%"];
        }
        $name = isset($this->post['name']) && $this->post['name'] !=="" ? trim($this->post['name']):"";
        if($name!=""){
            $where[]=['b.name',"like","%$name%"];
        }
        $count = Db::name('brand_book')
            ->alias('a')
            ->where($where)->count();
        $total = ceil($count / $size);
        $page = $page >= $total ? $total : $page;
        $list = Db::name('brand_book')->alias('a')
            ->join("brand c","a.brand_id=c.id","left")
            ->where($where)
            ->page($page,$size)
            ->field("a.*,c.brand_name")->order("addtime desc")
            ->select();
        return app_show(0,"获取成功",['list'=>$list,'count'=>$count]);
    }
    public function info(){
        $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
        if($id==""){
            return error_show(1002,"参数id不能为空");
        }
        $info =  Db::name("brand_book")->where(["id"=>$id])->find();
        if(!$info) {
            return error_show(1002, "未找到对应数据");
        }
		$info['brand_name'] = Db::name("brand")->where(["id"=>$info['brand_id']])->value("brand_name",'');
        
        return app_show(0,"获取成功",$info);
        
    }
    public function status(){
        $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']):"";
        if($id==""){
            return error_show(1002,"参数id不能为空");
        }
        $info =  Db::name("brand_book")->where([["id","=",$id]])->find();
        if(!$info){
            return error_show(1002,"未找到对应数据");
        }
        $status =  isset($this->post['status']) && $this->post['status']!==""? intval($this->post['status']):"";
        if($status===""){
            return error_show(1002,"参数status不能为空");
        }
        if(!in_array($status,[0,1])){
            return error_show(1002,"参数status无效");
        }
        $info['status']=$status;
        $info['updatetime']=date("Y-m-d H:i:s");
        $msg = $status==1?"启用":"禁用";
        $update = Db::name("brand_book")->save($info);
        return  $update? error_show(0,"{$msg}成功"):error_show(1004,"{$msg}失败");
    }
    public function del(){
        $id = $this->post['id'] && $this->post['id'] !=="" ? intval($this->post['id']) :"";
        if($id==""){
            return  error_show(1004,"参数id不能为空");
        }
        $str= Db::name('brand_book')->where(['id'=>$id,'is_del'=>0])->find();
        if(empty($str)){
            return error_show(1002,"未找到数据");
        }
        $end = Db::name('brand_book')->update(['id'=>$id,'is_del'=>1]);
        if($end){
            return error_show(0,"删除成功");
        }else{
            return error_show(1002,"删除失败");
        }
    }
    public function edit(){
        $param = $this->request->only([
        	"id",
    		'brand_book',
    		'gyscode',
    		'brand_id',
    		'is_del'=>0,
    		'remark',
    		'long',
    		'status',
    		'starttime'=>date('Y-m-d H:i:s'),
    		'endtime'=>null],'post','trim');
        $valud= Validate::rule([
        	'id|主键id'=>'require|number|gt:0',
        	'brand_book|授权书图片'=>'url',
        	'gyscode|供应商编号'=>'require|max:255',
        	'brand_id|品牌id'=>'require|number|gt:0|unique:brand_book,brand_id^gyscode^is_del',
        	'long|授权类型'=>'require|number|in:0,1',
        	'status|状态'=>'require|number|in:0,1',
        	'starttime|开始日期'=>'require|date|dateFormat:Y-m-d H:i:s',
        	'endtime|结束日期'=>'requireIf:long,1|date|dateFormat:Y-m-d H:i:s',
        	]);
        if($valud->check($param)==false) return error_show(1004,$valud->getError());
        $info = Db::name('brand_book')->where(["id"=>$param['id'],"is_del"=>0])->findOrEmpty();
        if(empty($info))return error_show(1004,"未找到数据");
        
        if($param['long']!=$info['long']&&$param['long']===1){
            $data['endtime']='';
        }
        if($param['gyscode']!=$info['gyscode']){
        	$userCommon = \app\admin\common\User::getIns();
	        $companyinfo =$userCommon->handle('getCodeAndName',['code'=>$param['gyscode']]);
	        if(!isset($companyinfo['data']) || empty($companyinfo['data'])){
        	 return error_show(1004,'供应商信息未找到');
        }
        $data['gysname'] =$companyinfo['data'][$param['gyscode']]?:'';
        }
        
       
        $info = Db::name('brand_book')->save(array_merge($param,$data));
        if($info){
        	return error_show(0,'更新成功');
        }else{
            return error_show(1002,'更新失败');
            }
    }
    
   public function query(){
    	$param =$this->request->only(["brand_id"=>"","gyscode"=>"","is_del"=>0],"post","trim");
    	$valid = Validate::rule(["brand_id|品牌id"=>"require|number|gt:0","gyscode|供应商编号"=>"require"]);
    	if($valid->check($param)==false)return error_show(1002,$valid->getError());
    	$info =  Db::name('brand_book')->where($param)->findOrEmpty();
    	return app_show(0,"获取成功",$info);
   }
   
}