采购订单表单插件,过滤人员信息里的部门

This commit is contained in:
刘鹏 2024-07-27 17:15:39 +08:00
parent 1448c34b80
commit 332add04f0
1 changed files with 147 additions and 0 deletions

View File

@ -0,0 +1,147 @@
package shkd.fi.fi.formplugin;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.org.OrgServiceHelper;
import org.apache.commons.lang3.ObjectUtils;
import java.util.*;
/**
* 采购订单表单插件
*
*/
public class PurOrderBillFormExtPlugin extends AbstractFormPlugin implements BeforeF7SelectListener {
private static final String DEPT = "dept";
private static final String OPERATOR = "operator";
private static final String ENTRY_ENTITY = "entryentity";
@Override
public void registerListener(EventObject e) {
super.registerListener(e);
BasedataEdit baseData = this.getView().getControl(DEPT);
baseData.addBeforeF7SelectListener(this);
}
@Override
public void beforeF7Select(BeforeF7SelectEvent beforeF7SelectEvent) {
String propertyName = beforeF7SelectEvent.getProperty().getName();
if (DEPT.equals(propertyName)){
QFilter filter = new QFilter("id", QFilter.in, listMatchDeptId());
beforeF7SelectEvent.setCustomQFilters(Collections.singletonList(filter));
}
}
@Override
public void propertyChanged(PropertyChangedArgs e) {
super.propertyChanged(e);
if (OPERATOR.equals(e.getProperty().getName())){
setDefaultDept();
}
}
/**
* 设置默认采购部门
*
*/
private void setDefaultDept() {
Optional<DynamicObject> user = getUser();
if (!user.isPresent()) {
return;
}
//部门信息分录
DynamicObjectCollection departEntries = user.get().getDynamicObjectCollection(ENTRY_ENTITY);
Set<Long> orgIds = listOrgSubUnitId();
DynamicObject targetDept = null;
for (DynamicObject departEntry : departEntries) {
//部门
DynamicObject dept = departEntry.getDynamicObject("dpt");
//部门ID
long deptId = dept.getLong("id");
if (!orgIds.contains(dept.getLong("id"))) {
continue;
}
//是否兼职
boolean isPartJob = departEntry.getBoolean("ispartjob");
//选取未兼职分录第一行 && 在采购组织下游
if (ObjectUtils.isEmpty(targetDept) && orgIds.contains(deptId)) {
targetDept = dept;
}
//存在兼职分录行
if (isPartJob) {
//分录行部门属于采购组织下游行政组织
targetDept = dept;
break;
}
}
this.getModel().setValue(DEPT, targetDept);
}
private Optional<DynamicObject> getUser() {
DynamicObject operator = (DynamicObject) this.getModel().getValue(OPERATOR);
if (ObjectUtils.isEmpty(operator)){
return Optional.empty();
}
long userId = operator.getDynamicObject("operatorid").getLong("id");
DynamicObject user = BusinessDataServiceHelper.loadSingle("bos_user", "id,dpt,ispartjob", new QFilter[]{new QFilter("id", QFilter.equals, userId)});
return Optional.ofNullable(user);
}
/**
* 获取采购组织下游业务单元ID列表
*
* @return 业务单元ID列表
*/
private Set<Long> listOrgSubUnitId(){
Set<Long> result = new HashSet<>(16);
DynamicObject org = (DynamicObject) this.getModel().getValue("org");
if (ObjectUtils.isEmpty(org)){
return result;
}
//查询组织下游行政组织ID列表
List<Long> subOrgIds = OrgServiceHelper.getOrgAllSubIds(org.getLong("id"), "01", true);
result.addAll(subOrgIds);
return result;
}
private Set<Long> listUserDeptId(){
Set<Long> result = new HashSet<>(16);
Optional<DynamicObject> user = getUser();
if (!user.isPresent()){
return result;
}
//部门信息分录
DynamicObjectCollection departEntries = user.get().getDynamicObjectCollection(ENTRY_ENTITY);
for (DynamicObject departEntry : departEntries) {
result.add(departEntry.getLong("dpt.id"));
}
return result;
}
/**
* 获取与采购组织匹配的采购部门ID列表
*
* @return 采购部门ID列表
*/
private Set<Long> listMatchDeptId(){
Set<Long> orgSubUnitId = listOrgSubUnitId();
Set<Long> userDeptIds = listUserDeptId();
Set<Long> result = new HashSet<>(16);
for (long userDeptId : userDeptIds) {
if (orgSubUnitId.contains(userDeptId)){
result.add(userDeptId);
}
}
return result;
}
}