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

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.casic.missiles.core.application.context.AbstractPermissionContext;
import com.casic.missiles.core.base.controller.BaseController;
import com.casic.missiles.core.datascope.DataScope;
import com.casic.missiles.core.model.auth.AuthUser;
import com.casic.missiles.core.util.EhcacheConstant;
import com.casic.missiles.model.response.ResponseData;
import com.casic.missiles.modular.alarm.model.AlarmLevelParam;
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 lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

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


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

    @Autowired
    private IAlarmLevelService alarmLevelService;
    @Resource
    private AbstractPermissionContext permissionService;
    @Resource
    private IDeptPermissionService deptPermissionService;
    @Resource
    private AbstractPermissionContext iCommonPermissionService;

    /**
     * 获取告警等级列表
     */
    @RequestMapping("/level/list")
    @ResponseBody
    public Object levelList(Long tenantId) {
        List<Map<String, Object>> levelList = new ArrayList<>();
        AuthUser currUser = permissionService.getAuthService().getLoginUser();
        if (ObjectUtil.isEmpty(tenantId)) {
            if (StrUtil.isEmpty(currUser.getTenantId())) {
                return ResponseData.error("无关联的项目id");
            }
            //当前用户所属组织机构关联的项目id
            tenantId = Long.valueOf(currUser.getTenantId());
        }

        DeptPermission deptPermission = deptPermissionService.selectDeptPermission(currUser.getDeptId());
        if (ObjectUtil.isEmpty(deptPermission)) {
            return levelList;
        }
        for (Object deviceType : JSON.parseObject(deptPermission.getDeviceType(), List.class)) {
            String deviceTypeName = EhcacheConstant.retBean().getDeviceTypeNameById(deviceType.toString());
            if (deviceTypeName == null) {
                continue;
            }
            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(level == null){
                    continue;
                }
                if (ObjectUtil.isEmpty(level.get("openAlarm"))) {
                    levelMap.put("openAlarm", "");
                } else {
                    levelMap.put("openAlarm", level.get("openAlarm").toString());
                }

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

                if (ObjectUtil.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) {
        AuthUser currUser = permissionService.getAuthService().getLoginUser();
        if (ObjectUtil.isEmpty(tenantId)) {
            if (StrUtil.isEmpty(currUser.getTenantId())) {
                return ResponseData.error("无关联的项目id");
            }
            //当前用户所属组织机构关联的项目id
            tenantId = Long.valueOf(currUser.getTenantId());
        }
        List<AlarmLevel> levelList = alarmLevelService.selectByNameAndTenList(tenantId, name);
        System.out.println(levelList.toString());
        return ResponseData.success(levelList);
    }

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


    @RequestMapping(value = "/level/add", method = RequestMethod.POST)
    @ResponseBody
    public Object add(@RequestBody AlarmLevelParam alarmLevelParam) {
        return alarmLevelService.addAlarmLevel(alarmLevelParam);
    }

    @RequestMapping(value = "/level/update", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@RequestBody  AlarmLevelParam alarmLevelParam) {
        return alarmLevelService.updateAlarmLevel(alarmLevelParam);
    }

    /**
     * 获取详情信息
     */
    @RequestMapping(value = "/level/detail/{id}")
    @ResponseBody
    public Object selectAlarmLevel(@PathVariable(name = "id") Long id) {
        AlarmLevel alarmLevel = alarmLevelService.selectByID(id);
        return ResponseData.success(alarmLevel);
    }

    @RequestMapping(value = "/level/delete")
    @ResponseBody
    public Object deleteAlarmLevel(String deviceType, String name, String tenantId) {
        return alarmLevelService.deleteByCondition(deviceType, name, tenantId);
    }
}
