package org.jeecg.modules.message.service.impl;

import org.jeecg.common.system.base.service.impl.JeecgServiceImpl;
import org.jeecg.modules.message.entity.SysMessage;
import org.jeecg.modules.message.handle.enums.SendMsgStatusEnum;
import org.jeecg.modules.message.mapper.SysMessageMapper;
import org.jeecg.modules.message.service.ISysMessageService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Description: 消息
 * @Author: jeecg-boot
 * @Date: 2019-04-09
 * @Version: V1.0
 */
@Service
@Transactional
public class SysMessageServiceImpl extends JeecgServiceImpl<SysMessageMapper, SysMessage> implements ISysMessageService {

    @Override
    @Async("asyncThreadPool")
    public void add(String title, String param, String content, String userIds, String type, String errMsg, Date sendTime) {
        String[] split = userIds.split(",");
        for (String userId : split) {
            SysMessage msg = build(title, param, content, userId, type, errMsg, sendTime);
            baseMapper.insert(msg);
        }
    }

    private static SysMessage build(String title, String param, String content, String userId, String type, String errMsg, Date sendTime) {
        SysMessage msg = new SysMessage();
        msg.setEsTitle(title);
        msg.setEsParam(param);
        msg.setEsContent(content);
        msg.setEsReceiver(userId);
        msg.setEsSendNum(1);
        msg.setEsSendStatus(SendMsgStatusEnum.SUCCESS.getCode());
        msg.setEsType(type);
        msg.setEsResult(errMsg);
        msg.setEsSendTime(sendTime);
        return msg;
    }
}
