ActivityThemeVo.java 4.26 KB
package cn.fw.dalaran.domain.vo;

import cn.fw.dalaran.domain.db.ActivityTheme;
import com.alibaba.fastjson.JSON;
import lombok.Data;

import javax.validation.constraints.Future;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wmy3969
 * @version 1.0
 * @date 2022/1/11 23:46
 * @Description 活动主题视图
 */
@Data
public class ActivityThemeVo {
    /**
     * 主键id
     */
    private Long id;
    /**
     * 所属配置组id
     */
    @NotNull(message = "所属配置组id不能为空")
    private Long configGroupId;
    /**
     * 集团id
     */
    private Long groupId;
    /**
     * 活动主题
     */
    @NotBlank(message = "活动主题不能为空")
    private String theme;
    /**
     * 活动话题
     */
    @NotEmpty(message = "话题不能为空")
    private List<String> topic;
    /**
     * 主题开始时间
     */
    @NotNull(message = "主题开始时间不能为空")
    private Date startTime;
    /**
     * 主题结束时间
     */
    @NotNull(message = "主题结束时间不能为空")
    @Future(message = "主题结束时间必须在当前时间之后")
    private Date endTime;
    /**
     * 是否为全部门店
     */
    private Integer allShop;
    /**
     * 适用门店清单(id)
     */
    private List<Long> shopIds;
    /**
     * 适用门店清单(name)
     */
    private List<String> shopNames;
    /**
     * 封面图文件信息
     */
    @NotEmpty(message = "必须指定封面图文件")
    private List<FileDesc> allFileDesc;
    /**
     * 能否修改主题
     */
    private boolean canAlter;

    /**
     * vo->db
     *
     * @return 将视图传参转换成DB
     */
    public static ActivityTheme toDB(ActivityThemeVo vo) {
        /*if (!vo.allShop) {
            if (CollectionUtils.isEmpty(vo.getShopIds()) || CollectionUtils.isEmpty(vo.getShopNames())) {
                throw new BusinessException("非全部门店时, 请必须指定具体门店");
            }
        }*/
        final ActivityTheme db = new ActivityTheme();
        db.setId(vo.getId());
        /*db.setGroupId(vo.getGroupId());*/
        db.setTheme(vo.getTheme());
        db.setTopic(JSON.toJSONString(vo.getTopic().stream()
                .map(item -> {
                    String replace = item.replace("#", "");
                    return "#" + replace;
                })
                .collect(Collectors.toList()))
                .replace("[", "")
                .replace("]", "")
                .replace("\"", ""));
        db.setStartTime(getThisDayMinTime(vo.getStartTime()));
        db.setEndTime(getThisDayMaxTime(vo.getEndTime()));
        /*db.setAllShop(vo.allShop ? 1 : 0);
        if (!vo.allShop) {
            db.setShopIds(vo.getShopIds()
                    .stream()
                    .map(Object::toString)
                    .collect(Collectors.joining(","))
                    .replace("\"", ""));
            db.setShopNames(JSON.toJSONString(vo.getShopNames())
                    .replace("[", "")
                    .replace("]", "")
                    .replace("\"", ""));
        }*/
        return db;
    }

    /**
     * 设置指定日期当天开始时间-->时分秒 00:00:00
     *
     * @param date 需要处理的时间
     * @return 该时间当天最小值
     */
    public static Date getThisDayMinTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 设置指定日期当天最晚时间-->时分秒 23:59:59
     *
     * @param date 需要处理的时间
     * @return 该时间当天最大值
     */
    public static Date getThisDayMaxTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }
}