<?php

namespace app\admin\model\users;

use app\admin\model\UserMoneyLog;
use think\Model;


class Users extends Model
{

    

    

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

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

    // 追加属性
    protected $append = [
        'sex_text',
        'prevtime_text',
        'logintime_text',
        'jointime_text',
        'signtime_text',
        'type_text',
        'user_type_text',
        'status_text'
    ];
    

    
    public function getSexList()
    {
        return ['0' => __('Sex 0'), '1' => __('Sex 1'), '2' => __('Sex 2')];
    }

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

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

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


    public function getSexTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['sex']) ? $data['sex'] : '');
        $list = $this->getSexList();
        return isset($list[$value]) ? $list[$value] : '';
    }


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


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


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

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

    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        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] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

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

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

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

    /**
     * @param $user_id
     * @param $num
     * @param int $detail_type
     * @param string $intro
     * @param null $logs_id
     * @param null $code
     * @param null $mune
     * @param null $order_id
     * @param int $status
     * @return |bool
     */
    public static function addMoney($user_id, $num,$type,$intro = '',$logs_id = null,$order_id = null,$before,$after){
        if (self::updateCount($user_id, 'total_money', $num)) {
            return UserMoneyLog::insertData($user_id,$num,$type,$intro,$logs_id,$order_id,$before,$after);
        }
        return false;
    }

    /**
     * @param $user_id
     * @param $num
     * @param int $detail_type
     * @param string $intro
     * @param null $logs_id
     * @param null $code
     * @param null $mune
     * @param null $order_id
     * @param int $status
     * @return |bool
     */
    public static function addShoppingmoney($user_id, $num,$type,$intro = '',$logs_id = null,$order_id = null,$before,$after){
        if (self::updateCount($user_id, 'shopping_money', $num)) {
            return UserMoneyLog::insertData($user_id,$num,$type,$intro,$logs_id,$order_id,$before,$after);
        }
        return false;
    }

    /**
     * @param $id
     * @param $col
     * @param int $num
     * @return int
     */
    public static function updateCount($id,$col,$num = 1){
        $id = (int)$id;
        if($num>0){
            return self::where('id',"=",$id)->setInc($col,$num);
        }else{
            return self::where('id',"=",$id)->setDec($col,abs($num));
        }
    }
}
