package com.yc.ship.module.trade.service.supplier.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.module.trade.dal.dataobject.order.TradeOrderDO;
import com.yc.ship.module.trade.dal.dataobject.supplier.SupplierRefundDO;
import com.yc.ship.module.trade.dal.dataobject.supplier.TradeSupplierOrderDO;
import com.yc.ship.module.trade.enums.RefundStatusEnum;
import com.yc.ship.module.trade.enums.TradeOrderOperateTypeEnum;
import com.yc.ship.module.trade.enums.TradeOrderStatusEnum;
import com.yc.ship.module.trade.framework.annotation.TradeOrderLog;
import com.yc.ship.module.trade.framework.common.TradeOrderLogUtils;
import com.yc.ship.module.trade.framework.mq.TradePublishUtils;
import com.yc.ship.module.trade.service.order.TradeOrderRepositoryService;
import com.yc.ship.module.trade.service.refund.TradeRefundRepositoryService;
import com.yc.ship.module.trade.service.refund.bo.TradeRefundBO;
import com.yc.ship.module.trade.service.supplier.TradeSupplierCallbackService;
import com.yc.ship.module.trade.service.supplier.TradeSupplierOrderRepositoryService;
import com.yc.ship.module.trade.service.supplier.bo.SupplierRefundRespBO;
import com.yc.ship.module.trade.service.supplier.bo.SupplierTradeOrderPayRespBO;
import com.yc.ship.module.trade.service.supplier.bo.SupplierTradeOrderRespBO;
import com.yc.ship.module.trade.service.supplier.bo.TradeSupplierOrderBO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Objects;

import static com.yc.ship.module.trade.enums.ErrorCodeConstants.REFUND_AUDITING;


/**
 * &#064;Description ：供应商下单回调service
 * &#064;Author ：qsl
 * &#064;Date ：2025/6/7 11:43
 */
@Service
@Slf4j
public class TradeSupplierCallbackServiceImpl implements TradeSupplierCallbackService {

    @Resource
    private TradeOrderRepositoryService tradeOrderRepositoryService;
    @Resource
    private TradeSupplierOrderRepositoryService tradeSupplierOrderRepositoryService;
    @Resource
    private TradePublishUtils tradePublishUtils;
    @Resource
    private TradeRefundRepositoryService tradeRefundRepositoryService;


    @Override
    public void afterAllOrderCreate(TradeOrderDO tradeOrderDO) {
        //更新订单状态
//        if(!Objects.equals(tradeOrderDO.getOrderStatus(), TradeOrderStatusEnum.YD.getStatus()) && !Objects.equals(tradeOrderDO.getOrderStatus(), TradeOrderStatusEnum.TRANSFER.getStatus())) {
//            tradeOrderDO.setOrderStatus(TradeOrderStatusEnum.UNPAID.getStatus());
//            tradeOrderRepositoryService.updateOrderStatus(tradeOrderDO);
//        }
    }

    @Override
    public void afterOrderCreate(SupplierTradeOrderRespBO respBO, TradeSupplierOrderBO tradeSupplierOrderBO) {
        //更新订单状态,供应商订单
        TradeSupplierOrderDO tradeSupplierOrderDO = tradeSupplierOrderBO.getTradeSupplierOrderDO();
        tradeSupplierOrderDO.setOrderStatus(TradeOrderStatusEnum.UNPAID.getStatus());
        //有些供应商无下单逻辑，只有支付逻辑，此处可能无订单返回
        if(respBO!=null && StrUtil.isNotBlank(respBO.getSupplierOrderNo())) {
            tradeSupplierOrderDO.setSupplierOrderNo(respBO.getSupplierOrderNo());
        }else{
            tradeSupplierOrderDO.setSupplierOrderNo("");
        }
        tradeSupplierOrderRepositoryService.updateAfterOrderSuccess(tradeSupplierOrderDO);
    }

    @Override
    public void afterOrderCreateFail(TradeSupplierOrderBO tradeSupplierOrderBO) {
        //更新订单状态为失败
        TradeSupplierOrderDO tradeSupplierOrderDO = tradeSupplierOrderBO.getTradeSupplierOrderDO();
        tradeSupplierOrderDO.setOrderStatus(TradeOrderStatusEnum.FAIL.getStatus());
        tradeSupplierOrderRepositoryService.updateOrderStatus(tradeSupplierOrderDO);
    }


    @Override
    public void afterAnyOrderCreateFail(TradeOrderDO tradeOrderDO) {
        //更新订单失败
        tradeOrderRepositoryService.createOrderFail(tradeOrderDO.getId());
        //异步通知归还库存等操作
        tradePublishUtils.publishOrderCreateFailMsg(tradeOrderDO.getId().toString());
        TradeOrderLogUtils.setOrderInfo(tradeOrderDO.getId(),null,tradeOrderDO.getOrderStatus(),TradeOrderLogUtils.append("result","失败"));
    }

    @Override
    @TradeOrderLog(operateType = TradeOrderOperateTypeEnum.ORDER_PROCESS)
    public void afterAllOrderPay(TradeOrderDO tradeOrderDO) {
        Integer orderStatus = tradeOrderDO.getOrderStatus();
        //更新订单状态
        tradeOrderDO.setOrderStatus(TradeOrderStatusEnum.UNUSED.getStatus());
        tradeOrderDO.setPayStatus(1);
        tradeOrderRepositoryService.updateOrderAfterAllSupplierPay(tradeOrderDO);
        //发送出票成功消息
        tradePublishUtils.publishProcessSuccessMsg(tradeOrderDO.getId().toString());
        //保存订单日志
        TradeOrderLogUtils.setOrderInfo(tradeOrderDO.getId(),orderStatus, tradeOrderDO.getOrderStatus(),MapUtil.<String, Object>builder().put("result", "成功").build());
    }

