StatisticsQueryController.java 4.6 KB
package com.skua.modules.statisticsquery.controller;

import cn.hutool.core.util.PinyinUtil;
import com.google.common.base.CaseFormat;
import com.skua.aop.annotation.CustomExceptionAnno;
import com.skua.core.api.vo.Result;
import com.skua.core.context.SpringContextUtils;
import com.skua.core.util.ConvertUtils;
import com.skua.modules.base.service.IBaseService;
import com.skua.modules.statisticsquery.constant.SQConstant;
import com.skua.modules.statisticsquery.dto.StatisticsQueryDTO;
import com.skua.modules.statisticsquery.enums.SQEnum;
import com.skua.modules.statisticsquery.service.IStatisticsQueryService;
import com.skua.modules.statisticsquery.service.IStatisticsSubService;
import com.skua.modules.statisticsquery.vo.StatisticsQueryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <pre>
 * 统计查询
 * </pre>
 *
 * @author sonin
 * @version 1.0 2023/9/22 14:13
 */
@Slf4j
@RestController
@RequestMapping("/statisticsQuery")
public class StatisticsQueryController {

    @Autowired
    private IStatisticsQueryService statisticsQueryService;
    @Autowired
    private IBaseService baseService;

    @PostMapping(value = "/insertAlias")
    @CustomExceptionAnno(description = "插入别名")
    public Result<Object> insertAliasCtrl(@RequestBody List<Map<String, Object>> paramsMapList) {
        Result<Object> result = new Result<>();
        Date nowDate = new Date();
        List<Map<String, Object>> resMapList = new ArrayList<>();
        for (Map<String, Object> paramsMap : paramsMapList) {
            String aliasName = ConvertUtils.getString(paramsMap.get("aliasName"));
            String id = PinyinUtil.getPinYin(aliasName);
            String nm = ConvertUtils.getString(paramsMap.get("nm"));
            String remark = ConvertUtils.getString(paramsMap.get("remark"));
            Map<String, Object> entityMap = new HashMap<String, Object>() {{
                put("id", id);
                put("nm", nm);
                put("alias_name", aliasName);
                put("alias_desc", aliasName);
                put("remark", remark);
                put("create_time", nowDate);
                put("del_flag", "0");
            }};
            try {
                baseService.insert(SQConstant.aliasTable, entityMap);
            } catch (Exception e) {
                e.printStackTrace();
            }
            resMapList.add(entityMap.keySet().stream().collect(Collectors.toMap(key -> CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, key), entityMap::get)));
        }
        result.setResult(resMapList);
        return result;
    }

    /**
     * <pre>
     * 查询 xsinsert || xxx_count || realtimedata || f_report_itemv 表数据
     * </pre>
     *
     * @param name
     * @param asyncFlag              是否启用异步查询,默认false
     * @param statisticsQueryDTOList
     * @author sonin
     * @Description: TODO(这里描述这个方法的需求变更情况)
     */
    @PostMapping(value = "/multiDataSource/{name}")
    @CustomExceptionAnno(description = "多重数据源数据")
    public Result<Object> multiDataSourceCtrl(@PathVariable String name, @RequestParam(defaultValue = "0") String asyncFlag, @RequestParam(defaultValue = "0") String dataSourceFlag, @RequestBody List<StatisticsQueryDTO> statisticsQueryDTOList) {
        Result<Object> result = new Result<>();
        List<StatisticsQueryVO> statisticsQueryVOList;
        // 判断查询哪种数据源
        if ("0".equals(dataSourceFlag)) {
            // 查询报表report
            statisticsQueryVOList = statisticsQueryService.handleHistoryFunc(asyncFlag, statisticsQueryDTOList, SQEnum.REPORT);
        } else if ("1".equals(dataSourceFlag)) {
            // 查询history(xsinsert || xxx_count)
            statisticsQueryVOList = statisticsQueryService.handleHistoryFunc(asyncFlag, statisticsQueryDTOList, SQEnum.HISTORY);
        } else if ("2".equals(dataSourceFlag)) {
            // 查询realtimedata
            statisticsQueryVOList = statisticsQueryService.handleRealtimeFunc(statisticsQueryDTOList);
        } else {
            // 默认
            statisticsQueryVOList = new ArrayList<>();
        }
        IStatisticsSubService statisticsSubService = (IStatisticsSubService) SpringContextUtils.getBean(name);
        if (statisticsSubService != null) {
            result.setResult(statisticsSubService.execute(statisticsQueryVOList));
        } else {
            result.setResult(statisticsQueryVOList);
        }
        return result;
    }

}