package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LifeRefundOrder;
import shop.alien.store.service.LifeUserOrderService;
import shop.alien.util.common.ListToPage;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Api(tags = {"一期-用户订单"})
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/userorder")
@RequiredArgsConstructor
public class UserOrderController {

    private final LifeUserOrderService userOrderService;

    @ApiOperation("订单列表")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "分页页数", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "分页条数", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "订单状态(0:已成交, 1:待使用, 4:退款)", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "quanName", value = "quanName", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "storeType", value = "storeType", dataType = "String", paramType = "query")})
    @GetMapping("/getOrderListByStatus")
    @Deprecated
    public R<IPage<Map<String, Object>>> getOrderListByStatus(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                              @RequestParam(value = "size", defaultValue = "10") Integer size,
                                                              @RequestParam(required = false) String userId,
                                                              @RequestParam(required = false) String status,
                                                              @RequestParam(required = false) String quanName,
                                                              @RequestParam(required = false) Integer storeType,
                                                              @RequestParam(required = false) String baojiaStatus,
                                                              @RequestParam(required = false) String orderNo,
                                                              @RequestParam(required = false) Integer storeId) {
        log.info("UserOrderController.getOrderListByStatus?page={},size={},userId={},status={}," +
                "quanName={},storeType={},baojiaStatus={},orderNo={},storeId={}", page, size, userId, status, quanName, storeType, baojiaStatus, orderNo, storeId);
        List<Map<String, Object>> orderListByStatus = userOrderService.getOrderListByStatus(page, size, userId, status, quanName, storeType, baojiaStatus, orderNo, storeId);
        return R.data(ListToPage.setPage(orderListByStatus, page, size));
    }

    @ApiOperation("订单详情")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({@ApiImplicitParam(name = "orderId", value = "orderId", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "jingdu", value = "jingdu", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "weidu", value = "weidu", dataType = "String", paramType = "query")})
    @GetMapping("/getOrderByOrderId")
    @Deprecated
    public R<Map<String, Object>> getOrderByOrderId(@RequestParam(required = false) String orderId,
                                                    @RequestParam(required = false) String jingdu,
                                                    @RequestParam(required = false) String weidu) {
        log.info("UserOrderController.getOrderByOrderId?orderId={},jingdu={},weidu={}", orderId, jingdu, weidu);
        return R.data(userOrderService.getOrderByOrderId(orderId, jingdu, weidu));
    }

    @ApiOperation("订单删除")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({@ApiImplicitParam(name = "orderId", value = "orderId", dataType = "String", paramType = "query")})
    @GetMapping("/deleteOrdersById")
    public R<Boolean> deleteOrdersById(@RequestParam("orderId") String orderId) {
        log.info("UserOrderController.deleteOrdersById?orderId={}", orderId);
        int num = userOrderService.deleteOrdersById(orderId);
        if (num == 0) {
            return R.fail("订单删除失败");
        }
        return R.success("订单删除成功");
    }

//    @ApiOperation("订单是否可以退款")
//    @ApiOperationSupport(order = 4)
//    @PostMapping("/refundCfheck")
//    @Deprecated
//    public R<Map<String, Object>> refundCheck(LifeRefundOrder refundOrder) {
//        log.info("UserOrderController.refundCheck?refundOrder={}", refundOrder.toString());
//        return R.data(userOrderService.refundCheck(refundOrder));
//    }

    @ApiOperation("订单退款")
    @ApiOperationSupport(order = 5)
    @PostMapping("/requestRefund")
    @Deprecated
    public R<String> requestRefund(LifeRefundOrder refundOrder) {
        log.info("UserOrderController.requestRefund?refundOrder={}", refundOrder.toString());
        return userOrderService.requestRefund(refundOrder);
    }

    @GetMapping("getQuanCodeList")
    public List<String> getQuanCodeList(@RequestParam("orderNo") String orderNo) {
        log.info("UserOrderController.getQuanCodeList?orderNo={}", orderNo);
        return userOrderService.getQuanCodeList(orderNo);
    }

    @ApiOperation("根据订单号获取可用优惠劵数量")
    @GetMapping("/getAvailableOrderNumByOrderNo")
    public R<Integer> getAvailableOrderNumByOrderNo(@RequestParam("orderNo") String orderNo) {
        log.info("UserOrderController.getAvailableOrderNumByOrderNo?orderNo={}", orderNo);
        return R.data(userOrderService.getAvailableOrderNumByOrderNo(orderNo));
    }

    @ApiOperation(value = "web端导出订单信息")
    @ApiOperationSupport(order = 6)
    @GetMapping("/exportExcel")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderNo", value = "订单编号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "storeId", value = "联系电话", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "quanName", value = "经营板块", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "baojiaStatus", value = "价保状态", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "订单状态", dataType = "String", paramType = "query"),
    })
    @ResponseBody
    public R exportExcel(@RequestParam(value = "orderNo", required = false) String orderNo
            , @RequestParam(value = "storeId", required = false) String storeId
            , @RequestParam(value = "quanName", required = false) String quanName
            , @RequestParam(value = "baojiaStatus", required = false) String baojiaStatus
            , @RequestParam(value = "status", required = false) String status) throws IOException {
        log.info("UserOrderController.exportExcel");
        String excelPath = userOrderService.exportExcel(orderNo, storeId, quanName, baojiaStatus, status);
        return R.data(excelPath);
    }
}
