<?php

namespace app\bug\controller;

use app\bug\model\Moudel;
use app\user\model\Account;use think\facade\Db;
use think\App;use think\facade\Validate;use think\helper\Str;

class Note extends Base
{
//	protected $noLogin=[];
	public function __construct(App $app)
	{
	    parent::__construct($app);
	    $this->model= new \app\bug\model\Note();
	}
	public function create(){
		$param = $this->request->param(["title"=>"","remark"=>"","company_type"=>"","model_id"=>[],"noteNo"=>"",
		"level"=>2,"weight"=>1,"type"=>2,"status"=>0,"submit"=>1],"post","trim");
	    $valid =Validate::rule([
	    	"title|标题"=>"require|max:255",
	    	"remark|描述"=>"require|max:5000",
	    	"company_type|公司平台类别"=>"require|max:255",
	    	"model_id|模块分类"=>"require",
	    	]);
	    if($valid->check($param)==false)return error($valid->getError());
	    $bugNo=makeNo("BG");
	    $data=[
	        "model_id"=>!empty($param['model_id']) ? array_pop($param['model_id']):"0",
	        "bugNo"=>$bugNo,
	        "noteNo"=>$param['noteNo'],
	        "title"=>$param['title'],
	        "remark"=>$param['remark'],
	        "company_type"=>$param['company_type'],
	        "apply_id"=>$this->uid,
	        "apply_name"=>$this->uname,
	        "updater"=>$this->uname,
	        "updaterid"=>$this->uid,
	        "level"=>$param['level'],
	        "type"=>$param['type'],
	        "submit"=>$param['submit'],
	        "weight"=>$param['weight'],
	        "status"=>$param['status']
	    ];
	    $create = \app\bug\model\Note::create($data);
	    if($create->isEmpty()) return error("创建失败");
	    event("wx_push",["type"=>"bug","data"=>$create->toArray()]);
	    return success("创建成功");
	}
	public function list(){
		$param = $this->request->param([
			'bugNo'=>'','is_created'=>'','company_type'=>'','model_id'=>'','noteNo'=>'','page'=>1,'size'=>10,
		'apply_name'=>'','type'=>'','status'=>'','start'=>'','end'=>'','level'=>'',"deal_name"=>"","title"=>""],
		'post','trim');
	    $where =[['is_del','=',0]];
	    if($param['bugNo']!=""){
	        $where[]=['bugNo',"like","%{$param['bugNo']}%"];
	    }
	    if($param['model_id']!=""){
	        $where[]=['model_id',"=",$param['model_id']];
	    }
	    if($param['noteNo']!=""){
	        $where[]=['noteNo',"like","%{$param['noteNo']}%"];
	    }
	    if($param['company_type']!=""){
	        $where[]=['company_type',"=",$param['company_type']];
	    }
	    if($param['apply_name']!==""){
	        $where[]=['apply_name',"like","%{$param['apply_name']}%"];
	    }
	    $status = $param['status'];
	    if($status!==""){
	        if($status<8){
	            $where[]=['status',"=",$status];
	        }elseif ($status==8){
	            $where[]=['status',"<=",$status];
	            $where[]=['status',"not in",[4,5]];
	        }elseif ($status===9){
	        }elseif ($status===10){
	            $str = Db::name('deal')->where(['action_id'=>$this->uid,'is_del'=>0])->select()->toArray();
	            $data =array_unique(array_column($str,'bugNo')) ;
	            $where[]=['bugNo',"in",$data];
	        }elseif ($status===11){
	            $var = Db::name('deal')->where(['deal_id'=>$this->uid,'is_del'=>0,'status'=>0])->select()->toArray();
	            $da =array_unique(array_column($var,'bugNo')) ;
	           $where[]=['bugNo',"in",$da];
	        }
	    }
	    if($param['is_created']==1){
	         $where[]=['apply_id',"=",$this->uid];
	    }
	    if($param['start']!==""){
	        $where[]=['addtime',">=",startTime($param['start'])];
	    }
	    if($param['end']!==""){
	        $where[]=['addtime',"<",endTime($param['end'])];
	    }
	    if($param['title']!==""){
	        $where[]=['title',"like","%{$param['title']}%"];
	    }
	    if($param['level']!==""){
	        $where[]=['level','=',$param['level']];
	    }
	    if($param['type']!==""){
	        $where[]=['type','=',$param['type']];
	    }
	 
	    if ($param['deal_name']!==""){
	        $item = \app\bug\model\Deal::where([["deal_name","like","%{$param['deal_name']}%"],["is_del","=",0],['status',"=",0]])
	        ->select()
	        ->toArray();
	        $cn =array_unique(array_column($item,'bugNo'));
	        $where[]=["bugNo","in",$cn];
	    }
	    $list = $this->model->where($where)->order('addtime desc')->paginate(["list_rows"=>$param['size'],
	    "page"=>$param['page']]);
	    $appLyid= array_column($list->items(),"apply_id");
	    $bugArr= array_column($list->items(),"bugNo");
	    $departByUid=(new \app\bug\model\DepartUser())->GetDepartNameByUid($appLyid);
	    $dealArr = \app\bug\model\Deal::where([["bugNo","in",$bugArr],["status","=",0],["is_del","=",0]])->column
	    ("deal_name","bugNo");
	    foreach ($list->items() as &$value){
	        $value['deal_name']=$dealArr[$value['bugNo']]??"";
	        $value['can']=Moudel::GetModelByCid($value['model_id'],$value['company_type']);
	        $value['company_name']=$departByUid[$value['apply_id']]??"";
	        $data[]=$value;
	    }
	    return success("获取成功",['list'=>$list->items(),'count'=>$list->total()]);
	}
	public function edit(){
		$param = $this->request->param(["id"=>"",'title'=>'','remark'=>'','company_type'=>'','model_id'=>[],'noteNo'=>'',
		'level'=>1,'weight'=>0,'type'=>0,'status'=>0,"deal_id"=>""],'post','trim');
	    $valid =Validate::rule([
	    	'id|标题Id'=>'require|number|gt:0',
	    	'title|标题'=>'require|max:255',
	    	'remark|描述'=>'require|max:5000',
	    	'company_type|公司平台类别'=>'require|max:255',
	    	'model_id|模块分类'=>'array',
	    	'level|优先级'=>'require|number|in:1,2,3',
	    	'type|问题类型'=>'require|number',
	    	]);
	    if($valid->check($param)==false)return error($valid->getError());
	    $info=$this->model->where(['id'=>$param['id'],'is_del'=>0])->findOrEmpty();
	    if($info->isEmpty())return error("未找到数据");
	    $inf=\app\bug\model\Deal::where(['bugNo'=>$info['bugNo'],'status'=>0,"is_del"=>0])->findOrEmpty();
	    $oldStatus=$info->status;
	    $deal = Account::with(["user_info"])->where("id",$param['deal_id'])->findOrEmpty();
	    $this->model->startTrans();
	    try{
	    	$info->title= $param['title'];
	    	$info->noteNo= $param['noteNo'];
	    	$info->remark= $param['remark'];
	    	$info->model_id= !empty($param['model_id'])?array_pop( $param['model_id']):0;
	    	$info->company_type= $param['company_type'];
	    	$info->updater= $this->uname;
	    	$info->updaterid= $this->uid;
	    	$info->level= $param['level'];
	    	$info->type= $param['type'];
	    	$info->weight= $param['weight'];
	    	$info->status= $param['status'];
	        $datainfo =$info->save();
	        if($datainfo){
	                    $inf->action_id=$this->uid;
	                    $inf->action_name=$this->uname;
	                    $inf->deal_id=$param['deal_id'];
	                    $inf->deal_name=$deal->nickname??"";
	                    $inf->bugNo=$info->bugNo;
	                    $inf->level= 1;
	                    $inf->type= 1;
	                    $inf->status=$oldStatus!=$param['status']?1:0;
	                    $dc = $inf->save();
	                    if($dc==false)throw new \Exception("更新失败");
	                }else throw new \Exception('更新失败');
	        
			$this->model->commit();
			if($oldStatus!=$param['status'])event('wx_push',['type'=>'bug','data'=>$info->toArray()]);
	        return  success("更新成功");
	    }catch (\Exception $e){
	        $this->model->rollback();
	        return error($e->getMessage());
	    }
	}
	public function info(){
	    $id=$this->request->post("id/d");
	    $info = $this->model->where(['id'=>$id,'is_del'=>0])->findOrEmpty();
	    $departByUid=(new \app\bug\model\DepartUser())->GetDepartNameByUid($info->apply_id);
	    $item = \app\bug\model\Deal::where(['bugNo'=>$info['bugNo'],'is_del'=>0,'status'=>0])->findOrEmpty();
	    $info['deal_id']=isset($item['deal_id']) ? $item['deal_id']:"";
	    $info['deal_name']=isset($item['deal_name']) ? $item['deal_name']:"";
	    $info['can']=Moudel::GetModelByCid($info['model_id'],$info['company_type']);
	    $info['company_name']=$departByUid[$info->apply_id]??"";
	    $info['model_id']=array_column($info['can'],"id");
	    if($info->isEmpty()) return error("未找到数据");
	    return success("获取成功",$info);
	}
    public function del(){
        $id=$this->request->post('id/d');
        $info = $this->model->where(['id'=>$id,'is_del'=>0])->findOrEmpty();
        if($info->isEmpty()) return error('未找到数据');
         $info->is_del=1;
         $del=$info->save();
         return $del ? success("删除成功") :error("删除失败");
    }

