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.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LifeRefundOrder;
import shop.alien.entity.store.dto.LifeUserOrderDto;
import shop.alien.entity.store.vo.LifeUserOrderVo;
import shop.alien.store.service.LifeUserOrderService;

import java.util.Map;

@Api(tags = {"2.5期-订单"})
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/userOrder")
@RequiredArgsConstructor
public class LifeUserOrderController {

    private final LifeUserOrderService lifeUserOrderService;


    @ApiOperation("创建订单")
    @ApiOperationSupport(order = 2)
    @PostMapping("/createUserOrder")
    public R createUserOrder(@RequestBody LifeUserOrderDto LifeUserOrderDto) {
        log.info("userOrder.createUserOrder:{}", LifeUserOrderDto);
        return lifeUserOrderService.createUserOrder(LifeUserOrderDto);
    }

    @ApiOperation("更新订单")
    @ApiOperationSupport(order = 3)
    @PostMapping("/updateUserOrder")
    public R<Map<String, Object>> updateUserOrder(@RequestBody LifeUserOrderDto LifeUserOrderDto) {
        log.info("userOrder.updateUserOrder:{}", LifeUserOrderDto);
        if (!lifeUserOrderService.updateUserOrder(LifeUserOrderDto)) {
            return R.fail("更新失败");
        }
        return R.success("更新成功");

    }


    @ApiOperation("删除订单")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单id", required = true)
    })
    @DeleteMapping("/deleteUserOrder")
    public R<Map<String, Object>> deleteUserOrder(@RequestParam(value = "orderId") String orderId) {
        log.info("userOrder.deleteUserOrder:{}", orderId);
        if (!lifeUserOrderService.deleteUserOrder(orderId)) {
            return R.fail("删除失败");
        }
        return R.success("删除成功");
    }

    @ApiOperation("查询订单列表")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true),
            @ApiImplicitParam(name = "size", value = "每页数量", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", required = false),
            @ApiImplicitParam(name = "storeId", value = "商户id", required = false),
            @ApiImplicitParam(name = "couponType", value = "订单类型,-1,全部（可以不传）,1,代金券；2，团购；", required = false),
            @ApiImplicitParam(name = "orderStatus", value = "订单状态,-1，全部（可以不传）；1，已支付/待使用;2，已完成；5，退款/售后", required = false),
            @ApiImplicitParam(name = "name", value = "订单名称", required = false)
    })
    @GetMapping("/queryUserOrderList")
    public R<IPage<LifeUserOrderVo>> queryUserOrderList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                        @RequestParam(value = "size", defaultValue = "10") Integer size,
                                                        @RequestParam(required = false) String userId,
                                                        @RequestParam(required = false) String storeId,
                                                        @RequestParam(required = false, defaultValue = "-1") String couponType,
                                                        @RequestParam(required = false, defaultValue = "-1") String orderStatus,
                                                        @RequestParam(required = false) String name,
                                                        @RequestParam(value = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd 00:00:00") String startTime,
                                                        @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd 23:59:59") String endTime) {
        log.info("userOrder.queryUserOrderList:page={}&size={}&userId={}&storeId={}&orderType={}&orderStatus={}&name={}&startTime={}&endTime={}", page, size, userId, storeId, couponType, orderStatus, name, startTime, endTime);
        if (StringUtils.isNotBlank(userId) && StringUtils.isNotBlank(storeId)) {
            return R.fail("用户id和商户id不能同时为空");
        }
        return R.data(lifeUserOrderService.queryUserOrderList(page, size, userId, storeId, couponType, orderStatus, name, startTime, endTime));
    }

    @ApiOperation("查询订单详情")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({@ApiImplicitParam(name = "longitude", value = "经度", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "latitude", value = "纬度", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "orderId", value = "订单id", dataType = "String", paramType = "query", required = true)})
    @GetMapping("/queryUserOrderDetail")
    public R queryUserOrderDetail(@RequestParam String orderId,
                                  @RequestParam(value = "longitude", required = false) String longitude,
                                  @RequestParam(value = "latitude", required = false) String latitude) {
        log.info("userOrder.queryUserOrderDetail:orderId={}&longitude={}&latitude={}", orderId, longitude, latitude);
        return lifeUserOrderService.queryUserOrderDetail(orderId, longitude, latitude);
    }

    @ApiOperation("提交支付团购套餐和代金券前检查")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({@ApiImplicitParam(name = "couponId", value = "优惠券id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "couponType", value = "优惠券类型，1代金券；2团购券", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "storeId", value = "商户id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "count", value = "数量", dataType = "int", paramType = "query", required = true)})
    @GetMapping("/buyCouponCheck")
    public R<Map<String, Object>> buyCouponCheck(@RequestParam("couponId") String couponId,
                                                 @RequestParam("couponType") Integer couponType,
                                                 @RequestParam("userId") String userId,
                                                 @RequestParam("storeId") String storeId,
                                                 @RequestParam("count") int count) {
        log.info("userOrder.buyCouponCheck:couponId={}&couponType={}&userId={}&storeId={}&count={}", couponId, couponType, userId, storeId, count);
        return R.data(lifeUserOrderService.buyCouponCheck(couponId, couponType, userId, storeId, count));
    }

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

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


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

    @ApiOperation("查询订单列表-用户端（包含各状态订单数量统计）")
    @ApiOperationSupport(order = 8)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true),
            @ApiImplicitParam(name = "size", value = "每页数量", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", required = false),
            @ApiImplicitParam(name = "storeId", value = "商户id", required = false),
            @ApiImplicitParam(name = "couponType", value = "订单类型,-1,全部（可以不传）,1,代金券；2，团购；", required = false),
            @ApiImplicitParam(name = "orderStatus", value = "订单状态,-1，全部（可以不传）；1，已支付/待使用;2，已完成；5，退款/售后", required = false),
            @ApiImplicitParam(name = "name", value = "订单名称", required = false)
    })
    @GetMapping("/queryUserOrderListWithStatistics")
    public R<Map<String, Object>> queryUserOrderListWithStatistics(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size,
            @RequestParam(required = false) String userId,
            @RequestParam(required = false) String storeId,
            @RequestParam(required = false, defaultValue = "-1") String couponType,
            @RequestParam(required = false, defaultValue = "-1") String orderStatus,
            @RequestParam(required = false) String name,
            @RequestParam(value = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd 00:00:00") String startTime,
            @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd 23:59:59") String endTime) {
        log.info("userOrder.queryUserOrderListWithStatistics:page={}&size={}&userId={}&storeId={}&orderType={}&orderStatus={}&name={}&startTime={}&endTime={}",
                page, size, userId, storeId, couponType, orderStatus, name, startTime, endTime);
        if (StringUtils.isNotBlank(userId) && StringUtils.isNotBlank(storeId)) {
            return R.fail("用户id和商户id不能同时为空");
        }
        return R.data(lifeUserOrderService.queryUserOrderListWithStatistics(page, size, userId, storeId, couponType, orderStatus, name, startTime, endTime));
    }
}
