Search in sources :

Example 11 with Department

use of com.rebuild.core.privileges.bizz.Department in project rebuild by getrebuild.

the class UserStoreTest method deptMethods.

@Test
public void deptMethods() {
    Department dept = Application.getUserStore().getDepartment(SIMPLE_DEPT);
    System.out.println(dept.getAllChildren());
    System.out.println(dept.isChildren(DepartmentService.ROOT_DEPT));
}
Also used : Department(com.rebuild.core.privileges.bizz.Department) Test(org.junit.jupiter.api.Test)

Example 12 with Department

use of com.rebuild.core.privileges.bizz.Department in project rebuild by getrebuild.

the class AdvFilterParser method parseItem.

/**
 * 解析查询项为 SQL
 *
 * @param item
 * @param values
 * @return
 */
private String parseItem(JSONObject item, JSONObject values) {
    String field = item.getString("field");
    // fix: _$unthy
    if (field.startsWith("&"))
        field = field.replace("&", "&");
    boolean hasNameFlag = field.startsWith("&");
    if (hasNameFlag) {
        field = field.substring(1);
    }
    Field fieldMeta = MetadataHelper.getLastJoinField(rootEntity, field);
    if (fieldMeta == null) {
        log.warn("Unknown field '{}' in '{}'", field, rootEntity.getName());
        return null;
    }
    DisplayType dt = EasyMetaFactory.getDisplayType(fieldMeta);
    if (dt == DisplayType.CLASSIFICATION || (dt == DisplayType.PICKLIST && hasNameFlag)) /* 快速查询 */
    {
        field = "&" + field;
    } else if (hasNameFlag) {
        if (!(dt == DisplayType.REFERENCE || dt == DisplayType.N2NREFERENCE)) {
            log.warn("Non reference-field '{}' in '{}'", field, rootEntity.getName());
            return null;
        }
        // 转化为名称字段
        if (dt == DisplayType.REFERENCE) {
            fieldMeta = fieldMeta.getReferenceEntity().getNameField();
            dt = EasyMetaFactory.getDisplayType(fieldMeta);
            field += "." + fieldMeta.getName();
        }
    }
    String op = item.getString("op");
    String value = item.getString("value");
    String valueEnd = null;
    // exists ( in (...) )
    if (hasNameFlag && dt == DisplayType.N2NREFERENCE) {
        Entity refEntity = fieldMeta.getReferenceEntity();
        String inWhere = String.format("select %s from %s where %s %s %s", refEntity.getPrimaryField().getName(), refEntity.getName(), refEntity.getNameField().getName(), ParseHelper.convetOperation(op), quoteValue('%' + value + '%', FieldType.STRING));
        return String.format("exists (select recordId from NreferenceItem where ^%s = recordId and belongField = '%s' and referenceId in (%s))", rootEntity.getPrimaryField().getName(), fieldMeta.getName(), inWhere);
    }
    // 根据字段类型转换 `op`
    final boolean isDatetime = dt == DisplayType.DATETIME;
    // 日期时间
    if (isDatetime || dt == DisplayType.DATE) {
        final boolean isREX = ParseHelper.RED.equalsIgnoreCase(op) || ParseHelper.REM.equalsIgnoreCase(op) || ParseHelper.REY.equalsIgnoreCase(op);
        if (ParseHelper.TDA.equalsIgnoreCase(op) || ParseHelper.YTA.equalsIgnoreCase(op) || ParseHelper.TTA.equalsIgnoreCase(op)) {
            if (ParseHelper.YTA.equalsIgnoreCase(op)) {
                value = formatDate(addDay(-1), 0);
            } else if (ParseHelper.TTA.equalsIgnoreCase(op)) {
                value = formatDate(addDay(1), 0);
            } else {
                value = formatDate(CalendarUtils.now(), 0);
            }
            if (isDatetime) {
                op = ParseHelper.BW;
                valueEnd = parseValue(value, op, fieldMeta, true);
            }
        } else if (ParseHelper.CUW.equalsIgnoreCase(op) || ParseHelper.CUM.equalsIgnoreCase(op) || ParseHelper.CUQ.equalsIgnoreCase(op) || ParseHelper.CUY.equalsIgnoreCase(op)) {
            Date begin = Moment.moment().startOf(op.substring(2)).date();
            value = formatDate(begin, 0);
            Date end = Moment.moment(begin).endOf(op.substring(2)).date();
            valueEnd = formatDate(end, 0);
            if (isDatetime) {
                value += ParseHelper.ZERO_TIME;
                valueEnd += ParseHelper.FULL_TIME;
            }
            op = ParseHelper.BW;
        } else if (ParseHelper.EQ.equalsIgnoreCase(op) && dt == DisplayType.DATETIME && StringUtils.length(value) == 10) {
            op = ParseHelper.BW;
            valueEnd = parseValue(value, op, fieldMeta, true);
        } else if (isREX || ParseHelper.FUD.equalsIgnoreCase(op) || ParseHelper.FUM.equalsIgnoreCase(op) || ParseHelper.FUY.equalsIgnoreCase(op)) {
            int xValue = NumberUtils.toInt(value) * (isREX ? -1 : 1);
            Date date;
            if (ParseHelper.REM.equalsIgnoreCase(op) || ParseHelper.FUM.equalsIgnoreCase(op)) {
                date = CalendarUtils.addMonth(xValue);
            } else if (ParseHelper.REY.equalsIgnoreCase(op) || ParseHelper.FUY.equalsIgnoreCase(op)) {
                date = CalendarUtils.addMonth(xValue * 12);
            } else {
                date = CalendarUtils.addDay(xValue);
            }
            if (isREX) {
                value = formatDate(date, 0);
                valueEnd = formatDate(CalendarUtils.now(), 0);
            } else {
                value = formatDate(CalendarUtils.now(), 0);
                valueEnd = formatDate(date, 0);
            }
            if (isDatetime) {
                value += ParseHelper.ZERO_TIME;
                valueEnd += ParseHelper.FULL_TIME;
            }
            op = ParseHelper.BW;
        }
    } else if (dt == DisplayType.TIME) {
        // 前端输入 HH:mm
        if (value != null && value.length() == 5) {
            if (ParseHelper.EQ.equalsIgnoreCase(op)) {
                op = ParseHelper.BW;
                valueEnd = value + ":59";
            }
            value += ":00";
        }
    } else if (dt == DisplayType.MULTISELECT) {
        // 多选的包含/不包含要按位计算
        if (ParseHelper.IN.equalsIgnoreCase(op) || ParseHelper.NIN.equalsIgnoreCase(op)) {
            op = ParseHelper.IN.equalsIgnoreCase(op) ? ParseHelper.BAND : ParseHelper.NBAND;
            long maskValue = 0;
            for (String s : value.split("\\|")) {
                maskValue += ObjectUtils.toLong(s);
            }
            value = maskValue + "";
        }
    }
    StringBuilder sb = new StringBuilder(field).append(' ').append(ParseHelper.convetOperation(op));
    // 无需值
    if (ParseHelper.NL.equalsIgnoreCase(op) || ParseHelper.NT.equalsIgnoreCase(op)) {
        return sb.toString();
    }
    sb.append(' ');
    if (ParseHelper.BFD.equalsIgnoreCase(op)) {
        value = formatDate(addDay(-NumberUtils.toInt(value)), isDatetime ? 1 : 0);
    } else if (ParseHelper.BFM.equalsIgnoreCase(op)) {
        value = formatDate(addMonth(-NumberUtils.toInt(value)), isDatetime ? 1 : 0);
    } else if (ParseHelper.BFY.equalsIgnoreCase(op)) {
        value = formatDate(addMonth(-NumberUtils.toInt(value) * 12), isDatetime ? 1 : 0);
    } else if (ParseHelper.AFD.equalsIgnoreCase(op)) {
        value = formatDate(addDay(NumberUtils.toInt(value)), isDatetime ? 2 : 0);
    } else if (ParseHelper.AFM.equalsIgnoreCase(op)) {
        value = formatDate(addMonth(NumberUtils.toInt(value)), isDatetime ? 2 : 0);
    } else if (ParseHelper.AFY.equalsIgnoreCase(op)) {
        value = formatDate(addMonth(NumberUtils.toInt(value) * 12), isDatetime ? 2 : 0);
    } else // 部门/用户
    if (ParseHelper.SFU.equalsIgnoreCase(op)) {
        value = getUser().toLiteral();
    } else if (ParseHelper.SFB.equalsIgnoreCase(op)) {
        Department dept = UserHelper.getDepartment(getUser());
        if (dept != null) {
            value = dept.getIdentity().toString();
            int ref = fieldMeta.getReferenceEntity().getEntityCode();
            if (ref == EntityHelper.User) {
                sb.insert(sb.indexOf(" "), ".deptId");
            } else if (ref == EntityHelper.Department) {
            // NOOP
            } else {
                value = null;
            }
        }
    } else if (ParseHelper.SFD.equalsIgnoreCase(op)) {
        Department dept = UserHelper.getDepartment(getUser());
        if (dept != null) {
            int refe = fieldMeta.getReferenceEntity().getEntityCode();
            if (refe == EntityHelper.Department) {
                value = StringUtils.join(UserHelper.getAllChildren(dept), "|");
            }
        }
    } else if (ParseHelper.SFT.equalsIgnoreCase(op)) {
        // No any
        if (value == null)
            value = "0";
        // In Sql
        value = String.format("( select userId from TeamMember where teamId in ('%s') )", StringUtils.join(value.split("\\|"), "', '"));
    }
    if (StringUtils.isBlank(value)) {
        log.warn("No search value defined : " + item.toJSONString());
        return null;
    }
    // 快速搜索的占位符 `{1}`
    if (value.matches("\\{\\d+}")) {
        if (values == null || values.isEmpty()) {
            return null;
        }
        String valHold = value.replaceAll("[{}]", "");
        value = parseValue(values.get(valHold), op, fieldMeta, false);
    } else {
        value = parseValue(value, op, fieldMeta, false);
    }
    // No value for search
    if (value == null) {
        return null;
    }
    // 区间
    final boolean isBetween = op.equalsIgnoreCase(ParseHelper.BW);
    if (isBetween && valueEnd == null) {
        valueEnd = parseValue(item.getString("value2"), op, fieldMeta, true);
        if (valueEnd == null) {
            valueEnd = value;
        }
    }
    // IN
    if (op.equalsIgnoreCase(ParseHelper.IN) || op.equalsIgnoreCase(ParseHelper.NIN) || op.equalsIgnoreCase(ParseHelper.SFD) || op.equalsIgnoreCase(ParseHelper.SFT)) {
        sb.append(value);
    } else {
        // LIKE
        if (op.equalsIgnoreCase(ParseHelper.LK) || op.equalsIgnoreCase(ParseHelper.NLK)) {
            value = '%' + value + '%';
        }
        sb.append(quoteValue(value, fieldMeta.getType()));
    }
    if (isBetween) {
        sb.insert(0, "( ").append(" and ").append(quoteValue(valueEnd, fieldMeta.getType())).append(" )");
    }
    return sb.toString();
}
Also used : Field(cn.devezhao.persist4j.Field) Entity(cn.devezhao.persist4j.Entity) Department(com.rebuild.core.privileges.bizz.Department) DisplayType(com.rebuild.core.metadata.easymeta.DisplayType)

Aggregations

Department (com.rebuild.core.privileges.bizz.Department)12 Field (cn.devezhao.persist4j.Field)4 JSONArray (com.alibaba.fastjson.JSONArray)4 JSONObject (com.alibaba.fastjson.JSONObject)4 ID (cn.devezhao.persist4j.engine.ID)3 BusinessUnit (cn.devezhao.bizz.security.member.BusinessUnit)2 Entity (cn.devezhao.persist4j.Entity)2 DisplayType (com.rebuild.core.metadata.easymeta.DisplayType)2 User (com.rebuild.core.privileges.bizz.User)2 Test (org.junit.jupiter.api.Test)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2 DepthEntry (cn.devezhao.bizz.privileges.DepthEntry)1 Privileges (cn.devezhao.bizz.privileges.Privileges)1 BizzDepthEntry (cn.devezhao.bizz.privileges.impl.BizzDepthEntry)1 EasyField (com.rebuild.core.metadata.easymeta.EasyField)1 CustomEntityPrivileges (com.rebuild.core.privileges.bizz.CustomEntityPrivileges)1 DataSpecificationException (com.rebuild.core.service.DataSpecificationException)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1