use of io.github.ihongs.db.Table in project HongsCORE by ihongs.
the class DataCmdlet method revert.
public static void revert(String[] args, Inst df) throws HongsException, InterruptedException {
Map opts = CmdletHelper.getOpts(args, new String[] { "conf=s", "form=s", "user:s", "memo:s", "time:i", "bufs:i", "drop:b", "includes:b", "cascades:b", "!A", "?Usage: revert --conf CONF_NAME --form FORM_NAME [--time TIMESTAMP] ID0 ID1 ..." });
String conf = (String) opts.get("conf");
String form = (String) opts.get("form");
String user = (String) opts.get("user");
String memo = (String) opts.get("memo");
long ct = Synt.declare(opts.get("time"), 0L);
int bn = Synt.declare(opts.get("bufs"), 1000);
Set<String> ds = Synt.asSet(opts.get(""));
Data dr = df.getInstance(conf, form);
dr.setUserId(Synt.defoult(user, Cnst.ADM_UID));
// user = dr.getUserId( );
form = dr.getFormId();
Map sd = new HashMap();
sd.put("memo", memo);
Table tb = dr.getTable();
String tn = tb.tableName;
// 查询迭代
Loop lp;
// 操作总数
int c = 0;
// 变更计数
int i = 0;
// 事务计数
int j = 0;
if (ct == 0) {
String fa = "`a`.*";
String fc = "COUNT(*) AS _cnt_";
String qa = "SELECT " + fa + " FROM `" + tn + "` AS `a` WHERE `a`.`form_id` = ? AND `a`.`etime` = ?";
String qc = "SELECT " + fc + " FROM `" + tn + "` AS `a` WHERE `a`.`form_id` = ? AND `a`.`etime` = ?";
if (!ds.isEmpty()) {
c = ds.size();
qa = qa + " AND a.id IN (?)";
lp = tb.db.query(qa, 0, 0, form, 0, ds);
} else {
lp = tb.db.query(qa, 0, 0, form, 0);
c = Synt.declare(tb.db.fetchOne(qc, form, 0).get("_cnt_"), 0);
}
} else {
String fx = "`x`.*";
String fa = "`a`.id, MAX(a.ctime) AS ctime";
String fc = "COUNT(DISTINCT a.id) AS _cnt_";
String qa = "SELECT " + fa + " FROM `" + tn + "` AS `a` WHERE `a`.`form_id` = ? AND `a`.`ctime` <= ?";
String qc = "SELECT " + fc + " FROM `" + tn + "` AS `a` WHERE `a`.`form_id` = ? AND `a`.`ctime` <= ?";
String qx = " WHERE x.id = b.id AND x.ctime = b.ctime AND x.`form_id` = ? AND x.`ctime` <= ?";
if (!ds.isEmpty()) {
c = ds.size();
qa = qa + " AND a.id IN (?)";
qx = qx + " AND x.id IN (?)";
qa = qa + " GROUP BY `a`.id";
qx = "SELECT " + fx + " FROM `" + tn + "` AS `x`, (" + qa + ") AS `b` " + qx;
lp = tb.db.query(qx, 0, 0, form, ct, ds, form, ct, ds);
} else {
qa = qa + " GROUP BY `a`.id";
qx = "SELECT " + fx + " FROM `" + tn + "` AS `x`, (" + qa + ") AS `b` " + qx;
lp = tb.db.query(qx, 0, 0, form, ct, form, ct);
c = Synt.declare(tb.db.fetchOne(qc, form, ct).get("_cnt_"), 0);
}
}
/**
* 清空全部数据
* 以便更新结构
*/
if (Synt.declare(opts.get("drop"), false)) {
IndexWriter iw = dr.getWriter();
/**/
String pd = dr.getPartId();
try {
if (pd != null && !pd.isEmpty()) {
iw.deleteDocuments(new Term("@" + Data.PART_ID_KEY, pd));
} else {
iw.deleteAll();
}
iw.commit();
iw.deleteUnusedFiles();
iw.maybeMerge();
} catch (IOException ex) {
throw new HongsException(ex);
}
}
/**
* 级联更新操作
* 默认不作级联
*/
Casc da = new Casc(dr, Synt.declare(opts.get("includes"), false), Synt.declare(opts.get("cascades"), false));
boolean pr = (Core.DEBUG == 0);
long tm = System.currentTimeMillis();
long tc = tm / 1000;
if (pr)
CmdletHelper.progres(tm, c, i);
dr.begin();
for (Map od : lp) {
String id = (String) od.get(Cnst.ID_KEY);
if (Synt.declare(od.get("etime"), 0L) != 0L) {
if (Synt.declare(od.get("state"), 1) >= 1) {
sd.put("rtime", od.get("ctime"));
dr.rev(id, sd, tc);
} else {
dr.del(id, sd, tc);
}
} else {
if (Synt.declare(od.get("state"), 1) >= 1) {
od = Synt.toMap(od.get("data"));
od.putAll(sd);
da.update(id, od);
} else {
dr.delDoc(id);
}
}
ds.remove(id);
i++;
j++;
if (j == bn) {
j = 0;
da.commit();
dr.begin();
if (pr) {
CmdletHelper.progres(tm, c, i);
}
}
}
da.commit();
dr.begin();
if (pr) {
CmdletHelper.progres(tm, c, i);
}
/**
* 删掉多余数据
*/
for (String id : ds) {
dr.delDoc(id);
}
da.commit();
if (pr) {
CmdletHelper.progres();
}
CmdletHelper.println("Revert " + i + " item(s) for " + form + " to " + dr.getDbName());
}
use of io.github.ihongs.db.Table in project HongsCORE by ihongs.
the class AuthKit method getMoreDepts.
/**
* 获取所在的下级分组
* @param uid
* @return
* @throws HongsException
*/
public static Set getMoreDepts(String uid) throws HongsException {
Table rel = DB.getInstance("master").getTable("dept_user");
List<Map> lst = rel.fetchCase().filter("user_id = ?", uid).select("dept_id").getAll();
Set set = new HashSet();
Dept dp = new Dept();
for (Map row : lst) {
String id = (String) row.get("dept_id");
set.addAll(dp.getChildIds(id, true));
}
return set;
}
use of io.github.ihongs.db.Table in project HongsCORE by ihongs.
the class AuthKit method openSign.
/**
* 第三方登录
* @param ah
* @param unit
* @param code
* @param uname 名称
* @param uhead 头像
* @return
* @throws HongsException
*/
public static Map openSign(ActionHelper ah, String unit, String code, String uname, String uhead) throws HongsException {
DB db = DB.getInstance("master");
Table tb = db.getTable("user_sign");
Table ub = db.getTable("user");
Map ud = tb.fetchCase().from(tb.tableName, "s").join(ub.tableName, "u", "`u`.`id` = `s`.`user_id`").filter("`s`.`unit` = ? AND `s`.`code` = ?", unit, code).select("`u`.`id`, `u`.`name`, `u`.`head`, `u`.`state`").getOne();
int stat = Synt.declare(ud.get("state"), 0);
String uuid;
if (!ud.isEmpty()) {
// 锁定或系统账号
if (stat <= 0) {
throw new Wrongs(Synt.mapOf("state", new Wrong("core.sign.state.invalid"))).setLocalizedContext("master");
// .setLocalizedOptions( stat );
}
uuid = (String) ud.get("id");
uname = (String) ud.get("name");
uhead = (String) ud.get("head");
} else {
ud = new HashMap();
ud.put("name", uname);
ud.put("head", uhead);
// 校验及下载头像
ud = new VerifyHelper().addRulesByForm("master", "user").verify(ud, true, true);
uuid = db.getModel("user").add(ud);
uname = (String) ud.get("name");
uhead = (String) ud.get("head");
// 第三方登录项
ud = new HashMap();
ud.put("user_id", uuid);
ud.put("unit", unit);
ud.put("code", code);
db.getTable("user_sign").insert(ud);
// 加入公共部门
ud = new HashMap();
ud.put("user_id", uuid);
ud.put("dept_id", "CENTRE");
db.getTable("dept_user").insert(ud);
// 赋予公共权限. 仅用部门即可(2019/02/28)
// ud = new HashMap( );
// ud.put("user_id", uuid );
// ud.put("role" , "centre");
// db.getTable("user_role").insert(ud);
}
ud = userSign(ah, unit, uuid, uname, uhead);
ud.put("unit", /**/
unit);
ud.put("regs", 0 == stat);
return ud;
}
use of io.github.ihongs.db.Table in project HongsCORE by ihongs.
the class AuthKit method getUserRoles.
/**
* 获取用户拥有的权限
* @param uid
* @return
* @throws HongsException
*/
public static Set getUserRoles(String uid) throws HongsException {
Table rel = DB.getInstance("master").getTable("user_role");
List<Map> lst = rel.fetchCase().filter("user_id = ?", uid).select("role").getAll();
Set set = new HashSet();
for (Map row : lst) {
set.add(row.get("role"));
}
return set;
}
use of io.github.ihongs.db.Table in project HongsCORE by ihongs.
the class QQAction method setUserSign.
public static void setUserSign(String unit, String code, String uid) throws HongsException {
Table tab = DB.getInstance("master").getTable("user_sign");
Map row = tab.fetchCase().filter("user_id = ?", uid).filter("unit = ?", unit).filter("code = ?", code).select("1").getOne();
if (row == null || row.isEmpty()) {
tab.insert(Synt.mapOf("user_id", uid, "unit", unit, "code", code));
}
}
Aggregations