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

import com.baomidou.mybatisplus.plugins.Page;
import com.casic.missiles.core.common.service.ICommonPermissionService;
import com.casic.missiles.core.datascope.DataScope;
import com.casic.missiles.core.util.EhcacheConstant;
import com.casic.missiles.core.util.ToolUtil;
import com.casic.missiles.modular.alarm.service.IAlarmRuleService;
import com.casic.missiles.modular.system.warpper.AlarmRuleWarpper;
import com.casic.missiles.core.base.controller.BaseController;
import com.casic.missiles.core.common.constant.factory.PageFactory;
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 javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import com.casic.missiles.core.base.response.ResponseData;

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


    @Autowired
    private IAlarmRuleService alarmRuleService;
    @Autowired
    private ICommonPermissionService iCommonPermissionService;


    /**
     * 获取列表
     */

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

        String deviceType = httpServletRequest.getParameter("deviceType");
        if (ToolUtil.isEmpty(deviceType))
            deviceType="";
        Page<Map<String,Object>> page = new PageFactory<Map<String,Object>>().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.getCurrUserDataScope();//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.getCurrUserDataScope();//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 (ToolUtil.isNotEmpty(sHigh))
            highValue = Float.parseFloat(sHigh);
        if (ToolUtil.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;
    }



}
