package com.fzzy.sys;
|
|
import com.bstek.dorado.annotation.DataProvider;
|
import com.bstek.dorado.annotation.DataResolver;
|
import com.bstek.dorado.annotation.Expose;
|
import com.bstek.dorado.data.entity.EntityState;
|
import com.bstek.dorado.data.entity.EntityUtils;
|
import com.fzzy.api.Constant;
|
import com.fzzy.api.entity.ApiTrigger;
|
import com.fzzy.data.ConfigData;
|
import com.fzzy.sys.entity.SysDept;
|
import com.fzzy.sys.entity.SysUser;
|
import com.fzzy.sys.repository.DeptRepository;
|
import com.fzzy.sys.repository.UserRepository;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import java.util.ArrayList;
|
import java.util.Arrays;
|
import java.util.List;
|
|
/**
|
* 部门管理
|
*/
|
@Component("sys.deptPR")
|
public class DeptPR {
|
|
@Autowired
|
private ConfigData configData;
|
@Autowired
|
private DeptRepository deptRepository;
|
@Autowired
|
private UserRepository userRepository;
|
|
/**
|
* 根据parentId查询组织架构及库区信息
|
* sys.deptPR#listDept
|
* @return
|
*/
|
@DataProvider
|
public List<SysDept> listDept() {
|
return deptRepository.listDept();
|
}
|
|
/**
|
* sys.deptPR#findAll
|
* @return
|
*/
|
@DataProvider
|
public List<SysDept> findAll() {
|
return deptRepository.findAll();
|
}
|
|
/**
|
* 根据parentId查询组织架构及库区信息
|
* sys.deptPR#findDeptByParentId
|
*
|
* @param parentId
|
* @return
|
*/
|
@DataProvider
|
public List<SysDept> findDeptByParentId(String parentId) {
|
return deptRepository.listDeptByParent(parentId);
|
}
|
|
|
/**
|
* sys.deptPR#updateSave
|
*
|
* @param depts
|
*/
|
@DataResolver
|
public void updateSave(List<SysDept> depts) {
|
if(depts != null){
|
EntityState state = null;
|
for(SysDept dept : depts){
|
state = EntityUtils.getState(dept);
|
if(state.equals(EntityState.NEW)){
|
this.saveData(dept);
|
}
|
if (state.equals(EntityState.MODIFIED)
|
|| state.equals(EntityState.MOVED)) {
|
this.saveData(dept);
|
}
|
if (dept.getChildren() != null) {
|
SysDept[] sysDepts = new SysDept[dept
|
.getChildren().size()];
|
updateSave(Arrays.asList(dept.getChildren().toArray(sysDepts)));
|
}
|
if (state.equals(EntityState.DELETED)) {
|
if (countChildren(dept.getId()) == 0
|
&& getUserByDeptId(dept.getId()).size() == 0) {
|
this.delData(dept);
|
} else {
|
throw new RuntimeException("请先删除子部门或是部门下是用户!");
|
}
|
}
|
|
}
|
}
|
|
}
|
|
/**
|
* sys.deptPR#countChildren
|
*
|
* @param parentId
|
* @return
|
*/
|
@Expose
|
public int countChildren(String parentId) {
|
List<SysDept> depts = deptRepository.listDeptByParent(parentId);
|
return depts.size();
|
}
|
|
/**
|
* sys.deptPR#delData
|
*
|
* @param data
|
*/
|
@Expose
|
public String delData(SysDept data) {
|
deptRepository.deleteById(data.getId());
|
return null;
|
}
|
|
/**
|
* 获取组织类型(10表示省市区县,20表示公司)
|
* ${dorado.getDataProvider("sys.deptPR#triggerDeptType").getResult()}
|
*
|
* @return
|
*/
|
@DataProvider
|
public List<ApiTrigger> triggerDeptType() {
|
List<ApiTrigger> list = new ArrayList<ApiTrigger>();
|
list.add(new ApiTrigger(Constant.DEPT_TYPE_10, "公司"));
|
list.add(new ApiTrigger(Constant.DEPT_TYPE_20, "库点"));
|
list.add(new ApiTrigger(Constant.DEPT_TYPE_30, "部门"));
|
|
return list;
|
}
|
|
|
private SysDept findLastDeptId(String parentId) {
|
// if (StringUtils.isEmpty(companyId)) {
|
// companyId = ContextUtil.getCompanyId(true);
|
// }
|
List<SysDept> list = deptRepository.listDeptByParent(parentId);
|
if (list != null && list.size() > 0) {
|
return list.get(list.size() - 1);
|
} else {
|
return null;
|
}
|
}
|
|
//保存
|
public void saveData(SysDept entity) {
|
// 手动将doradoEntity对象转换为标准Bean对象
|
SysDept data = new SysDept();
|
BeanUtils.copyProperties(entity, data);
|
|
deptRepository.save(data);
|
}
|
|
public List<SysUser> getUserByDeptId(String deptId) {
|
List<SysUser> list = userRepository.getUserByDeptId(deptId);
|
if(list == null){
|
list = new ArrayList<>();
|
}
|
return list;
|
}
|
}
|