<?php

namespace app\common\model\user\withdrawal;

use app\common\model\BaseModel;
use think\Model;
use traits\model\SoftDelete;

class Userwithdrawal extends BaseModel
{

    use SoftDelete;

    

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

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [

    ];
    

    







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


    public function detail($user_id)
    {
        $withdrawal = self::where('user_id',$user_id)->find();;
        return compact('withdrawal');
    }


    /** 添加或修改提现信息
     * @param $user_id
     * @param $params
     * @param $trans
     * @return true
     * @throws \Exception
     */
    public function addOrupdateWithdrawal($user_id,$params,$trans=false){

        //表结构如下：
        //CREATE TABLE `dyqc_user_withdrawal` (
        //  `id` int(11) NOT NULL AUTO_INCREMENT,
        //  `user_id` int(11) NOT NULL COMMENT '提现用户',
        //  `name` varchar(255) NOT NULL COMMENT '转账账户名',
        //  `bank_name` varchar(255) NOT NULL COMMENT '开户行名称（具体到支行）',
        //  `bank_user_name` varchar(255) NOT NULL COMMENT '银行账户号',
        //  `id_number` varchar(255) NOT NULL COMMENT '身份证号',
        //  `createtime` bigint(11) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',
        //  `updatetime` bigint(11) unsigned DEFAULT '0' COMMENT '修改时间',
        //  `deletetime` bigint(11) unsigned DEFAULT NULL COMMENT '删除时间',
        //  PRIMARY KEY (`id`)
        //) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户银行卡绑定';
        //这里应对基础提交信息做验证：开户行名称bank_name，转账账户名name，银行账户号bank_user_name，身份证号id_number
        //以下对每个字段做验证，如果验证不通过，则抛出异常

        $rule =  [
            'bank_name'=>'require',
            'name'=>'require',
            'bank_user_name'=>'require',
            'id_number'=>'require',
        ];


        $rule_msg = [
            'bank_name.require'=>'开户行名称不能为空',
            'name.require'=>'转账账户名不能为空',
            'bank_user_name.require'=>'银行账户号不能为空',
            'id_number.require'=>'身份证号不能为空',

        ];

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


//判断逻辑
        if($trans){
            self::beginTrans();
        }
        $res = true;
        try{
            //此接口需要给用户添加银行卡信息，如果用户没绑过，则添加，如果用户已经绑定过，则修改
            $userWithdrawal = self::where('user_id',$user_id)->find();
            if($userWithdrawal){
                $userWithdrawal->save($params);
            }else{
                $userWithdrawal = new self();
                $userWithdrawal->save($params);
            }
            if($trans){
                self::commitTrans();
            }
        }catch (\Exception $e){
            if($trans){
                self::rollbackTrans();
            }
            throw new \Exception($e->getMessage().$e->getFile().$e->getLine());
        }
        return $userWithdrawal;
    }





}
