<?php

namespace app\common\model\project;

use liuniu\BaseModel;
use think\Model;


class ProjectDonationUser extends BaseModel
{

    

    

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

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

    // 追加属性
    protected $append = [
        'anonymous_text',
        'invoice_text',
        'user_type_text'
    ];
    

    
    public function getAnonymousList()
    {
        return ['0' => __('Anonymous 0'), '1' => __('Anonymous 1')];
    }

    public function getInvoiceList()
    {
        return ['0' => __('Invoice 0'), '1' => __('Invoice 1')];
    }

    public function getUserTypeList()
    {
        return ['0' => __('User_type 0'), '1' => __('User_type 1')];
    }


    public function getAnonymousTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['anonymous']) ? $data['anonymous'] : '');
        $list = $this->getAnonymousList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getInvoiceTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['invoice']) ? $data['invoice'] : '');
        $list = $this->getInvoiceList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getUserTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['user_type']) ? $data['user_type'] : '');
        $list = $this->getUserTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    /**
     * 设置默认用户
     * @param $id 地址id
     * @param $uid 用户uid
     * @return bool
     */
    public static function setDefault($id,$uid,$type=0,$cid=0)
    {
        self::beginTrans();
        $model = new self;
        if ($cid>0) $model = $model->where('cid',$cid);
        $res1 =$model::where('uid',$uid)->where('type',$type)->update(['is_default'=>0]);
        $res2 = $model::where('id',$id)->where('uid',$uid)->where('type',$type)->update(['is_default'=>1]);
        $res =$res1 !== false && $res2 !== false;
        self::checkTrans($res);
        return $res;
    }

    /**
     * 获取默认用户
     * @param $uid
     * @param int $type
     * @param string $field
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static  function getDefault($uid,$type=0,$field="*",$cid=0)
    {
        return self::where('cid',$cid)->where('uid',$uid)->where('type',$type)->where('is_default',1)->field($field)->find();
    }



}
