<commit_before>package io.github.ihongs.serv.auth;

import io.github.ihongs.Cnst;
import io.github.ihongs.Core;
import io.github.ihongs.CoreSerial;
import io.github.ihongs.HongsException;
import io.github.ihongs.action.ActionHelper;
import io.github.ihongs.db.DB;
import io.github.ihongs.db.util.FetchCase;
import io.github.ihongs.db.Table;
import io.github.ihongs.util.Syno;
import io.github.ihongs.util.Synt;
import java.io.File;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 用户组记录
 * @author Hongs
 */
public class RoleSet extends CoreSerial implements CoreSerial.LastModified, Set<String> {

    public /**/String userId;
    public Set<String> roles;
    public long        rtime;

    private RoleSet(String userId) throws HongsException {
        this.userId = userId;
        init("master/role/" + Syno.splitPath(userId));
    }

    @Override
    protected void imports() throws HongsException {
        roles = new HashSet();

        DB        db;
        Table     tb;
        Table     td;
        Table     tt;
        FetchCase fc;
        List<Map> rz;

        db = DB.getInstance("master");

        //** 查询用户权限 **/

        tb = db.getTable("user_role");
        fc = new FetchCase( FetchCase.STRICT )
                .from  (tb.tableName, tb.name)
                .select(tb.name+".role")
                .filter(tb.name+".user_id = ?", userId);
        rz = db.fetchMore(fc);
        for (Map rm : rz) {
            roles.add((String) rm.get("role"));
        }

        //** 查询部门权限 **/

        tb = db.getTable("dept_role");
        td = db.getTable("dept_user");
        tt = db.getTable("dept");
        fc = new FetchCase( FetchCase.STRICT )
                .from  (tb.tableName, tb.name)
                .join  (td.tableName, td.name , tb.name+".dept_id = "+td.name+".dept_id")
                .join  (tt.tableName, tt.name , td.name+".dept_id = "+tt.name+".id" /**/)
                .select(tb.name+".role")
                .filter(td.name+".user_id = ?", userId)
                .filter(tt.name+".state > 0" );
        rz = db.fetchMore(fc);
        for (Map rm : rz) {
            roles.add((String) rm.get("role"));
        }

        //** 当前保存时间 **/

        rtime = System.currentTimeMillis() / 1000L;
    }

