package cn.com.poc.equity.processor;

import cn.com.poc.common.utils.JsonUtils;
import cn.com.poc.equity.aggregate.MemberEquityService;
import cn.com.poc.equity.constants.EquityEnum;
import cn.com.poc.equity.constants.ModifyEventEnum;
import cn.com.poc.equity.domain.modifyEquityInfo.PayModifyEventInfo;
import cn.com.poc.equity.entity.BizMemberEquityPayOrderLogEntity;
import cn.com.poc.equity.entity.BizPaymentPackageConfigurationEntity;
import cn.com.poc.equity.service.BizMemberEquityPayOrderLogService;
import cn.com.poc.thirdparty.resource.demand.pay.constants.DgtoolsPayOrderConstants;
import cn.com.poc.thirdparty.resource.demand.pay.entity.DgtoolsPayOrderEntity;
import cn.com.poc.thirdparty.resource.demand.pay.processor.AbstractPayPostProcessor;
import cn.com.poc.thirdparty.resource.demand.pay.service.DgtoolsPayOrderService;
import cn.com.yict.framemax.core.exception.BusinessException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author alex.yao
 * @date 2024/12/9
 */
@Component
public class PointPayPostProcessor implements AbstractPayPostProcessor {

    @Resource
    private MemberEquityService memberEquityService;

    @Resource
    private BizMemberEquityPayOrderLogService bizMemberEquityPayOrderLogService;

    @Resource
    private DgtoolsPayOrderService dgtoolsPayOrderService;

    @Override
    public boolean support(String payOrderType) {
        return DgtoolsPayOrderConstants.PayOrderType.POINT.equals(payOrderType);
    }

    @Override
    public boolean postProcessSuccess(String orderSn) {
        try {
            PayModifyEventInfo modifyEventInfo = new PayModifyEventInfo();
            modifyEventInfo.setPayOrderSn(orderSn);
            DgtoolsPayOrderEntity payOrderEntity = dgtoolsPayOrderService.findPayOrderByPaySn(orderSn);
            if (payOrderEntity == null) {
                throw new BusinessException("支付回调,找不到账单信息");
            }
            if (!DgtoolsPayOrderConstants.PayStatus.PAID.equals(payOrderEntity.getPayStatus())) {
                throw new BusinessException("支付回调,账单未支付");
            }
            String retainInformation = payOrderEntity.getRetainInformation();
            Integer memberId = payOrderEntity.getMemberId();
            BizPaymentPackageConfigurationEntity bizPaymentPackageConfigurationEntity = JsonUtils.deSerialize(retainInformation, BizPaymentPackageConfigurationEntity.class);
            String type = bizPaymentPackageConfigurationEntity.getType();
            String domain = bizPaymentPackageConfigurationEntity.getDomain();
            String expiredType = bizPaymentPackageConfigurationEntity.getExpiredType();

            // 以小数点后两位为单位，添加积分
            long pointNum = bizPaymentPackageConfigurationEntity.getResourceCount().longValue() * 100;
            memberEquityService.addPoint(memberId.longValue(), pointNum, EquityEnum.VALIDITY_UNIT.valueOf(expiredType), ModifyEventEnum.top_up, modifyEventInfo);
            //记录支付日志
            BizMemberEquityPayOrderLogEntity bizMemberEquityPayOrderLogEntity = new BizMemberEquityPayOrderLogEntity();
            bizMemberEquityPayOrderLogEntity.setMemberId(memberId.longValue());
            bizMemberEquityPayOrderLogEntity.setPayOrderSn(payOrderEntity.getPayOrderSn());
            bizMemberEquityPayOrderLogEntity.setPayAmount(payOrderEntity.getPaidAmount());
            bizMemberEquityPayOrderLogEntity.setCurrency(payOrderEntity.getCurrency());
            bizMemberEquityPayOrderLogEntity.setType(type);
            bizMemberEquityPayOrderLogEntity.setDomain(domain);
            bizMemberEquityPayOrderLogEntity.setExpiredNum(bizPaymentPackageConfigurationEntity.getExpiredNum());
            bizMemberEquityPayOrderLogEntity.setExpiredType(expiredType);
            bizMemberEquityPayOrderLogEntity.setTotalPoints(pointNum);
            bizMemberEquityPayOrderLogEntity.setPayChannel(payOrderEntity.getPayChannel());
            bizMemberEquityPayOrderLogEntity.setPayTime(payOrderEntity.getPayTime());
            bizMemberEquityPayOrderLogService.save(bizMemberEquityPayOrderLogEntity);
            return true;
        } catch (Exception e) {
            //todo 异常处理
            return false;
        }
    }

    @Override
    public void postProcessFail(String orderSn) {
        throw new BusinessException("暂不支持");
    }
}
