package com.docus.server.reportmanager.webservice;

import com.docus.core.util.Func;
import com.docus.infrastructure.core.exception.BaseException;
import com.docus.infrastructure.core.utils.SpringUtil;
import com.docus.server.reportmanager.entity.AfCollectTask;
import com.docus.server.reportmanager.mapper.CollectTaskMapper;
import com.docus.server.reportmanager.mapper.ScanAssortMapper;
import com.docus.server.reportmanager.util.XmlUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.w3c.dom.Node;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 省中医报告管理服务接口
 *
 * @author YongBin Wen
 * @date 2024/1/3 16:11
 */
@Service("SzyReportManagerService")
@Slf4j
public class SzyReportManagerServiceImpl implements ReportManagerService {
    @Resource
    private CollectTaskMapper collectTaskMapper;
    @Resource
    private ScanAssortMapper scanAssortMapper;

    @Override
    public String DeleteReport(String message) {
        log.info("广东省中医报告管理，收到作废报告的信息：{}", message);
        try {
            XmlUtil xmlUtil = XmlUtil.of(message);
            Node reportNoNode = xmlUtil.getNode("/request/REPORT_NO");
            String reportNo = reportNoNode.getTextContent();
            // todo 此方法暂时只做电生理 的作废，
            cancelElectrophysiologyReport(reportNo);
            return success();
        } catch (BaseException baseException) {
            log.error(baseException.getMessage(), baseException);
            return error(baseException.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return error("系统内部错误，请联系开发人员");
        }
    }

    /**
     * 作废电生理的报告，电生理采集来源id为 13
     *
     * @param reportNo 报告唯一号
     * @date 2024/1/3 17:19
     * @author YongBin Wen
     */
    private void cancelElectrophysiologyReport(String reportNo) {
        final String sysFlag = "13";
        AfCollectTask task = new AfCollectTask();
        task.setC1(reportNo);
        task.setSysflag(sysFlag);
        List<AfCollectTask> tasks = collectTaskMapper.getTasks(task);
        if (Func.isEmpty(tasks)) {
            throw new BaseException("无纸化系统没有可作废的电生理报告任务:" + reportNo);
        }
        ArrayList<Long> taskIds = new ArrayList<>();
        ArrayList<Long> scanAssortIds = new ArrayList<>();
        for (AfCollectTask collectTask : tasks) {
            taskIds.add(collectTask.getId());
            List<Long> scanAssortIdsByTask = scanAssortMapper.getIdByPatientTask(collectTask.getPatientId(), collectTask.getId());
            scanAssortIds.addAll(scanAssortIdsByTask);
        }
        SzyReportManagerServiceImpl service = SpringUtil.getBean(SzyReportManagerServiceImpl.class);
        service.cancelReportTaskAndFile(taskIds, scanAssortIds);
    }


    @Transactional(rollbackFor = Exception.class)
    public void cancelReportTaskAndFile(List<Long> taskIds, List<Long> scanAssortIds) {
        if (Func.isNotEmpty(taskIds)) {
            collectTaskMapper.cancel(taskIds);
        }
        if (Func.isNotEmpty(scanAssortIds)) {
            scanAssortMapper.cancel(scanAssortIds);
        }
    }

    private String error(String msg) {
        return "<Response>\n" +
                "    <RetInfo>\n" +
                "        <RetCode>1</RetCode>\n" +
                "        <RetCon>" + msg + "</RetCon>\n" +
                "    </RetInfo>\n" +
                "</Response>";
    }

    private String success() {
        return "<Response>\n" +
                "    <RetInfo>\n" +
                "        <RetCode>0</RetCode>\n" +
                "        <RetCon>成功</RetCon>\n" +
                "    </RetInfo>\n" +
                "</Response>";
    }
}
