<?php
/**
 * Created by CRMEB.
 * Copyright (c) 2017~2019 http://www.crmeb.com All rights reserved.
 * Author: liaofei <136327134@qq.com>
 * Date: 2019/3/27 21:44
 */

namespace app\models\user;

use crmeb\traits\ModelTrait;
use crmeb\basic\BaseModel;

/**
 * TODO 用户消费新增金额明细 model
 * Class UserBill
 * @package app\models\user
 */
class Achievement extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'achievement';

    use ModelTrait;


    public static function lastMonthAchievement($uid)
    {
        $achievement = self::where('uid', $uid)
            ->where('from_uid', '<>', $uid)
            ->whereTime('add_time', 'last month')
            ->sum('achievement');
        return $achievement;
    }

    public static function achievement($uid)
    {
        $achievement = self::where('uid', $uid)
            ->where('from_uid', '<>', $uid)
            ->sum('achievement');
        return $achievement;
    }

    public static function recommendeAchievement($uid)
    {
        $uids = User::where('spread_uid', $uid)->column('uid');
        $achievement = self::where('uid', 'in', $uids)
            ->where('from_uid = uid')
            ->sum('achievement');
        return $achievement;
    }


    public static function setLastMonthAchievementUse()
    {
        return self::whereTime('add_time', 'last month')
            ->where('month', 0)
            ->update(['month' => 1]);
    }

    public static function monthAward()
    {
        $users = User::select();
        $get = [];
        $award = sys_data('month_award');
        array_multisort(array_column($award, 'achievement'), SORT_ASC, $award);
        $res = true;
        foreach ($users as $v) {
            $achievement = self::lastMonthAchievement($v['uid']);
            $sum_award = 0;
            $sum_integral = 0;
            foreach ($award as $k => $vv) {
                if ($vv['achievement'] <= $achievement) {
                    //如果业绩大于已发放业绩并且业绩大于等于发放下限（10000）
                    if (isset($award[$k + 1])) {
                        //下一级的发放下限/业绩~本级发放下限业绩
                        $send = $award[$k + 1]['achievement'] > $achievement ? ($award[$k + 1]['achievement'] - $vv['achievement']) : ($achievement - $vv['achievement']);
                    } else {
                        $send = $achievement - $vv['achievement'];
                    }
                    $sum_award = bcadd($sum_award, bcmul($send, bcdiv($vv['ratio'], 100, 2), 2), 2);
                    $sum_integral = bcadd($sum_integral, bcmul($send, bcdiv($vv['integral_ratio'], 100, 2), 2), 2);
                }
            }
            if ($sum_award > 0) {
                $real_send = bcmul($sum_award, 0.5, 2);
                $sum_integral = bcadd($sum_integral, bcsub($sum_award, $real_send, 2), 2);
                $res = $res && User::where('uid', $v['uid'])->inc('brokerage_price', $real_send)->update();
                $res = $res && UserBill::income('月度业绩分红', $v['uid'], 'now_money', 'brokerage', $real_send, 0, User::where('uid', $v['uid'])->value('brokerage_price'), '月业绩' . $achievement . '分红获得佣金' . $real_send);
            }
            if ($sum_integral > 0) {
                $res = $res && User::where('uid', $v['uid'])->inc('integral', $sum_integral)->update();
                $res = $res && UserBill::income('月度业绩分红（消费券）', $v['uid'], 'integral', 'brokerage_month_integral', $sum_integral, 0, User::where('uid', $v['uid'])->value('integral'), '月业绩' . $achievement . '分红获得消费券' . $sum_integral);
            }
        }
        return $res && self::setLastMonthAchievementUse();
    }
}
