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

import com.alibaba.fastjson.JSONArray;
import com.casic.missiles.core.util.EhcacheConstant;
import com.casic.missiles.core.base.warpper.BaseControllerWarpper;
import com.casic.missiles.core.util.ToolUtil;

import java.util.ArrayList;
import java.util.HashMap;
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())
        {
//            String[] keyNames = new String[]{"jobBelongTo"};
//            List<String> realNames = new ArrayList<>();
//            if (realNames.contains(key))
//            {
//            }
            if (key.contains("JobPerson")|| key.contains("jobBelongTo"))
            {
                Object object = map.get(key);
                if (object!=null)
                {
                    String newVal = EhcacheConstant.retBean().getUsernameById((long)map.get(key));
                    map.replace(key,newVal);
                }
            }
        }

        Map<String,Object> wellInfoMap = new HashMap<>();
        String alarmTypeStr = map.get("alarmType").toString();
        String wellCode = map.get("wellCode").toString();
        String alarmContentNameStr = map.get("alarmContentName").toString();
        String jobStatusStr = map.get("jobStatus").toString();//if notnull
//        int alarmContent = ToolUtil.isEmpty(alarmContentStr)? -1: Integer.valueOf(alarmContentStr) ;//if notnull
//        int jobStatus = ToolUtil.isEmpty(jobStatusStr)? -1: Integer.valueOf(jobStatusStr) ;//if notnull
//        int alarmType = ToolUtil.isEmpty(alarmTypeStr)? -1: Integer.valueOf(alarmTypeStr);
//        int status = ToolUtil.isEmpty(statusStr)? -1: Integer.valueOf(statusStr);
        if (alarmContentNameStr.contains("井盖") || "2".equals(alarmTypeStr))
        {
            map.replace("alarmValue","--");
        }
        //wellCode isnotNull
        wellInfoMap = EhcacheConstant.retBean().getWellInfoViaCode(wellCode);
        String deptidStr = wellInfoMap.get("deptid").toString();
        long deptid = ToolUtil.isEmpty(deptidStr)? -1: Long.valueOf(deptidStr);

        map.put("alarmTypeName",EhcacheConstant.retBean().getAlarmTypeNameByCodeNum(alarmTypeStr));
        map.put("deptName",EhcacheConstant.retBean().getDeptNameById(deptid));
        map.put("alarmContent",EhcacheConstant.retBean().getAlarmContentByName(alarmContentNameStr));
//        map.put("alarmContentName",EhcacheConstant.retBean().getAlarmContentName(alarmContent));
//        map.put("statusName",EhcacheConstant.retBean().getDictNameByCodeNum(status,"alarmStatus"));//std dictionary
        map.put("jobStatusName",EhcacheConstant.retBean().getJobStatusNameByCodeNum(jobStatusStr));

        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);
        }
        map.putAll(wellInfoMap);
    }
}
