package com.fs.store.domain;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fs.common.annotation.Excel;
import com.fs.common.core.domain.BaseEntity;
import com.fs.store.config.CustomLocalDateTimeDeserializer;
import com.hc.openapi.tool.util.StringUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections4.CollectionUtils;

/**
 * 定时发放优惠券队列对象 fs_coupon_schedule
 *
 * @author fs
 * @date 2025-03-08
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class FsCouponSchedule extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** 主键ID */
    private Long id;

    /** 用户ID */
    @Excel(name = "用户ID")
    private Long userId;

    /**
     * 用户名称
     */
    @Excel(name = "用户名称")
    private String userName;
    /** 订单ID */
    @Excel(name = "订单ID")
    private Long orderId;

    /**
     * 订单编号
     */
    private String orderCode;

    /** 套餐ID */
    @Excel(name = "套餐ID")
    private Long setmealId;
    /**
     * 套餐名称
     */
    @Excel(name = "套餐名称")
    private String setmealTitle;

    /** 总月数 */
    @Excel(name = "总月数")
    private Long month;

    /** 当前次数 */
    @Excel(name = "当前次数")
    private Long count;

    /**
     * 小程序跳转页面
     */
    @Excel(name = "小程序跳转页面")
    private String page;

    /**
     * 优惠券备注信息
     */
    @Excel(name = "备注")
    private String remark;

    /** 0待处理, 1正在处理, 2成功, -1失败, 3用户拒签或者退货 */
    @Excel(name = "0待处理, 1正在处理, 2成功, -1失败, 3用户拒签或者退货")
    private Integer status;

    /** 下单时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Excel(name = "下单时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime orderTime;

    /** 优惠券ID */
    @Excel(name = "优惠券ID")
    private Set<Long> couponId;
    /**
     * 优惠券id列表
     */
    private String couponIds;

    public Set<Long> getCouponId() {
        if(StringUtils.isNotBlank(couponIds)){
            String[] split = couponIds.split(",");
            HashSet<Long> longs = new HashSet<>();
            for (String e : split) {
                longs.add(Long.valueOf(e.trim()));
            }
            couponId=longs;
        }
        return couponId;
    }

    public String getCouponIds() {
        if(CollectionUtils.isNotEmpty(couponId)){
            couponIds = couponId.stream()
                    .map(String::valueOf)
                    .collect(Collectors.joining(","));
        }

        return couponIds;
    }

    /** 预计发送时间 */
    @JsonDeserialize(using = CustomLocalDateTimeDeserializer.class)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Excel(name = "预计发送时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime sendTime;

    /** 实际发送时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Excel(name = "实际发送时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime actualSendTime;

    /** 错误信息 */
    @Excel(name = "错误信息")
    private String errorMessage;

    /** 重试次数 */
    @Excel(name = "重试次数")
    private Integer retryCount;

    /** 最大重试次数 */
    @Excel(name = "最大重试次数")
    private Integer maxRetries = 3;


    /**
     * 公司id
     */
    private Long companyId;

    /**
     * 公司用户id
     */
    private Long companyUserId;
}
