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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.persagy.dmp.common.constant.CommonConstant;
import com.persagy.dmp.common.constant.ResponseCode;
import com.persagy.dmp.common.context.AppContext;
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.digital.entity.ObjectRelation;
import com.persagy.dmp.rwd.simple.domain.CountRequestData;
import com.persagy.dmp.rwd.simple.service.RelationReportService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.ArrayList;
import java.util.List;
import java.util.Map;

/***
 * Description: 关系数据相关的统计
 * @author : lijie
 * @date :2021/10/28 10:14
 * Update By lijie 2021/10/28 10:14
 */
@Slf4j
@RestController
@RequestMapping("/api/report/relation/")
@RequiredArgsConstructor
public class RelationReportController {


    private final RelationReportService relationReportService;


    /***
     * Description: 根据对象id统计关系数量
     * @param requestData : 请求参数
     * @author : lijie
     * @date :2021/10/28 10:16
     * Update By lijie 2021/10/28 10:16
     */
    @PostMapping("queryRelationCountByObjIds")
    public CommonResult<List<ObjectRelation>> queryRelationCountByObjIds(@RequestBody CountRequestData requestData) {
        //基础参数校验
        ParamCheckUtil.checkParam(CommonConstant.QUERY_GROUPCODE);
        if (CollUtil.isEmpty(requestData.getGroups())){
            throw new BusinessException(ResponseCode.A0400.getCode(),ResponseCode.A0400.getDesc());
        }
        if (CollUtil.isEmpty(requestData.getProjectIds())
                && StrUtil.isNotBlank(AppContext.getContext().getProjectId())){
            requestData.setProjectIds(CollUtil.newHashSet(AppContext.getContext().getProjectId()));
        }
        return ResultHelper.multi(relationReportService.queryRelationCountByObjIds(requestData));
    }

}
