package com.wzj.soopin.content.domain.bo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wzj.soopin.content.domain.po.Vlog;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
import org.dromara.common.core.domain.BaseBO;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 视频上传业务参数
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class VlogBO extends BaseBO {
    private String id;

    @ApiModelProperty(value = "作者id", hidden = true)
    private String vlogerId;
    private String url;
    private String cover;
    private String title;
    private Integer width;
    private Integer height;
    private Integer likeCounts;
    private Integer commentsCounts;
    private String fileId;

    private String reason;
    private Integer cityCode;
    private String firstFrameImg;

    @ApiModelProperty("排序字段，例如 commentCounts, likeCounts, reason")
    private String Column;

    @ApiModelProperty("排序方向，true 为升序，false 为降序")
    private Boolean asc;

    @ApiModelProperty("手机号(模糊查询)")
    private String mobile;

    @ApiModelProperty("用户昵称(模糊查询)")
    private String nickname;

    @ApiModelProperty("视频标题(模糊查询)")
    private String titleQuery;

    @ApiModelProperty("视频状态(精确查询)")
    private Integer status;

    @ApiModelProperty("创建时间范围-开始时间,格式:yyyy-MM-dd HH:MM:ss")
    private LocalDateTime startTime;

    @ApiModelProperty("创建时间范围-结束时间,格式:yyyy-MM-dd HH:MM:ss")
    private LocalDateTime endTime;

    private List<String> blockVd;

    private List<String> blockUser;

    private List<String> ids;

    @Override
    public LambdaQueryWrapper<Vlog> toWrapper() {
        LambdaQueryWrapper<Vlog> wrapper = new LambdaQueryWrapper<>();

        // 手机号和昵称的查询条件将不在toWrapper中处理，而是在Service层处理
        // if (StringUtils.isNotBlank(mobile)) {
        //     wrapper.like(Vlog::getMobile, mobile);
        // }
        // if (StringUtils.isNotBlank(nickname)) {
        //     wrapper.like(Vlog::getNickname, nickname);
        // }

        if (StringUtils.isNotBlank(titleQuery)) {
            wrapper.like(Vlog::getTitle, titleQuery);
        }

        // 添加精确查询条件
        if (status != null) {
            wrapper.eq(Vlog::getStatus, status);
        }

        // 添加时间范围查询
        if (startTime != null && endTime != null) {
            String startTimeStr = startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            String endTimeStr = endTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            wrapper.ge(Vlog::getCreateTime, startTimeStr);
            wrapper.lt(Vlog::getCreateTime, endTimeStr);
        }

        // 添加排序条件
        if (StringUtils.isNotBlank(Column)) {
            String column = org.dromara.common.core.utils.StringUtils.toUnderScoreCase(Column);
            boolean Asc = Boolean.TRUE.equals(asc);

            if ("reason".equals(column)) {
                // 自定义状态排序：待审核, 复审, 通过, 不通过
                if (Asc) {
                    wrapper.apply("ORDER BY FIELD(reason, 0, 1, 2, 3)");
                } else {
                    wrapper.apply("ORDER BY FIELD(reason, 3, 2, 1, 0)"); // 反向排序
                }
            } else if ("create_time".equals(column)) { // 假设create_time是Vlog的直接字段
                if (Asc) {
                    wrapper.orderByAsc(Vlog::getCreateTime);
                } else {
                    wrapper.orderByDesc(Vlog::getCreateTime);
                }
            }
            // like_counts 和 comments_counts 不在Vlog实体中，无法在此直接排序
            // 需要在Service层获取数据后进行内存排序，或修改Mapper进行联表查询
        } else {
            // 默认按创建时间倒序排序
            wrapper.orderByDesc(Vlog::getCreateTime);
        }

        return wrapper;
    }
}
