package com.casic.missiles.modular.system.warpper;

import com.alibaba.fastjson.JSONArray;
import com.casic.missiles.core.util.EhcacheConstant;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ObjectUtil;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

//@Service
//@Primary
public class AlarmJobWarpper extends BaseControllerWarpper {
//    private EhcacheConstant ehcacheConstant = new EhcacheConstant();
    public AlarmJobWarpper(Object list) { super(list); }
    @Override
    public void warpTheMap(Map<String, Object> map) {

        // 设置人员姓名
        for (String key : map.keySet())
        {
            if (key.contains("JobPerson")|| key.contains("jobBelongTo"))
            {
                Object object = map.get(key);
                if (object!=null)
                {
                    String newVal = EhcacheConstant.retBean().getUsernameById(Long.parseLong(String.valueOf(object)));
                    map.replace(key,newVal);
                }
            }
        }

        //设置闸井相关字段
        String wellCode = map.get("wellCode").toString();
        Map<String,Object> wellInfoMap = EhcacheConstant.retBean().getWellInfoViaCode(wellCode);
        // 设置权属单位、负责人、联系电话
        String deptidStr = wellInfoMap.get("deptid").toString();
        long deptid = StrUtil.isEmpty(deptidStr)? -1: Long.valueOf(deptidStr);
        map.put("deptName",EhcacheConstant.retBean().getDeptNameById(deptid));
        if(deptid>0){
            Long staffstr = EhcacheConstant.retBean().getUserByDept(deptid);
            if(ObjectUtil.isEmpty(staffstr)){
                wellInfoMap.replace("staff","");
                wellInfoMap.replace("tel","");
            }else {
                Map<String,Object> userInfoMap = EhcacheConstant.retBean().getUserInfoById(staffstr);
                wellInfoMap.putAll(userInfoMap);
            }
        }
        map.putAll(wellInfoMap);

        // 告警类型
        String alarmTypeStr = map.get("alarmType").toString();
        map.put("alarmTypeName",EhcacheConstant.retBean().getAlarmTypeNameByCodeNum(alarmTypeStr));

        // 告警内容
        String alarmContentNameStr = map.get("alarmContentName").toString();
        map.put("alarmContent",EhcacheConstant.retBean().getAlarmContentByName(alarmContentNameStr));

        // 告警值加单位
        if(ObjectUtil.isNotEmpty(map.get("alarmValue"))){
            String alarmValue = map.get("alarmValue").toString();
            String alarmContentName = map.get("alarmContentName").toString();
            if("液位超限".equals(alarmContentName)) {
                alarmValue += "m";
                map.replace("alarmValue",alarmValue);
            }
            if("浓度超限".equals(alarmContentName)) {
                alarmValue += "%LEL";
                map.replace("alarmValue",alarmValue);
            }
            if("疑似开挖".equals(alarmContentName)) {
                alarmValue += "mg";
                map.replace("alarmValue",alarmValue);
            }
        }

        // 工单状态
        String jobStatusStr = map.get("jobStatus").toString();
        map.put("jobStatusName",EhcacheConstant.retBean().getJobStatusNameByCodeNum(jobStatusStr));

        // 告警值
        if (alarmContentNameStr.contains("井盖") || "2".equals(alarmTypeStr))
        {
            map.replace("alarmValue","--");
        }

        // 流转记录（工单详情用）
        if (map.containsKey("flow"))
        {
            String flow = (String) map.get("flow");
            flow = "["+flow.replaceFirst(",","")+"]";
            List<Map> list = new ArrayList<>();
            JSONArray jsonArray = JSONArray.parseArray(flow);
            list = jsonArray.toJavaList(Map.class);
            map.replace("flow",list);
        }

    }
}
