<?php

namespace app\admin\controller;

use app\BaseController;
use think\App;
use think\facade\Db;
use app\admin\model\ActionLog;
class Supplier extends BaseController
{
public $post = "";
public function __construct(App $app){
    parent::__construct($app);
    $this->post = $this->request->post();
//    $token = isset($this->post['token']) ? trim($this->post['token']) : "";
//    if($token==""){
//        return error_show(101,'token不能为空');
//    }
//    $effetc = VerifyTokens($token);
//    if(!empty($effetc) && $effetc['code']!=0){
//        return error_show($effetc['code'],$effetc['message']);
//    }
}
/*列表*/
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 = [['is_del',"=",0]];
    $name = isset($this->post['name']) && $this->post['name'] !== "" ? trim($this->post['name']) : "";
    if ($name !== "") {
        $where[] = ["name","like", "%{$name}%"];
    }
    $code = isset($this->post['code']) && $this->post['code'] !== "" ? trim($this->post['code']) : "";
    if ($code !== "") {
        $where[] = ["code","=", $code];
    }
    $is_platform = isset($this->post['is_platform']) && $this->post['is_platform'] !== "" ? intval($this->post['is_platform']) : "";
    if ($is_platform !== "") {
        $where[] = ['is_platform',"=", $is_platform];
    }

