MsgParamCondition.java 11 KB
package cn.fw.hermes.sdk.api.para;

import lombok.Data;
import org.springframework.util.CollectionUtils;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author 张志伟
 * @date 2018-09-18 14:58
 * @description
 */
@Data
public class MsgParamCondition {
    /**
     * 消息类型
     */
    private MsgTypeEnum msgTypeEnum;
    /**
     * 是否使用组合消息
     */
    private boolean unitMsg;
    /**
     * 是否使用离线消息,默认为true
     */
    private boolean useOfflineMsg;
    /**
     * 消息离线保存时长(秒数),最长为 7 天(604800s)。若消息只发在线用户,不想保存离线,则该字段填 0。若不填,则默认保存 7 天
     */
    private Integer msgLifeTime;
    /**
     * 目标账号id
     */
    private Long userId;
    /**
     * 指定发送账号业务类型
     */
    private BusinessType businessType;
    /**
     * 文本消息体
     */
    private TextContent textContent;
    /**
     * 地理位置消息
     */
    private LocationContent locationContent;
    /**
     * 表情消息
     */
    private FaceContent faceContent;
    /**
     * 自定义消息
     */
    private CustomContent customContent;
    /**
     * 组合消息的消息体,严格区分先后顺序
     */
    private List<? extends BaseContent> contentList;
    /**
     * 离线消息推送设置
     */
    private OfflinePushInfo offlinePushInfo;
    /**
     * 消息接收人是否为业务人员
     */
    private Boolean isStaff;

    /**
     * 消息唯一ID
     */
    private String messageId;

    public MsgParamCondition() {
    }

    private MsgParamCondition(MsgTypeEnum msgTypeEnum, TextContent textContent, LocationContent locationContent, FaceContent faceContent,
                    CustomContent customContent, boolean unitMsg, List<? extends BaseContent> contentList, boolean useOfflineMsg,
                    OfflinePushInfo offlinePushInfo, Integer msgLifeTime, Long userId, BusinessType businessType,boolean isStaff) {
        this.msgTypeEnum = msgTypeEnum;
        this.textContent = textContent;
        this.locationContent = locationContent;
        this.faceContent = faceContent;
        this.customContent = customContent;
        this.unitMsg = unitMsg;
        this.contentList = contentList;
        this.useOfflineMsg = useOfflineMsg;
        this.offlinePushInfo = offlinePushInfo;
        this.msgLifeTime = msgLifeTime;
        this.userId = userId;
        this.businessType = businessType;
        this.isStaff = isStaff;
    }

    /**
     * 离线推送文本消息
     *
     * @param text     内容
     * @param lifeTime 离线保留时长(秒) 最长7天
     * @param title    离线推送标题
     * @param desc     离线推送描述
     * @param ext      离线推送扩展信息 (废弃此参数)
     * @param userId   目标
     * @return
     */
    public static Builder getOfflineTxetPara(String text, Integer lifeTime, String title, String desc, Map<String, Object> ext
            , Long userId) {
        lifeTime = lifeTime == null ? 604800 : lifeTime;
        if (lifeTime / 604800 > 0) {
            lifeTime = 604800;
        }
        TextContent textContent = new TextContent();
        textContent.setText(text);
        OfflinePushInfo offlinePushInfo = new OfflinePushInfo();
        offlinePushInfo.setDesc(desc);
        offlinePushInfo.setTitle(title);
        return new Builder()
                .setMsgType(MsgTypeEnum.TEXT)
                .setTextContent(textContent)
                .setUserId(userId)
                .setUseOfflineMsg(true)
                .setOfflinePushInfo(offlinePushInfo)
                .setMsgLifeTime(lifeTime)
                .setUnitMsg(false);
    }

    /**
     * @param text     内容
     * @param lifeTime 离线保留时长(秒) 最长7天
     * @param userId   目标
     * @return
     */
    public static Builder getTextPara(String text, Integer lifeTime, Long userId) {
        TextContent textContent = new TextContent();
        textContent.setText(text);
        lifeTime = lifeTime == null ? 604800 : lifeTime;
        if (lifeTime / 604800 > 0) {
            lifeTime = 604800;
        }
        return new Builder()
                .setMsgType(MsgTypeEnum.TEXT)
                .setTextContent(textContent)
                .setUserId(userId)
                .setMsgLifeTime(lifeTime)
                .setUseOfflineMsg(false)
                .setUnitMsg(false);
    }

    /**
     * @param text     地理位置描述
     * @param lifeTime 离线保留时长(秒) 最长7天
     * @param lat      纬度
     * @param lng      经度
     * @param userId   目标
     * @return
     */
    public static Builder getLocationMsg(String text, Integer lifeTime, BigDecimal lat, BigDecimal lng, Long userId) {
        LocationContent locationContent = new LocationContent();
        locationContent.setDesc(text);
        locationContent.setLat(lat);
        locationContent.setLng(lng);
        lifeTime = lifeTime == null ? 604800 : lifeTime;
        if (lifeTime / 604800 > 0) {
            lifeTime = 604800;
        }
        return new Builder()
                .setMsgType(MsgTypeEnum.LOCATION)
                .setLocationContent(locationContent)
                .setMsgLifeTime(lifeTime)
                .setUserId(userId)
                .setUnitMsg(false)
                .setUseOfflineMsg(false);
    }

