package com.prolog.rdc.tms.service.message.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.prolog.rdc.tms.constant.DataDictionaryConstant;
import com.prolog.rdc.tms.constant.SeriaNnumberConstant;
import com.prolog.rdc.tms.dao.mapper.TmsMessageMapper;
import com.prolog.rdc.tms.model.dto.message.ChannelMessageDTO;
import com.prolog.rdc.tms.model.entity.message.TmsMessageModel;
import com.prolog.rdc.tms.service.SysCodeengineService;
import com.prolog.rdc.tms.service.message.MessageChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.stream.Collectors;

/**
 * 站内消息
 * @author hongbo.pan
 * @date 2022/11/17
 */
@Slf4j
@Service
public class WebChannel implements MessageChannel {

    @Autowired
    private TmsMessageMapper tmsMessageMapper;

    @Autowired
    private SysCodeengineService sysCodeengineService;

    @Override
    public void send(ChannelMessageDTO channelMessageDTO) {
        log.info("topic={},channelMessageDTO={}", "WebChannel", JSON.toJSONString(channelMessageDTO));
        if (CollectionUtil.isNotEmpty(channelMessageDTO.getChannelMessageUserDTOList())) {
            channelMessageDTO.getChannelMessageUserDTOList().forEach(channelMessageUserDTO -> {
                TmsMessageModel tmsMessageModel = new TmsMessageModel();
                tmsMessageModel.setId(Long.valueOf(sysCodeengineService.getCodeNumberByCode(SeriaNnumberConstant.TMS_MESSAGE_ID, "L")));
                tmsMessageModel.setMessageConfigId(channelMessageDTO.getMessageConfigDTO().getId());
                tmsMessageModel.setMessageType(channelMessageDTO.getMessageConfigDTO().getType());
                tmsMessageModel.setMessageName(channelMessageDTO.getMessageConfigDTO().getName());
                tmsMessageModel.setMessageSendType(channelMessageDTO.getMessageConfigDTO().getSendType());
                tmsMessageModel.setRefbillid(channelMessageDTO.getSendMessageDTO().getBillCode().stream().collect(Collectors.joining(",")));
                tmsMessageModel.setRefbillno(channelMessageDTO.getSendMessageDTO().getBillCode().stream().collect(Collectors.joining(",")));
                tmsMessageModel.setContent(channelMessageDTO.getMessageConfigDTO().getContent());
//                tmsMessageModel.setCustomerIds();
//                tmsMessageModel.setCustomerCodes();
//                tmsMessageModel.setCustomerNames();
                tmsMessageModel.setSendTime(new Date());
//                tmsMessageModel.setSendUserId();
                tmsMessageModel.setSendUserName("系统管理员");
//                tmsMessageModel.setSendDepartmentId();
//                tmsMessageModel.setSendDepartmentName();
                if (ObjectUtil.isNotNull(channelMessageUserDTO.getUserId())) {
                    tmsMessageModel.setReceiveUserId(channelMessageUserDTO.getUserId().toString());
                }
                tmsMessageModel.setReceiveUserName(channelMessageUserDTO.getUserName());
                if (ObjectUtil.isNotNull(channelMessageUserDTO.getDepartmentId())) {
                    tmsMessageModel.setReceiveDepartmentId(channelMessageUserDTO.getDepartmentId().toString());
                }
                tmsMessageModel.setReceiveDepartmentName(channelMessageUserDTO.getDepartmentName());
                tmsMessageModel.setWhetherRead(DataDictionaryConstant.WHETHER_READ_N);
//                tmsMessageModel.setReadTime();
                tmsMessageModel.setEnterpriseId(channelMessageDTO.getUser().getEnterpriseId());
                tmsMessageModel.setEnterpriseName(channelMessageDTO.getUser().getEnterpriseName());
                tmsMessageModel.setCreatetime(new Date());
                tmsMessageModel.setCreateperson(channelMessageDTO.getUser().getNickname());
                tmsMessageModel.setUpdatetime(new Date());
                tmsMessageModel.setUpdateperson(channelMessageDTO.getUser().getNickname());
                tmsMessageModel.setMessageNoticeType(channelMessageDTO.getMessageConfigDTO().getNoticeType());
                tmsMessageModel.setShipbillId(channelMessageDTO.getShipbillId());
                tmsMessageModel.setAlarmVoice(channelMessageDTO.getMessageConfigDTO().getAlarmVoice());
                tmsMessageMapper.save(tmsMessageModel);
                WebSocketServer.sendMessage(JSON.toJSONString(tmsMessageModel), channelMessageUserDTO.getUserId().toString());
            });
        }
    }
}
