All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fastchar.extjs.systemtool.entity.FinalLogHttpEntity Maven / Gradle / Ivy

The newest version!
package com.fastchar.extjs.systemtool.entity;

import com.fastchar.annotation.AFastClassFind;
import com.fastchar.core.FastChar;
import com.fastchar.database.FastPage;
import com.fastchar.database.info.FastSqlInfo;
import com.fastchar.extjs.FastExtHelper;
import com.fastchar.extjs.core.FastExtEntity;
import com.fastchar.utils.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 系统HTTP日志数据库实体类
 *
 * @author Janesen
 * @date 2021/08/23 10:38
 */
@AFastClassFind("com.fastchar.http.core.FastHttpRequest")
public class FinalLogHttpEntity extends FastExtEntity {
    private static final long serialVersionUID = 1L;

    public static FinalLogHttpEntity dao() {
        return FastChar.getOverrides().singleInstance(FinalLogHttpEntity.class);
    }

    public static FinalLogHttpEntity newInstance() {
        return FastChar.getOverrides().newInstance(FinalLogHttpEntity.class);
    }

    @Override
    public String getTableName() {
        return "final_log_http";
    }

    @Override
    public String getTableDetails() {
        return "系统HTTP日志";
    }

    @Override
    public String getEntityCode() {
        return this.getClass().getSimpleName();
    }

    @Override
    public FastPage showList(int page, int pageSize) {


        String sqlStr = "select t.*" +
                " from final_log_http as t" +
                " ";
        FastSqlInfo sqlInfo = toSelectSql(sqlStr);
        return selectBySql(page, pageSize, sqlInfo.getSql(), sqlInfo.toParams());
    }

    @Override
    public void setDefaultValue() {
        set("logId", FastMD5Utils.MD5To16(FastStringUtils.buildUUID()));
        set("logDateTime", FastDateUtils.getDateString());
    }

    @Override
    public void convertValue() {
        super.convertValue();

    }


    /**
     * 获得数据详情
     */
    public FinalLogHttpEntity getDetails(int logId) {

        String sqlStr = "select t.* from final_log_http as t" +
                " " +
                " where t.logId = ?  ";
        FinalLogHttpEntity entity = selectFirstBySql(sqlStr, logId);
        if (entity != null) {
            //to-do something
        }
        return entity;
    }

    /**
     * 获得本实体列表集合
     *
     * @return 分页数据
     */
    public FastPage getList(int page, int pageSize, Map where, Map sort) {


        StringBuilder sqlStr = new StringBuilder("select t.* from final_log_http as t" +
                " " +
                " where 1=1 ");

        List values = new ArrayList<>();

        if (where != null) {
            for (String key : where.keySet()) {
                if (key.startsWith("^")) {
                    continue;
                }
                Object value = where.get(key);
                if (FastNumberUtils.isNumber(value)) {
                    sqlStr.append(" and t.").append(key).append(" = ? ");
                    values.add(value);
                } else {
                    sqlStr.append(" and t.").append(key).append(" like ? ");
                    values.add("%" + value + "%");
                }
            }
        }

        if (sort != null) {
            List sortKeys = new ArrayList<>();
            for (String key : sort.keySet()) {
                if (key.startsWith("^")) {
                    continue;
                }
                sortKeys.add(" t." + key + " " + sort.get(key));
            }
            if (sortKeys.size() > 0) {
                sqlStr.append(" order by ").append(FastStringUtils.join(sortKeys, ","));
            }
        }

        FastPage pageList = selectBySql(page, pageSize, sqlStr.toString(), values);
        for (FinalLogHttpEntity entity : pageList.getList()) {

        }
        return pageList;
    }


    public void clearData(int keepDay) {
        ReentrantLock lock = FastLockUtils.getLock(this.getClass().getSimpleName());
        if (lock.isLocked()) {
            return;
        }
        lock.lock();
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Calendar minCalendar = Calendar.getInstance();
            minCalendar.add(Calendar.DAY_OF_MONTH, -Math.max((keepDay - 1), 0));
            String format = simpleDateFormat.format(minCalendar.getTime());

            String sqlStr = "delete from " + getTableName() + " where logDateTime < ? ";

            int updateBySql = updateBySql(sqlStr, format);
            if (updateBySql > 0) {
                FastChar.getLogger().info(this.getClass(), "已成功删除" +
                        format + "之前的" + updateBySql + "条系统SQL日志!");
            }
        } finally {
            lock.unlock();
        }

    }


}