    @Override
    protected byte read(File f) throws HongsException {
        DB        db;
        Table     tb;
        Table     td;
        FetchCase fc;
        Map       rs;
        int       st;
        long      rt;
        long      ot;

        db = DB.getInstance("master");

        tb = db.getTable("user");
        fc = new FetchCase( FetchCase.STRICT )
                .from  (tb.tableName, tb.name)
                .select(tb.name+".state, "+tb.name+".rtime")
                .filter(tb.name+".id = ?", userId);
        rs = db.fetchLess(fc);
        st = Synt.declare(rs.get("state"), 0 );
        rt = Synt.declare(rs.get("rtime"), 0L);
        if (st <= 0) {
            return -1; // 用户不存在或已锁定，则删除
        }

        tb = db.getTable("dept");
        td = db.getTable("dept_user");
        fc = new FetchCase( FetchCase.STRICT )
                .from  (tb.tableName, tb.name)
                .join  (td.tableName, td.name , td.name+".dept_id = "+tb.name+".id" /***/ )
                .select("MAX("+tb.name+".state) AS state, MAX("+tb.name+".rtime) AS rtime")
                .filter(td.name+".user_id = ?", userId   )
                .gather(td.name+".user_id");
        rs = db.fetchLess(fc);
        st = Synt.declare(rs.get("state"), 1 );
        ot = Synt.declare(rs.get("rtime"), 0L);
        if (st <= 0) {
            return -1; // 所在的分组均已锁定，则删除<commit_msg>重构缓存策略<commit_after>package io.github.ihongs.serv.auth;

import io.github.ihongs.Cnst;
import io.github.ihongs.Core;
import io.github.ihongs.CoreSerial;
import io.github.ihongs.HongsException;
import io.github.ihongs.action.ActionHelper;
import io.github.ihongs.db.DB;
import io.github.ihongs.db.util.FetchCase;
import io.github.ihongs.db.Table;
import io.github.ihongs.util.Syno;
import io.github.ihongs.util.Synt;
import java.io.File;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 用户组记录
 * @author Hongs
 */
public class RoleSet extends CoreSerial implements CoreSerial.LastModified, Set<String> {

    public /**/String userId;
    public Set<String> roles;
    public long        rtime;

    private RoleSet(String userId) throws HongsException {
        this.userId = userId;
        init("master/role/" + Syno.splitPath(userId));
    }

    @Override
    protected void imports() throws HongsException {
        roles = new HashSet();

        DB        db;
        Table     tb;
        Table     td;
        Table     tt;
        FetchCase fc;
        List<Map> rz;

        db = DB.getInstance("master");

        //** 查询用户权限 **/

        tb = db.getTable("user_role");
        fc = new FetchCase( FetchCase.STRICT )
                .from  (tb.tableName, tb.name)
                .select(tb.name+".role")
                .filter(tb.name+".user_id = ?", userId);
        rz = db.fetchMore(fc);
        for (Map rm : rz) {
            roles.add((String) rm.get("role"));
        }

        //** 查询部门权限 **/

        tb = db.getTable("dept_role");
        td = db.getTable("dept_user");
        tt = db.getTable("dept");
        fc = new FetchCase( FetchCase.STRICT )
                .from  (tb.tableName, tb.name)
                .join  (td.tableName, td.name , tb.name+".dept_id = "+td.name+".dept_id")
                .join  (tt.tableName, tt.name , td.name+".dept_id = "+tt.name+".id" /**/)
                .select(tb.name+".role")
                .filter(td.name+".user_id = ?", userId)
                .filter(tt.name+".state > 0" );
        rz = db.fetchMore(fc);
        for (Map rm : rz) {
            roles.add((String) rm.get("role"));
        }

        //** 当前保存时间 **/

        rtime = System.currentTimeMillis() / 1000L;
    }

    @Override
    protected byte read(File f) throws HongsException {
        DB        db;
        Table     tb;
        Table     td;
        FetchCase fc;
        Map       rs;
        int       st;
        long      rt;
        long      ot;

        db = DB.getInstance("master");

        tb = db.getTable("user");
        fc = new FetchCase( FetchCase.STRICT )
                .from  (tb.tableName, tb.name)
                .select(tb.name+".state, "+tb.name+".rtime")
                .filter(tb.name+".id = ?", userId);
        rs = db.fetchLess(fc);
        st = Synt.declare(rs.get("state"), 0 );
        rt = Synt.declare(rs.get("rtime"), 0L);
        if (st <= 0) {
            rtime = 0;
            return -1; // 用户不存在或已锁定，则删除
        }

        tb = db.getTable("dept");
        td = db.getTable("dept_user");
        fc = new FetchCase( FetchCase.STRICT )
                .from  (tb.tableName, tb.name)
                .join  (td.tableName, td.name , td.name+".dept_id = "+tb.name+".id" /***/ )
                .select("MAX("+tb.name+".state) AS state, MAX("+tb.name+".rtime) AS rtime")
                .filter(td.name+".user_id = ?", userId   )
                .gather(td.name+".user_id");
        rs = db.fetchLess(fc);
        st = Synt.declare(rs.get("state"), 1 );
        ot = Synt.declare(rs.get("rtime"), 0L);
        if (st <= 0) {
            rtime = 0;
            return -1; // 所在的分组均已锁定，则删除
{'fraction_non_alphanumeric': 0.15382568196939456, 'fraction_numerical': 0.0037258815701929474, 'mean_word_length': 1.760190965846493, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3957892', 'n_tokens_mistral': 2648, 'n_tokens_neox': 2595, 'n_words': 485}