<?php

/**
 *
 * @author: xaboy<365615158@qq.com>
 * @day: 2017/11/02
 */

namespace app\models\auction;

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

/**
 * 竞拍上坪 Model
 * Class WechatNews
 * @package app\admin\model\wechat
 */
class AuctionProduct extends BaseModel
{

    use ModelTrait;

    protected $pk = 'id';

    protected $name = 'auction_product';
    protected $autoWriteTimestamp = true;

    /**
     * 竞拍商品列表
     * @param $data
     * @param $uid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function list($data, $uid){
        $model = self::where('is_show', 1)->where('auction_id', $data['id'])->order('sort DESC,id DESC');
        $model->page($data['page'], $data['limit']);
        if ($data['name']) $model->where('name', 'like', '%'.$data['name'].'%');
        $list = $model->select();
        $list = empty($list)? [] : $list->toArray();
        $lists = [];
        if ($list){
            foreach ($list as $k => $v) {
                $auction = Auction::where('id', $v['auction_id'])->find();
                $order = AuctionOrder::where('product_id', $v['id'])->where('status', '>', 0)->where('frequency', $auction['frequency'])->find();
                if ($order){
                    $list[$k]['status'] = 2;// 已被购买
                    $list[$k]['str'] = '已卖完';
                }else{
                    $list[$k]['status'] = 1;// 能购买
                    $list[$k]['str'] = '购买';
                }
                if ($v['is_admin'] == 2){
                    $time = AuctionTime::where([['auction_id', '=', $auction['id']], ['product_id', '=', $v['id']], ['add_time', '=', strtotime(date('Y-m-d', time()))]])->find();
                    if (!$time){
                        unset($list);
                    } else{
                        $lists[] = $v;
                    }
                }else{
                    $lists[] = $v;
                }
            }
        };
        return $lists;
    }

    /**
     * 用户商品
     * @param $data
     * @param $uid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function user_product($data, $uid){
        $model = self::where('is_show', 1)->where('uid', $uid)->order('sort DESC,id DESC');
        $model->page($data['page'], $data['limit']);

        $list = $model->select();
        $list = empty($list)? [] : $list->toArray();

        return $list;
    }


}