package org.example.mybatisplus.Controller;

import com.alibaba.fastjson2.JSONObject;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.example.mybatisplus.model.auto.CmsEnterpriseRiskInfoCheckBO;
import org.example.mybatisplus.model.auto.CmsEnterpriseRiskInfoDeclaration;
import org.example.mybatisplus.model.auto.CmsEnterpriseRiskQxbInfoBO;
import org.example.mybatisplus.module.AuditResult;
import org.example.mybatisplus.module.BasicInfo;
import org.example.mybatisplus.module.ServiceProviderAuditReport;
import org.example.mybatisplus.module.vo.ServiceProviderInfo;
import org.example.mybatisplus.tools.WordPoiExporter;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;

@ApiOperation("控制类")
@RestController
public class IndexController {
    public static void main(String[] args) {
        String wordFilePath = System.getProperty("user.dir")+File.separator+ "jijinhui.docx";
        String wordExportpath = System.getProperty("user.dir")+File.separator+ "jijinhui1.docx";
        String pdfFilePath =System.getProperty("user.dir")+File.separator+  "download.pdf";

        // todo:
        ServiceProviderInfo serviceProviderInfo=new ServiceProviderInfo();
        serviceProviderInfo.setName("海潮基金会");
        serviceProviderInfo.setType("基金会");
        serviceProviderInfo.setReportDate("2025-03-11");
        AuditResult auditResult=new AuditResult();
        auditResult.setAuditDate("2025-03-11");
        auditResult.setScore("222");
        auditResult.setOpinion("test");
        // auditResult.setMocaRating("test");
        // auditResult.setDonationIncomeScale("test");
        // auditResult.setManagementExpense("test");
        // auditResult.setOperationYears("2025");
        // auditResult.setPublicExpenseGrowth("test");
        BasicInfo basicInfo = new BasicInfo();
        // ...

        // 最后把所有片段合成一个完整报告

        ServiceProviderAuditReport report=new ServiceProviderAuditReport();
        report.setServiceProviderInfo(serviceProviderInfo);
        report.setAuditResult(auditResult);
        report.setBasicInfo(basicInfo);
        try {
            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(report));
            // 生成Word文档
            //WordPoiExporter.init(wordFilePath,wordExportpath,jsonObject);
            // 将Word文档转换为PDF

            WordPoiExporter.convert("G:\\Word\\房屋租赁合同书.docx",pdfFilePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //只要接口中，返回值存在实体类,就会被扫描到swagger中
    @GetMapping("/export")
    public ResponseEntity<Resource> export(@ApiParam("param")String param /* todo: 参数：服务商ID，校验ID，报告类型*/){

        String wordFilePath = System.getProperty("user.dir")+File.separator+ "jijinhui.docx";
        String wordExportpath = System.getProperty("user.dir")+File.separator+ "jijinhui1.docx";
        String pdfFilePath =System.getProperty("user.dir")+File.separator+  "download.pdf";

        // todo:
        ServiceProviderInfo serviceProviderInfo=new ServiceProviderInfo();
        serviceProviderInfo.setName("海潮基金会");
        serviceProviderInfo.setReportDate("2025-03-24");
        // 1. getWordTemplate for BasicInfo
        // 2. retrieve data
        //    2.1 get CmsEnterpriseRiskQxbInfoBO
        CmsEnterpriseRiskQxbInfoBO cmsEnterpriseRiskQxbInfoBO = org.example.mybatisplus.model.auto.CmsEnterpriseRiskQxbInfoBO.builder().build(); // mock
        CmsEnterpriseRiskInfoDeclaration cmsEnterpriseRiskInfoDeclaration = new CmsEnterpriseRiskInfoDeclaration(); // mock
        CmsEnterpriseRiskInfoCheckBO cmsEnterpriseRiskInfoCheckBO = new CmsEnterpriseRiskInfoCheckBO();

        // 3. fill 
        AuditResult auditResult=new AuditResult();
        auditResult.setReportVersion("PLUS版");
        auditResult.setServiceProviderName("上海医药行业协会");
        cmsEnterpriseRiskQxbInfoBO.fillAuditResultVO(auditResult);
        cmsEnterpriseRiskInfoDeclaration.fillAuditResultVO(auditResult);
        cmsEnterpriseRiskInfoCheckBO.fillAuditResultVO(auditResult);


        BasicInfo basicInfo = new BasicInfo();
        cmsEnterpriseRiskQxbInfoBO.fillBasicInfoVO(basicInfo);
        cmsEnterpriseRiskInfoDeclaration.fillBasicInfoVO(basicInfo);
        cmsEnterpriseRiskInfoCheckBO.fillBasicInfoVO(basicInfo);

        // 4. render WordTemplate with BasicInfo

        // ...

        // 最后把所有片段合成一个完整报告

        ServiceProviderAuditReport report=new ServiceProviderAuditReport();
        report.setServiceProviderInfo(serviceProviderInfo);
        report.setBasicInfo(basicInfo);
        report.setAuditResult(auditResult);
        try {
            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(report));
            // 生成Word文档
            WordPoiExporter.init(wordFilePath,wordExportpath,jsonObject);
            // 将Word文档转换为PDF
           WordPoiExporter.convert(wordExportpath, pdfFilePath);
        } catch (Exception e) {
            e.printStackTrace();
        }

        File file = new File(wordExportpath);
        Resource resource = new FileSystemResource(file);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getName());

        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(file.length())
                .contentType(MediaType.APPLICATION_PDF)
                .body(resource);
    }


}
