<?php


namespace app\models\point_plan;


use app\models\user\User;
use app\models\user\UserBill;
use app\models\user\UserMoney;
use crmeb\basic\BaseModel;
use crmeb\traits\ModelTrait;
use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class UserPointPlan extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';
    /**
     * 模型名称
     * @var string
     */
    protected $name = 'user_point_plan';
    use ModelTrait;

    public static function is_point($uid)
    {
        return self::where('uid', $uid)
            ->find();
    }

    public static function all_point()
    {
        return self::column('uid');
    }

    public static function day_release()
    {
        $list = self::where('last_release_day', '<>', date('Y-m-d'))
            ->whereOr('last_release_day', null)
            ->select();
//        var_dump(self::getLastSql());
        $res = true;
        BaseModel::beginTrans();
        foreach ($list as $v) {
            if ($v['release_time_now'] < $v['release_time_all']) {
                $day_release = bcdiv($v['release_money'], $v['release_time_all'], 8);
//                var_dump($day_release);
                $day_release = bcmul($day_release, $v['buy_num'], 8);
                $res = $res && UserMoney::incomeMoney($v['uid'], $v['release_money_type'], $day_release, 'release', '节点释放', '节点释放' . ' 第' . ($v['release_time_now'] + 1) . '期');
                $res = $res && self::where('id', $v['id'])
                        ->inc('release_time_now', 1)
                        ->update();
                $res = $res && self::where('id', $v['id'])
                        ->update(['last_release_day' => date('Y-m-d')]);
            }
        }
        BaseModel::checkTrans($res);
        return $res;
    }

    public static function day_release_ticket()
    {
        if (PointAwardLog::be(['add_date' => date('Y-m-d')])) return true;
        //昨日总票数
        $res = true;
        $plan = PointPlan::get(1);
        if (!$plan) return true;
        $points = UserPointPlan::sum('buy_num');
        $list = UserPointPlan::group('uid')->field('SUM(buy_num) as buys,uid')->select();
        BaseModel::beginTrans();
        $money_type = sys_data('money_type');
        foreach ($money_type as $v) {
            $sum_ticket = UserBill::where('status', 1)->where('category', $v['code'])
                ->whereTime('add_time', 'yesterday')
                ->where('type', 'lala_ticket')->value('SUM(number)');
            if ($sum_ticket > 0) {
                $brokerageRatio = bcdiv($plan['ticket_ratio'], 100, 4);
                $brokeragePrice = bcmul($sum_ticket, $brokerageRatio, 8);
                $every_price = bcdiv($brokeragePrice, $points, 8);
                foreach ($list as $vv) {
                    $brokeragePrice = bcmul($vv['buys'], $every_price, 8);
                    if ($brokeragePrice > 0) {
                        $mark = '昨日拼购门票' . $v['code'] . '合计' . $sum_ticket . ',获得节点门票分红' . $brokeragePrice . $v['code'];
                        $res = $res && UserMoney::incomeMoney($vv['uid'], $v['code'], $brokeragePrice, 'lala_point_award', '节点分红', $mark);
                    }
                }
            }
        }
        $res = $res && PointAwardLog::create(['add_date' => date('Y-m-d')]);
        BaseModel::checkTrans($res);
        return $res;
    }


    /**
     * @param $where
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getList($where)
    {
        $model = new self();
        if (isset($where['id']) && $where['id'] != '') {
            $model->where('plan_id', $where['id']);
        }
        $count = $model->count();
        $data = $model->page((int)$where['page'], (int)$where['limit'])->select()->each(function ($item) {
            $item['_status'] = ($item['status'] == 1) ? "考核成功" : (($item['status'] == 2) ? "考核失败" : "考核中");
            $item['user'] = User::getUserInfo($item['uid']);
            $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
            $item['_pass_time'] = $item['pass_time'] ? date('Y-m-d H:i:s', $item['pass_time']) : '--';
            $item['_check_end_time'] = date('Y-m-d H:i:s', $item['check_end_time']);
        });
        return compact('count', 'data');
    }

}