package com.cusc.nirvana.user.rnr.fp.service.impl;

import com.alibaba.fastjson.JSON;
import com.cusc.nirvana.common.result.Response;
import com.cusc.nirvana.user.rnr.fp.common.ResponseCode;
import com.cusc.nirvana.user.rnr.fp.dto.FpRnrSmsInfoDTO;
import com.cusc.nirvana.user.rnr.fp.dto.UnbindReceiceSMSDTO;
import com.cusc.nirvana.user.rnr.fp.handler.MessageCallBackContext;
import com.cusc.nirvana.user.rnr.fp.handler.MessageCallbackHandler;
import com.cusc.nirvana.user.rnr.fp.service.IFpCardUnBindService;
import com.cusc.nirvana.user.rnr.fp.service.IMessageCallBackService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author yubo
 * @since 2022-05-06 16:36
 */
@Service
@Slf4j
public class MessageCallBackServiceImpl implements IMessageCallBackService {

    @Autowired
    IFpCardUnBindService cardUnBindService;

    @Autowired
    private MessageCallBackContext messageCallBackContext;

    @Override
    public Response callBack(UnbindReceiceSMSDTO bean) {
        log.info("短信平台回调入参: {}", JSON.toJSONString(bean));
        long receiveTIme = System.currentTimeMillis();
        //获取当前时间  校验时间合法  校验短信内容
        FpRnrSmsInfoDTO smsInfoDTO = new FpRnrSmsInfoDTO();
        smsInfoDTO.setSendPhone(bean.getPhoneNumber());
        smsInfoDTO.setSendSmsId(bean.getMessageId());

        Response response = this.checkMessageStatus(smsInfoDTO);
        if (!response.isSuccess()) {
            return Response.createError(response.getMsg(), response.getData());
        }
        //存储短信信息
        Response<FpRnrSmsInfoDTO> fpRnrSmsInfoDTOResponse = cardUnBindService.receiveMessage(bean);
        if (!fpRnrSmsInfoDTOResponse.isSuccess() || fpRnrSmsInfoDTOResponse.getData() == null) {
            return Response.createError("短信回调失败", response.getData());
        }
        FpRnrSmsInfoDTO rnrSmsInfoDTOResponseData = fpRnrSmsInfoDTOResponse.getData();

        MessageCallbackHandler workBackHandler = messageCallBackContext.getWorkBackHandler(rnrSmsInfoDTOResponseData.getBizType());
        if (workBackHandler != null) {
            return workBackHandler.callBack(rnrSmsInfoDTOResponseData, bean, receiveTIme);
        } else {
            log.error("未找到指定的消息回调处理类{}", rnrSmsInfoDTOResponseData.getBizType());
            return Response.createError("未找到指定的消息回调处理类");
        }
    }

    //校验短信状态
    private Response checkMessageStatus(FpRnrSmsInfoDTO bean) {
        log.info("自然人解绑校验短信入参: {}", JSON.toJSONString(bean));

        Response<FpRnrSmsInfoDTO> messageStatus = cardUnBindService.getMessageStatus(bean);
        if (!messageStatus.isSuccess()) {
            return Response.createError(messageStatus.getMsg(), messageStatus.getCode());
        }
        if (messageStatus.getData() == null) {
            return Response.createError("获取上行短信消息失败", ResponseCode.INVALID_DATA.getCode());
        }
        if (StringUtils.isNotBlank(messageStatus.getData().getReceiveContent())) {
            return Response.createError("上行短信消息已处理", ResponseCode.INVALID_DATA.getCode());
        }
        return Response.createSuccess("短信状态有效");
    }
}
