package com.qs.mp.common.enums;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.IEnum;
import com.qs.mp.common.utils.DateUtils;
import io.swagger.annotations.ApiModel;

import java.util.Date;

/**
 * 活动状态枚举类
 * @author Cup
 * @date 2022/5/16
 */
@ApiModel("营销活动状态枚举类")
public enum MarketingStatusEnum implements IEnum<Integer> {

    CLOSE(-1, "已关闭"),

    OFF(0,"草稿"),

    ON(1,"开启"),

    UNSTART(2,"未开始"),

    START(3,"进行中"),

    END(4,"已结束");

    private Integer value;
    private String desc;

    MarketingStatusEnum(Integer value, String desc) {
        this.value = value;
        this.desc = desc;
    }

    @Override
    public Integer getValue() {
        return value;
    }

    public String getDesc() {
        return desc;
    }


    /**
     * 重写toString，单个转化成json
     * @return
     */
    @Override
    public String toString() {
        JSONObject object = new JSONObject();
        object.put("value",value);
        object.put("desc", desc);
        return object.toString();
    }

    public static MarketingStatusEnum getByValue(Integer value) {
        for (MarketingStatusEnum statusEnum : MarketingStatusEnum.values()) {
            if (statusEnum.getValue().equals(value)) {
                return statusEnum;
            }
        }
        return null;
    }

    public static String getStatus(Date startTime, Date endTime, Integer isOn) {
        if (MarketingStatusEnum.OFF.getValue().equals(isOn)) {
            return MarketingStatusEnum.OFF.toString();
        }
        if (MarketingStatusEnum.CLOSE.getValue().equals(isOn)) {
            return MarketingStatusEnum.CLOSE.toString();
        }

        Date now = DateUtils.getNowDate();
        if (now.before(startTime)) {
            return MarketingStatusEnum.UNSTART.toString();
        }

        if (now.after(endTime)) {
            return MarketingStatusEnum.END.toString();
        }

        return MarketingStatusEnum.START.toString();
    }
}
