package com.docus.server.report.webservice.impl;

import com.docus.core.util.DateUtil;
import com.docus.core.util.Func;
import com.docus.infrastructure.core.exception.BaseException;
import com.docus.server.report.converter.impl.HIP1264ReceiveMessageConverter;
import com.docus.server.report.converter.MaternalInfantRelationshipConverter;
import com.docus.server.report.dto.MaternalInfantRelationshipDTO;
import com.docus.server.report.entity.MaternalInfantRelationship;
import com.docus.server.report.mapper.MaternalInfantRelationshipMapper;
import com.docus.server.report.util.IdUtil;
import com.docus.server.report.webservice.WebserviceMessageType;
import com.docus.server.report.webservice.WebserviceReceiveServerHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * HIP1264-母婴关联关系推送
 *
 * @author wyb
 */
@Service(WebserviceMessageType.HIP1264)
@Slf4j
public class HIP1264MaternalInfantRelationshipHandler implements WebserviceReceiveServerHandler {
    @Resource
    private MaternalInfantRelationshipMapper maternalInfantRelationshipMapper;

    @Override
    public String handle(String receiveMessage) {
        log.info("收到母婴关系消息：{}", receiveMessage);
        MaternalInfantRelationshipDTO dto = new MaternalInfantRelationshipDTO();
        try {
            HIP1264ReceiveMessageConverter converter = new HIP1264ReceiveMessageConverter(receiveMessage);
            // 转换dto，验证dto，转换实体，保存
            dto = converter.xml2dto();
            verifyDto(dto);
            MaternalInfantRelationshipConverter maternalInfantRelationshipConverter = new MaternalInfantRelationshipConverter(dto);
            MaternalInfantRelationship maternalInfantRelationship = maternalInfantRelationshipConverter.dto2po();
            maternalInfantRelationshipMapper.saveOrUpdate(maternalInfantRelationship);
            return insertSuccess(dto.getMsgId());
        } catch (BaseException baseException) {
            log.error(baseException.getMessage(), baseException);
            return insertFailed(dto.getMsgId(), baseException.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return insertFailed(dto.getMsgId(), "系统错误！");
        }
    }

    private void verifyDto(MaternalInfantRelationshipDTO dto) {
        if (Func.isBlank(dto.getBabyId())) {
            throw new BaseException("新生儿住院id不能为空！");
        }
        if (Func.isBlank(dto.getMomId())) {
            throw new BaseException("母亲住院id不能为空！");
        }
    }

    /**
     * 返回母婴关系保存成功响应消息
     *
     * @param msgId 消息id
     */
    public String insertSuccess(String msgId) {

        return "<MCCI_IN000002UV01 xmlns=\"urn:hl7-org:v3\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ITSVersion=\"XML_1.0\" xsi:schemaLocation=\"urn:hl7-org:v3  file:///E:/hl7/HL7/v3ballot_fullsite_2011MAY/v3ballot/html/processable/multicacheschemas/MCCI _IN000002UV01.xsd\">" + "<id extension=\"" + IdUtil.standardUUID() + "\"/>" + "<creationTime value=\"" + Func.format(new Date(), DateUtil.PATTERN_DATETIME_MINI) + "\"/>" + "<interactionId root=\"2.16.840.1.113883.1.6\" extension=\"MCCI_IN000002UV01\"/>" + "<processingCode code=\"P\"/>" + "<processingModeCode/>" + "<acceptAckCode code=\"AL\"/>" + "<receiver typeCode=\"RCV\">" + "<device classCode=\"DEV\" determinerCode=\"INSTANCE\">" + "<id>" + "<item extension=\"Orion-001\"/>" + "</id>" + "</device>" + "</receiver>" + "<sender typeCode=\"SND\">" + "<device classCode=\"DEV\" determinerCode=\"INSTANCE\">" + "<id>" + "<item extension=\"PaperlessManagementMRIS-001\"/>" + "</id>" + "</device>" + "</sender>" + "<acknowledgement typeCode=\"AA\">" + "<targetMessage>" + "<id extension=\"" + msgId + "\"/>" + "</targetMessage>" + "<acknowledgementDetail>" + "<text value=\"成功\"/>" + "</acknowledgementDetail>" + "</acknowledgement>" + "</MCCI_IN000002UV01>";
    }

    /**
     * 返回母婴关系保存失败响应消息
     *
     * @param msgId 消息id
     */
    public String insertFailed(String msgId, String failedMessage) {
        return "<MCCI_IN000002UV01 xmlns=\"urn:hl7-org:v3\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ITSVersion=\"XML_1.0\" xsi:schemaLocation=\"urn:hl7-org:v3  file:///E:/hl7/HL7/v3ballot_fullsite_2011MAY/v3ballot/html/processable/multicacheschemas/MCCI _IN000002UV01.xsd\">" + "<id extension=\"" + IdUtil.standardUUID() + "\"/>" + "<creationTime value=\"" + Func.format(new Date(), DateUtil.PATTERN_DATETIME_MINI) + "\"/>" + "<interactionId root=\"2.16.840.1.113883.1.6\" extension=\"MCCI_IN000002UV01\"/>" + "<processingCode code=\"P\"/>" + "<processingModeCode/>" + "<acceptAckCode code=\"AL\"/>" + "<receiver typeCode=\"RCV\">" + "<device classCode=\"DEV\" determinerCode=\"INSTANCE\">" + "<id>" + "<item extension=\"Orion-001\"/>" + "</id>" + "</device>" + "</receiver>" + "<sender typeCode=\"SND\">" + "<device classCode=\"DEV\" determinerCode=\"INSTANCE\">" + "<id>" + "<item extension=\"PaperlessManagementMRIS-001\"/>" + "</id>" + "</device>" + "</sender>" + "<acknowledgement typeCode=\"AE\">" + "<targetMessage>" + "<id extension=\"" + msgId + "\"/>" + "</targetMessage>" + "<acknowledgementDetail>" + "<text value=\"" + failedMessage + "\"/>" + "</acknowledgementDetail>" + "</acknowledgement>" + "</MCCI_IN000002UV01>";
    }

}
