<?php


namespace app\Home\controller;
use think\Db;

class Addr extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @param id 地址id
     */
    public function Info(){
        $addrid = isset($this->post['id'])&&$this->post['id']!==""?$this->post['id']:"";
        if($addrid==""){
            return error_show(1005,"参数id 不能为空");
        }
        $list = Db::name("rela_addr")->alias('a')->join("addr b","a.addrid=b.id","left")->where(["
        a.accountid"=>$this->userinfo['id'],"b.is_del"=>0,"b.id"=>$addrid])->field("b.id,b.provice,b.provice_name,b.city,
        b.city_name,b.area,b.area_name,b.addr,b.contector,b.mobile,b.addtime")->find();
        if(empty($list)){
            return error_show(1004,"未找到数据");
        }
        return app_show(0,"获取数据成功",$list);
    }

    /**
     * @param
     */
    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;
        $count= Db::name("rela_addr")->alias('a')->join("addr b","a.addrid=b.id","left")->where(["
        a.accountid"=>$this->userinfo['id'],"b.is_del"=>0])->count();
        $total = ceil($count/$size);
        $page=$page>$total ? $total:$page;
        $list = Db::name("rela_addr")->alias('a')->join("addr b","a.addrid=b.id","left")->where(["
        a.accountid"=>$this->userinfo['id'],"b.is_del"=>0])->page($page,$size)->field("b.id,b.provice,b.provice_name,b.city,
        b.city_name,b.area,b.area_name,b.addr,b.contector,b.mobile,b.addtime")->select();
        return  app_show(0,"获取成功", ["list"=>$list,"count"=>$count]);
    }

    /**
     * @param contector
     * @param mobile
     * @param provice
     * @param city
     * @param area
     */
    public function Add(){
        $contector = isset($this->post['contector'])&&$this->post['contector']!=""? trim($this->post['contector']):"";

        if($contector==""){
            return error_show(1004,"参数contector 不能为空");
        }
        $mobile = isset($this->post['mobile'])&&$this->post['mobile']!="" ? trim($this->post['mobile']):"";
        if($mobile==""){
            return error_show(1005,"参数mobile 不能为空");
        }
        $province =  isset($this->post['provice'])&&$this->post['provice']!=""? trim($this->post['provice']):"";
        $city =  isset($this->post['city'])&&$this->post['city']!=""? trim($this->post['city']):"";
        $area =  isset($this->post['area'])&&$this->post['area']!=""? trim($this->post['area']):"";
        if($province==""){
            return error_show(1005,"参数provice 不能为空");
        }
        if($city==""){
            return error_show(1005,"参数city 不能为空");
        }
        if($area==""){
            return error_show(1005,"参数area 不能为空");
        }
        $province_info = Db::name("province")->where(['province_code'=>$province])->find();
        if(empty($province_info)){
            return error_show(1005,"省级数据未找到");
        }
       $cityinfo = Db::name("city")->where(['province_code'=>$province,"city_code"=>$city])->find();
        if(empty($cityinfo)){
            return error_show(1005,"市级数据未找到");
        }
        $areainfo = Db::name("area")->where(['area_code'=>$area,"city_code"=>$city])->find();
        if(empty($areainfo)){
            return error_show(1005,"区县级数据未找到");
        }
        $addr = isset($this->post['addr'])&&$this->post['addr']!=""?$this->post['addr']:"";
        if($addr==""){
            return error_show(1005,"参数addr 不能为空");
        }
        Db::startTrans();
        try{
            $data=[
                "addr"=>$addr,
                "provice"=>$province,
                "provice_name"=>$province_info['name'],
                "city"=>$city,
                "city_name"=>$cityinfo['name'],
                "area"=>$area,
                "area_name"=>$areainfo['name'],
                "contector"=>$contector,
                "mobile"=>$mobile,
                'status'=>1,
                "is_del"=>0,
                "addtime"=>date("Y-m-d H:i:s"),
                "updatetime"=>date("Y-m-d H:i:s")
            ];
            $addrid =Db::name("addr")->insert($data,false,true);
            if($addrid>0){
                $rela = Db::name("rela_addr")->insert(['accountid'=>$this->userinfo['id'],"addrid"=>$addrid]);
                if($rela){
                    Db::commit();
                    return app_show(0,"地址新建成功");
                }
            }
            Db::rollback();
            return error_show(1008,"地址新建失败");
        }catch (\Exception $e){
            Db::rollback();
            return error_show(1008,$e->getMessage());
        }
    }
    /**
     * @param contector
     * @param mobile
     * @param provice
     * @param city
     * @param area
     * @param id
     */
    public function Edit(){
        $addrid = isset($this->post['id'])&&$this->post['id']!==""?$this->post['id']:"";
        if($addrid==""){
            return error_show(1005,"参数id 不能为空");
        }
        $list = Db::name("rela_addr")->alias('a')->join("addr b","a.addrid=b.id","left")->where(["
        a.accountid"=>$this->userinfo['id'],"b.is_del"=>0,"b.id"=>$addrid])->field("b.id,b.provice,b.provice_name,b.city,
        b.city_name,b.area,b.area_name,b.addr,b.contector,b.mobile,b.addtime")->find();
        if(empty($list)){
            return error_show(1004,"未找到数据");
        }
        $contector = isset($this->post['contector'])&&$this->post['contector']!=""? trim($this->post['contector']):"";
        if($contector==""){
            return error_show(1004,"参数contector 不能为空");
        }
        $mobile = isset($this->post['mobile'])&&$this->post['mobile']!="" ? trim($this->post['mobile']):"";
        if($mobile==""){
            return error_show(1005,"参数mobile 不能为空");
        }
        $province =  isset($this->post['provice'])&&$this->post['provice']!=""? trim($this->post['provice']):"";
        $city =  isset($this->post['city'])&&$this->post['city']!=""? trim($this->post['city']):"";
        $area =  isset($this->post['area'])&&$this->post['area']!=""? trim($this->post['area']):"";
        if($province==""){
            return error_show(1005,"参数provice 不能为空");
        }
        if($city==""){
            return error_show(1005,"参数city 不能为空");
        }
        if($area==""){
            return error_show(1005,"参数area 不能为空");
        }
        $province_info = Db::name("province")->where(['province_code'=>$province])->find();
        if(empty($province_info)){
            return error_show(1005,"省级数据未找到");
        }
        $cityinfo = Db::name("city")->where(['province_code'=>$province,"city_code"=>$city])->find();
        if(empty($cityinfo)){
            return error_show(1005,"市级数据未找到");
        }
        $areainfo = Db::name("area")->where(['area_code'=>$area,"city_code"=>$city])->find();
        if(empty($areainfo)){
            return error_show(1005,"区县级数据未找到");
        }
        $addr = isset($this->post['addr'])&&$this->post['addr']!=""?$this->post['addr']:"";
        if($addr==""){
            return error_show(1005,"参数addr 不能为空");
        }
        Db::startTrans();
        try{
            $data=[
                "id"=>$addrid,
                "addr"=>$addr,
                "provice"=>$province,
                "provice_name"=>$province_info['name'],
                "city"=>$city,
                "city_name"=>$cityinfo['name'],
                "area"=>$area,
                "area_name"=>$areainfo['name'],
                "contector"=>$contector,
                "mobile"=>$mobile,
                "updatetime"=>date("Y-m-d H:i:s")
            ];
            $addrup =Db::name("addr")->update($data);
            if($addrup){
                    Db::commit();
                    return app_show(0,"地址修改成功");
            }
            Db::rollback();
            return error_show(1008,"地址修改失败");
        }catch (\Exception $e){
            Db::rollback();
            return error_show(1008,$e->getMessage());
        }
    }

    /**
     * @param id 地址id
     */
    public function Delete(){
        $addrid = isset($this->post['id'])&&$this->post['id']!==""?$this->post['id']:"";
        if($addrid==""){
            return error_show(1005,"参数id 不能为空");
        }
        $list = Db::name("rela_addr")->alias('a')->join("addr b","a.addrid=b.id","left")->where(["
        a.accountid"=>$this->userinfo['id'],"b.is_del"=>0,"b.id"=>$addrid])->field("b.id,b.provice,b.provice_name,b.city,
        b.city_name,b.area,b.area_name,b.addr,b.contector,b.mobile,b.addtime")->find();
        if(empty($list)){
            return error_show(1004,"未找到数据");
        }
        $del= Db::name("addr")->where(['id'=>$addrid])->update(["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
        if($del){
            return app_show(0,"地址删除成功");
        }
        return error_show(1005,"地址删除失败");
    }
}