<?php

namespace app\model;

use app\exception\RepositoryException;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\model\relation\HasOne;

class AccountCoupon extends Base
{
    public const STATUS_NORMAL  = 'normal';//正常
    public const STATUS_USED    = 'used';//已使用
    public const STATUS_INVALID = 'invalid';//已失效

    public const CODE_SALT        = 'R5S6Y1';//优惠券加密密码盐

    /**
     * 状态列表
     *
     * @return string[]
     */
    public static function statusList(): array
    {
        return [self::STATUS_NORMAL, self::STATUS_USED, self::STATUS_INVALID];
    }

    public function coupon(): HasOne
    {
        return $this->hasOne(Coupon::class, 'id', 'coupon_id')->bind(
            [
                'amount',
                'name',
                'cover',
                'type',
                'condition',
                'begin_at',
                'end_at',
            ]
        );
    }
    public function account(): HasOne
    {
        return $this->hasOne(Account::class, 'id', 'account_id')->bind(
            [
                'nickname',
                'mobile',
            ]
        );
    }

    /**
     * 优惠券使用
     *
     * @param  int  $accountId
     * @param  int  $id
     * @param  string  $orderCoding
     * @param  string  $checkBy
     * @return bool
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @throws RepositoryException
     */
    public static function use(int $accountId, int $id, string $orderCoding = '',$checkBy =""): bool
    {
        $where = ['coupon_id' => $id, 'account_id' => $accountId, 'status' => AccountCoupon::STATUS_NORMAL];
        if (!$accountCoupon = self::findOne($where)) {
            throw new RepositoryException('您的优惠券不存在');
        }

        if (!empty($accountCoupon['deleted_at'])) {
            throw new RepositoryException('您的优惠券不存在-2');
        }

        if (!$coupon = Coupon::findById($id)) {
            throw new RepositoryException('此优惠券不存在或已下架');
        }

        if (!empty($coupon['deleted_at'])) {
            throw new RepositoryException('此优惠券已下架');
        }

        $now = date('Y-m-d H:i:s');
        if ($now < $coupon['begin_at'] || $now > $coupon['end_at']) {
            throw new RepositoryException('此优惠券不在有效期');
        }

        if($accountCoupon["status"] != AccountCoupon::STATUS_NORMAL){
            throw new RepositoryException('优惠券已使用或已失效');
        }

        return $accountCoupon->save([
            'used_at'      => $now,
            'status'       => self::STATUS_USED,
            'order_coding' => $orderCoding,
            'check_by' => $checkBy,
        ]);
    }

    /**
     * 获取首页优惠券
     * 没有或已领取则返回null
     *
     * @param  int  $accountId
     * @return Coupon|array|\think\Model|null
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function homeCoupon(int $accountId)
    {
        if (!$coupon = Coupon::homeCoupon()) {
            return null;
        }

        if (self::where('account_id', $accountId)->where('coupon_id', $coupon['id'])->count() > 0) {
            return null;
        }

        return $coupon;
    }
}