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

import com.casic.missiles.core.base.controller.BaseController;
import com.casic.missiles.core.common.service.ICommonPermissionService;
import com.casic.missiles.core.datascope.DataScope;
import com.casic.missiles.core.exception.GunsException;
import com.casic.missiles.core.exception.GunsExceptionEnum;
import com.casic.missiles.core.base.response.SuccessResponseData;
import com.casic.missiles.core.util.ToolUtil;
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.IBusWellInfoService;
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 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 {

    @Autowired
    private ICommonPermissionService permissionService;

    @Autowired
    private IAlarmNowViewService alarmNowViewService;

    @Autowired
    private IBusWellInfoService busWellInfoService;

    /**
     * 获取当前全部告警列表
     */
    @RequestMapping(value = "/alarmNow")
    @ResponseBody
    public Object alarmNow() {

        // 查询当前有效的报警记录（status=1）
        DataScope dataScope = permissionService.getCurrUserDataScope();
        List<AlarmNowView> alarmNowViewList = alarmNowViewService.getNowAlarmRecords(dataScope);

        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(ToolUtil.isEmpty(id)){
            throw new GunsException(GunsExceptionEnum.REQUEST_NULL);
        }

        SuccessResponseData successResult = new SuccessResponseData();

        // 查询闸井信息
        BusWellInfo busWellInfo = busWellInfoService.selectById(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.getDeptName(String.valueOf(busWellInfo.getDeptid())));
            dataMap.put("wellTypeName",busWellInfo.getWellTypeName());
            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;
    }

}
