
package com.lideeyunji.core.framework.controller;

import com.lideeyunji.tool.framework.common.constant.lideeYunJiBaseConstant;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.lideeyunji.tool.framework.yunji.model.global.BaseWebResult;
import com.lideeyunji.core.framework.utils.Func;
import com.lideeyunji.core.framework.utils.FuncWeb;
import com.lideeyunji.tool.framework.yunji.model.ResultDataModel;
import com.lideeyunji.tool.framework.yunji.utils.FuncBase;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

@Slf4j
@Tag(name = "低代码框架-报表接口")
@RestController
@AllArgsConstructor
@RequestMapping(lideeYunJiBaseConstant.REQUEST_URL_START + "/report-data")
public class ReportDataController extends BaseController {

    @PreAuthorize("@ss.hasPermission('lideeyunji:report-data:query:'+#reportCode)")//根据报表编号 来判断是否有权限访问
    @PostMapping({"/list/{reportCode}"})
    @ApiOperationSupport(order = 2)
    @Operation(tags = "报表统计", summary = "获取数据列表 - 单个列表")
    public BaseWebResult getDataList(@PathVariable("reportCode") String reportCode, HttpServletRequest req) {
        Map<String, Object> params = FuncWeb.getParameterBodyMap(req);
        ResultDataModel pages = getReportDataList(reportCode, params);
        return BaseWebResult.success(pages);
    }


    @PreAuthorize("@ss.hasPermission('lideeyunji:report-data:query:'+#reportCodeListStr)")//根据报表编号来判断是否有权限访问
    @PostMapping({"/batch/list/{reportCodeListStr}"})
    @ApiOperationSupport(order = 2)
    @Operation(tags = "报表统计", summary = "获取数据列表 - 多个列表")
    public BaseWebResult getDataPage(@PathVariable("reportCodeListStr") String reportCodeListStr, HttpServletRequest req) {
        Map<String, Object> params = FuncWeb.getParameterBodyMap(req);
        List<String> reportCodeList = Func.toStrList(reportCodeListStr);
        Map<String, ResultDataModel> resultMap = new ConcurrentHashMap<>();
        ForkJoinPool pool = null;
        try {
            pool = FuncBase.lideeyunjiForkJoinPool();
            pool.submit(() -> reportCodeList.parallelStream().forEach(reportCode -> {
                // 处理代码
                Map<String, Object> subParam = (Map) params.get(reportCode);
                if (Func.isEmpty(subParam)) {
                    subParam = new HashMap<>();
                }
                ResultDataModel pages = getReportDataList(reportCode, subParam);
                resultMap.put(reportCode, pages);
            })).get();
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            if (pool != null) {
                pool.shutdown();
            }
        }
        return BaseWebResult.success(resultMap);
    }


}
