package com.gxwebsoft.shop.entity;

import java.math.BigDecimal;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import java.time.LocalDateTime;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.baomidou.mybatisplus.annotation.TableLogic;
import java.io.Serializable;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 用户优惠券
 *
 * @author 科技小王子
 * @since 2025-08-11 23:51:41
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Schema(name = "ShopUserCoupon对象", description = "用户优惠券")
public class ShopUserCoupon implements Serializable {
    private static final long serialVersionUID = 1L;

    // 优惠券类型常量
    public static final int TYPE_REDUCE = 10;    // 满减券
    public static final int TYPE_DISCOUNT = 20;  // 折扣券
    public static final int TYPE_FREE = 30;      // 免费券

    // 使用状态常量
    public static final int STATUS_UNUSED = 0;   // 未使用
    public static final int STATUS_USED = 1;     // 已使用
    public static final int STATUS_EXPIRED = 2;  // 已过期

    // 获取方式常量
    public static final int OBTAIN_ACTIVE = 10;    // 主动领取
    public static final int OBTAIN_SYSTEM = 20;    // 系统发放
    public static final int OBTAIN_ACTIVITY = 30;  // 活动赠送

    // 适用范围常量
    public static final int APPLY_ALL = 10;        // 全部商品
    public static final int APPLY_GOODS = 20;      // 指定商品
    public static final int APPLY_CATEGORY = 30;   // 指定分类

    @Schema(description = "id")
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @Schema(description = "优惠券模板ID")
    private Integer couponId;

    @Schema(description = "用户ID")
    private Integer userId;

    @Schema(description = "优惠券名称")
    private String name;

    @Schema(description = "优惠券描述")
    private String description;

    @Schema(description = "优惠券类型(10满减券 20折扣券 30免费劵)")
    private Integer type;

    @Schema(description = "满减券-减免金额")
    @JsonSerialize(using = ToStringSerializer.class)
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private BigDecimal reducePrice;

    @Schema(description = "折扣券-折扣率(0-100)")
    private Integer discount;

    @Schema(description = "最低消费金额")
    @JsonSerialize(using = ToStringSerializer.class)
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private BigDecimal minPrice;

    @Schema(description = "适用范围(10全部商品 20指定商品 30指定分类)")
    private Integer applyRange;

    @Schema(description = "到期类型(10领取后生效 20固定时间)")
    private Integer expireType;

    @Schema(description = "领取后生效-有效天数")
    private Integer expireDay;

    @Schema(description = "适用范围配置(json格式)")
    private String applyRangeConfig;

    @Schema(description = "是否过期(0未过期 1已过期)")
    private Integer isExpire;

    @Schema(description = "有效期开始时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime startTime;

    @Schema(description = "有效期结束时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime endTime;

    @Schema(description = "使用状态(0未使用 1已使用 2已过期)")
    private Integer status;

    @Schema(description = "使用时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime useTime;

    @Schema(description = "使用订单ID")
    private Integer orderId;

    @Schema(description = "是否已使用")
    private Integer isUse;

    @Schema(description = "使用订单号")
    private String orderNo;

    @Schema(description = "获取方式(10主动领取 20系统发放 30活动赠送)")
    private Integer obtainType;

    @Schema(description = "获取来源描述")
    private String obtainSource;

    @Schema(description = "是否删除, 0否, 1是")
    @TableLogic
    private Boolean deleted;

    @Schema(description = "租户id")
    private Integer tenantId;

    @Schema(description = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @Schema(description = "修改时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    @TableField(exist = false)
    private ShopCoupon couponItem;

    /**
     * 判断优惠券是否可用
     * @return true-可用，false-不可用
     */
    public boolean isAvailable() {
        return this.status != null && this.status == STATUS_UNUSED && !isExpired();
    }

    /**
     * 判断优惠券是否已使用
     * @return true-已使用，false-未使用
     */
    public boolean isUsed() {
        return this.status != null && this.status == STATUS_USED;
    }

    /**
     * 判断优惠券是否已过期
     * @return true-已过期，false-未过期
     */
    public boolean isExpired() {
        if (this.status != null && this.status == STATUS_EXPIRED) {
            return true;
        }
        return this.endTime != null && this.endTime.isBefore(LocalDateTime.now());
    }

    /**
     * 获取优惠券状态描述
     * @return 状态描述
     */
    public String getStatusDesc() {
        if (isExpired()) {
            return "已过期";
        } else if (isUsed()) {
            return "已使用";
        } else if (isAvailable()) {
            return "可使用";
        } else {
            return "未知状态";
        }
    }

    /**
     * 更新优惠券状态为已使用
     * @param orderId 订单ID
     * @param orderNo 订单号
     */
    public void markAsUsed(Integer orderId, String orderNo) {
        this.status = STATUS_USED;
        this.isUse = 1;
        this.useTime = LocalDateTime.now();
        this.orderId = orderId;
        this.orderNo = orderNo;
    }

    /**
     * 更新优惠券状态为已过期
     */
    public void markAsExpired() {
        this.status = STATUS_EXPIRED;
        this.isExpire = 1;
    }

}
