package com.yami.shop.service.xsb.impl;


import com.alibaba.fastjson.JSONObject;
import com.yami.shop.bean.enums.ChannelType;
import com.yami.shop.bean.model.OrderRefund;
import com.yami.shop.bean.vo.xsb.RefundOrderPushStatusVo;
import com.yami.shop.common.bean.smqjh.Result;
import com.yami.shop.common.enums.SMQJHResultCode;
import com.yami.shop.common.util.HttpUtil;
import com.yami.shop.common.util.R;
import com.yami.shop.dao.PointsRecordMapper;
import com.yami.shop.service.xsb.XSBPushRefundOrderService;
import com.yami.shop.utils.SmqjhUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@AllArgsConstructor
public class XSBPushRefundOrderServiceImpl implements XSBPushRefundOrderService {

    @Autowired
    PointsRecordMapper pointsRecordMapper;

    private final SmqjhUtil smqjhUtil;

    /**
     * 推送市民请集合状态
     * @param orderRefund
     * @return
     */
    @Override
    public R<String> pushRefundStatus(OrderRefund orderRefund) {
        log.info("推送市民请集合状态请求参数,{}", com.alibaba.fastjson2.JSONObject.toJSONString(orderRefund));
        if (orderRefund==null){
            return R.FAIL("退款订单信息不存在");
        }
        if (!ChannelType.SMQJH.value().equals(orderRefund.getChannelType())){
            return R.FAIL("退款订单信息不属于市民请集合订单,不允许推送");
        }
        RefundOrderPushStatusVo refundOrderPushStatusVo = new RefundOrderPushStatusVo();
        refundOrderPushStatusVo.setRefundNumber(orderRefund.getRefundSn());
        refundOrderPushStatusVo.setReturnMoneySts(orderRefund.getReturnMoneySts());
        refundOrderPushStatusVo.setApplyType(orderRefund.getApplyType());
        log.info("推送市民请集合状态推送请求参数,{}", com.alibaba.fastjson2.JSONObject.toJSONString(refundOrderPushStatusVo));
        String post = HttpUtil.post(smqjhUtil.getOmsBaseUrl()+SmqjhUtil.omsSmqjhRefundUrlUpdateStatus, refundOrderPushStatusVo);
        log.info("推送市民请集合状态推送返回参数,{}", post);
        if (StringUtils.isNotEmpty(post)){
            Result result = JSONObject.parseObject(post, Result.class);
            if (result==null){
                return R.FAIL("推送订单状态返回失败,返回信息为空");
            }
            if (!SMQJHResultCode.SUCCESS.getCode().equals(result.getCode())){
                return R.FAIL("推送订单状态返回失败："+result.getMsg());
            }
        }
        return R.SUCCESS();
    }




}
