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

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.model.exception.ServiceException;
import com.casic.missiles.model.exception.enums.CoreExceptionEnum;
import com.casic.missiles.model.response.SuccessResponseData;
import com.casic.missiles.modular.system.service.IDataAnalysisService;
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.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/well")
public class WellDataController extends BaseController {
    @Resource
    private AbstractPermissionContext permissionService;
    @Resource
    private IDataAnalysisService dataAnalysisService;


    /**
     * 获取闸井设备数据
     */
    @RequestMapping(value = "/watchDataByWell", method = RequestMethod.GET)
    @ResponseBody
    public Object watchDataByWell(Long id) {
        if (null == id) {
            throw new ServiceException(CoreExceptionEnum.REQUEST_NULL);
        }
        SuccessResponseData resultData = new SuccessResponseData();
        resultData.setData(dataAnalysisService.getWatchDatas(id));

        return resultData;
    }

    /**
     * 根据布防状态统计闸井数量接口
     */
    @RequestMapping(value = "/countByBfzt", method = RequestMethod.GET)
    @ResponseBody
    public Object countByBfzt() {
        DataScope dataScope = permissionService.getAuthService().getLoginUserDataScope();
        Set<String> roleTipes = permissionService.getAuthService().getLoginUser().getRoleTips();
        List<Long> deptIds = null;
        Boolean isApp = false;
        if (roleTipes.contains("leader") || roleTipes.contains("member")) {
            deptIds = permissionService.getDeptService().getIdsByPid(permissionService.getAuthService().getLoginUser().getDeptId(), null);
            deptIds.add(permissionService.getAuthService().getLoginUser().getDeptId());
            isApp = true;
        } else {
            deptIds = dataScope.getDeptIds();
            isApp = false;
        }
        SuccessResponseData resultData = new SuccessResponseData();
        dataAnalysisService.getBfztCountData(deptIds, isApp);
        resultData.setData(dataAnalysisService.getBfztCountData(deptIds, isApp));
        return resultData;
    }

    /**
     * 获取布防类型（APP用）
     */
    @RequestMapping(value = "/bfType", method = RequestMethod.GET)
    @ResponseBody
    public Object bfType(Long id) {
        if (null == id) {
            throw new ServiceException(CoreExceptionEnum.REQUEST_NULL);
        }
        SuccessResponseData resultData = new SuccessResponseData();
        resultData.setData(dataAnalysisService.getWatchDatas(id));
        return resultData;
    }

}
