package com.liquidnet.service.goblin.controller.manage;

import com.github.xiaoymin.knife4j.annotations.ApiSupport;import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.goblin.dto.vo.GoblinBackOrderDetailsVo;
import com.liquidnet.service.goblin.dto.vo.GoblinStoreBackOrderListPageVo;
import com.liquidnet.service.goblin.dto.vo.GoblinStoreBackOrderListVo;
import com.liquidnet.service.goblin.param.GoblinStoreOrderRefundParam;
import com.liquidnet.service.goblin.service.manage.IGoblinStoreBackOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;

@ApiSupport(order = 149101)
@Api(tags = "店铺-退款订单")
@Slf4j
@Validated
@RestController
@RequestMapping("store/orderBack")
public class GoblinStoreBackOrderController {
    @Autowired
    IGoblinStoreBackOrderService goblinStoreBackOrderService;

    @ApiOperation(value = "订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "Integer", name = "page", value = "页数"),
            @ApiImplicitParam(type = "form", required = false, dataType = "String", name = "orderBackCode", value = "订单退款编号"),
            @ApiImplicitParam(type = "form", required = false, dataType = "Integer", name = "type", value = "退款类型[1-退款|2-退货|3-退款退货]"),
            @ApiImplicitParam(type = "form", required = false, dataType = "String", name = "cst", value = "申请起始时间"),
            @ApiImplicitParam(type = "form", required = false, dataType = "String", name = "cet", value = "申请终止时间"),
            @ApiImplicitParam(type = "form", required = false, dataType = "String", name = "orderCode", value = "订单编号"),
            @ApiImplicitParam(type = "form", required = false, dataType = "String", name = "spuName", value = "商品名称"),
            @ApiImplicitParam(type = "form", required = false, dataType = "String", name = "status", value = "审核状态，多个用英文逗号分隔。0-商铺发起退款|1-退款申请|2-退款成功|3-退款拒绝|4-退货申请|5-退货拒绝|6-待填物流|7-待收货|8-退货完成|9-退货失败|10-退款失败|11-取消退款"),
    })
    @GetMapping(value = "list")
    public ResponseDto<GoblinStoreBackOrderListPageVo> orderBackList(@RequestParam(value = "page", required = true) @Valid Integer page,
                                                                           @RequestParam(value = "orderBackCode", required = false) String orderBackCode,
                                                                           @RequestParam(value = "type", required = false) Integer type,
                                                                           @RequestParam(value = "cst", required = false) String cst,
                                                                           @RequestParam(value = "cet", required = false) String cet,
                                                                           @RequestParam(value = "orderCode", required = false) String orderCode,
                                                                           @RequestParam(value = "spuName", required = false) String spuName,
                                                                           @RequestParam(value = "status", required = false) String status) {

        return goblinStoreBackOrderService.orderBackList(page, orderBackCode, type, cst, cet, orderCode, spuName, status);
    }

    @ApiOperation(value = "退款订单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "backOrderId", value = "订单id"),
    })
    @GetMapping(value = "details")
    public ResponseDto<GoblinBackOrderDetailsVo> orderList(@RequestParam(value = "backOrderId", required = true) @Valid String backOrderId) {
        return goblinStoreBackOrderService.orderDetails(backOrderId);
    }

    @ApiOperation(value = "同意退款")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "backOrderId", value = "订单id"),
    })
    @GetMapping(value = "agree")
    public ResponseDto<Boolean> agreeRefund(@RequestParam(value = "backOrderId", required = true) @Valid String backOrderId) {
        return goblinStoreBackOrderService.agreeRefund(backOrderId);
    }

    @ApiOperation(value = "拒绝退款")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "backOrderId", value = "订单id"),
    })
    @GetMapping(value = "refused")
    public ResponseDto<Boolean> refusedRefund(@RequestParam(value = "backOrderId", required = true) @Valid String backOrderId) {
        return goblinStoreBackOrderService.refusedRefund(backOrderId);
    }

    @ApiOperation(value = "发起退款")
    @PostMapping(value = "refund")
    public ResponseDto<Boolean> refundOrder(@RequestBody @Valid GoblinStoreOrderRefundParam param) {
        return goblinStoreBackOrderService.refundOrder(param);
    }

    @ApiOperation(value = "修改金额")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "backOrderId", value = "订单id"),
            @ApiImplicitParam(type = "form", required = true, dataType = "Integer", name = "changeType", value = "类型[1-物流金额|2-退款金额]"),
            @ApiImplicitParam(type = "form", required = true, dataType = "Number", name = "refundPrice", value = "价格"),
            @ApiImplicitParam(type = "form", required = false, dataType = "String", name = "orderSkuId", value = "orderSkuId"),
    })
    @GetMapping(value = "change")
    public ResponseDto<Boolean> changeRefund(@RequestParam(value = "backOrderId", required = true) @Valid String backOrderId,
                                             @RequestParam(value = "changeType", required = true) @Valid Integer changeType,
                                             @RequestParam(value = "refundPrice", required = true) @Valid BigDecimal refundPrice,
                                             @RequestParam(value = "orderSkuId", required = false) String orderSkuId) {
        if (changeType == 1) {
            return goblinStoreBackOrderService.changeExpressRefund(backOrderId, refundPrice);
        } else if (changeType == 2) {
            return goblinStoreBackOrderService.changeSkuRefund(backOrderId, refundPrice, orderSkuId);
        } else {
            return ResponseDto.failure("参数异常");
        }
    }
}