    @Override
    public void afterOrderPaySuccess(SupplierTradeOrderPayRespBO respBO, TradeSupplierOrderDO tradeSupplierOrderDO) {
        tradeSupplierOrderRepositoryService.updateAfterPaySuccess(respBO,tradeSupplierOrderDO);


    }

    @Override
    public void afterOrderPayFail(TradeSupplierOrderDO tradeSupplierOrderDO) {
        //更新供应商订单为出票失败
        tradeSupplierOrderDO.setOrderStatus(TradeOrderStatusEnum.PROCESS_FAIL.getStatus());
        tradeSupplierOrderRepositoryService.updateOrderStatus(tradeSupplierOrderDO);
    }

    @Override
    @TradeOrderLog(operateType = TradeOrderOperateTypeEnum.ORDER_PROCESS)
    public void afterAnyOrderPayFail(TradeOrderDO tradeOrderDO) {
        //如果是分销订单直接出票失败返回，其他渠道可以重试
        Integer orderStatus = tradeOrderDO.getOrderStatus();
        tradeOrderDO.setOrderStatus(TradeOrderStatusEnum.PROCESS_FAIL.getStatus());
        tradeOrderRepositoryService.updateOrderStatus(tradeOrderDO);
        //发送出票失败消息，可发起自动退款
        tradePublishUtils.publishProcessFailMsg(tradeOrderDO.getId().toString());
        //如果是分销订单直接出票失败返回，其他渠道可以重试
        //订单日志
        TradeOrderLogUtils.setOrderInfo(tradeOrderDO.getId(),orderStatus, tradeOrderDO.getOrderStatus(),MapUtil.<String, Object>builder().put("result", "失败").build());
    }

    @Override
    public void beforeOrderPay(TradeSupplierOrderDO tradeSupplierOrderDO) {
        tradeSupplierOrderDO.setOrderStatus(TradeOrderStatusEnum.PROCESSING.getStatus());
        tradeSupplierOrderRepositoryService.updateOrderStatus(tradeSupplierOrderDO);

    }

    @Override
    public void afterAnyRefundFail(CommonResult<?> resp, TradeRefundBO tradeRefundBO) {
        if(ObjectUtil.equal(resp.getCode(),REFUND_AUDITING.getCode())){
            //供应商退款审核中
            tradeRefundRepositoryService.updateRefundStatus(tradeRefundBO.getRefundDO().getId(), RefundStatusEnum.SUPPLIER_REFUND_AUDITING);
            tradeRefundRepositoryService.updateRefundSupplierStatus(tradeRefundBO.getRefundDO().getId(),RefundStatusEnum.SUPPLIER_REFUND_AUDITING);
        }else{
            //供应商退款失败，通知人工介入
            tradeRefundRepositoryService.updateRefundStatus(tradeRefundBO.getRefundDO().getId(), RefundStatusEnum.SUPPLIER_REFUND_MAN_HANDLE);
            tradeRefundRepositoryService.updateRefundSupplierStatus(tradeRefundBO.getRefundDO().getId(),RefundStatusEnum.REFUND_FAIL);
            //TODO 发消息通知
        }
    }

    @Override
    public void afterAllRefund(TradeRefundBO tradeRefundBO) {
        //更新退款单为等待退款
        tradeRefundRepositoryService.updateRefundStatus(tradeRefundBO.getRefundDO().getId(), RefundStatusEnum.WAIT_REFUND);
        if(tradeRefundBO.getRefundDO().getSupplierRefundStatus()!=null){
            tradeRefundRepositoryService.updateRefundSupplierStatus(tradeRefundBO.getRefundDO().getId(),RefundStatusEnum.REFUND);
        }
        tradePublishUtils.publishSupplierRefundSuccessMsg(tradeRefundBO.getRefundDO().getId().toString());
    }

    @Override
    public void afterRefundFail(CommonResult<SupplierRefundRespBO> resp, SupplierRefundDO supplierRefundDO) {
        if(ObjectUtil.equal(resp.getCode(),REFUND_AUDITING.getCode())){
            //退款需要审核
            supplierRefundDO.setRefundStatus(RefundStatusEnum.AUDITING.getStatus());
            supplierRefundDO.setSupplierRefundNo(resp.getData().getSupplierRefundNo());
        }else{
            //退款失败
            supplierRefundDO.setRefundStatus(RefundStatusEnum.REFUND_FAIL.getStatus());
        }
        supplierRefundDO.setErrorMsg(resp.getMsg());
        tradeSupplierOrderRepositoryService.updateRefundStatus(supplierRefundDO);
    }

    @Override
    public void afterRefundSuccess(SupplierRefundRespBO respBO, SupplierRefundDO supplierRefundDO) {
        supplierRefundDO.setSupplierRefundNo(respBO.getSupplierRefundNo());
        supplierRefundDO.setRefundStatus(RefundStatusEnum.REFUND.getStatus());
        supplierRefundDO.setRefundTime(LocalDateTime.now());
        tradeSupplierOrderRepositoryService.updateRefundStatus(supplierRefundDO);
        //如果是向平台退款，看是否需要退门店款到旅行社 12月10日已废弃此功能，旅行社不对门店进行资金管理
//        if(supplierRefundDO.getSupplierType() == SUPPLIER_TYPE_PLAT){
//            //如果门店下的自助机和窗口下单，需要归还门店的余额,重试不进入
//            if(supplierRefundDO.getRetryCount() == null || supplierRefundDO.getRetryCount() == 0){
//                SpringUtil.getBean(PlatSupplierTradeService.class).deductAgencyStoreAccount(supplierRefundDO);
//            }
//        }
    }
}
