package com.docus.server.gdszyzh.controller;

import cn.hutool.core.io.FileUtil;
import com.docus.infrastructure.web.api.CommonResult;
import com.docus.server.archive.mapper.TBasicMapper;
import com.docus.server.gdszyzh.entity.GdSzyZhReportDataView;
import com.docus.server.gdszyzh.mapper.GdSzyZhEcgReportDataViewMapper;
import com.docus.server.gdszyzh.mapper.GdSzyZhIcuReportDataViewMapper;
import com.docus.server.gdszyzh.mapper.GdSzyZhShouMaReportDataViewMapper;
import com.docus.server.gdszyzh.util.PrintConfig;
import com.docus.server.gdszyzh.util.WebPagePrinter;
import com.docus.server.rpc.GdSzyZhReportService;
import com.docus.server.rpc.dto.GdSzyZhReportDetailDto;
import com.docus.server.rpc.dto.GdSzyZhReportListDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @author YongBin Wen
 * @date 2026/4/7 16:35
 */
@RestController
@RequestMapping("/gdszyzhtest")
@Api(tags = "广东省中医珠海测试接口")
public class TestController {
    @Autowired
    private GdSzyZhIcuReportDataViewMapper icuReportDataViewMapper;
    @Autowired
    private GdSzyZhEcgReportDataViewMapper ecgReportDataViewMapper;
    @Autowired
    private GdSzyZhShouMaReportDataViewMapper shouMaReportDataViewMapper;
    @Autowired
    private TBasicMapper tBasicMapper;
    @Autowired
    private GdSzyZhReportService gdSzyZhReportService;

    @GetMapping("/shouma/get")
    @ApiOperation("手麻视图测试数据")
    public CommonResult<List<GdSzyZhReportDataView>> shoumaTest(@RequestParam("jzh") String jzh) {
        List<GdSzyZhReportDataView> byJzhs = shouMaReportDataViewMapper.getByJzhs(Collections.singletonList(jzh));
        return CommonResult.success(byJzhs);
    }

    @GetMapping("/ecg/get")
    @ApiOperation("ecg视图测试数据")
    public CommonResult<List<GdSzyZhReportDataView>> ecgTest(@RequestParam("jzh") String jzh) {
        List<GdSzyZhReportDataView> byJzhs = ecgReportDataViewMapper.getByJzhs(Collections.singletonList(jzh));
        return CommonResult.success(byJzhs);
    }

    @GetMapping("/icu/get")
    @ApiOperation("重症视图测试数据")
    public CommonResult<List<GdSzyZhReportDataView>> icuTest(@RequestParam("zyh") String inpatientNo, @RequestParam("zycs") Integer zycs) {
        List<GdSzyZhReportDataView> dataViewList = icuReportDataViewMapper.getByInpatientNo(inpatientNo);
        dataViewList= dataViewList.stream()
                .filter(e -> e.getAdmissTimes().equals(zycs))
                .collect(Collectors.toList());
        return CommonResult.success(dataViewList);
    }


    @GetMapping("/pacsList/get")
    @ApiOperation("PACS检查列表接口测试 时间格式yyyy-MM-dd")
    public CommonResult<GdSzyZhReportListDto> pacsListTest(@RequestParam("empId") String empId, @RequestParam("jzh") String jzh,
                                                           @RequestParam("beginDate") String beginDate, @RequestParam("endDate") String endDate) {
        GdSzyZhReportListDto dto = gdSzyZhReportService.pacsList(empId, jzh, beginDate, endDate);
        return CommonResult.success(dto);
    }

    @GetMapping("/pacsDetail/get")
    @ApiOperation("PACS检查报告明细接口测试")
    public CommonResult<GdSzyZhReportDetailDto> pacsDetailTest(@RequestParam("examNo") String examNo) {
        GdSzyZhReportDetailDto pacsDetail = gdSzyZhReportService.pacsDetail(examNo);
        return CommonResult.success(pacsDetail);
    }

    @GetMapping("/lisList/get")
    @ApiOperation("LIS检验列表接口测试 时间格式yyyy-MM-dd HH:mm:ss")
    public CommonResult<GdSzyZhReportListDto> lisListTest(@RequestParam("empId") String empId, @RequestParam("jzh") String jzh,
                                                          @RequestParam("beginDate") String beginDate, @RequestParam("endDate") String endDate) {
        GdSzyZhReportListDto dto = gdSzyZhReportService.lisList(empId, jzh, beginDate, endDate);
        return CommonResult.success(dto);
    }


    @GetMapping("/html/print")
    @ApiOperation("html打印测试")
    public CommonResult<String> icuPrintTest(@RequestParam(value = "url") String url,
                                             @RequestParam(value = "landscape", required = false) Boolean landscape,
                                             @RequestParam(value = "displayHeaderFooter", required = false) Boolean displayHeaderFooter,
                                             @RequestParam(value = "printBackground", required = false) Boolean printBackground,
                                             @RequestParam(value = "scale", required = false) Double scale,
                                             @RequestParam(value = "paperWidth", required = false) Double paperWidth,
                                             @RequestParam(value = "paperHeight", required = false) Double paperHeight,
                                             @RequestParam(value = "marginTop", required = false) Double marginTop,
                                             @RequestParam(value = "marginBottom", required = false) Double marginBottom,
                                             @RequestParam(value = "marginLeft", required = false) Double marginLeft,
                                             @RequestParam(value = "marginRight", required = false) Double marginRight,
                                             @RequestParam(value = "pageRanges", required = false) String pageRanges,
                                             @RequestParam(value = "ignoreInvalidPageRanges", required = false) Boolean ignoreInvalidPageRanges,
                                             @RequestParam(value = "headerTemplate", required = false) String headerTemplate,
                                             @RequestParam(value = "footerTemplate", required = false) String footerTemplate,
                                             @RequestParam(value = "preferCSSPageSize", required = false) Boolean preferCSSPageSize
    ) {
        try (WebPagePrinter printer = new WebPagePrinter()) {
            printer.init();
            System.out.println(url);
            PrintConfig config = new PrintConfig();


            byte[] pdfBytes = printer.printToPdf(url, config, landscape, displayHeaderFooter, printBackground, scale,
                    paperWidth, paperHeight, marginTop, marginBottom, marginLeft, marginRight, pageRanges,
                    ignoreInvalidPageRanges, headerTemplate, footerTemplate, preferCSSPageSize);
            String pdfPath = "urlPrintToPdf.pdf";
            FileUtil.writeBytes(pdfBytes, pdfPath);
            System.out.println("PDF已生成: " + pdfPath);
            return CommonResult.success("成功");

        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("失败");
        }
    }
}
