package shop.alien.storeplatform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.vo.LifeUserOrderVo;
import shop.alien.storeplatform.service.LifeUserOrderPlatformService;

/**
 * 用户订单控制器
 * 提供订单相关的接口服务，包括订单的创建、更新、删除、查询等功能
 * 支持代金券和团购券类型的订单管理，以及订单退款等操作
 *
 * @author alien-cloud
 * @date 2025-11-18
 */
@Api(tags = {"商家端-订单"})
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/userOrderPlatform")
@RequiredArgsConstructor
public class LifeUserOrderPlatformController {

    private final LifeUserOrderPlatformService lifeUserOrderService;

    /**
     * 查询订单列表
     * 支持按用户、商户、订单类型、订单状态、时间范围等多条件分页查询订单
     *
     * @param pageNum 当前页码
     * @param pageSize 每页数量
     * @param userId 用户ID，可选
     * @param storeId 商户ID，可选
     * @param couponType 订单类型，-1：全部，1：代金券，2：团购
     * @param status 订单状态，-1：全部，1：已支付/待使用，2：已完成，5：退款/售后
     * @param couponName 订单名称，可选
     * @param startTime 开始时间，可选
     * @param endTime 结束时间，可选
     * @return R<IPage<LifeUserOrderVo>> 返回分页订单列表
     * @author alien-cloud
     * @date 2025-11-18
     */
    @ApiOperation("查询订单列表")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = true),
            @ApiImplicitParam(name = "pageSize", 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 = "pageNum", defaultValue = "1") Integer pageNum,
                                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                        @RequestParam(required = false) String userId,
                                                        @RequestParam(required = false) String storeId,
                                                        @RequestParam(required = false, defaultValue = "-1") String couponType,
                                                        @RequestParam(required = false, defaultValue = "-1") String status,
                                                        @RequestParam(required = false) String couponName,
                                                        @RequestParam(required = false) String orderNo,
                                                        @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={}", pageNum, pageSize, userId, storeId, couponType, status, couponName, startTime, endTime);

        if (storeId == null || storeId == "" ) {
            return R.data(null);
        }
        return R.data(lifeUserOrderService.queryUserOrderList(pageNum, pageSize, userId, storeId, couponType, status, couponName, startTime, endTime, orderNo));
    }

    /**
     * 查询订单详情
     * 根据订单ID查询订单的详细信息，包括商户信息、距离计算等
     *
     * @param orderId 订单ID
     * @param longitude 经度，可选，用于计算距离
     * @param latitude 纬度，可选，用于计算距离
     * @return R 返回订单详情信息
     * @author alien-cloud
     * @date 2025-11-18
     */
    @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);
    }


}