    $count = Db::name("supplier")->where($where)->count();
    $total = ceil($count/$size);
    $page = $page >= $total ? $total : $page;
    $list = Db::name('supplier')->where($where)->page($page,$size)->select();
    return app_show(0,"获取成功",["list"=>$list,'count'=>$count]);
}
/*新建*/
public function create(){
    $name = isset($this->post['name']) && $this->post['name'] !=="" ? trim($this->post['name']) :"";
    if($name==""){
        return error_show(1002,"数据标题不能为空");
    }
    $rename = Db::name('supplier')->where(['is_del' => 0, 'name' => $name])->find();
    if (!empty($rename)) {
        return error_show(1002, "公司名称已存在");
    }
    $code = rand(0000,9999);
    $str = sprintf("%04d",$code);
    $tr="GYS-".date("Ymd")."-".$str;
    $source= isset($this->post['source']) && $this->post['source']!==""? trim($this->post['source']) :"";
    if($source==""){
        return error_show(1002,"供应商来源不能为空");
    }
    $type = isset($this->post['type']) && $this->post['type']!==""? trim($this->post['type']) :"";
    if($type==""){
        return error_show(1002,"申请类型不能为空");
    }
    $area = isset($this->post['area']) && $this->post['area']!==""? trim($this->post['area']) :"";
    if($area==""){
        return error_show(1002,"大区不能为空");
    }
    $city = isset($this->post['city']) && $this->post['city']!==""? trim($this->post['city']) :"";
    if($city==""){
        return error_show(1002,"城市不能为空");
    }
    $nature = isset($this->post['nature']) && $this->post['nature']!==""? trim($this->post['nature']) :"";
    if($nature==""){
        return error_show(1002,"公司类型不能为空");
    }
    $is_platform = isset($this->post['is_platform']) && $this->post['is_platform']!==""? intval($this->post['is_platform'])
        :"0";
    $ticket_type = isset($this->post['ticket_type']) && $this->post['ticket_type']!==""? trim($this->post['ticket_type']) :"";
    if($ticket_type==""){
        return error_show(1002,"回款方式不能为空");
    }
    $legaler = isset($this->post['legaler']) && $this->post['legaler']!==""? trim($this->post['legaler']) :"";
    if($legaler==""){
        return error_show(1002,"法人不能为空");
    }
    $addr = isset($this->post['addr']) && $this->post['addr']!==""? trim($this->post['addr']) :"";
    if($addr==""){
        return error_show(1002,"地址不能为空");
    }
    $data=[
        "name"=>$name,
        "source"=>$source,
        "code"=>$tr,
        "type"=>$type,
        "area"=>$area,
        "city"=>$city,
        "nature"=>$nature,
        "legaler"=>$legaler,
        "addr"=>$addr,
        "is_del"=>0,
        "ticket_type"=>$ticket_type,
        "addtime"=>date('Y-m-d H:i:s'),
        "updatetime"=>date("Y-m-d H:i:s"),
        "is_platform"=>$is_platform
    ];
    $join = Db::name('supplier')->insert($data);
    $st = ["order_code"=>"GYS","status"=>0,"action_remark"=>'',"action_type"=>"create"];
    ActionLog::logAdd($this->post['token'],$st,"role_share",0,$st);
    return $join? error_show(0,"添加成功") :error_show(1002,"添加失败");
}
/*编辑*/
public function edit(){
    $id = isset($this->post['id']) && $this->post['id'] !==""? trim($this->post['id']):"";
    $ion = Db::name('supplier')->where(['id'=>$id,'is_del'=>0])->find();
    if(empty($ion)){
        return error_show(1004,"供应商不存在");
    }
    $name = isset($this->post['name']) && $this->post['name']!=="" ? $this->post['name']:"";
    if($name==""){
        return error_show(1002,'数据标题不能为空');
    }
    $rename =Db::name('supplier')->where(['name'=>$name,'is_del'=>0])->where('id','<>',$id)->find();
    if(!empty($rename)){
        return error_show(1004,"数据标题已存在");
    }
    $source = isset($this->post['source']) && $this->post['source'] !==""? $this->post['source']:"";
    if($source==""){
        return error_show(1002,"供应商来源不能为空");
    }
    $type = isset($this->post['type']) && $this->post['type']!==""? trim($this->post['type']) :"";
    if($type==""){
        return error_show(1002,"申请类型不能为空");
    }
    $area = isset($this->post['area']) && $this->post['area']!==""? trim($this->post['area']) :"";
    if($area==""){
        return error_show(1002,"大区不能为空");
    }
    $city = isset($this->post['city']) && $this->post['city']!==""? trim($this->post['city']) :"";
    if($city==""){
        return error_show(1002,"城市不能为空");
    }
    $nature = isset($this->post['nature']) && $this->post['nature']!==""? trim($this->post['nature']) :"";
    if($nature==""){
        return error_show(1002,"公司类型不能为空");
    }
    $legaler = isset($this->post['legaler']) && $this->post['legaler']!==""? trim($this->post['legaler']) :"";
    if($legaler==""){
        return error_show(1002,"法人不能为空");
    }
    $is_platform = isset($this->post['is_platform']) && $this->post['is_platform']!==""? intval($this->post['is_platform'])
        :"0";
    $addr = isset($this->post['addr']) && $this->post['addr']!==""? trim($this->post['addr']) :"";
    if($addr==""){
        return error_show(1002,"地址不能为空");
    }
    $vmp=[
        "id"=>$id,
        "legaler"=>$legaler,
        "name"=>$name,
        "source"=>$source,
        "type"=>$type,
        "area"=>$area,
        "city"=>$city,
        "nature"=>$nature,
        "addr"=>$addr,
        "is_del"=>0,
        "is_platform"=>$is_platform,
        "addtime"=>date('Y-m-d H:i:s'),
        "updatetime"=>date("Y-m-d H:i:s")
    ];
    $ed = Db::name('supplier')->where(['is_del'=>0,'id'=>$id])->save($vmp);
    $st = ["order_code"=>$id,"status"=>0,"action_remark"=>'',"action_type"=>"edit"];
    ActionLog::logAdd($this->post['token'],$st,"GYS",0,$vmp);
    return $ed ? error_show(0,"编辑成功") : error_show(1002,"编辑失败");
}
/*查询*/
public function selec(){
    $id= isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']) :"";
    if($id==""){
        return error_show(1002,"供应商不存在");
    }
    $se = Db::name('supplier')->where(['id'=>$id,'is_del'=>0])->find();
    return app_show(0,"获取成功",$se);
}
/*删除*/
    public function del(){
    $id = isset($this->post['id']) && $this->post['id'] !==""? intval($this->post['id']) :"";
    $sup = Db::name('supplier')->where(["is_del"=>0,'id'=>$id])->find();
    if($sup==false){
        return error_show(1002,"供应商信息不存在");
    }
    $supp= Db::name('supplier')->update(['id'=>$id,'is_del'=>1,"updatetime"=>date("Y-m-d H:i:s")]);
    if($supp){
//        $st = ["order_code"=>$id,"status"=>0,"action_remark"=>'',"action_type"=>"del"];
//        ActionLog::logAdd($this->post['token'],$st,"GYS",0,$supp);
        return error_show(0,"供应商信息删除成功");
    }else{
        return error_show(1002,"供应商信息删除失败");
    }
}
}