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

import com.casic.missiles.modular.system.dto.BusWellInfoDto;
import com.casic.missiles.modular.system.dto.DeviceDataDto;
import com.casic.missiles.modular.system.dto.DeviceInfoDto;
import com.casic.missiles.modular.system.dto.WatchDataDto;
import com.casic.missiles.modular.system.model.BusWellInfo;
import com.casic.missiles.modular.system.service.IBusWellInfoService;
import com.casic.missiles.core.base.controller.BaseController;
import com.casic.missiles.core.base.response.ResponseData;
import com.casic.missiles.core.base.response.SuccessResponseData;
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.util.ToolUtil;
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;

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

    @Autowired
    private ICommonPermissionService permissionService;

    @Autowired
    private IBusWellInfoService busWellInfoService;

    /**
     * 获取列表
     */
    @RequestMapping(value = "/wellList")
    @ResponseBody
    public Object wellList(@RequestParam(required = false) String keywords,
                           @RequestParam(required = false) String wellType,
                           @RequestParam(required = false) String deptid,
                           @RequestParam(required = false) String isAlarm) {

        DataScope dataScope = permissionService.getCurrUserDataScope();
        List<BusWellInfoDto> wellList = new ArrayList<BusWellInfoDto>();

        if (null == isAlarm || isAlarm.equals("") == true) {
            // isAlarm参数为空，查询所有的闸井

            // 查询并添加当前部门的闸井
            List<BusWellInfoDto> myList = busWellInfoService.getWellList(dataScope, keywords, wellType, deptid);
            if (null != myList && myList.isEmpty() == false) {
                wellList.addAll(myList);
            }

            /**
             * 不需要了，由dataScope控制权限
             // 查询并添加下级部门的闸井
             List<Long> subDepts = permissionService.getSubDepts(deptid,null);
             if (null != subDepts && subDepts.isEmpty() == false) {
             for (Long subDeptId : subDepts) {
             List<BusWellInfo> subList = busWellInfoService.getWellList(dataScope, keywords, wellType, String.valueOf(subDeptId));
             if (null != subList && subList.isEmpty() == false) {
             wellList.addAll(subList);
             }
             }
             }
             */
        } else if (isAlarm.equals("1")) {
            // isAlarm = 1 查询所有报警状态的闸井
            wellList = busWellInfoService.getAlarmWellList(dataScope, keywords, wellType, deptid);
        } else {
            // isAlarm = 0 查询所有不是报警状态的闸井
            wellList = busWellInfoService.getNotAlarmWellList(dataScope, keywords, wellType, deptid);
        }


        return ResponseData.success(wellList);
    }

    /**
     * 获取设备列表
     */
    @RequestMapping(value = "/deviceList")
    @ResponseBody
    public Object deviceList() {

        DataScope dataScope = permissionService.getCurrUserDataScope();
        List<DeviceInfoDto> wellList = new ArrayList<DeviceInfoDto>();
        wellList = busWellInfoService.getDeviceList(dataScope);
        return ResponseData.success(wellList);
    }

    /**
     * 获取设备详情
     */
    @RequestMapping(value = "/deviceDetail", method = RequestMethod.GET)
    @ResponseBody
    public Object deviceDetail(@RequestParam(required = true) Long id) {
        if (ToolUtil.isEmpty(id)) {
            throw new GunsException(GunsExceptionEnum.REQUEST_NULL);
        }
        DeviceInfoDto deviceInfoDto = busWellInfoService.selectDeviceById(id);
        SuccessResponseData successResult = new SuccessResponseData();
        if (null == deviceInfoDto) {
            successResult.setCode(200);
            successResult.setMessage("success");
            successResult.setData(new HashMap<String, Object>());
        } else {
            // 设置最新的监测数据
            List<DeviceDataDto> deviceDataDtoList = busWellInfoService.
                    getDeviceDatas(deviceInfoDto.getDeviceTypeName(), deviceInfoDto.getDevcode(), deviceInfoDto.getOnlineState());
            if (ToolUtil.isNotEmpty(deviceDataDtoList)) {
                deviceInfoDto.setRealtimeData(deviceDataDtoList);
            }
            successResult.setMessage("success");
            successResult.setData(deviceInfoDto);
        }
        return successResult;
    }


    /**
     * 获取闸井详情
     */
    @RequestMapping(value = "/wellInfo", method = RequestMethod.GET)
    @ResponseBody
    public Object wellInfo(@RequestParam(required = true) Long id) {
        if (ToolUtil.isEmpty(id)) {
            throw new GunsException(GunsExceptionEnum.REQUEST_NULL);
        }

        BusWellInfo busWellInfo = busWellInfoService.selectById(id);
        SuccessResponseData successResult = new SuccessResponseData();
        if (null == busWellInfo) {
            successResult.setCode(200);
            successResult.setMessage("success");
            successResult.setData(new HashMap<String, Object>());
        } else {
            BusWellInfoDto busWellInfoDto = new BusWellInfoDto();
            busWellInfoDto.convertBusWellInfo(busWellInfo);

            // 设置字典值
            busWellInfoDto.setBfztName(permissionService.getDictNameByCode("bfzt", busWellInfo.getBfzt()));
            busWellInfoDto.setWellTypeName(permissionService.getDictNameByCode("sluicewellType", busWellInfo.getWellType()));
            busWellInfoDto.setDeptName(permissionService.getDeptName(busWellInfo.getDeptid()));

            // 设置最新的监测数据
            List<WatchDataDto> watchDataDtos = busWellInfoService.getWatchDatas(id);
            if (null != watchDataDtos && watchDataDtos.isEmpty() == false) {
                for (WatchDataDto watchDataDto : watchDataDtos) {
                    watchDataDto.setValue(watchDataDto.getData());
                }
            }

            busWellInfoDto.setWatchData(watchDataDtos);

            successResult.setMessage("success");
            successResult.setData(busWellInfoDto.toMap());
        }

        return successResult;
    }

}
