package com.gxwebsoft.ai.controller;

import com.alibaba.fastjson.JSONObject;
import com.gxwebsoft.ai.dto.AuditContentRequest;
import com.gxwebsoft.ai.dto.export.StateAssetsAuditExportEntity;
import com.gxwebsoft.ai.service.AuditContent6StateAssetsService;
import com.gxwebsoft.ai.utils.ExcelExportTool;
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;

/**
 * 审计内容6控制器 - 国资管理情况审计
 */
@Slf4j
@Tag(name = "审计内容6-国资管理情况")
@RestController
@RequestMapping("/api/ai/auditContent6")
public class AuditContent6Controller extends BaseAuditContentController {
    
    @Autowired
    private AuditContent6StateAssetsService auditContent6StateAssetsService;
    
    /**
     * 生成国有资产管理审计表数据
     */
    @Operation(summary = "生成国有资产管理审计表")
    @PostMapping("/generateAssetsTable")
    public ApiResult<?> generateAssetsTable(@RequestBody AuditContentRequest request, HttpServletRequest servletRequest) {
        return generateTableData(request, servletRequest.getRequestURI(), 
            (params) -> auditContent6StateAssetsService.generateStateAssetsAuditTableData(
                params.knowledgeBaseId, params.libraryKbIds, params.projectLibrary,
                params.username, params.history, params.suggestion
            ));
    }
    
    /**
     * 导出国有资产管理审计表到Excel
     */
    @Operation(summary = "导出国有资产管理审计表到Excel")
    @PostMapping("/exportAssetsTable")
    public void exportAssetsTable(@RequestBody Map<String, Object> request, HttpServletResponse response) {
        List<Map<String, Object>> dataList = (List<Map<String, Object>>) request.get("data");
        String companyName = (String) request.get("companyName");
        
        List<StateAssetsAuditExportEntity> exportData = convertToExportEntityList(dataList);
        
        String fileName = "国有资产管理审计表_" + (companyName != null ? companyName : "未知公司");
        String title = companyName != null ? companyName + " - 国有资产管理审计表" : "国有资产管理审计表";
        String sheetName = "国资管理审计";
        
        ExcelExportTool.exportExcel(exportData, StateAssetsAuditExportEntity.class, fileName, sheetName, title, response);
    }
    
    /**
     * 数据转换
     */
    private List<StateAssetsAuditExportEntity> convertToExportEntityList(List<Map<String, Object>> originalData) {
        return originalData.stream().map(this::convertToExportEntity).collect(Collectors.toList());
    }
    
    private StateAssetsAuditExportEntity convertToExportEntity(Map<String, Object> item) {
        StateAssetsAuditExportEntity entity = new StateAssetsAuditExportEntity();
        entity.setIndex(getStringValue(item, "index"));
        entity.setAssetName(getStringValue(item, "assetName"));
        entity.setAcquisitionMethod(getStringValue(item, "acquisitionMethod"));
        entity.setAssetValue(getStringValue(item, "assetValue"));
        entity.setAssetAddress(getStringValue(item, "assetAddress"));
        entity.setArea(getStringValue(item, "area"));
        entity.setTenant(getStringValue(item, "tenant"));
        entity.setContractAmount(getStringValue(item, "contractAmount"));
        entity.setLeasePeriod(getStringValue(item, "leasePeriod"));
        entity.setRentPaymentTime(getStringValue(item, "rentPaymentTime"));
        entity.setPlatformLease(getStringValue(item, "platformLease"));
        entity.setApprovalDoc(getStringValue(item, "approvalDoc"));
        entity.setInBudget(getStringValue(item, "inBudget"));
        entity.setRemark(getStringValue(item, "remark"));
        entity.setWorkPaperIndex(formatWorkPaperIndex(item.get("workPaperIndex")));
        return entity;
    }
}