<?php

namespace app\admin\model;

use think\Model;

class Userinfo extends Model
{
    public function updateUserInfo($condition, $data)
    {
        $result = $this
            ->where($condition)
            ->update($data);

        return $result;
    }

    public function getUserInfo($condition)
    {
        $result = $this
            ->where($condition)
            ->find();

        return $result;
    }

    public function findOne($uid, $format = 0)
    {
        $ret = $this->where(['user_id' => $uid])->find();
        if (!$ret) {
            return false;
        }
        if ($format) {
            return $this->FormatData($ret);
        } else {
            return $ret;
        }
    }

    private function FormatData($model)
    {
        $return = [];
        $arr = $model->toArray();
        $allKey = $this->KeyMap();
        $skipF = ['userInfo_id', 'user_id'];
        $imgF = ['userInfo_header', 'img_front', 'img_back', 'businesslicense'];

        foreach ($arr as $key => $val) {
            if (in_array($key, $skipF)) {
                continue;
            }
            if (!empty($val)) {
                $kname = $allKey[$key];
                if (in_array($key, $imgF)) {
                    $return[$kname] = "<a href='$val' target='_blank'><img width='120' src='$val'></a>";
                } else {
                    $return[$kname] = $val;
                }
            }
        }
        return $return;
    }

    private function KeyMap()
    {
        return [
            'userInfo_id' => '用户信息自增ID',
            'user_id' => '用户ID',
            'userInfo_money' => '用户余额',
            'userInfo_header' => '用户头像',
            'userInfo_name' => '真实姓名',
            'userInfo_identity' => '身份证号码',
            'userInfo_bank_card' => '银行卡卡号',
            'userInfo_debit_card' => '借记卡卡号',
            'img_front' => '证件正面',
            'img_back' => '证件反面',
            'enterprise' => '企业名称',
            'area' => '企业地区',
            'enterprise_location' => '企业所在地',
            'enterprise_address' => '企业通讯地址',
            'contact_name' => '联系人姓名',
            'whether' => '是否三证合一',
            'credit_code' => '社会信用代码',
            'bank_location' => '开户银行所在地',
            'bank' => '银行名称',
            'bankbranch' => '银行支行名称',
            'businesslicense' => '营业执照',
        ];
    }

}
