package com.skr.mdm.handle;

import cn.hutool.json.JSONUtil;
import com.skr.mdm.dao.DeviceEquipmentDao;
import com.skr.mdm.dao.SysSendMsgDao;
import com.skr.mdm.dao.SysSendMsgUserDao;
import com.skr.mdm.enums.EquipmentStateEnum;
import com.skr.mdm.enums.RabbitMqRoutingKeyEnum;
import com.skr.mdm.entity.SysSendMsgDO;
import com.skr.mdm.entity.SysSendMsgUserDO;
import com.skr.mdm.bean.so.RabbitMqSO;
import com.skr.mdm.bean.vo.WebSocketResultMessageVO;
import com.skr.mdm.rabbitmq.RabbitMQProvider;
import com.skr.mdm.util.AESUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * @author nfq
 * @date 2020/9/3 15:24
 */
@Component
public class IssuedUserHandler {

    @Autowired
    private SysSendMsgDao sysSendMsgDao;
    @Autowired
    private SysSendMsgUserDao sysSendMsgUserDao;
    @Autowired
    private RabbitMQProvider rabbitMQProvider;
    @Autowired
    private DeviceEquipmentDao equipmentDao;


    /**
     * issued
     * @param equipmentIdList
     * @param companyId
     * @param cmdType
     * @param data
     */
    public void issuedUser(List<Integer> equipmentIdList, Integer companyId, String cmdType, String data) {
        if (CollectionUtils.isEmpty(equipmentIdList)) {
            return;
        }
        // 去重
        equipmentIdList = equipmentIdList.stream().distinct().collect(Collectors.toList());
        Long sendMsgId = insertSendMsg(equipmentIdList, companyId, cmdType, data);
        List<String> channelIdList = equipmentDao.listOnlineChannelIdByIdList(EquipmentStateEnum.WS_ONLINE.getState(), equipmentIdList);
        issued(channelIdList, sendMsgId, cmdType, data);
    }

    public Long insertSendMsg(List<Integer> equipmentIdList, Integer companyId, String cmdType, String data) {
        if (CollectionUtils.isNotEmpty(equipmentIdList)) {
            // 数据入库
            SysSendMsgDO sysSendMsgDO = new SysSendMsgDO();
            data = data == null ? "" : AESUtils.encrypt(data);
            sysSendMsgDO.setData(data);
            sysSendMsgDO.setCreateTime(new Date());
            sysSendMsgDO.setCmdType(cmdType);
            sysSendMsgDO.setCompanyId(companyId);
            sysSendMsgDao.insert(sysSendMsgDO);
            List<SysSendMsgUserDO> sysSendMsgUserDOList = new ArrayList<>(equipmentIdList.size());
            equipmentIdList.forEach(equipmentId -> {
                SysSendMsgUserDO sendMsgUser = new SysSendMsgUserDO();
                sendMsgUser.setEquipmentId(equipmentId);
                sendMsgUser.setSendMsgId(sysSendMsgDO.getId());
                sendMsgUser.setState((byte) 1);
                sysSendMsgUserDOList.add(sendMsgUser);
            });
            sysSendMsgUserDao.insertBatch(sysSendMsgUserDOList);
            return sysSendMsgDO.getId();
        }
       return null;
    }

    private void issued(List<String> channelIdList, Long sendMsgId, String cmdType, String data) {
        // 在线用户下发
        if (CollectionUtils.isNotEmpty(channelIdList) && sendMsgId != null) {
            WebSocketResultMessageVO resultMessageVO = WebSocketResultMessageVO.builder()
                    .msgId(sendMsgId)
                    .type(cmdType)
                    .data(AESUtils.encrypt(data))
                    .sendTime(System.currentTimeMillis())
                    .build();
            List<WebSocketResultMessageVO> messageVOList = new ArrayList<>();
            messageVOList.add(resultMessageVO);
            RabbitMqSO rabbitMqSO = RabbitMqSO.builder().message(JSONUtil.toJsonStr(JSONUtil.parseArray(messageVOList))).channelIdList(channelIdList).build();
            rabbitMQProvider.sendDataToQueue(RabbitMqRoutingKeyEnum.ROUTING_KEY.getRoutingKey(), rabbitMqSO);
        }
    }
}
