<?php


namespace app\controller\admin\warehouse;
use crmeb\basic\BaseController;
use think\facade\Db;

class Agent extends BaseController
{
    public function operation()
    {
        try {
            $params=['account','status','contacts','contact_number','company_subject','business_license','province','city','county','type','id'];
            $data = $this->request->params($params);
            $check=$this->check_params($data);
            if($check['code']!=200) {
                return app('json')->fail($check['msg']);
            }
            if($data['type']==1){//添加
                $res=$this->warehouse_add($data);//执行方法
                if($res){
                    return app('json')->success('添加成功');
                }else{
                    return app('json')->success('添加失败');
                }
            }elseif ($data['type']==2){//修改
                $res=$this->warehouse_update($data);//执行方法
                if($res!==false){
                    return app('json')->success('修改成功');
                }else{
                    return app('json')->success('修改失败');
                }
            }
            }catch (\Exception $exception){
                return '代理商错误：'.$exception->getFile().'：'.$exception->getLine().'【'.$exception->getMessage().'】';
            }
    }
    //大仓添加
    public function warehouse_add($data)
    {
        unset($data['type']);
        unset($data['id']);
        $data['create_time']=time();
        $data['uid']=Db::name('user')->where('account',$data['account']??'')->value('uid');
        $res=Db::name('warehouse_agent')->insert($data);
        return $res;

    }
    //大仓修改
    public function warehouse_update($data)
    {
        //修改代理商 城市表
        $this->update_agent($data);
        $id=$data['id'];
        unset($data['type']);
        unset($data['id']);
        unset($data['account']);
        $data['update_time']=time();
//        $data['uid']=Db::name('user')->where('account',$data['account']??0)->value('uid');
        $res=Db::name('warehouse_agent')->where('id',$id)->update($data);
        return $res;

    }
    //update_agent
    public function update_agent($data)
    {
        $data=Db::name('warehouse_agent')->where('id',$data['id'])->find();
        if($data['county']!=0){
            Db::name('user_area')->where('area_code',$data['county'])->update(['uid'=>0]);
        }elseif ($data['city']!=0){
            Db::name('user_city')->where('city_code',$data['city'])->update(['uid'=>0]);
        }elseif ($data['province']!=0){
            Db::name('user_province')->where('province_code',$data['province'])->update(['uid'=>0]);
        }
    }
    //检查参数
    public function check_params($data)
    {
        if(empty($data['province'])&&empty($data['city'])&&empty($data['county'])){
            return ['code'=>1,'msg'=>'参数不完整'];
        }

        $uid=Db::name('user')->where('account',$data['account'])->value('uid');
        if(!$uid){
            return ['code'=>1,'msg'=>'手机号暂未注册'];
        }
        if($data['type']!=1&&$data['type']!=2){
            return  ['code'=>1,'msg'=>'请求类型不一致'];
        }

        if(!empty($data['type'])&&$data['type']==1){
            $uu=Db::name('warehouse_agent')->where('account',$data['account'])->value('id');
            if($uu){
                return ['code'=>1,'msg'=>'该账号已注册代理商'];
            }
        }
        if($data['county']!=0){
            $u=Db::name('user_area')->where('area_code',$data['county'])->value('uid');
            if($u){
                if($uid!=$u){
                    return ['code'=>1,'msg'=>'该区域已有代理商'];
                }
            }
            Db::name('user_area')->where('area_code',$data['county'])->update(['uid'=>$uid]);
//            dump(Db::name('log')->getLastSql());
        }elseif ($data['city']!=0){
            $u=Db::name('user_city')->where('city_code',$data['city'])->value('uid');
            if($u){
                if($uid!=$u) {
                    return ['code' => 1, 'msg' => '该城区已有代理商'];
                }
            }
            Db::name('user_city')->where('city_code',$data['city'])->update(['uid'=>$uid]);
        }elseif ($data['province']!=0){
            $u=Db::name('user_province')->where('province_code',$data['province'])->value('uid');
            if($u){
                if($uid!=$u) {
                    return ['code' => 1, 'msg' => '该省份已有代理商'];
                }
            }
            Db::name('user_province')->where('province_code',$data['province'])->update(['uid'=>$uid]);
        }else{
            return ['code'=>1,'msg'=>'请选择地域选项'];
        }
        return ['code'=>200,'msg'=>''];
    }
    //列表
    public function lst()
    {
        [$page, $limit] = $this->getPage();
        $list=Db::name('warehouse_agent')->page($page,$limit)->select()->toArray();
        foreach ($list as $k=>&$v){
            $v['create_time']=date('Y-m-d H:i:s',$v['create_time']);
            if($v['update_time']){
                $v['update_time']=date('Y-m-d H:i:s',$v['update_time']);
            }else{
                $v['update_time']='';
            }
        }
        $count=Db::name('warehouse_agent')->count('id');
        $return_data=[
            'count'=>$count,
            'list'=>$list
        ];
        return app('json')->success($return_data);
    }
    //单个详情
    public function details()
    {
        $id=$this->request->params(['id']);
        if(empty($id['id'])){
            return app('json')->fail('id不存在');
        }
        $data=Db::name('warehouse_agent')->where('id',$id['id'])->find();
        if($data){
            $data['create_time']=date('Y-m-d H:i:s',$data['create_time']);
            if($data['update_time']){
                $data['update_time']=date('Y-m-d H:i:s',$data['update_time']);
            }else{
                $data['update_time']='';
            }
            return app('json')->success($data);
        }else{
            return app('json')->fail('未查询到数据');
        }
    }
    //更改单个状态
    public function switchstatus($id)
    {
        $data=Db::name('warehouse_agent')->where('id',$id)->find();
        if($data){
           if($data['status']==1){
               Db::name('warehouse_agent')->where('id',$id)->dec('status',1)->update();
           }elseif ($data['status']==0){
               Db::name('warehouse_agent')->where('id',$id)->inc('status',1)->update();
           }
           return app('json')->success('修改成功');
        }else{
            return app('json')->fail('未查询到数据');
        }
    }
    /*获取省份*/
    public function  getProvince(){
        $data = Db::name("user_province")
            ->where(['status'=>1])
            ->field("province_id,province_code,province_name")
            ->select();
        return  app('json')->success($data);
    }

    /*获取城市*/
    public function  getCity($id){
        $data = Db::name("user_city")
            ->where(['status'=>1,'province_code'=>$id])
            ->field("city_id,city_code,city_name")
            ->select();
        return  app('json')->success($data);
    }

    /*获取区域*/
    public function  getArea($id){
        $data = Db::name("user_area")
            ->where(['status'=>1,'city_code'=>$id])
            ->field("area_id,area_code,area_name")
            ->select();
        return  app('json')->success($data);
    }
}