    /**
     * 发送表情消息
     *
     * @param text     内容
     * @param lifeTime 离线保留时长(秒) 最长7天
     * @param index    表情索引
     * @param userId   目标
     * @return
     */
    public static Builder getFaceMsg(String text, Integer lifeTime, int index, Long userId) {
        FaceContent faceContent = new FaceContent();
        faceContent.setData(text);
        faceContent.setIndex(index);
        lifeTime = lifeTime == null ? 604800 : lifeTime;
        if (lifeTime / 604800 > 0) {
            lifeTime = 604800;
        }
        return new Builder()
                .setMsgType(MsgTypeEnum.FACE)
                .setFaceContent(faceContent)
                .setMsgLifeTime(lifeTime)
                .setUserId(userId)
                .setUnitMsg(false)
                .setUseOfflineMsg(false);
    }

    /**
     * 自定义消息
     * <p>
     * 其中业务通知时ext必须要有type字段才能解析 value参考 {@link MessageBusinessType} 可以直接使用枚举也可以使用其字符串
     * 推荐直接使用枚举
     *
     * @param desc       内容(描述信息)
     * @param title      标题(为空的时候不会在通知栏提示,静默消息)
     * @param ext        扩展信息
     * @param lifeTime   离线保留时长(秒) 最长7天
     * @param userId     目标
     * @param useOffline 是否离线推送
     * @param isStaff    消息接收人是否为业务人员,必填
     * @return
     */
    public static Builder getCustomMsg(String desc, String title, Map<String, Object> ext, Integer lifeTime, Long userId, boolean useOffline,Boolean isStaff) {
        CustomContent customContent = new CustomContent();
        title = title == null ? "" : title;
        customContent.setData(title);
        customContent.setDesc(desc);
        customContent.setExt(ext);
        lifeTime = lifeTime == null ? 604800 : lifeTime;
        if (lifeTime / 604800 > 0) {
            lifeTime = 604800;
        }
        Builder builder = new Builder()
                .setMsgType(MsgTypeEnum.CUSTOM)
                .setCustomContent(customContent)
                .setMsgLifeTime(lifeTime)
                .setUserId(userId)
                .setUnitMsg(false)
                .setUseOfflineMsg(useOffline)
                .setIsStaff(isStaff);
        if (useOffline) {
            OfflinePushInfo o = new OfflinePushInfo();
            o.setTitle(title);
            o.setDesc(desc);
            o.setExt(ext);
            builder.setOfflinePushInfo(o);
        }
        return builder;
    }


    public static Builder newBuilder() {
        return new Builder();
    }

    public static class Builder {
        private MsgTypeEnum msgTypeEnum;
        private TextContent textContent;
        private LocationContent locationContent;
        private FaceContent faceContent;
        private CustomContent customContent;
        private boolean unitMsg;
        private List<? extends BaseContent> contentList;
        private boolean useOfflineMsg;
        private OfflinePushInfo offlinePushInfo;
        private Integer msgLifeTime;
        private Long userId;
        private BusinessType businessType;
        private Boolean isStaff;


        public Builder setMsgType(MsgTypeEnum msgType) {
            this.msgTypeEnum = msgType;
            return this;
        }

        public Builder setTextContent(TextContent textContent) {
            this.textContent = textContent;
            return this;
        }

        public Builder setLocationContent(LocationContent locationContent) {
            this.locationContent = locationContent;
            return this;
        }

        public Builder setFaceContent(FaceContent faceContent) {
            this.faceContent = faceContent;
            return this;
        }

        public Builder setCustomContent(CustomContent customContent) {
            this.customContent = customContent;
            return this;
        }

        public Builder setUnitMsg(boolean unitMsg) {
            this.unitMsg = unitMsg;
            return this;
        }

        public Builder setContentList(List<? extends BaseContent> contentList) {
            this.contentList = contentList;
            return this;
        }

        public Builder setUseOfflineMsg(boolean useOfflineMsg) {
            this.useOfflineMsg = useOfflineMsg;
            return this;
        }

        public Builder setOfflinePushInfo(OfflinePushInfo offlinePushInfo) {
            this.offlinePushInfo = offlinePushInfo;
            return this;
        }

        public Builder setMsgLifeTime(Integer msgLifeTime) {
            this.msgLifeTime = msgLifeTime;
            return this;
        }

        public Builder setUserId(Long userId) {
            this.userId = userId;
            return this;
        }

        public Builder setBusinessType(BusinessType businessType) {
            this.businessType = businessType;
            return this;
        }

        public Builder setIsStaff(Boolean isStaff) {
            this.isStaff = isStaff;
            return this;
        }
        public MsgParamCondition build() {
            if (CollectionUtils.isEmpty(contentList)) {
                unitMsg = false;
            }
            if (offlinePushInfo == null) {
                useOfflineMsg = false;
            }
            if (userId == null) {
                throw new IllegalArgumentException("目标不存在");
            }
            return new MsgParamCondition(msgTypeEnum, textContent, locationContent, faceContent, customContent, unitMsg, contentList,
                    useOfflineMsg, offlinePushInfo, msgLifeTime, userId, businessType,isStaff);
        }
    }
}