package com.gxwebsoft.ai.controller;

import com.alibaba.fastjson.JSONObject;
import com.gxwebsoft.ai.dto.AuditContentRequest;
import com.gxwebsoft.ai.dto.export.InvestmentSituationExportEntity;
import com.gxwebsoft.ai.service.AuditContent7InvestmentService;
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;

/**
 * 审计内容7控制器 - 重大投资情况审计
 */
@Slf4j
@Tag(name = "审计内容7-重大投资情况")
@RestController
@RequestMapping("/api/ai/auditContent7")
public class AuditContent7Controller extends BaseAuditContentController {
    
    @Autowired
    private AuditContent7InvestmentService auditContent7InvestmentService;
    
    /**
     * 生成重大投资情况审计表数据
     */
    @Operation(summary = "生成重大投资情况审计表")
    @PostMapping("/generateInvestmentSituationTable")
    public ApiResult<?> generateInvestmentSituationTable(@RequestBody AuditContentRequest request, HttpServletRequest servletRequest) {
        return generateTableData(request, servletRequest.getRequestURI(), 
            (params) -> auditContent7InvestmentService.generateInvestmentSituationTableData(
                params.knowledgeBaseId, params.libraryKbIds, params.projectLibrary,
                params.username, params.history, params.suggestion
            ));
    }
    
    /**
     * 导出重大投资情况审计表到Excel
     */
    @Operation(summary = "导出重大投资情况审计表到Excel")
    @PostMapping("/exportInvestmentSituationTable")
    public void exportInvestmentSituationTable(@RequestBody Map<String, Object> request, HttpServletResponse response) {
        exportToExcelWithAuditTime(request, response, "重大投资情况审计表", 
            this::convertToInvestmentSituationEntityList, InvestmentSituationExportEntity.class);
    }
    
    // ========== 数据转换方法 ==========
    
    private List<InvestmentSituationExportEntity> convertToInvestmentSituationEntityList(List<Map<String, Object>> originalData) {
        return originalData.stream().map(this::convertToInvestmentSituationEntity).collect(Collectors.toList());
    }
    
    private InvestmentSituationExportEntity convertToInvestmentSituationEntity(Map<String, Object> item) {
        InvestmentSituationExportEntity entity = new InvestmentSituationExportEntity();
        entity.setCategory(getStringValue(item, "category"));
        entity.setAuditContent(getStringValue(item, "auditContent"));
        entity.setCheckEvidence(getStringValue(item, "checkEvidence"));
        entity.setTestResult(getStringValue(item, "testResult"));
        entity.setWorkPaperIndex(formatWorkPaperIndex(item.get("workPaperIndex")));
        entity.setFileIndex(formatWorkPaperIndex(item.get("fileIndex")));
        return entity;
    }
}