package com.foreverwin.mesnac.anomaly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.foreverwin.mesnac.anomaly.mapper.AbnormalBillMapper;
import com.foreverwin.mesnac.anomaly.model.AbnormalBill;
import com.foreverwin.mesnac.anomaly.service.AbnormalBillService;
import com.foreverwin.mesnac.anomaly.service.AnomalyJobService;
import com.foreverwin.mesnac.common.mapper.MessageMapper;
import com.foreverwin.mesnac.common.model.Message;
import com.foreverwin.mesnac.common.service.MessageService;
import com.foreverwin.mesnac.common.util.ActiveMQUtil;
import com.foreverwin.mesnac.common.util.StringUtil;
import com.foreverwin.mesnac.meapi.mapper.UserGroupMapper;
import com.foreverwin.mesnac.meapi.model.NwaUser;
import com.foreverwin.mesnac.meapi.service.NwaUserService;
import com.foreverwin.modular.core.util.CommonMethods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * @Description TODO
 * @Author zhaojiawei
 * @Since 2021-07-22
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AnomalyJobServiceImpl implements AnomalyJobService {

    @Autowired
    private MessageService messageService;

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private AbnormalBillMapper abnormalBillMapper;

    @Autowired
    private AbnormalBillService abnormalBillService;

    @Autowired
    private ActiveMQUtil activeMQUtil;

    @Autowired
    private NwaUserService nwaUserService;

    @Autowired
    private UserGroupMapper userGroupMapper;


    @Override
    public void anomalyJobProcess(String site) {
        LocalDateTime now = LocalDateTime.now();
        String user = CommonMethods.getUser();
        //找到状态为N，并且升级时间大于当前时间
        QueryWrapper<Message> abnormalMessageQueryWrapper = new QueryWrapper<>();
        abnormalMessageQueryWrapper.eq("STATUS","N");
        abnormalMessageQueryWrapper.lt("UP_DATE_TIME",now);
        AbnormalBill abnormalBill = new AbnormalBill();
        List<Message> abnormalMessages = messageMapper.selectList(abnormalMessageQueryWrapper);
        for(Message _abnormalMessage : abnormalMessages){
            String uuid = UUID.randomUUID().toString();
            Message abnormalMessage = new Message();
            //设置abnormalMessage的handle
            abnormalMessage.setHandle(uuid);
            //设置abnormalMessage的site
            abnormalMessage.setSite(site);
            //设置abnormalMessage的异常单Bo
            abnormalMessage.setObjectBo(_abnormalMessage.getObjectBo());
            //设置abnormalMessage的type
            abnormalMessage.setType(_abnormalMessage.getType());
            //设置abnormalMessage的message_type
            abnormalMessage.setMessageType(_abnormalMessage.getUpMessageType());
            //设置发送用户组和升级用户组
            if(!StringUtil.isBlank(_abnormalMessage.getUpUserGroup())){
                //当前发送用户组就等于升级用户组，升级用户组就等于空
                abnormalMessage.setSendUserGroup(_abnormalMessage.getUpUserGroup());
                abnormalMessage.setUpUserGroup(null);
            }

            //设置发送用户和升级用户
            if(!StringUtil.isBlank(_abnormalMessage.getUpUser())){
                //当前发送用户就等于升级用户，升级用户就等于空
                abnormalMessage.setSendUser(_abnormalMessage.getUpUser());
                abnormalMessage.setUpUser(null);
            }

            abnormalBill.setHandle(abnormalMessage.getMessageType());
            String message = abnormalBillService.formatMessage(abnormalBill,abnormalMessage.getMessageType(),site);

            abnormalMessage.setContent(message+"，It's Job send");

            //设置UP_DATE_TIME、UP_MESSAGE_TYPE、GRADE、RESPONSE_DATE_TIME、ABNORMAL_NODE、STATUS
            abnormalMessage.setUpMessageType(null);
            abnormalMessage.setUpDateTime(null);

            abnormalMessage.setGrade(_abnormalMessage.getGrade() + 1);
            abnormalMessage.setResponseDateTime(now);
            abnormalMessage.setNode(_abnormalMessage.getNode());
            abnormalMessage.setStatus("Y");
            _abnormalMessage.setStatus("Y");

            //设置时间
            abnormalMessage.setCreatedUser(user);
            abnormalMessage.setCreatedDateTime(now);
            abnormalMessage.setModifiedUser(user);
            abnormalMessage.setModifiedDateTime(now);

//            if(Constants.ABNORMAL_MESSAGE_PROGRAM_CONFIRM.equals(abnormalMessage.getAbnormalNode())){
//                String sendUsers = abnormalMessage.getSendUser().replace(',', '|');
//                activeMQUtil.wechatSendMessage(sendUsers,abnormalMessage.getContent());
//            }else if(!Constants.ABNORMAL_MESSAGE_RESPONSE.equals(abnormalMessage.getAbnormalNode())){
                //判断是否有该用户组

            List<NwaUser> nwaUsers = nwaUserService.checkUserGroup(site,abnormalMessage.getSendUserGroup());
                StringBuilder sendUsers = new StringBuilder();
                for (int i = 0; i < nwaUsers.size(); i++) {
                    if(i == (nwaUsers.size() - 1)){
                        sendUsers.append(nwaUsers.get(i));
                    }else{
                        sendUsers.append(nwaUsers.get(i)).append("|");
                    }
                }
                activeMQUtil.wechatSendMessage(sendUsers.toString(),abnormalMessage.getContent());
//            }
            messageService.saveOrUpdate(abnormalMessage);
           // messageService.saveOrUpdate(_abnormalMessage);
        }
    }
}
