<?php

namespace app\model;

use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

/**
 * 优惠券
 * Class Coupon
 * @package app\model
 */
class Coupon extends Base
{
    public const TYPE_DISCOUNT = 'discount';//满减
    public const TYPE_TASTE    = 'taste';//体验

    /**
     * 类型文案
     *
     * @return string[]
     */
    public static function typeTextList(): array
    {
        return [
            self::TYPE_DISCOUNT => '满减券',
            self::TYPE_TASTE    => '体验券',
        ];
    }

    /**
     * 可领取列表
     *
     * @param  array  $without 需要排除的ID列表
     * @return Coupon[]|array|Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function normalList(array $without = [])
    {
        $now = date('Y-m-d H:i:s');
        return self::whereNotIn('id', $without)
            ->whereNull('deleted_at')
            ->where('begin_at', '<', $now)
            ->where('end_at', '>', $now)
            ->where('remain', '>', 0)
            ->order('sort', 'desc')
            ->order('created_at', 'desc')
            ->select();
    }

    /**
     * 获取首页推荐优惠券
     *
     * @return Coupon|array|\think\Model|null
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function homeCoupon()
    {
        $now = date('Y-m-d H:i:s');
        return self::where('is_home', self::COMMON_ON)
            ->whereNull('deleted_at')
            ->where('begin_at', '<', $now)
            ->where('end_at', '>', $now)
            ->where('remain', '>', 0)
            ->order('sort', 'desc')
            ->order('created_at', 'desc')
            ->find();
    }
}