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

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.casic.missiles.core.base.controller.BaseController;
import com.casic.missiles.model.response.ResponseData;
import com.casic.missiles.core.page.PageFactory;
import com.casic.missiles.core.application.context.AbstractPermissionContext;
import com.casic.missiles.core.datascope.DataScope;
import com.casic.missiles.core.util.EhcacheConstant;
import com.casic.missiles.modular.alarm.service.IAlarmRuleService;
import com.casic.missiles.modular.system.dto.AlarmRuleDTO;
import com.casic.missiles.modular.system.warpper.AlarmRuleWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.hutool.core.util.StrUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 控制器
 *
 * @author dev
 * @Date 2019-05-17 10:49:14
 */
@Controller
@RequestMapping("/alarmRule")
public class AlarmRuleController extends BaseController {


    @Autowired
    private IAlarmRuleService alarmRuleService;
    @Resource
    private AbstractPermissionContext iCommonPermissionService;

    @Autowired
    private IDeviceRuleService iDeviceRuleService;


    /**
     * 获取列表
     */

    @RequestMapping(value = "/list")
    @ResponseBody
    public Map<String, Object> list(String keywords, HttpServletRequest httpServletRequest) {

        String deviceType = httpServletRequest.getParameter("deviceType");
        if (StrUtil.isEmpty(deviceType))
            deviceType = "";
        Page<Map<String, Object>> page = PageFactory.defaultPage();
        Map<String, Object> retMap = new HashMap<>();
        boolean bSessionValid = false;
        bSessionValid = super.getSession().getAttribute("sessionFlag") == null ? false : (boolean) super.getSession().getAttribute("sessionFlag");
        if (!bSessionValid) {
            retMap.put("code", 505);
            retMap.put("message", "sessionExpired");
            return retMap;
        }
        List<Map<String, Object>> retList = new ArrayList<>();
        DataScope dataScope = iCommonPermissionService.getAuthService().getLoginUserDataScope();//datascope
        retList = alarmRuleService.ruleListViaParam(page, keywords, deviceType, dataScope);
        new AlarmRuleWarpper(retList).warp();
        page.setRecords(retList);
        retMap.put("code", 200);
        retMap.put("message", "success");
        retMap.put("data", super.packForBT(page));
        return retMap;
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public Map<String, Object> updateRule(@RequestParam(value = "id", required = true) int id, HttpServletRequest httpServletRequest) {
        Map<String, Object> retMap = new HashMap<>();
        DataScope dataScope = iCommonPermissionService.getAuthService().getLoginUserDataScope();//datascope
        if (super.getSession().getAttribute("sessionFlag") == null) {
            retMap.put("code", 505);
            retMap.put("message", "sessionExpired");
            return retMap;
        }
        String sHigh = httpServletRequest.getParameter("highValue");
        String sLow = httpServletRequest.getParameter("lowValue");
        float highValue = 99999;
        float lowValue = -99999;
        if (StrUtil.isNotEmpty(sHigh))
            highValue = Float.parseFloat(sHigh);
        if (StrUtil.isNotEmpty(sLow))
            lowValue = Float.parseFloat(sLow);

        if (alarmRuleService.updateRule(id, highValue, lowValue)) {
            retMap.put("code", 200);
            retMap.put("message", "success");
        } else {
            retMap.put("code", 200);
            retMap.put("message", "failure");
        }
        return retMap;
    }

    @RequestMapping(value = "resetCache")
    @ResponseBody
    public Map<String, Object> resetCache() {
        Map<String, Object> retMap = new HashMap<>();
        EhcacheConstant.retBean().resetEhcache();
        retMap.put("code", 200);
        retMap.put("message", "success");
        return retMap;
    }

    /**
     * 新增告警规则
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(@RequestParam(required = true) String devcode,
                      @RequestParam(required = true) Long deviceType,
                      @RequestParam(required = true) Float highValue,
                      @RequestParam(required = true) Float lowValue) {
        try {
            iDeviceRuleService.updateRule(devcode, highValue, lowValue);
        } catch (Exception e) {
            return ResponseData.error(ResponseData.DEFAULT_ERROR_CODE, "新增失败");
        }
        return ResponseData.success();
    }

    /**
     * 删除告警规则
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer id) {
        alarmRuleService.deleteById(id);
        return ResponseData.success();
    }


    /**
     * 获取未配置告警规则的设备列表
     */
    @RequestMapping(value = "/getNoAlarmDevice")
    @ResponseBody
    public Map<String, Object> getNoAlarmDevice() {

        Map<String, Object> retMap = new HashMap<>();
        DataScope dataScope = iCommonPermissionService.getAuthService().getLoginUserDataScope();//datascope
        List<AlarmRuleDTO> alarmRuleList= alarmRuleService.getNoAlarmDevice(dataScope);
        retMap.put("code", 200);
        retMap.put("message", "success");
        retMap.put("data", alarmRuleList);
        return retMap;
    }
}
