package com.zd.laboratory.service.impl;

import java.util.Arrays;
import java.util.List;

import com.zd.common.core.utils.DateUtils;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.security.utils.SaveUtil;

import com.zd.laboratory.mqtt.constants.MqttConstants;
import com.zd.laboratory.mqtt.enums.SendMode;
import com.zd.laboratory.mqtt.service.impl.CommonSend;
import com.zd.system.api.laboratory.domain.LabMessageContent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.zd.laboratory.mapper.LabContentMachineMsgMapper;
import com.zd.laboratory.domain.LabContentMachineMsg;
import com.zd.laboratory.service.ILabContentMachineMsgService;

/**
 * 一体机实验室通知消息Service业务层处理
 *
 * @author liubo
 * @date 2022-03-08
 */
@Service
public class LabContentMachineMsgServiceImpl implements ILabContentMachineMsgService {
    private Logger logger = LoggerFactory.getLogger(LabContentMachineMsgServiceImpl.class);
    @Autowired
    private LabContentMachineMsgMapper labContentMachineMsgMapper;
    @Autowired
    private CommonSend commonSend;

    /**
     * 查询一体机实验室通知消息
     *
     * @param id 一体机实验室通知消息主键
     * @return 一体机实验室通知消息
     */
    @Override
    public LabContentMachineMsg selectLabContentMachineMsgById(Long id) {
        return labContentMachineMsgMapper.selectLabContentMachineMsgById(id);
    }

    /**
     * 查询一体机实验室通知消息列表
     *
     * @param labContentMachineMsg 一体机实验室通知消息
     * @return 一体机实验室通知消息
     */
    @Override
    public List<LabContentMachineMsg> selectLabContentMachineMsgList(LabContentMachineMsg labContentMachineMsg) {
        return labContentMachineMsgMapper.selectLabContentMachineMsgList(labContentMachineMsg);
    }

    /**
     * 新增一体机实验室通知消息
     *
     * @param labContentMachineMsg 一体机实验室通知消息
     * @return 结果
     */
    @Override
    public int insertLabContentMachineMsg(LabContentMachineMsg labContentMachineMsg) {
        return labContentMachineMsgMapper.insertLabContentMachineMsg(labContentMachineMsg);
    }

    /**
     * 修改一体机实验室通知消息
     *
     * @param labContentMachineMsg 一体机实验室通知消息
     * @return 结果
     */
    @Override
    public int updateLabContentMachineMsg(LabContentMachineMsg labContentMachineMsg) {
        labContentMachineMsg.setUpdateTime(DateUtils.getNowDate());
        labContentMachineMsg.setUpdateBy(SecurityUtils.getUsername());
        return labContentMachineMsgMapper.updateLabContentMachineMsg(labContentMachineMsg);
    }

    /**
     * 批量删除一体机实验室通知消息
     *
     * @param ids 需要删除的一体机实验室通知消息主键
     * @return 结果
     */
    @Override
    public int deleteLabContentMachineMsgByIds(Long[] ids) {
        return labContentMachineMsgMapper.deleteLabContentMachineMsgByIds(ids);
    }

    /**
     * 删除一体机实验室通知消息信息
     *
     * @param id 一体机实验室通知消息主键
     * @return 结果
     */
    @Override
    public int deleteLabContentMachineMsgById(Long id) {
        return labContentMachineMsgMapper.deleteLabContentMachineMsgById(id);
    }

    @Override
    public int insert(LabMessageContent labMessageContent) {

        List<String> subIds = Arrays.asList(labMessageContent.getSubIds().split(","));

        LabContentMachineMsg labContentMachineMsg;
        for (String subId : subIds) {
            labContentMachineMsg = new LabContentMachineMsg();
            labContentMachineMsg.setContentId(labMessageContent.getId());
            labContentMachineMsg.setContentMsg(labMessageContent.getContent());
            labContentMachineMsg.setSubId(Long.parseLong(subId));

            labContentMachineMsg.setUserId(labMessageContent.getUserId());
            labContentMachineMsg.setCreateBy(labMessageContent.getCreateBy());
            labContentMachineMsg.setCreateTime(labMessageContent.getCreateTime());
            labContentMachineMsg.setDeptId(labMessageContent.getDeptId());
            labContentMachineMsg.setDeptName(labMessageContent.getDeptName());

            insertLabContentMachineMsg(labContentMachineMsg);

            // 生成一条通知一个，此处不应该有重复的实验室ID
            commonSend.send(MqttConstants.LAB_CONTENT + subId, "收到一条新的消息通知！");
            logger.info("发送实验室一体机消息通知：" + subId);
        }

        return 1;
    }
}
