package com.zsc.edu.gateway.modules.operationLog.query;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zsc.edu.gateway.modules.operationLog.entity.OperationLog;
import com.zsc.edu.gateway.modules.operationLog.entity.FunctionTypeEnum;
import com.zsc.edu.gateway.modules.operationLog.entity.ModuleTypeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author zhuang
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class OperationLogQuery {
    private String username;
    private ModuleTypeEnum moduleType;
    private FunctionTypeEnum functionType;
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime publishTimeBegin;
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime publishTimeEnd;

    public LambdaQueryWrapper<OperationLog> wrapper() {
        LambdaQueryWrapper<OperationLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(this.username), OperationLog::getContent, this.username);
        queryWrapper.eq(Objects.nonNull(this.moduleType), OperationLog::getModuleType, this.moduleType);
        queryWrapper.eq(Objects.nonNull(this.functionType), OperationLog::getFunctionType, this.functionType);
        if (publishTimeBegin != null && publishTimeEnd != null) {
            queryWrapper.between(OperationLog::getMakeTime, this.publishTimeBegin, this.publishTimeEnd);
        }
        return queryWrapper;
    }
}
