<?php

namespace app\common\model;

use think\Model;
/**
 * 会员模型
 */
class User extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 追加属性
    protected $append = [
        'url',
    ];

    /**
     * 获取个人URL
     * @param   string $value
     * @param   array  $data
     * @return string
     */
    public function getUrlAttr($value, $data)
    {
        return "/u/" . $data['id'];
    }

    /**
     * 获取头像
     * @param   string $value
     * @param   array  $data
     * @return string
     */
    public function getAvatarAttr($value, $data)
    {
        if (!$value) {
            //如果不需要启用首字母头像，请使用
            //$value = '/assets/img/avatar.png';
            $value = letter_avatar($data['nickname']);
        }
        return $value;
    }

    /**
     * 获取会员的组别
     */
    public function getGroupAttr($value, $data)
    {
        return UserGroup::get($data['group_id']);
    }

    /**
     * 获取验证字段数组值
     * @param   string $value
     * @param   array  $data
     * @return  object
     */
    public function getVerificationAttr($value, $data)
    {
        $value = array_filter((array)json_decode($value, true));
        $value = array_merge(['email' => 0, 'mobile' => 0], $value);
        return (object)$value;
    }

    /**
     * 设置验证字段
     * @param mixed $value
     * @return string
     */
    public function setVerificationAttr($value)
    {
        $value = is_object($value) || is_array($value) ? json_encode($value) : $value;
        return $value;
    }

    /**
     * 变更会员余额
     * @param int    $money   余额
     * @param int    $user_id 会员ID
     * @param string $memo    备注
     */
    public static function money($money, $user_id, $memo)
    {
        $user = self::get($user_id);
        if ($user && $money != 0) {
            $before = $user->money;
            //$after = $user->money + $money;
            $after = function_exists('bcadd') ? bcadd($user->money, $money, 2) : $user->money + $money;
            //更新会员信息
            $user->save(['money' => $after]);
            //写入日志
            MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo]);
        }
    }

    /**
     * 变更会员积分
     * @param int    $score   积分
     * @param int    $user_id 会员ID
     * @param string $memo    备注
     */
    public static function score($score, $user_id, $memo)
    {
        $user = self::get($user_id);
        if ($user && $score != 0) {
            $before = $user->score;
            $after = $user->score + $score;
            $level = self::nextlevel($after);
            //更新会员信息
            $user->save(['score' => $after, 'level' => $level]);
            //写入日志
            ScoreLog::create(['user_id' => $user_id, 'score' => $score, 'before' => $before, 'after' => $after, 'memo' => $memo]);
        }
    }

    /**
     * 根据积分获取等级
     * @param int $score 积分
     * @return int
     */
    public static function nextlevel($score = 0)
    {
        $lv = array(1 => 0, 2 => 30, 3 => 100, 4 => 500, 5 => 1000, 6 => 2000, 7 => 3000, 8 => 5000, 9 => 8000, 10 => 10000);
        $level = 1;
        foreach ($lv as $key => $value) {
            if ($score >= $value) {
                $level = $key;
            }
        }
        return $level;
    }

    public static function subMoney($money,$user_id,$memo)
    {
        
    }

    /**
     * 变更冻结金额
     * @param int    $money   余额
     * @param int    $user_id 会员ID
     * @param string $memo    备注
     */
    public static function frozen($money, $user_id, $memo)
    {
        $user = self::get($user_id);
        if ($user && $money != 0) {
            $before = $user->frozen;
            //$after = $user->money + $money;
            $after = function_exists('bcadd') ? bcadd($user->frozen, $money, 2) : $user->frozen + $money;
            //更新会员信息
            $user->save(['frozen' => $after]);
            //写入日志
        }
    }

    /**
     * 三级分销
     * @param int    $money   金额
     * @param int    $user_id 用户
     */
    public static function distribution($money, $user_id)
    {   
        // 查询用户信息
        $user = self::get($user_id);
        if(!$user){
            return $this->error(__('没有此用户'));
        }
        // 查询三级分销比例
        $distribution = TtDistribution::where('id',1)->find();
        // 计算分销金额
        $distribution_money = $money * ($distribution['primary_distribution']/100 + $distribution['secondary_distribution']/100 +$distribution['third_distribution']/100);
        // 计算现有余额扣除分销金额
        $now_money = $user->money + ($money - $distribution_money);
        // 更新用户的余额
        $user_money = self::where('id',$user_id)->update(['money' => $now_money])
        // 一级分销开始
        if($user->parent_id != 0){
            // 查询我的直属上级
            $first_level = self::where('id',$user->parent_id)->find();
            // 计算直属上级的余额加上分销的金额
            $first_distribution = $money * ($distribution['primary_distribution']/100) + $first_level['money'];
            // 更新直属上级的余额
            $first_user_money = self::where('id',$first_level['id'])->update(['money' => $first_distribution]);
            // 写入余额变动日志
            if($first_user_money){
                MoneyLog::create(['user_id' => $first_level['id'], 'money' => $money * ($distribution['primary_distribution']/100), 'before' => $first_level['money'], 'after' => $first_user_money, 'memo' => '直属上级获得佣金']);
            }
            //二级分销开始
            if($first_level['parent_id'] != 0){
                // 查询我上级的上级
                $second_level = self::where('id',$first_level['parent_id'])->find();
                // 计算二级上级的余额加上分销的金额
                $second_distribution = $money * ($distribution['secondary_distribution']/100) + $second_level['money'];
                // 更新二级上级的余额
                $second_user_money = self::where('id',$second_level['id'])->update(['money' => $second_distribution]);
                // 写入余额变动日志
                if($second_user_money){
                    MoneyLog::create(['user_id' => $second_level['id'], 'money' => $money * ($distribution['secondary_distribution']/100), 'before' => $second_level['money'], 'after' => $second_user_money, 'memo' => '二级上级获得佣金']);
                }
                //三级分销开始
                if($second_level['parent_id'] != 0){
                    // 查询我上级的上级的上级
                    $third_level = self::where('id',$second_level['parent_id'])->find();
                    // 计算三级上级的余额加上分销的金额
                    $third_distribution = $money * ($distribution['third_distribution']/100) + $third_level['money'];
                    // 更新三级上级的余额
                    $third_user_money = self::where('id',$third_level['id'])->update(['money' => $third_distribution]);
                    // 写入余额变动日志
                    if($third_user_money){
                        MoneyLog::create(['user_id' => $third_level['id'], 'money' => $money * ($distribution['third_distribution']/100), 'before' => $third_level['money'], 'after' => $third_user_money, 'memo' => '三级上级获得佣金']);
                    }
                }
                //三级分销技术
            }
            //二级分销结束
        }
        //一级分销结束
        if($user_money){
            // 写入余额变动日志
            MoneyLog::create(['user_id' => $user_id, 'money' => $money - $distribution_money, 'before' => $user->money, 'after' => $now_money, 'memo' => '分销获得佣金']);
            return true;
        } else {
            return false;
        }
    }
}
