<?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('sort DESC,id DESC');
        $id = [];
//        if ($data['advance']){
//            $model->where('id', 'in', $id);
//        }
        $slider_image = AuctionGu::where('id', $data['auction_gu_id'])->find();
        $list['data'] = $model->select();
        $list['data'] = count($list['data']) ? $list['data']->toArray() : [];
        if ($list){
            foreach ($list['data'] as  $k =>$v)
            {
                $list['data'][$k]['time'] = strtotime($v['end_time']);
                $list['data'][$k]['start_time'] = strtotime($v['radd_time']);
                $list['data'][$k]['top_time'] = strtotime($v['rend_time']);
                $list['data'][$k]['day'] = date('Y-m-d H:i:s', strtotime($v['end_time']));
                $booking = AuctionBooking::where([['uid', '=', $uid], ['frequency', '=', $v['frequency']]])->field('auction_id')->select();
                foreach ($booking as $value) {
                    $id[] = $value['auction_id'];
                }
                if (in_array($v['id'], $id)){
                    $list['data'][$k]['sta'] = 2; // 进入
                    $list['data'][$k]['str'] = '进入';
                }else{
                    $list['data'][$k]['sta'] = 1; // 预约
                    $list['data'][$k]['str'] = '预约';
                }
            }
            $list['slider_image'] = json_decode($slider_image['slider_image']);
        }
        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]);
//                }
//
//            }
//        }
//
//
//    }

}
