package com.casic.missiles.modular.alarm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.casic.missiles.core.base.controller.BaseController;
import com.casic.missiles.core.base.response.ResponseData;
import com.casic.missiles.core.common.service.ICommonPermissionService;
import com.casic.missiles.core.shiro.ShiroUser;
import com.casic.missiles.core.util.EhcacheConstant;
import com.casic.missiles.core.util.ToolUtil;
import com.casic.missiles.modular.alarm.service.IAlarmLevelService;
import com.casic.missiles.modular.system.model.AlarmLevel;
import com.casic.missiles.modular.system.model.DeptPermission;
import com.casic.missiles.modular.system.service.IDeptPermissionService;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.mapper.EntityWrapper;

import java.util.*;


/**
 * 控制器
 *
 * @author dev
 * @Date 2019-10-15 17:00:34
 */
@Controller
@RequestMapping("/alarm")
public class AlarmLevelController extends BaseController {

    @Autowired
    private IAlarmLevelService alarmLevelService;
    @Autowired
    private ICommonPermissionService permissionService;
    @Autowired
    private IDeptPermissionService deptPermissionService;

    /**
     * 获取告警等级列表
     */
    @RequestMapping("/levelList")
    @ResponseBody
    public Object levelList(Long tenantId){
        List<Map<String,Object>> levelList  = new ArrayList<>();
        ShiroUser currUser = permissionService.getCurrLoginUser();
        if(ToolUtil.isEmpty(tenantId)){
            if(ToolUtil.isEmpty(currUser.getTenantId())){
                return ResponseData.error("无关联的项目id");
            }
            tenantId = Long.valueOf(currUser.getTenantId()); //当前用户所属组织机构关联的项目id
        }
        DeptPermission deptPermission = deptPermissionService.selectDeptPermission(currUser.getDeptId());
        if(ToolUtil.isEmpty(deptPermission)){
            return levelList;
        }
        for(Object deviceType: JSON.parseObject(deptPermission.getDeviceType(), List.class)){
            String deviceTypeName = EhcacheConstant.retBean().getDeviceTypeNameById(deviceType.toString());
            List<String> names = new ArrayList<>();
            if(deviceTypeName.contains("液位")){
                names.add("液位");
            }
            if(deviceTypeName.contains("有害气体")){
                names.add("一氧化碳");
                names.add("硫化氢");
                names.add("甲烷");
                names.add("氧气");
            }
            if(deviceTypeName.contains("燃气")){
                names.add("燃气");
            }
            if(deviceTypeName.contains("温湿度")){
                names.add("温度");
                names.add("湿度");
            }
            if(deviceTypeName.contains("开挖")){
                names.add("开挖");
            }
            for(String name: names){
                Map<String, Object> levelMap = new HashMap<>();
                Map<String, Object> level = alarmLevelService.selectByNameAndTenGroup(tenantId,name);
                levelMap.put("name",name);
                levelMap.put("deviceType",deviceType);
                levelMap.put("deviceTypeName",deviceTypeName);

                if(ToolUtil.isEmpty(level.get("openAlarm"))){
                    levelMap.put("openAlarm","");
                }else{
                    levelMap.put("openAlarm",level.get("openAlarm").toString());
                }

                if(ToolUtil.isEmpty(level.get("openJob"))){
                    levelMap.put("openJob","");
                }else{
                    levelMap.put("openJob",level.get("openJob").toString());
                }

                if(ToolUtil.isEmpty(level.get("ts"))){
                    levelMap.put("ts","");
                }else{
                    levelMap.put("ts",level.get("ts").toString());
                }

                levelList.add(levelMap);
            }
        }
        return ResponseData.success(levelList);
    }

    /**
     * 获取监测内容的告警等级
     */
    @RequestMapping("/levelByType")
    @ResponseBody
    public Object levelByType(Long tenantId, String name){
        ShiroUser currUser = permissionService.getCurrLoginUser();
        if(ToolUtil.isEmpty(tenantId)){
            if(ToolUtil.isEmpty(currUser.getTenantId())){
                return ResponseData.error("无关联的项目id");
            }
            tenantId = Long.valueOf(currUser.getTenantId()); //当前用户所属组织机构关联的项目id
        }
        List<AlarmLevel> levelList = alarmLevelService.selectByNameAndTenList(tenantId, name);
        return ResponseData.success(levelList);
    }

    @RequestMapping("/configLevel")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public Object configLevel(@RequestBody List<AlarmLevel> levelList){
        ShiroUser currUser = permissionService.getCurrLoginUser();
        String userTenantId = currUser.getTenantId(); //当前用户所属组织机构关联的项目id
        Date currDate = new Date();
        for(AlarmLevel alarmLevel:levelList){
            if(ToolUtil.isAllEmpty(alarmLevel.getTenantId(),userTenantId)){
                return ResponseData.error("无关联的项目id");
            }
            Long tenantId = ToolUtil.isEmpty(alarmLevel.getTenantId()) ? Long.valueOf(userTenantId) : alarmLevel.getTenantId();
            AlarmLevel oldAlarmLevel = alarmLevelService.selectByNameAndTenAndLevel(tenantId,alarmLevel.getName(),alarmLevel.getLevel());
            if(ToolUtil.isEmpty(oldAlarmLevel)){
                //insert
                alarmLevel.setTenantId(tenantId);
                alarmLevel.setTs(currDate);
                alarmLevelService.insert(alarmLevel);
            }else{
                //update
                alarmLevel.setId(oldAlarmLevel.getId());
                alarmLevel.setTenantId(tenantId);
                alarmLevel.setTs(currDate);
                alarmLevelService.updateById(alarmLevel);
            }
        }
        return ResponseData.success();
    }
}
