package com.docus.server.rpc.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.docus.server.rpc.MzZyyLisService;
import com.docus.server.rpc.dto.MzZyyLisExportDto;
import com.docus.server.rpc.dto.MzZyyTestDto;
import com.docus.server.rpc.enums.MzZyyLisType;
import com.docus.server.util.JaxWsDynamicClientUtil;
import com.docus.server.util.TableJsonRead;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/**
 * @author YongBin Wen
 * @date 2025/3/18 9:27
 */
@Service
@Slf4j
public class MzZyyLisServiceImpl implements MzZyyLisService {
    final String configPath = "data-config";
    final String configName = "mzzyy-ws-config";

    @Override
    public List<MzZyyTestDto> getTestFormPdf(String jzh, String inpatientNo, String patientType) {

        TableJsonRead jsonReader = new TableJsonRead();
        JSONObject configOjb = jsonReader.Read(configPath, configName, JSONObject.class);

        JSONObject lisWsConfig = configOjb.getJSONObject("lis");
        String wsUrl = lisWsConfig.getString("url");
        String namespaceUri = lisWsConfig.getString("namespaceUri");
        String method = lisWsConfig.getString("pdfListOperationName");
        String[] params = {jzh, inpatientNo, patientType};



        Object result = JaxWsDynamicClientUtil.send(wsUrl, namespaceUri, method, params);
        String reportDataJsonStr = JSONObject.toJSONString(result);
        JSONObject reportDataObject = JSONObject.parseObject(reportDataJsonStr);
        JSONArray jsonArray = reportDataObject.getJSONArray("string");

        return jsonArray.toJavaList(String.class).stream()
                .map(str ->{
                    String[] split = str.split("\\|");
                    MzZyyTestDto dto = new MzZyyTestDto();
                    dto.setTestDate(split[0]);
                    dto.setMachineId(split[1]);
                    dto.setSampleId(split[2]);
                    dto.setHisItemNameList(split[3]);
                    dto.setType(split[4]);
                    return dto;
                }).sorted(Comparator.comparing(MzZyyTestDto::getTestDate)).collect(Collectors.toList());
    }



    @Override
    public List<MzZyyLisExportDto> exportTestFormPdf(MzZyyLisType type, String machineId, String sampleId, String testdate) {
        TableJsonRead jsonReader = new TableJsonRead();
        JSONObject configOjb = jsonReader.Read(configPath, configName, JSONObject.class);
        JSONObject lisWsConfig = configOjb.getJSONObject("lis");
        String wsUrl = lisWsConfig.getString("url");
        String namespaceUri = lisWsConfig.getString("namespaceUri");
        String method = null;
        if (type == MzZyyLisType.GM) {
            method = lisWsConfig.getString("GMBase64OperationName");
        } else if (type == MzZyyLisType.RT) {
            method = lisWsConfig.getString("RTBase64OperationName");
        } else {
            return null;
        }
        String[] params = {machineId, sampleId, testdate};
        Object resultObj = JaxWsDynamicClientUtil.send(wsUrl, namespaceUri, method, params);
        String result = String.valueOf(resultObj);
        return   JSONObject.parseArray(result,MzZyyLisExportDto.class);
    }
}
