package com.brilliance.isc.common.oit.service.impl;

import org.springframework.util.ObjectUtils;
import com.brilliance.isc.bo.Oit;
import com.brilliance.isc.common.oit.service.OitService;
import com.brilliance.isc.common.oit.vo.NoticeVo;
import com.brilliance.isc.constant.LimCons;
import com.brilliance.isc.mda.dao.OitMapper;
import com.brilliance.isc.utils.StringUtils;
import com.brilliance.isc.vo.WarnVo;
import com.brilliance.isc.warn.WarnService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class OitServiceImpl implements OitService {
    @Autowired
    private OitMapper oitMapper;
    @Autowired
    private WarnService warnService;

    /**
     * 停用通知列表
     */
    private ThreadLocal <List<Map<String,Object>>> errmaps= new ThreadLocal(){
        @Override
        protected List<Map<String,Object>> initialValue() {
            return new ArrayList<>();
        }
    };

    /**
     * 批量查询通知
     *
     * @param noticeVos
     */
    public void queryOitMessage(List<NoticeVo> noticeVos,String type) {
        List<Oit> oits1 = null;
        List<Oit> oits2 = null;
        List<NoticeVo> noticeVos1=noticeVos.stream().filter(noticeVo -> !StringUtils.isEmpty(noticeVo.getObjinr())).collect(Collectors.toList());
        List<NoticeVo> noticeVos2=noticeVos.stream().filter(noticeVo -> !StringUtils.isEmpty(noticeVo.getExtkey())).collect(Collectors.toList());

        if (noticeVos1 != null && noticeVos1.size() > 0) {
            oits1 = oitMapper.selectByObjWithBatch(noticeVos1);
        }
        if (noticeVos2 != null && noticeVos2.size() > 0) {
            oits2 = oitMapper.selectByExtkeyWithBatch(noticeVos2);
        }
        if(oits1==null){
            return;
        }
        if(oits1!=null&&oits2!=null) {
            oits1.addAll(oits2);
        }
        List<Oit> targetoits=oits1.stream().distinct().collect(Collectors.toList());
        if(LimCons.OIT_TYPE_TRANS.equals(type)){
            transferNoticeToWarning(targetoits);
        }else {
            transferNoticeToWarningAndError(targetoits);
        }
    }
   public void  transferNoticeToWarning(List<Oit> oits){
       oits.forEach(oit -> {
           warnService.tipWarn(oit.getPath(), oit.getInr(), WarnVo.SOURCE_BACK, oit.getInftxt());
       });
   }

    /**
     * 通知转警告
     *
     * @param oits
     */
    public void transferNoticeToWarningAndError(List<Oit> oits) {
        errmaps.remove();
        if (ObjectUtils.isEmpty(oits)) {
            return;
        }

        oits.forEach(oit -> {
            switch (oit.getInflev()) {
                case "%":
                    warnService.importantWarn(oit.getPath(), oit.getInr(), WarnVo.SOURCE_BACK, oit.getInftxt());
                    break;
                case "0":
                    warnService.tipWarn(oit.getPath(), oit.getInr(), WarnVo.SOURCE_BACK, oit.getInftxt());
                    break;
                case "1":
                case "2":
                case "3":
                case "9":
                    Map errmap=new HashMap();
                    errmap.put("key",oit.getPath());
                    errmap.put("oitinr",oit.getInr());
                    errmap.put("message",oit.getInftxt());
                    errmaps.get().add(errmap);
                    break;
                default:
                    log.info(String.format("oit警告#####oitinr is %s ,%s[%s]",oit.getInr(),oit.getPath(), oit.getInftxt()));
                    break;
            }
        });
    }
    @Override
    public List<Map<String, Object>> huoquErrorMaps() {
        return errmaps.get().stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(
                Comparator.comparing(map -> (String)map.get("oitinr") )
        )), ArrayList::new));
    }

    @Override
    public Map<String, List<Oit>> filterOit(String objinr, String objtyp) {
        List<Oit> oits = oitMapper.selectByObjInrAndTyp(objinr, objtyp);
        Map<String, List<Oit>> maps = new HashMap<>();
        if (oits != null) {
            List errorOits = oits.stream().filter(oit -> ("9".equals(oit.getInflev())
                    || "3".equals(oit.getInflev())
                    || "2".equals(oit.getInflev())
                    || "1".equals(oit.getInflev()))).collect(Collectors.toList());
            if (errorOits.size() > 0) {
                maps.put("ERROR", errorOits);
            }
            List warnOits = oits.stream().filter(oit -> "0".equals(oit.getInflev())).collect(Collectors.toList());
            if (warnOits.size() > 0) {
                maps.put("WARN", warnOits);
            }
            List infoOits = oits.stream().filter(oit -> "$".equals(oit.getInflev())).collect(Collectors.toList());
            if (infoOits.size() > 0) {
                maps.put("INFO", infoOits);
            }
        }
        return maps;
    }
}