package com.gxwebsoft.ai.controller;

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

/**
 * 审计内容10控制器 - 党风廉政建设责任制审计
 */
@Slf4j
@Tag(name = "审计内容10-廉政情况")
@RestController
@RequestMapping("/api/ai/auditContent10")
public class AuditContent10Controller extends BaseAuditContentController {
    
    @Autowired
    private AuditContent10PartyConductService auditContent10PartyConductService;
    
    /**
     * 生成党风廉政建设责任制审计表数据
     */
    @Operation(summary = "生成党风廉政建设责任制审计表")
    @PostMapping("/generatePartyConductTable")
    public ApiResult<?> generatePartyConductTable(@RequestBody AuditContentRequest request, HttpServletRequest servletRequest) {
        return generateTableData(request, servletRequest.getRequestURI(), 
            (params) -> auditContent10PartyConductService.generatePartyConductTableData(
                params.knowledgeBaseId, params.libraryKbIds, params.projectLibrary,
                params.username, params.history, params.suggestion
            ));
    }
    
    /**
     * 导出党风廉政建设责任制审计表到Excel
     */
    @Operation(summary = "导出党风廉政建设责任制审计表到Excel")
    @PostMapping("/exportPartyConductTable")
    public void exportPartyConductTable(@RequestBody Map<String, Object> request, HttpServletResponse response) {
        exportToExcel(request, response, "党风廉政建设责任制审计表", 
            this::convertToPartyConductEntityList, PartyConductExportEntity.class);
    }
    
    // ========== 数据转换方法 ==========
    
    private List<PartyConductExportEntity> convertToPartyConductEntityList(List<Map<String, Object>> originalData) {
        return originalData.stream().map(this::convertToPartyConductEntity).collect(Collectors.toList());
    }
    
    private PartyConductExportEntity convertToPartyConductEntity(Map<String, Object> item) {
        PartyConductExportEntity entity = new PartyConductExportEntity();
        entity.setCategory(getStringValue(item, "category"));
        entity.setSubCategory(getStringValue(item, "subCategory"));
        entity.setDetailCategory(getStringValue(item, "detailCategory"));
        entity.setContent(getStringValue(item, "content"));
        entity.setExecutionStatus(getStringValue(item, "executionStatus"));
        entity.setWorkPaperIndex(formatWorkPaperIndex(item.get("workPaperIndex")));
        return entity;
    }
}