package com.docus.server.archive.controller;

import com.docus.infrastructure.core.exception.BaseException;
import com.docus.infrastructure.web.api.ResultCode;
import com.docus.server.archive.dto.Message;
import com.docus.server.archive.dto.MessageResponse;
import com.docus.server.archive.service.FoShanThreeHospDeptInfoSyncService;
import com.docus.server.archive.service.FoShanThreeHospUserInfoSyncService;
import com.docus.server.archive.utils.XmlUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.Node;

/**
 * @author YongBin Wen
 * @date 2024/7/10 9:35
 */
@Slf4j
@RequestMapping("/fssy-basic-receive")
@Api(tags = "佛山三院基础信息统一消息转发接收接口")
@RestController
public class FoShanThreeHospUnifyMessageReceiveController {


    @Autowired
    private FoShanThreeHospUserInfoSyncService userInfoSyncService;

    @Autowired
    private FoShanThreeHospDeptInfoSyncService deptInfoSyncService;


    @PostMapping("/user/do")
    @ApiOperation("佛山三院用户信息推送接收")
    public MessageResponse userSyncByUnifyMessageReceive(@RequestBody Message message) {
        log.info("佛山三院用户信息推送接收：{}", message);
        String source = "unknown";
        String msgId = "unknown";
        try {
            XmlUtil xmlUtil = XmlUtil.of(message.getMessage());
            Node sourceNode = xmlUtil.getNode("/Request/Header/SourceSystem");
            Node msgIdNode = xmlUtil.getNode("/Request/Header/MessageID");
            if (sourceNode != null) {
                source = sourceNode.getTextContent();
            }
            if (msgIdNode != null) {
                msgId = msgIdNode.getTextContent();
            }
            userInfoSyncService.syncByUnifyMessageReceive(message.getMessage());
            return new MessageResponse(ResultCode.SUCCESS.getCode(), userSuccess(source, msgId));
        } catch (BaseException baseException) {
            log.error(baseException.getMessage(), baseException);
            // 业务异常，不希望重试，直接消息返回错误
            MessageResponse response = new MessageResponse(ResultCode.FAILED.getCode(), userFail(source, msgId, baseException.getMessage()));
            response.setRetry(0);
            return response;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            // 不可知的错误，希望重试
            MessageResponse response = new MessageResponse(ResultCode.FAILED.getCode(), userFail(source, msgId, "系统错误"));
            response.setRetry(1);
            return response;
        }
    }


    @PostMapping("/dept/do")
    @ApiOperation("佛山三院科室信息推送接收")
    public MessageResponse deptSyncByUnifyMessageReceive(@RequestBody Message message) {
        log.info("佛山三院科室信息推送接收：{}", message);
        String source = "unknown";
        String msgId = "unknown";
        try {
            XmlUtil xmlUtil = XmlUtil.of(message.getMessage());
            Node sourceNode = xmlUtil.getNode("/Request/Header/SourceSystem");
            Node msgIdNode = xmlUtil.getNode("/Request/Header/MessageID");
            if (sourceNode != null) {
                source = sourceNode.getTextContent();
            }
            if (msgIdNode != null) {
                msgId = msgIdNode.getTextContent();
            }
            deptInfoSyncService.syncByUnifyMessageReceive(message.getMessage());
            return new MessageResponse(ResultCode.SUCCESS.getCode(), deptSuccess(source, msgId));
        } catch (BaseException baseException) {
            log.error(baseException.getMessage(), baseException);
            // 业务异常，不希望重试，直接消息返回错误
            MessageResponse response = new MessageResponse(ResultCode.FAILED.getCode(), deptFail(source, msgId, baseException.getMessage()));
            response.setRetry(0);
            return response;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            // 不可知的错误，希望重试
            MessageResponse response = new MessageResponse(ResultCode.FAILED.getCode(), deptFail(source, msgId, "系统错误"));
            response.setRetry(1);
            return response;
        }
    }


    private String userSuccess(String source, String msgId) {
        return "<Response>\n" +
                "    <Header>\n" +
                "        <SourceSystem>" + source + "</SourceSystem>\n" +
                "        <MessageID>" + msgId + "</MessageID>\n" +
                "    </Header>\n" +
                "    <Body>\n" +
                "        <ResultCode>AA</ResultCode>\n" +
                "        <ResultContent>成功</ResultContent>\n" +
                "    </Body>\n" +
                "</Response>";

    }

    private String userFail(String source, String msgId, String message) {
        return "<Response>\n" +
                "    <Header>\n" +
                "        <SourceSystem>" + source + "</SourceSystem>\n" +
                "        <MessageID>" + msgId + "</MessageID>\n" +
                "    </Header>\n" +
                "    <Body>\n" +
                "        <ResultCode>AE</ResultCode>\n" +
                "        <ResultContent>" + message + "</ResultContent>\n" +
                "    </Body>\n" +
                "</Response>";
    }

    private String deptSuccess(String source, String msgId) {
        return "<Response>\n" +
                "    <Header>\n" +
                "        <SourceSystem>" + source + "</SourceSystem>\n" +
                "        <MessageID>" + msgId + "</MessageID>\n" +
                "    </Header>\n" +
                "    <Body>\n" +
                "        <ResultCode>AA</ResultCode>\n" +
                "        <ResultContent>成功</ResultContent>\n" +
                "    </Body>\n" +
                "</Response>";
    }

    private String deptFail(String source, String msgId, String message) {
        return "<Response>\n" +
                "    <Header>\n" +
                "        <SourceSystem>" + source + "</SourceSystem>\n" +
                "        <MessageID>" + msgId + "</MessageID>\n" +
                "    </Header>\n" +
                "    <Body>\n" +
                "        <ResultCode>AE</ResultCode>\n" +
                "        <ResultContent>" + message + "</ResultContent>\n" +
                "    </Body>\n" +
                "</Response>";
    }

}
