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

import cn.hutool.core.util.ObjectUtil;
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.util.EhcacheConstant;
import com.casic.missiles.model.exception.ServiceException;
import com.casic.missiles.model.exception.enums.CoreExceptionEnum;
import com.casic.missiles.model.response.SuccessResponseData;
import com.casic.missiles.modular.system.dto.AlarmNowView;
import com.casic.missiles.modular.system.model.BusWellInfo;
import com.casic.missiles.modular.system.service.IAlarmNowViewService;
import com.casic.missiles.modular.system.service.IBusWellInfoSupportService;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

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

/**
 * 地图总览模块的controller
 *
 * @author tanyue
 * @Date 2019-05-13 19:20:29
 */
@Controller
@RequestMapping("/overview")
public class AlarmMapOverviewController extends BaseController {

    @Resource
    private AbstractPermissionContext permissionService;

    @Autowired
    private IAlarmNowViewService alarmNowViewService;

    @Autowired
    private IBusWellInfoSupportService busWellInfoService;

    /**
     * 获取当前全部告警列表
     */
    @RequestMapping(value = "/alarmNow")
    @ResponseBody
    public Object alarmNow(String alarmType,Long deviceType) {
        // 查询当前有效的报警记录（status=1）
        DataScope dataScope = permissionService.getAuthService().getLoginUserDataScope();
        List<AlarmNowView> alarmNowViewList = alarmNowViewService.getNowAlarmRecords(dataScope,alarmType,deviceType);
        SuccessResponseData successResult = new SuccessResponseData();
        successResult.setData(alarmNowViewList);
        successResult.setMessage("success");
        return successResult;
    }


    /**
     * 获取闸井的全部告警信息
     */
    @RequestMapping(value = "/alarmsByWell", method = RequestMethod.GET)
    @ResponseBody
    public Object alarmsByWell(@RequestParam(required = true) Long id) {
        if (ObjectUtil.isEmpty(id)) {
            throw new ServiceException(CoreExceptionEnum.REQUEST_NULL);
        }
        SuccessResponseData successResult = new SuccessResponseData();
        // 查询闸井信息
        BusWellInfo busWellInfo = busWellInfoService.getById(id);
        Map<String, Object> dataMap = new HashMap<String, Object>();
        if (null != busWellInfo) {
            dataMap.put("id", busWellInfo.getId());
            dataMap.put("wellCode", busWellInfo.getWellCode());
            dataMap.put("deptId", busWellInfo.getDeptid());
            dataMap.put("deptName", permissionService.getDeptService().getDeptName(Long.valueOf(String.valueOf(busWellInfo.getDeptid()))));
            dataMap.put("wellTypeName", EhcacheConstant.retBean().getDictNameByCodeCodeNum(busWellInfo.getWellType(),"sluicewellType"));
            dataMap.put("position", busWellInfo.getPosition());
            dataMap.put("deep", busWellInfo.getDeep());

            List<AlarmNowView> alarmNowViewList = alarmNowViewService.getAlarmsByWell(id);
            for (AlarmNowView view : alarmNowViewList) {
                if ("1".equals(view.getAlarmType()) && "液位超限".equalsIgnoreCase(view.getAlarmContent())) {

                } else {
                    view.setAlarmValue("--");
                }
            }
            if (null != alarmNowViewList && alarmNowViewList.isEmpty() == false) {
                dataMap.put("alarmList", alarmNowViewList);
            } else {
                dataMap.put("alarmList", new ArrayList<AlarmNowView>());
            }
        }

        successResult.setMessage("success");
        successResult.setData(dataMap);

        return successResult;
    }

}
