package com.wzj.soopin.order.domain.form;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.wzj.soopin.order.domain.vo.ManagerOrderVO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDateTime;

@Data
@Schema(description = "后台订单查询请求体")
public class ManagerOrderQueryForm {
    @Schema(name = "orderId", description = "订单id", required = true, implementation = Long.class)
    private Long orderId;

    @Schema(name = "orderSn", description = "订单编号", required = true, implementation = String.class)
    private String orderSn;

    @Schema(name = "productId", description = "商品id", required = true, implementation = Long.class)
    private Long productId;

    @Schema(name = "productName", description = "商品名称", required = true, implementation = String.class)
    private String productName;

    @Schema(name = "userPhone", description = "用户名称（手机号）", required = true, implementation = String.class)
    private String userPhone;

    @Schema(name = "payType", description = "支付方式 0->未支付；1->支付宝；2->微信", required = true, implementation = Integer.class)
    private Integer payType;

    @Schema(name = "status", description = "订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单", required = true, implementation = Integer.class)
    private Integer status;

    @Schema(description = "省份/直辖市 精确匹配")
    private String receiverProvince;

    @Schema(description = "城市 精确匹配")
    private String receiverCity;

    @Schema(description = "区 精确匹配")
    private String receiverDistrict;

    @Schema(name = "startTime", description = "开始时间", required = true, implementation = LocalDateTime.class)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime startTime;

    @Schema(name = "endTime", description = "结束时间", required = true, implementation = LocalDateTime.class)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime endTime;

    public LambdaQueryWrapper<ManagerOrderVO> toWrapper() {
        LambdaQueryWrapper<ManagerOrderVO> queryWrapper = new LambdaQueryWrapper<>();

        if (orderSn != null && !orderSn.isEmpty()) {
            queryWrapper.eq(ManagerOrderVO::getOrderSn, orderSn);
        }
        if (productId != null) {
            queryWrapper.eq(ManagerOrderVO::getProductId, productId);
        }
        if (productName != null && !productName.isEmpty()) {
            queryWrapper.like(ManagerOrderVO::getProductName, productName);
        }
        if (userPhone != null && !userPhone.isEmpty()) {
            queryWrapper.eq(ManagerOrderVO::getUserPhone, userPhone);
        }
        if (payType != null) {
            queryWrapper.eq(ManagerOrderVO::getPayType, payType);
        }
        if (status != null) {
            queryWrapper.eq(ManagerOrderVO::getStatus, status);
        }
        if (receiverProvince != null && !receiverProvince.isEmpty()) {
            queryWrapper.eq(ManagerOrderVO::getReceiverProvince, receiverProvince);
        }
        if (receiverCity != null && !receiverCity.isEmpty()) {
            queryWrapper.eq(ManagerOrderVO::getReceiverCity, receiverCity);
        }
        if (receiverDistrict != null && !receiverDistrict.isEmpty()) {
            queryWrapper.eq(ManagerOrderVO::getReceiverDistrict, receiverDistrict);
        }
        if (startTime != null && endTime != null) {
            queryWrapper.between(ManagerOrderVO::getCreateTime, startTime, endTime);
        } else if (startTime != null) {
            queryWrapper.ge(ManagerOrderVO::getCreateTime, startTime);
        } else if (endTime != null) {
            queryWrapper.le(ManagerOrderVO::getCreateTime, endTime);
        }

        return queryWrapper;
    }
}
