package com.yami.shop.platform.controller.hb;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.dto.OrderRefundDto;
import com.yami.shop.bean.model.OrderRefund;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.common.util.R;
import com.yami.shop.common.util.hb.HBR;
import com.yami.shop.service.OrderRefundService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 订单退款管理实现
 *
 * @author lgh
 * @version 1.0.0
 * @since 2018-09-15
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/hb/platform")
public class HBOrderRefundController {

    private final OrderRefundService orderRefundService;


    //*************************** 售后接口 ***************************
    /**
     * 创建退款单(上行）
     * 海博售后订单创建接口-推送接口
     *
     * @param channelOrderId 海博订单单号
     * @param afterSaleOrder  退款单号
     */
    @PostMapping("/createRefundOrder")
    public HBR createRefundOrder(String channelOrderId, String afterSaleOrder) {
        log.info("海博售后订单推送channelOrderId：{},afterSaleOrder:{}",channelOrderId,afterSaleOrder);
        return HBR.success(orderRefundService.createRefundOrder(channelOrderId,afterSaleOrder));
    }

    /**
     * 查询退款单详情（下行）-回调接口
     */
    @PostMapping("/details")
    public HBR details(@RequestBody JSONObject hBRequest) {
        log.info("查询退款单详情{}",hBRequest);
        return orderRefundService.details(hBRequest);
    }


    /**
     * 退款-退款单状态同步（上行）
     * @param afterSaleOrder  退款单号
     *  @param  refundStatus 退款单状态 10:待审核 20:处理中 30:驳回退款 40:撤销退款 60:待退货（一审同意） 65:待确认收货（二审待审核） 70:退款完成
     */
    @PostMapping("/changeStatus")
    public HBR changeStatus(String afterSaleOrder,Integer refundStatus) {
        log.info("海博售后订单状态同步afterSaleOrder:{}",afterSaleOrder);
        return HBR.success(orderRefundService.changeStatus(afterSaleOrder,refundStatus));
    }


    /**
     * 商家审核（下行）-回调接口
     * @param hBRequest 海博请求参数
     */
    @PostMapping("/audit")
    public HBR audit(@RequestBody JSONObject hBRequest) {
        log.info("进入海博商家审核接口{}",hBRequest);
        return orderRefundService.audit(hBRequest);
    }

    /**
     * 商家确认收货（下行）-回调接口
     */
    @PostMapping("/confirmOrder")
    public HBR confirmOrder(@RequestBody JSONObject hBRequest) {
        log.info("进入海博商家确认收货接口");
        return orderRefundService.confirmOrder(hBRequest);
    }

    /**
     * 商家发起部分退款（下行）-回调接口
     */
    @PostMapping("/partRefund")
    public HBR partRefund(@RequestBody JSONObject hBRequest) {
        log.info("进入海博商家发起部分退款接口");
        return orderRefundService.partRefund(hBRequest);
    }

    /**
     * 退款-退款创建结果通知接口-回调接口
     */
    @PostMapping("/createResult")
    public HBR createResult(@RequestBody JSONObject hBRequest) {
        log.info("进入海博退款创建结果通知接口{}",hBRequest);
        return orderRefundService.createResult(hBRequest);
    }
}
