package com.gree.mall.manager.controller.order;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.gree.mall.manager.bean.order.OrderRefundDetailBean;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.order.OrderRefundLogic;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@RestController
@Api(value = "订单售后管理", tags ={"订单售后管理"} )
@RequestMapping(value = "/order/refund", produces = "application/json; charset=utf-8")
public class OrderRefundController {

    @Autowired
    OrderRefundLogic orderRefundLogic;

    @GetMapping("/list")
    @ApiOperation("列表")
    public ResponseHelper<Page<OrderRefundDetailBean>> list(HttpServletRequest request,
                                                            @ApiParam(value = "订单号",required = false) @RequestParam(required = false) String orderId,
                                                            @ApiParam(value = "售后订单号",required = false) @RequestParam(required = false) String orderRefundId,
                                                            @ApiParam(value = "用户名",required = false) @RequestParam(required = false) String userName,
                                                            @ApiParam(value = "手机号",required = false) @RequestParam(required = false) String phone,
                                                            @ApiParam(value = "订单状态 DSJCL=待商家处理  DSJFH=待商家发货 DMJCL=待买家处理 OVER=已完成",required = false) @RequestParam(required = false) String orderStatus,
                                                            @ApiParam(value = "开始申请时间",required = false) @RequestParam(required = false) String startCreateTime,
                                                            @ApiParam(value = "结束申请时间",required = false) @RequestParam(required = false) String endCreateTime,
                                                            @ApiParam(value = "开始处理时间",required = false) @RequestParam(required = false) String startExamineTime,
                                                            @ApiParam(value = "结束处理时间",required = false) @RequestParam(required = false) String endExamineTime,
                                                            @ApiParam(value = "页号",required = true) @RequestParam(required = true) Integer pageNum,
                                                            @ApiParam(value = "页大小",required = true) @RequestParam(required = true) Integer pageSize
    ) throws RemoteServiceException {
        IPage<OrderRefundDetailBean> list = orderRefundLogic
                .list(request,orderId, orderRefundId, userName, phone, orderStatus, startCreateTime, endCreateTime, startExamineTime, endExamineTime, pageNum, pageSize);
        return ResponseHelper.success(list);
    }

    @GetMapping("/refund/detail")
    @ApiOperation("售后订单-订单退款详情")
    public ResponseHelper<OrderRefundDetailBean> refundDetail(
            @ApiParam(value = "售后订单id",required = true) @RequestParam String orderRefundId
    ){
        OrderRefundDetailBean orderRefundDetailBean = orderRefundLogic.orderRefundDetail(orderRefundId);
        return ResponseHelper.success(orderRefundDetailBean);
    }

    @GetMapping("/expressInfo/select")
    @ApiOperation("售后订单-查看物流")
    public ResponseHelper queryExpressInfo(
            @ApiParam(value = "物流单号", required = true) @RequestParam(required = true) String logisticsNo
    ) throws RemoteServiceException {
        orderRefundLogic.queryExpressInfo(logisticsNo);
        return ResponseHelper.success();
    }


    @PostMapping("/examine")
    @ApiOperation("审核")
    public ResponseHelper examine(
            @ApiParam(value = "售后单号",required = true) @RequestParam(required = true) String orderRefundId,
            @ApiParam(value = "OK=同意  FAIL=不同意",required = true) @RequestParam(required = true) String examineStatus,
            @ApiParam(value = "卖家留言",required = false) @RequestParam(required = false) String refundMessage,
            @ApiParam(value = "退款密码") @RequestParam(required = false) String refundPassword
    ) throws Exception {
        orderRefundLogic.examine(orderRefundId, examineStatus, refundMessage, refundPassword);
        return ResponseHelper.success();
    }

    @PostMapping("/ack")
    @ApiOperation("商家确认收货")
    public ResponseHelper examine(
            @ApiParam(value = "售后单号",required = true) @RequestParam(required = true) String orderRefundId
    ) throws Exception {
        orderRefundLogic.ack(orderRefundId);
        return ResponseHelper.success();
    }


    @PostMapping("/remark")
    @ApiOperation("备注")
    public ResponseHelper remark(
            @ApiParam(value = "售后单号",required = true) @RequestParam(required = true) String orderRefundId,
            @ApiParam(value = "备注",required = true) @RequestParam(required = true) String remark
    ){
        orderRefundLogic.remark(orderRefundId,remark);
        return ResponseHelper.success();
    }




    @GetMapping("/downLoadExcel")
    @ApiOperation("导出售后报表")
    public void downLoadExcel(HttpServletRequest request, HttpServletResponse response,
                                         @ApiParam(value = "订单号",required = false) @RequestParam(required = false) String orderId,
                                         @ApiParam(value = "售后订单号",required = false) @RequestParam(required = false) String orderRefundId,
                                         @ApiParam(value = "用户名",required = false) @RequestParam(required = false) String userName,
                                         @ApiParam(value = "手机号",required = false) @RequestParam(required = false) String phone,
                                         @ApiParam(value = "订单状态 DSJCL=待商家处理  DSJFH=待商家发货 DMJCL=待买家处理 OVER=已完成",required = false) @RequestParam(required = false) String orderStatus,
                                         @ApiParam(value = "开始申请时间",required = false) @RequestParam(required = false) String startCreateTime,
                                         @ApiParam(value = "结束申请时间",required = false) @RequestParam(required = false) String endCreateTime,
                                         @ApiParam(value = "开始处理时间",required = false) @RequestParam(required = false) String startExamineTime,
                                         @ApiParam(value = "结束处理时间",required = false) @RequestParam(required = false) String endExamineTime
    ) throws Exception {
        orderRefundLogic.downLoadOrderRefundExcel(request,response,orderId,orderRefundId,userName,phone,orderStatus,startCreateTime,endCreateTime,startExamineTime,endExamineTime);
    }

}
