package com.huatek.busi.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.huatek.busi.dto.BusiEqmWarningDto;
import com.huatek.busi.entity.BusiEqmWarning;
import com.huatek.busi.mapper.WarningResultMapper;
import com.huatek.busi.service.*;
import com.huatek.busi.vo.warning.BusiEqmWarningTaskVo;
import com.huatek.frame.base.util.BeanUtils;
import com.huatek.torch.sql.dao.impl.BaseDao;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.*;

@Component
@Service(interfaceClass = WarningResultService.class)
@Transactional(rollbackFor = Exception.class)
public class WarningResultServiceImpl extends BaseDao<BusiEqmWarningTaskVo, String> implements WarningResultService {

    @Resource
    private WarningResultMapper warningResultMapper;
    @Override
    public BusiEqmWarningTaskVo warningInfo(String id) throws ParseException {
        BusiEqmWarningTaskVo warnInfo = warningResultMapper.findWarningInfo(id);
        if (!ObjectUtils.isEmpty(warnInfo)) {

            String eType = warnInfo.getEqType();
            String gType = warnInfo.getGzType();
            if ("1".equals(eType)) {
                warnInfo.setEqType("变压器");
            }
            if ("2".equals(eType)) {
                warnInfo.setEqType("GIS");
            }
            if ("3".equals(eType)) {
                warnInfo.setEqType("避雷器");
            }
            if ("4".equals(eType)) {
                warnInfo.setEqType("测温柜");
            }
            if ("5".equals(eType)) {
                warnInfo.setEqType("其他");
            }
            if ("1".equals(gType)) {
                warnInfo.setGzType("油色谱");
            }
            if ("2".equals(gType)) {
                warnInfo.setGzType("局放");
            }
            if ("3".equals(gType)) {
                warnInfo.setGzType("铁芯夹件");
            }
            if ("4".equals(gType)) {
                warnInfo.setGzType("红外测温");
            }
            if ("5".equals(gType)) {
                warnInfo.setGzType("SF6告警");
            }
            if ("6".equals(gType)) {
                warnInfo.setGzType("避雷器告警");
            }
            if ("7".equals(gType)) {
                warnInfo.setGzType("测温柜测温告警");
            }
            if ("8".equals(gType)) {
                warnInfo.setGzType("铁芯夹件告警");
            }
            if ("9".equals(gType)) {
                warnInfo.setGzType("运行工况告警");
            }
            if ("10".equals(gType)) {
                warnInfo.setGzType("测温点增长率");
            }
            if ("11".equals(gType)) {
                warnInfo.setGzType("读表数据告警");
            }
            if ("12".equals(gType)) {
                warnInfo.setGzType("容性告警");
            }
            if ("13".equals(gType)) {
                warnInfo.setGzType("套管告警");
            }
            if ("21".equals(gType)) {
                warnInfo.setGzType("SF6Env告警");
            }
            String jsonWarnInfo = warnInfo.getWarningInfo();
            JSONObject data = JSONObject.fromObject(jsonWarnInfo);
            JSONArray list = (JSONArray) data.get("list");
            JSONObject temp = new JSONObject();
            JSONArray finJson = new JSONArray();
            for (int i = 0; i < list.size(); i++) {
                net.sf.json.JSONObject insertObj = (JSONObject) list.get(i);
                Set<String> keys = insertObj.keySet();
                for (String key : keys){
                        if ("gasName".equals(key)) {
                            temp.put("气体名称",insertObj.getString("gasName"));
                        }
                        if ("thresholdValue".equals(key)) {
                            temp.put("阈值",insertObj.getString("thresholdValue"));
                        }
                        if ("gasValue".equals(key)) {
                            temp.put("报警值",insertObj.getString("gasValue"));
                        }
                }
                finJson.add(temp);
            }
                warnInfo.setWarningInfo(finJson.toString());
                String threeRatio = (String) data.get("threeRatio");
                String threeSub = threeRatio.substring(0,3);
                if ("001".equals(threeSub)){
                    String threeValue = "故障代码-001,故障信息-低温过热＜150℃@@一般性的绝缘导线过热";
                    warnInfo.setThreeRatio(threeValue);
                }
                if ("020".equals(threeSub)){
                    String threeValue = "故障代码-020,故障信息-低温过热150--300℃@@引线夹件螺丝松动";
                    warnInfo.setThreeRatio(threeValue);
                }
                if ("021".equals(threeSub)){
                    String threeValue = "故障代码-021,故障信息-中温过热300℃--700℃@@铁芯局部过热";
                    warnInfo.setThreeRatio(threeValue);
                }
                if ("002".equals(threeSub)){
                    String threeValue = "高温过热＞700℃@@热点温度增加，从铁芯中的小热点，铁芯短路，由于涡流引起的铜过热，接头或接触不良、焊接不良（形成丝炭）及铁芯、和外壳的环流、铁芯多点接地。";
                    warnInfo.setThreeRatio(threeValue);
                }
                if ("010".equals(threeSub)){
                    String threeValue = "局部放电@@由于不完全浸渍引起含气孔穴中的放电，或过分饱和或高温度引起的孔穴中的放电。";
                    warnInfo.setThreeRatio(threeValue);
                }
                if ("101".equals(threeSub)||"100".equals(threeSub)||"102".equals(threeSub)||"110".equals(threeSub)||"111".equals(threeSub)||"112".equals(threeSub)){
                    String threeValue = "故障信息-高能放电@@引线对为固定的直接连续火花放电，分接抽头引线和油隙闪络，不同电位之间的油中火花放电，悬浮电位之间火花放电";
                    warnInfo.setThreeRatio(threeValue);
                }
                if ("120".equals(threeSub)||"121".equals(threeSub)||"122".equals(threeSub)){
                    String threeValue = "故障信息-高能放电兼过热@@引线对为固定的直接连续火花放电，分接抽头引线和油隙闪络，不同电位之间的油中火花放电，悬浮电位之间火花放电";
                    warnInfo.setThreeRatio(threeValue);
                }
                if ("200".equals(threeSub)||"201".equals(threeSub)||"202".equals(threeSub)||"210".equals(threeSub)||"211".equals(threeSub)||"212".equals(threeSub)){
                    String threeValue = "故障信息-低能放电@@引线时电位未固定的部件之间连续火花放电，分接抽头引线和油隙闪络，不同电位之间的油中火花放电或悬浮之间火花放电";
                    warnInfo.setThreeRatio(threeValue);
                }
                if ("220".equals(threeSub)||"221".equals(threeSub)||"222".equals(threeSub)){
                    String threeValue = "故障信息-低能放电兼过热@@引线时电位未固定的部件之间连续火花放电，分接抽头引线和油隙闪络，不同电位之间的油中火花放电或悬浮之间火花放电。";
                    warnInfo.setThreeRatio(threeValue);
                }
                String time = warnInfo.getWarningTime();
                String warnTime = time.replaceAll("[[\\s-.:punct:]]", "");
                warnInfo.setWarningTime(warnTime);
        }

        return warnInfo;
    }


    @Override
    public Map<String, Object> warningEqm(Map<String, Object> warningId) {
        return warningResultMapper.findWarningEqm(warningId);
    }

}
