当前位置:网站首页>MP advanced operation: time operation, SQL, querywapper, lambdaquerywapper (condition constructor) quick filter enumeration class

MP advanced operation: time operation, SQL, querywapper, lambdaquerywapper (condition constructor) quick filter enumeration class

2022-07-04 23:15:00 pingzhuyan

Catalog

1. Original method of time screening : 

 2. sql sentence :TIMESTAMPDIFF() Of Method Use  

Query the current time 30 Minutes ago ( Be overdue 30 minute ) and 60 Minutes later ( Due soon 60 minute )

3.  Integrate condition constructors ( Time )

3.1 Write an enumeration class ( Time )

3.2  Write a class These three attributes are required

3.3 Condition constructor writing

3.4  Use in code


1. Original method of time screening : 

obtain Starting time -> End time  

A month , A year All need calculation

queryWapper.ge(timeStatus, "update_date", appointEntity.getStartTime())// Greater than the start time 
                .le(timeStatus, "update_date", appointEntity.getEndTime())// Less than or equal to the end time 

 2. sql sentence :TIMESTAMPDIFF() Of Method Use  

Query the current time 30 Minutes ago ( Be overdue 30 minute ) and 60 Minutes later ( Due soon 60 minute )

select id from  surface  
           where (TIMESTAMPDIFF(MINUTE, #{nowTime}, ap_time) >0 and  TIMESTAMPDIFF(MINUTE, #{nowTime}, ap_time) <= 60 )
          or 
           (TIMESTAMPDIFF(MINUTE, ap_time, #{nowTime})>0 and TIMESTAMPDIFF(MINUTE, ap_time, #{nowTime}) <30) 

3.  Integrate condition constructors ( Time )

3.1 Write an enumeration class ( Time )

package com.aisce.common.enums;

public enum DataTimeTypeEnum {

    /**
     *  Last week 
     */
    ONE_WEEK(1, " Last week "),
    /**
     *  Nearly a month 
     */
    ONE_MONTH(2, " Nearly a month "),
    /**
     *  Nearly three months 
     */
    THREE_MONTH(3, " Nearly three months "),
    /**
     *  Nearly a year 
     */
    ONE_YEAR(4, " Nearly a year "),
    /**
     *  all 
     */
    ALL(5, " all "),
    /**
     *  Customize 
     */
    CUSTOMIZE(6, " Customize ")
    ;
    private final int code;
    private final String name;

    DataTimeTypeEnum(int code, String info) {
        this.code = code;
        this.name = info;
    }

    public static String getValue(int code) {
        DataTimeTypeEnum[] enums = values();
        for (DataTimeTypeEnum item : enums) {
            if (item.code == code) {
                return item.getName();
            }
        }
        return null;
    }

    public int getCode() {
        return code;
    }

    public String getName() {
        return name;
    }
}

3.2  Write a class These three attributes are required

   /**
     *  Time type 
     */
    @TableField(exist = false )
    private Integer TimeType ;
    /**
     *  Starting time 
     */
    @TableField(exist = false )
    private Date startTime;

    /**
     *  End time 
     */
    @TableField(exist = false )
    private Date endTime;

3.3 Condition constructor writing

/**
     * lambdaQuery  Ultimate writing   Time planning 
     * @param dto
     * @param <T>
     * @return
     */
    private <T extends AixiBillRecord> LambdaQueryWrapper<T> buildQueryWrapper(DataBaseDTO dto){
        Integer timeType = dto.getTimeType();
//        Integer timeType = 3;
        Date currentDate = new Date();
        LambdaQueryWrapper<T> lqw = Wrappers.lambdaQuery();
        lqw.ge(timeType == DataTimeTypeEnum.ONE_WEEK.getCode(), T::getCreateTime, DateUtil.offsetWeek(currentDate, -1));
        lqw.ge(timeType == DataTimeTypeEnum.ONE_MONTH.getCode(), T::getCreateTime, DateUtil.offsetMonth(currentDate, -1));
        lqw.ge(timeType == DataTimeTypeEnum.THREE_MONTH.getCode(), T::getCreateTime, DateUtil.offsetMonth(currentDate, -3));
        lqw.ge(timeType == DataTimeTypeEnum.ONE_YEAR.getCode(), T::getCreateTime, DateUtil.offsetMonth(currentDate, -12));
        lqw.ge(timeType == DataTimeTypeEnum.CUSTOMIZE.getCode(), T::getCreateTime, dto.getStartTime());
        lqw.le(timeType == DataTimeTypeEnum.CUSTOMIZE.getCode(), T::getCreateTime, dto.getEndTime());
        return lqw;
    }

3.4  Use in code

( There may be a better way to use Searching )

DataBaseDTO dto = new DataBaseDTO();
dto.setTimeType(1);
        
LambdaQueryWrapper<*> LambdaQueryWrapper = buildQueryWrapper(dto);
LambdaQueryWrapper.eq(*::getCompanyId,user.getCompanyId());
   
// Get all the data first 
List<*> RecordList = baseMapper.selectList(LambdaQueryWrapper);

原网站

版权声明
本文为[pingzhuyan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207042234410380.html