
package com.jeelowcode.core.framework.controller;

import com.jeelowcode.core.framework.service.IReportService;
import com.jeelowcode.framework.global.JeeLowCodeBaseConstant;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jeelowcode.framework.utils.model.global.BaseWebResult;
import com.jeelowcode.core.framework.utils.Func;
import com.jeelowcode.core.framework.utils.FuncWeb;
import com.jeelowcode.framework.exception.JeeLowCodeException;
import com.jeelowcode.framework.utils.model.ResultDataModel;
import com.jeelowcode.framework.utils.utils.FuncBase;
import com.jeelowcode.module.api.JeeLowCodeApi;
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.beans.factory.annotation.Autowired;
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(JeeLowCodeBaseConstant.REQUEST_URL_START + "/report-data")
public class ReportDataController extends BaseController {

    private final JeeLowCodeApi jeeLowCodeApi;;

    @PreAuthorize("@ss.hasPermission('jeelowcode: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);
        jeeLowCodeApi.syncData(reportCode,params,pages);
        return BaseWebResult.success(pages);
    }


    @PreAuthorize("@ss.hasPermission('jeelowcode: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.jeelowcodeForkJoinPool();
            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);
    }


}
