package com.gxwebsoft.ai.controller;

import com.alibaba.fastjson.JSONObject;
import com.gxwebsoft.ai.dto.AuditContentRequest;
import com.gxwebsoft.ai.dto.export.StrategyAuditExportEntity;
import com.gxwebsoft.ai.service.AuditContent2StrategyService;
import com.gxwebsoft.common.core.web.ApiResult;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 审计内容2控制器 - 单位发展战略执行审计
 */
@Slf4j
@Tag(name = "审计内容2-单位发展战略执行")
@RestController
@RequestMapping("/api/ai/auditContent2")
public class AuditContent2Controller extends BaseAuditContentController {
    
    @Autowired
    private AuditContent2StrategyService auditContent2StrategyService;
    
    /**
     * 生成单位发展战略执行审计表数据
     */
    @Operation(summary = "生成单位发展战略执行审计表")
    @PostMapping("/generateStrategyAuditTable")
    public ApiResult<?> generateStrategyAuditTable(@RequestBody AuditContentRequest request, HttpServletRequest servletRequest) {
        return generateTableData(request, servletRequest.getRequestURI(), 
            (params) -> auditContent2StrategyService.generateStrategyAuditTableData(
                params.knowledgeBaseId, params.libraryKbIds, params.projectLibrary,
                params.username, params.history, params.suggestion
            ));
    }
    
    /**
     * 导出单位发展战略执行审计表到Excel
     */
    @Operation(summary = "导出单位发展战略执行审计表到Excel")
    @PostMapping("/exportStrategyAuditTable")
    public void exportStrategyAuditTable(@RequestBody Map<String, Object> request, HttpServletResponse response) {
        exportToExcel(request, response, "单位发展战略执行审计表", 
            this::convertToExportEntityList, StrategyAuditExportEntity.class);
    }
    
    /**
     * 数据转换
     */
    private List<StrategyAuditExportEntity> convertToExportEntityList(List<Map<String, Object>> originalData) {
        return originalData.stream().map(this::convertToExportEntity).collect(Collectors.toList());
    }
    
    private StrategyAuditExportEntity convertToExportEntity(Map<String, Object> item) {
        StrategyAuditExportEntity entity = new StrategyAuditExportEntity();
        entity.setIndex(getStringValue(item, "index"));
        entity.setAuditContent(getStringValue(item, "auditContent"));
        entity.setCheckEvidence(getStringValue(item, "checkEvidence"));
        entity.setTestResult(getStringValue(item, "testResult"));
        entity.setWorkPaperIndex(formatWorkPaperIndex(item.get("workPaperIndex")));
        return entity;
    }
}