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

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.wzj.soopin.order.domain.entity.Order;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Data
@Schema(description = "管理后台订单VO")
public class ManagerOrderVO {
    @Schema(name = "id", description = "订单id", required = true, implementation = Long.class)
    private Long id;

    @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 = "nickName", description = "用户昵称", required = true, implementation = String.class)
    private String nickName;

    @Schema(name = "avatar", description = "用户头像", required = true, implementation = String.class)
    private String avatar;

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

    @Schema(description="是否处于退款状态")
    private Boolean aftersale;

    @Schema(name = "pic", description = "商品图片", required = true, implementation = String.class)
    private String pic;

    @Schema(name = "userName", description = "用户名称", required = true, implementation = String.class)
    private String userName;

    @Schema(name = "buyNum", description = "购买数量", required = true, implementation = Integer.class)
    private Integer buyNum;

    @Schema(name = "totalAmount", description = "订单总金额", required = true, implementation = BigDecimal.class)
    private BigDecimal totalAmount;

    @Schema(name = "payAmount", description = "应付金额", required = true, implementation = BigDecimal.class)
    private BigDecimal payAmount;

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

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

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

    @Schema(name = "receiveTime", description = "确认收货时间", required = true, implementation = LocalDateTime.class)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime receiveTime;

    @Schema(name = "note", description = "备注", required = true, implementation = String.class)
    private String note;

    @Schema(description = "商家备注")
    private String merchantNote;

    @Schema(name = "spData", description = "商品sku属性", required = true, implementation = String.class)
    private String spData;

    @Schema(name = "productInfo", description = "订单商品数据")
    private List<ManagerOrderProductVO> productList;

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

    @Schema(name = "deliverySn", description = "物流单号", required = true, implementation = String.class)
    private String deliverySn;

    private String receiverName;

    private String receiverPhone;

    private String receiverProvince;

    private String receiverCity;

    private String receiverDistrict;

    private String receiverDetailAddress;

    private String mark;

    private BigDecimal couponAmount;

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

        if (id != null) {
            queryWrapper.eq(Order::getId, id);
        }
        if (orderSn != null && !orderSn.isEmpty()) {
            queryWrapper.eq(Order::getOrderSn, orderSn);
        }

        if (status != null) {
            queryWrapper.eq(Order::getStatus, status);
        }
        if (aftersale != null) {
            queryWrapper.eq(Order::getAftersale, aftersale);
        }
        if (totalAmount != null) {
            queryWrapper.eq(Order::getTotalAmount, totalAmount);
        }
        if (payAmount != null) {
            queryWrapper.eq(Order::getPayAmount, payAmount);
        }
        if (createTime != null) {
            queryWrapper.eq(Order::getCreateTime, createTime);
        }

        if (payType != null) {
            queryWrapper.eq(Order::getPayType, payType);
        }

        if (note != null && !note.isEmpty()) {
            queryWrapper.like(Order::getNote, note);
        }
        if (merchantNote != null && !merchantNote.isEmpty()) {
            queryWrapper.like(Order::getMerchantNote, merchantNote);
        }
        if (receiverProvince != null && !receiverProvince.isEmpty()) {
            queryWrapper.eq(Order::getReceiverProvince, receiverProvince);
        }
        if (receiverCity != null && !receiverCity.isEmpty()) {
            queryWrapper.eq(Order::getReceiverCity, receiverCity);
        }
        if (receiverDistrict != null && !receiverDistrict.isEmpty()) {
            queryWrapper.eq(Order::getReceiverDistrict, receiverDistrict);
        }
        if (receiverDetailAddress != null && !receiverDetailAddress.isEmpty()) {
            queryWrapper.like(Order::getReceiverDetailAddress, receiverDetailAddress);
        }
        if (couponAmount != null) {
            queryWrapper.eq(Order::getCouponAmount, couponAmount);
        }

        return queryWrapper;
    }
}
