<?php

namespace app\models\auction;

use app\models\store\StoreProduct;
use app\models\user\User;
use crmeb\services\SystemConfigService;
use think\facade\Db;
use crmeb\traits\ModelTrait;
use crmeb\basic\BaseModel;

/**
 * TODO 场馆model
 * Class Article
 * @package app\models\article
 */
class Auction extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

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

    use ModelTrait;


    public function list($data, $uid)
    {
        $model = self::where([['delete_time', '=', 0], ['status' ,'=', '1'], ['auction_gu_id', '=', $data['auction_gu_id']]]);
        $model->page($data['page'], $data['limit']);
        $model->order('id DESC, sort DESC');
        $id = [];
        $booking = AuctionBooking::where([['uid', '=', $uid], ['create_time', '>', strtotime(date('Y-m-d'), time())]])->field('auction_id')->select();
        foreach ($booking as $v) {
            $id[] = $v['auction_id'];
        }
        if ($data['advance']){
            $model->where('id', 'in', $id);
        }
        $list = $model->select();
        $list = count($list) ? $list->toArray() : [];
        if ($list){
            foreach ($list as  $k =>$v)
            {
                $list[$k]['time'] = strtotime($v['end_time']);
                $list[$k]['day'] = date('Y-m-d H:i:s', strtotime($v['end_time']));
                if (in_array($v['id'], $id)){
                    $list[$k]['sta'] = 2; // 进入
                    $list[$k]['str'] = '进入';
                }else{
                    $list[$k]['sta'] = 1; // 预约
                    $list[$k]['str'] = '预约';
                }
            }
        }
        return $list;
    }

    /**
     * 更新场次
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function frequency()
    {
        $list = self::select();
        foreach ($list as $k => $v){
            if ($v['day_time'] < time()){
                $find = self::find($v['id']);
                $find['day_time'] = strtotime(date('Y-m-d 23:59:59'));
                $find['frequency'] = $v['frequency'] + 1;
                $find->save();
            }
        }
    }


    public static function is_new()
    {
        $user = User::where('is_new', 1)->select();
        if ($user){
            foreach ($user as $k => $v)
            {
                if ($v['add_time']+3600 < time()){
                    User::where('uid', $v['uid'])->update(['is_new' => 0]);
                }

            }
        }


    }

}