    public function wxpushList(){
	    $param = $this->request->param(["start"=>"","end"=>"","bugNo"=>"","push_id"=>"","template_name"=>"",
	    "template_id"=>"","status"=>"",'belong'=>'','action_name'=>'',"page"=>1,"size"=>10],'post','trim');
	    $where=[];
	    if($param['start']!==""){
	        $where[]=['createTime',">",startTime($param['start'])];
	    }
	    if($param['end']!==""){
	        $where[]=['createTime',"<",endTime($param['end'])];
	    }
	    if($param['bugNo']!==""){
	        $where[]=['bugNo',"like","%{$param['bugNo']}%"];
	    }
	    if($param['push_id']!==""){
	        $where[]=['push_id',"=",$param['push_id']];
	    }
	    if($param['template_id']!==""){
	        $where[]=['template_id',"=",$param['template_id']];
	    }
	    if($param['template_name']!==""){
	        $where[]=['template_name',"like","%{$param['template_name']}%"];
	    }
	    if($param['status']!==""){
	        $where[]=['status',"=",$param['status']];
	    }
	    if($param['belong']!==""){
	        $where[]=['belong',"=",$param['belong']];
	    }
	    if($param['action_name']!==""){
	        $where[]=['action_name',"like","%{$param['action_name']}%"];
	    }
	    $list = \app\bug\model\WechatPush::where($where)->order("createTime desc")->paginate(["list_rows"=>$param['size'],"page"=>$param['page']]);
	    return success("获取成功",['list'=>$list->items(),'count'=>$list->total()]);
    }
}