package com.zd.laboratory.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Stream;

import com.zd.common.core.enums.WarnMessageTypeEnum;
import com.zd.common.core.enums.WarnUserAttrEnum;
import com.zd.common.core.utils.DateUtils;

import com.zd.laboratory.domain.LabRiskPlan;
import com.zd.laboratory.domain.vo.LabWarnPushMessageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zd.laboratory.mapper.LabWarnPushMessageMapper;
import com.zd.laboratory.domain.LabWarnPushMessage;
import com.zd.laboratory.service.ILabWarnPushMessageService;

/**
 * 预警推送消息Service业务层处理
 *
 * @author zhoupan
 * @date 2021-10-06
 */
@Service
public class LabWarnPushMessageServiceImpl implements ILabWarnPushMessageService
{
    @Autowired
    private LabWarnPushMessageMapper labWarnPushMessageMapper;


    /**
     * 查询预警推送消息列表-根据预警分组ID
     *
     * @param riskGroup 风险分组ID
     * @return 预警推送消息
     */
    @Override
    public List<LabWarnPushMessageVO> selectLabWarnPushMessageList(Long riskGroup)
    {
        LabWarnPushMessage labWarnPushMessage = new LabWarnPushMessage();
        labWarnPushMessage.setRiskGroup(riskGroup);
        return labWarnPushMessageMapper.selectLabWarnPushMessageVOList(riskGroup);
    }

    /**
     * 新增预警推送消息
     *
     * @param labWarnPushMessage 预警推送消息
     * @return 结果
     */
    @Override
    public int insertLabWarnPushMessage(LabWarnPushMessage labWarnPushMessage)
    {
        labWarnPushMessage.setCreateTime(DateUtils.getNowDate());
        return labWarnPushMessageMapper.insertLabWarnPushMessage(labWarnPushMessage);
    }

    /**
     * 新增预警推送消息
     *
     * @param userids     实验室内部用户
     * @param groupId
     * @param newRiskPlan
     * @return 结果
     */
    @Override
    public void insertLabWarnPushMessage(Long subId,String userids, Long groupId, LabRiskPlan newRiskPlan,WarnUserAttrEnum warnUserAttrEnum) {
        String[] split = userids.split(",");

        Stream.of(split).forEach(a->{

            try {
                LabWarnPushMessage labWarnPushMessage = new LabWarnPushMessage();
                labWarnPushMessage.setRiskGroup(groupId);
                labWarnPushMessage.setContext(newRiskPlan.getTopName()+""+newRiskPlan.getMessage());
                labWarnPushMessage.setPushType(WarnMessageTypeEnum.SMS);
                labWarnPushMessage.setWarnId(newRiskPlan.getId());
                labWarnPushMessage.setSubId(subId);
                labWarnPushMessage.setCreateTime(new Date());
                labWarnPushMessage.setUserAttributes(warnUserAttrEnum);
                labWarnPushMessage.setRecipientId(Long.parseLong(a));
                insertLabWarnPushMessage(labWarnPushMessage);
            } catch (Exception e) {

            }
        });
    }
}
