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

import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.liquidnet.commons.lang.util.CurrentUtil;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.goblin.dto.vo.GoblinStoreOrderListPageVo;
import com.liquidnet.service.goblin.dto.vo.GoblinStoreOrderListVo;
import com.liquidnet.service.goblin.service.manage.IGoblinStoreOrderService;
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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.math.BigDecimal;

@ApiSupport(order = 149101)
@Api(tags = "店铺-订单")
@Slf4j
@Validated
@RestController
@RequestMapping("store/order")
public class GoblinStoreOrderController {
    @Autowired
    IGoblinStoreOrderService goblinStoreOrderService;
//    @Autowired
//    IGoblinOrderService goblinOrderService;

    @ApiOperation(value = "订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "Integer", name = "page", value = "页数"),
            @ApiImplicitParam(type = "form", required = false, dataType = "String", name = "orderCode", value = "订单编号"),
            @ApiImplicitParam(type = "form", required = false, dataType = "Integer", name = "type", value = "订单类型[0-全部|1-普通订单]"),
            @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 = "expressContacts", value = "收货人姓名"),
            @ApiImplicitParam(type = "form", required = false, dataType = "String", name = "phone", value = "手机号"),
            @ApiImplicitParam(type = "form", required = false, dataType = "Integer", name = "status", value = "订单状态[0-待支付|2-待发货|3-已发货|4-已完成|6-已退款]"),
    })
    @GetMapping(value = "list")
    public ResponseDto<GoblinStoreOrderListPageVo> orderList(@RequestParam(value = "page", required = true) @Valid Integer page,
                                                                   @RequestParam(value = "orderCode", required = false) String orderCode,
                                                                   @RequestParam(value = "type", required = false) Integer type,
                                                                   @RequestParam(value = "cst", required = false) String cst,
                                                                   @RequestParam(value = "cet", required = false) String cet,
                                                                   @RequestParam(value = "expressContacts", required = false) String expressContacts,
                                                                   @RequestParam(value = "phone", required = false) String phone,
                                                                   @RequestParam(value = "status", required = false) Integer status) {

        return goblinStoreOrderService.orderList(page, orderCode, type, cst, cet, expressContacts, phone, status);
    }

    @ApiOperation(value = "订单列表导出")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = false, dataType = "String", name = "orderCode", value = "订单编号"),
            @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 = "expressContacts", value = "收货人姓名"),
            @ApiImplicitParam(type = "form", required = false, dataType = "String", name = "phone", value = "手机号"),
            @ApiImplicitParam(type = "form", required = false, dataType = "Integer", name = "status", value = "订单状态[0-代付款|2-待发货|3-已发货|4-已完成]"),
    })
    @GetMapping(value = "list/export")
    public void exportOrderList(HttpServletResponse response,
                                @RequestParam(value = "orderCode", required = false) String orderCode,
                                @RequestParam(value = "cst", required = false) String cst,
                                @RequestParam(value = "cet", required = false) String cet,
                                @RequestParam(value = "expressContacts", required = false) String expressContacts,
                                @RequestParam(value = "phone", required = false) String phone,
                                @RequestParam(value = "status", required = false) Integer status) {
        goblinStoreOrderService.exportOrderList(response, orderCode, cst, cet, expressContacts, phone, status);
    }

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

    @ApiOperation(value = "取消订单")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "orderId", value = "订单id"),
    })
    @GetMapping(value = "cancel")
    public ResponseDto<Boolean> orderCancel(@RequestParam(value = "orderId", required = true) @Valid String orderId) {
        return goblinStoreOrderService.orderCancel(orderId);
    }

    @ApiOperation(value = "修改快递价格")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "orderId", value = "订单id"),
            @ApiImplicitParam(type = "form", required = true, dataType = "BigDecimal", name = "price", value = "快递金额"),
    })
    @GetMapping(value = "change/expressPrice")
    public ResponseDto<Boolean> changeExpressPrice(@RequestParam(value = "orderId", required = true) @Valid String orderId,
                                                   @RequestParam(value = "price", required = true) @Valid BigDecimal price) {
        return goblinStoreOrderService.changeExpressPrice(orderId, price);
    }

    @ApiOperation(value = "修改sku价格")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "orderId", value = "订单id"),
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "orderSkuId", value = "orderSkuId"),
            @ApiImplicitParam(type = "form", required = true, dataType = "BigDecimal", name = "price", value = "修改金额"),
    })
    @GetMapping(value = "change/skuPrice")
    public ResponseDto<Boolean> changeSkuPrice(@RequestParam(value = "orderId", required = true) @Valid String orderId,
                                               @RequestParam(value = "orderSkuId", required = true) @Valid String orderSkuId,
                                               @RequestParam(value = "price", required = true) @Valid BigDecimal price) {
        return goblinStoreOrderService.changeSkuPrice(orderId, orderSkuId, price);
    }

    @ApiOperation(value = "修改收货地址")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "orderId", value = "订单id"),
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "expressAddressDetail", value = "详细快递地址"),
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "expressContacts", value = "联系人"),
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "expressPhone", value = "联系人电话"),
    })
    @GetMapping(value = "change/address")
    public ResponseDto<Boolean> changeAddress(@RequestParam(value = "orderId", required = true) @Valid String orderId,
                                              @RequestParam(value = "expressContacts", required = true) @Valid String expressContacts,
                                              @RequestParam(value = "expressPhone", required = true) @Valid String expressPhone,
                                              @RequestParam(value = "expressAddressDetail", required = true) @Valid String expressAddressDetail) {
        return goblinStoreOrderService.changeAddress(orderId, expressContacts, expressPhone, expressAddressDetail);
    }

    @ApiOperation(value = "退款(sku/整单)-废弃")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "orderId", value = "订单id"),
            @ApiImplicitParam(type = "form", required = false, dataType = "String", name = "orderSkuId", value = "orderSkuId[整单退不传]"),
            @ApiImplicitParam(type = "form", required = true, dataType = "BigDecimal", name = "price", value = "退款金额"),
    })
    @GetMapping(value = "refund/sku")
    public ResponseDto<Boolean> refundOrderSku(@RequestParam(value = "orderId", required = true) @Valid String orderId,
                                               @RequestParam(value = "orderSkuId", required = false) String orderSkuId,
                                               @RequestParam(value = "price", required = true) @Valid BigDecimal price) {
        return goblinStoreOrderService.refundOrderSku(orderId, orderSkuId, price);
    }

    @ApiOperation(value = "发货")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "orderId", value = "订单id"),
            @ApiImplicitParam(type = "form", required = false, dataType = "String", name = "orderSkuIds", value = "逗号隔开orderSkuId"),
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "mailNo", value = "快递单号"),
    })
    @GetMapping(value = "express")
    public ResponseDto<Boolean> express(@RequestParam(value = "orderId", required = true) @Valid String orderId,
                                        @RequestParam(value = "orderSkuIds", required = false) @Valid String orderSkuIds,
                                        @RequestParam(value = "mailNo", required = true) @Valid String mailNo) {
        return goblinStoreOrderService.express(orderId, orderSkuIds, mailNo, CurrentUtil.getCurrentUid(), "");
    }

    @ApiOperation(value = "修改快递单号")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "orderId", value = "订单id"),
            @ApiImplicitParam(type = "form", required = false, dataType = "String", name = "mailId", value = "快递id"),
            @ApiImplicitParam(type = "form", required = true, dataType = "String", name = "mailNo", value = "快递单号"),
    })
    @GetMapping(value = "express/change")
    public ResponseDto<Boolean> changeExpressMailNo(@RequestParam(value = "orderId", required = true) @Valid String orderId,
                                                    @RequestParam(value = "mailId", required = false) @Valid String mailId,
                                                    @RequestParam(value = "mailNo", required = true) @Valid String mailNo) {
        return goblinStoreOrderService.changeExpressMailNo(orderId, mailId, mailNo);
    }

}
