<?php
namespace app\user\model;

use think\Exception;use think\Model;class Headquarters extends Base
{
//设置字段信息
    protected $schema = [
        'id'       =>'bigint',//
        'code'       =>'varchar',//编号
        'name'       =>'varchar',//名称
        'type'       =>'tinyint',//1企业(业务公司),2客户,3供应商(只有供应商升级为业务公司的时候)
        'company_type'       =>'tinyint',//公司类型,只有企业有效,对应于company_type表
        'invoice_title'       =>'varchar',//发票抬头
        'invoice_people'       =>'varchar',//发票纳税人
        'invoice_addr'       =>'varchar',//发票地址
        'invoice_mobile'       =>'varchar',//发票联系方式
        'invoice_code'       =>'varchar',//纳税识别号
        'invoice_bank'       =>'varchar',//纳税银行
        'invoice_bankNo'       =>'varchar',//纳税银行账户
        'invoice_img'       =>'varchar',//营业执照
        'remark'       =>'varchar',//备注
        'status'       =>'tinyint',//使用状态,1启用,0禁用
        'is_del'       =>'tinyint',//是否删除,0未删除,1已删除
        'creater'       =>'varchar',//创建人
        'createrid'       =>'int',//创建人id
        'addtime'       =>'datetime',//创建时间
        'updater'       =>'varchar',//修改人
        'updaterid'       =>'int',//修改人id
        'updatetime'       =>'datetime',//修改时间
        'relation_code'       =>'char',//关联编码(目前只有供应商升级为业务公司时有用,存储业务公司编码)
    ];
    protected $createTime = 'addtime';
    protected $updateTime = 'updatetime';

    /**数据总表创建
    * @param $object
    * @param $type 1企业(业务公司),2客户,3供应商(只有供应商升级为业务公司的时候)
    * @return bool
    * @throws Exception
     */
    public static function createInfo($object,$type){

        $result =self::checkObject($object,$type);
        if(empty($result))throw new Exception("公司数据创建失败");
        $isobj= self::where(['code'=>$result['code'],'is_del'=>0])->findOrEmpty();
        if(!$isobj->isEmpty() && $type==3 && $object->is_upgrade==1){
            self::sycCompany($isobj->relation_code,$object);
        }

        $result['id']=$isobj->isEmpty()?null:$isobj->id;
        $info = $isobj->save($result);
        return $info;
    }
    /** 数据转换
     * @param $object
     * @param $type 1企业(业务公司),2客户,3供应商(只有供应商升级为业务公司的时候)
     * @return array
     * @throws Exception
     */
    private static function checkObject($object,$type){
        $result = [];
        switch ($type){
            case 1://业务公司
                $result = [
                    'code'       =>$object->companyNo,//编号
                    'name'       =>$object->company,//名称
                    'type'       =>1,//1企业(业务公司),2客户,3供应商(只有供应商升级为业务公司的时候)
                    'company_type'       =>0,//公司类型,只有企业有效,对应于company_type表
                    'invoice_title'       =>$object->invoice_title,//发票抬头
                    'invoice_people'       =>$object->inv_legaler,//发票纳税人
                    'invoice_addr'       =>$object->inv_addr,//发票地址
                    'invoice_mobile'       =>$object->invoice_mobile,//发票联系方式
                    'invoice_code'       =>$object->inv_code,//纳税识别号
                    'invoice_bank'       =>$object->inv_bank,//纳税银行
                    'invoice_bankNo'       =>$object->inv_bankNo,//纳税银行账户
                    'invoice_img'       =>$object->license_img,//营业执照
                    'remark'       =>'',//备注
                    "is_del"=>$object->is_del??0,
                    "creater"=>$object->creater,
                    "createrid"=>$object->createrid,
                    'updater' => $object->updater,
                    'updaterid' => $object->updaterid,
                 ];
                break;
            case 2://客户
               $result = [
                    'code'       =>$object->companyNo,//编号
                    'name'       =>$object->companyName,//名称
                    'type'       =>2,//1企业(业务公司),2客户,3供应商(只有供应商升级为业务公司的时候)
                    'company_type'       =>0,//公司类型,只有企业有效,对应于company_type表
                    'invoice_title'       =>$object->invoice_title??"",//发票抬头
                    'invoice_people'       =>$object->invoice_people??'',//发票纳税人
                    'invoice_addr'       =>$object->invoice_people??'',//发票地址
                    'invoice_mobile'       =>$object->invoice_mobile??'',//发票联系方式
                    'invoice_code'       =>$object->invoice_code??'',//纳税识别号
                    'invoice_bank'       =>$object->invoice_bank??'',//纳税银行
                    'invoice_bankNo'       =>$object->invoice_bankNo??'',//纳税银行账户
                    'invoice_img'       =>'',//营业执照
                    'remark'       =>'',//备注
                     'is_del'=>$object->is_del??0,
                    'creater'=>$object->creater,
                    'createrid'=>$object->createrid,
                    'updater' => $object->updater,
                    'updaterid' => $object->updaterid,
                   ];
                break;
            case 3://供应商
               $result = [
                    'code'       =>$object->code,//编号
                    'name'       =>$object['name'],//名称
                    'type'       =>3,//1企业(业务公司),2客户,3供应商(只有供应商升级为业务公司的时候)
                    'company_type'       =>0,//公司类型,只有企业有效,对应于company_type表
                    'invoice_title'       =>$object['name']??'',//发票抬头
                    'invoice_people'       =>$object->legaler??'',//发票纳税人
                    'invoice_addr'       =>$object->addr??'',//发票地址
                    'invoice_mobile'       =>'',//发票联系方式
                    'invoice_code'       =>$object->registercode??'',//纳税识别号
                    'invoice_bank'       =>'',//纳税银行
                    'invoice_bankNo'       =>'',//纳税银行账户
                    'invoice_img'       =>$object->license_img??'',//营业执照
                    'remark'       =>'',//备注
                    'is_del'=>$object->is_del??0,
                    'creater'=>$object->creater,
                    'createrid'=>$object->createrid,
                    'updater' => $object->updater,
                    'updaterid' => $object->updaterid,
                  ];
                break;
            default:
               $result=[];
               break;
        }
        return $result;
    }

    /** 供应商更新 同步业务公司数据
    * @param $relation_code
    * @param $object
    * @throws Exception
    */
    private static function sycCompany($relation_code,$object){
        $result = Business::where(['companyNo'=>$relation_code,"is_del"=>0])->findOrEmpty();
        if(!$result->isEmpty()){
            $result->company=$object['name'];
            $result->inv_code=$object->registercode;
            $result->license_img=$object->license_img;
            $result->inv_legaler=$object->legaler;
            $result->legaler_type=$object->legaler_type;
            $result->inv_time=$object->registertime;
            $result->addr=$object->addr;
            $result->inv_scope=$object->scope;
            $result->contactor=$object->contactor;
            $result->mobile=$object->mobile;
            $result->updater=$object->updater;
            $result->updaterid=$object->updaterid;
            $busup=$result->save();
            if(!$busup) throw new \Exception("业务公司同步失败");
            self::createInfo($result,1);
        }
    }

    public function companyInfo(){
        return $this->belongsTo(Business::class,'code','companyNo');
    }

    public function supplierInfo(){
        return $this->belongsTo(Supplier::class,'code','code');
    }

    public function customerInfo(){
        return $this->belongsTo(CustomerInfo::class,'code','companyNo');
    }

    public static function onAfterWrite($model) {
        event("copyCompany",$model->toArray());
     }
}