<?php

namespace app\admin\service\coupon;

use app\model\CouponDetail;
use support\Db;

class CouponService
{
    /**
     * @Desc 优惠券自动过期
     * @Author Gorden
     * @Date 2024/8/26 17:58
     *
     * @return void
     */
    public static function checkCouponExpired()
    {
        $couponDetails = Db::select("select * from app_coupon_detail where (coupon_detail_status = 'INIT' OR coupon_detail_status = 'PENDING' OR coupon_detail_status = 'ACTIVED' OR coupon_detail_status = 'WAITING') AND coupon_detail_deadline_datetime != '' AND CAST(UNIX_TIMESTAMP(coupon_detail_deadline_datetime) as SIGNED) < " . time());
        foreach ($couponDetails as $detail) {
            $endTimeUnix = strtotime($detail->coupon_detail_deadline_datetime);
            if ($endTimeUnix < time()) {
                CouponDetail::where('coupon_detail_id', $detail->coupon_detail_id)->update(['coupon_detail_status' => 'EXPIRED']);
                echo $detail->coupon_detail_id . "已过期\n";
            }
        }
    }

    public static function couponClassifyInfo($classify, $category, $value, $limit)
    {
        try {
            switch ($classify) {
                case "满减券":
                    if (!empty($limit) && $category == 'NORMAL') {
                        return "满" . $limit . '减' . $value;
                    }elseif (!empty($limit) && !empty($value) && $category == 'PIECE'){
                        return "满" . intval($limit) . '件减' . $value;
                    }
                    break;
                case "立减券":
                    return "立减" . $value;
                    break;
                case "抵用券":
                    return "抵用券";
                    break;
                case "折扣券":
                    if (!empty($limit) && $category == 'NORMAL') {
                        return "满" . $limit . '打' . ($value / 10) . '折';
                    }else if (!empty($limit) && !empty($value) && $category == 'PIECE') {
                        return "满" . intval($limit) . '件打' . ($value / 10) . '折';
                    } else {
                        return $value/10 . '折';
                    }
                    break;
                case "赠品券":
                    return "赠品券";
                    break;
                case "福利券":
                    return '抵扣' . $value;
                    break;
                case "年卡":
                    return "年卡";
                    break;
                case "季卡":
                    return "季卡";
                    break;
                case "月卡":
                    return "月卡";
                    break;
            }
        } catch (\Exception $e) {
        }

    }
}