<?php

namespace app\common\model\manystore;

use app\common\model\BaseModel;
use app\common\model\dyqc\ManystoreShop;
use app\common\model\school\classes\Teacher;
use app\common\model\school\classes\Verification;
use app\common\model\User;
use think\Model;


class ShopApply extends BaseModel
{

    

    

    // 表名
    protected $name = 'manystore_shop_apply';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = "create_time";
    protected $updateTime = "update_time";
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'create_time_text',
        'update_time_text'
    ];




    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function shop()
    {
        return $this->belongsTo(Shop::class, 'shop_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function apply($type,$user_id=0,$params,$check=false,$trans=false){

        if(!in_array($type,['1','2'])) throw new \Exception('类型参数错误');

        if($check){
            $user = User::get($user_id ?: ($params["user_id"] ?? 0));
            if(!$user)throw new \Exception("用户不存在");
            //已经是机构
            $shop = ManystoreShop::where( 'user_id',$user_id)->find();
            if($shop){
                throw new \Exception("已是机构，请勿重复申请");
            }else{
                //申请认证的时候不能是老师或其他机构的核销员
                $teacher = Teacher::where("user_id" ,$user_id)->find();
                if($teacher) throw new \Exception("您已经是老师，不能申请认证！");

                $verification = Verification::where("user_id" ,$user_id)->find();
                if($verification) throw new \Exception("您已经是机构核销员，不能申请认证！");
            }

        }


        switch ($type) {
            case '1': //个人
                $rule = [
                    'realname' => 'require',
                    'user_id' => 'require',
                    'mobile' => 'require',
                    'address' => 'require',
                ];
                $rule_msg = [
                    'user_id.require' => '请指定提交用户',
//                    'name.require' => '机构名称必须填写',
                    'realname.require' => '联系人必须填写',
                    'mobile.require' => '联系人电话必须是数字',
                    'address.require' => '联系地址必须填写',
                ];
                break;
            case '2': //机构

                $rule = [
                    'name' => 'require',
                    'realname' => 'require',
                    'mobile' => 'require',
                    'address' => 'require',
                    'user_id' => 'require',
                ];

                $rule_msg = [
                    'user_id.require' => '请指定提交用户',
                    'name.require' => '机构名称必须填写',
                    'realname.require' => '联系人必须填写',
                    'mobile.require' => '联系人电话必须是数字',
                    'address.require' => '联系地址必须填写',
                ];

                break;
        }
        self::check($params,$rule,$rule_msg);




//判断逻辑
        if($trans){
            self::beginTrans();
        }
//        $res = false;
        try{
            $res = self::create($params);

            if($trans){
                self::commitTrans();
            }
        }catch (\Exception $e){
            if($trans){
                self::rollbackTrans();
            }
            throw new \Exception($e->getMessage().$e->getFile().$e->getLine());
        }
        return $res;
    }
}
