package com.persagy.dmp.rwd.simple.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.persagy.dmp.basic.dto.RequestData;
import com.persagy.dmp.common.constant.CommonConstant;
import com.persagy.dmp.common.constant.ResponseCode;
import com.persagy.dmp.common.exception.BusinessException;
import com.persagy.dmp.common.model.response.CommonResult;
import com.persagy.dmp.common.utils.ParamCheckUtil;
import com.persagy.dmp.common.utils.ResultHelper;
import com.persagy.dmp.rwd.digital.service.IObjectDigitalService;
import com.persagy.dmp.rwd.simple.entity.CountTypeEquipmentVO;
import com.persagy.dmp.rwd.simple.service.IEquipmentReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 简洁API - 设备统计相关接口
 * @author Charlie Yu
 * @date 2021-09-02
 */
@Slf4j
@RestController
@RequestMapping("/api/report/equipment")
public class EquipmentReportController {

    @Autowired
    private IObjectDigitalService digitalService;
    @Autowired
    private IEquipmentReportService reportService;

    /**
     * 统计项目设备总数和待报废设备数量
     * 待报废设备 = ifnull(预计报废日期, '9999-12-31') < (当前日期+6个月)
     * @param projectIds 为空查所有
     * @return
     */
    @PostMapping("/queryExpectScrapCountByProjects")
    public CommonResult<List<Map<String, Object>>> queryExpectScrapCountByProjects(
            @RequestBody(required = false) List<String> projectIds) {
        //基础参数校验
        ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE);
        // 统计
        List<Map<String, Object>> result = reportService.queryExpectScrapCountByProjects(projectIds);
        return ResultHelper.multi(result);
    }

    /**
     * 按对象类型统计设备总数
     * @param projectIds 为空查所有
     * @return 专业类统计 - 系统类统计 - 设备类统计 层级结构
     */
    @PostMapping("/queryEquipmentCountByType")
    public CommonResult<List<CountTypeEquipmentVO>> queryEquipmentCountByType(@RequestBody(required = false) List<String> projectIds) {
        //基础参数校验
        ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE);
        // 统计
        List<CountTypeEquipmentVO> result = reportService.queryEquipmentCountByType(projectIds);
        return ResultHelper.multi(result);
    }
    /**
     * 根据对象信息点统计数量
     * @param requestData:请求参数
     * @return
     */
    @PostMapping("/queryObjectCountByObjInfos")
    public CommonResult<List<Map<String, Object>>> queryObjectCountByObjInfos(@RequestBody RequestData requestData) {
        //基础参数校验
        ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE);
        if (CollUtil.isEmpty(requestData.getGroups())){
            throw new BusinessException(ResponseCode.A0402.getCode(),ResponseCode.A0402.getDesc());
        }
        // 统计
        List<Map<String, Object>> result = reportService.queryObjectCountByObjInfos(requestData);
        return ResultHelper.multi(result);
    }

    /**
     * 查询当前设备类型下的部件分类以及数量
     * @param requestData:请求参数
     * @return
     */
    @PostMapping("/queryComponentsCountByEquipId")
    public CommonResult<List<Map<String, Object>>> queryComponentsCountByEquipId(@RequestBody RequestData requestData) {
        //基础参数校验
        ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE);
        if (StrUtil.isEmpty(requestData.getEquipmentId())){
            throw new BusinessException(ResponseCode.A0402.getCode(),ResponseCode.A0402.getDesc());
        }
        // 统计
        List<Map<String, Object>> result = reportService.queryComponentsCountByEquipId(requestData.getEquipmentId());
        return ResultHelper.multi(result);
    }


    /**
     * 查询当前设备类型的允许的所有部件类型，设备下的部件对应数量
     * @param requestData:请求参数
     * @return
     */
    @PostMapping("/queryEquipComponentsByProjectId")
    public CommonResult<List<Map<String, Object>>> queryEquipComponentsByProjectId(@RequestBody RequestData requestData) {
        //基础参数校验
        ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE);
        // 统计
        List<Map<String, Object>> result = reportService.queryEquipComponentsByProjectId(requestData.getClassCode(), requestData.getEquipmentId());
        return ResultHelper.multi(result);
    }

    /**
     * 项目下所有实例(system/space/equipment..)类型以及名称
     */
    @PostMapping("/queryDefineInfoByObjType")
    public CommonResult<List<Map<String,Object>>> queryDefineInfoByObjType(@RequestBody RequestData requestData) {
        //基础参数校验
        ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE);
        if (CollUtil.isEmpty(requestData.getObjTypes())){
            throw new BusinessException(ResponseCode.A0402.getCode(),ResponseCode.A0402.getDesc());
        }
        // 统计
        List<Map<String, Object>> result = reportService.queryDefineInfoByObjType(requestData);
        return ResultHelper.multi(result);
    }

}
