diff --git a/src/main/resources/modules/SpringBoot/java/action/ajax/system/DeptAjax.java b/src/main/resources/modules/SpringBoot/java/action/ajax/system/DeptAjax.java index 11553b50..4c8e41ab 100644 --- a/src/main/resources/modules/SpringBoot/java/action/ajax/system/DeptAjax.java +++ b/src/main/resources/modules/SpringBoot/java/action/ajax/system/DeptAjax.java @@ -3,33 +3,35 @@ package ${basePackage}.action.ajax.system; import com.fasterxml.jackson.core.TreeNode; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.multipart.MultipartFile; -import ${basePackage}.frame.excel.WSheet; -import ${basePackage}.frame.utils.LogUtil; import ${basePackage}.frame.auth.LocalData; +import ${basePackage}.frame.base.BaseResponse; import ${basePackage}.frame.base.ErrorType; +import ${basePackage}.frame.excel.WSheet; +import ${basePackage}.frame.excel.exception.ReadErrorException; +import ${basePackage}.frame.excel.exception.TemplateNotMatchException; +import ${basePackage}.frame.utils.LogUtil; import ${basePackage}.frame.utils.MapperUtil; -import ${basePackage}.frame.base.BaseResponse; import ${basePackage}.frame.utils.ResponseUtil; import ${basePackage}.frame.utils.ValidationUtil; -import ${basePackage}.frame.excel.exception.ReadErrorException; -import ${basePackage}.frame.excel.exception.TemplateNotMatchException; import ${basePackage}.module.system.ent.Dept; import ${basePackage}.module.system.mgr.DeptManager; import ${basePackage}.module.system.req.DeptCreateRequest; import ${basePackage}.module.system.req.DeptDeleteRequest; import ${basePackage}.module.system.req.DeptFindRequest; import ${basePackage}.module.system.req.DeptGetRequest; +import ${basePackage}.module.system.req.DeptTreeRequest; import ${basePackage}.module.system.req.DeptUpdateRequest; import ${basePackage}.module.system.rsp.DeptCreateResponse; import ${basePackage}.module.system.rsp.DeptDeleteResponse; import ${basePackage}.module.system.rsp.DeptFindResponse; import ${basePackage}.module.system.rsp.DeptGetResponse; +import ${basePackage}.module.system.rsp.DeptTreeResponse; import ${basePackage}.module.system.rsp.DeptUpdateResponse; import java.io.IOException; import java.util.List; -public class DeptAjax{ +public class DeptAjax { @Autowired private DeptManager deptManager; @@ -54,7 +56,11 @@ public class DeptAjax{ return deptManager.get(request, LocalData.getToken()); } - public Object template(){ + public DeptTreeResponse tree(DeptTreeRequest request) { + return deptManager.tree(request, LocalData.getToken()); + } + + public Object template() { return ResponseUtil.apply(new WSheet<>(Dept.class)); } diff --git a/src/main/resources/modules/SpringBoot/java/module/system/ent/DeptTree.java b/src/main/resources/modules/SpringBoot/java/module/system/ent/DeptTree.java new file mode 100644 index 00000000..c9bcf0bd --- /dev/null +++ b/src/main/resources/modules/SpringBoot/java/module/system/ent/DeptTree.java @@ -0,0 +1,34 @@ +package xyz.wbsite.module.system.ent; + +import java.util.ArrayList; +import java.util.List; + +/** + * DEPT - 部门树 + * + * @author wangbing + * @version 0.0.1 + * @since 2019-12-24 + */ +public class DeptTree extends Dept { + + private Long pId; + + private List children = new ArrayList<>(); + + public Long getpId() { + return pId; + } + + public void setpId(Long pId) { + this.pId = pId; + } + + public List getChildren() { + return children; + } + + public void setChildren(List children) { + this.children = children; + } +} \ No newline at end of file diff --git a/src/main/resources/modules/SpringBoot/java/module/system/mgr/DeptManager.java b/src/main/resources/modules/SpringBoot/java/module/system/mgr/DeptManager.java index c625cf54..6838c278 100644 --- a/src/main/resources/modules/SpringBoot/java/module/system/mgr/DeptManager.java +++ b/src/main/resources/modules/SpringBoot/java/module/system/mgr/DeptManager.java @@ -1,16 +1,18 @@ -package ${basePackage}.module.system.mgr; +package xyz.wbsite.module.system.mgr; -import ${basePackage}.module.system.req.DeptCreateRequest; -import ${basePackage}.module.system.req.DeptDeleteRequest; -import ${basePackage}.module.system.req.DeptFindRequest; -import ${basePackage}.module.system.req.DeptGetRequest; -import ${basePackage}.module.system.req.DeptUpdateRequest; -import ${basePackage}.module.system.rsp.DeptCreateResponse; -import ${basePackage}.module.system.rsp.DeptDeleteResponse; -import ${basePackage}.module.system.rsp.DeptFindResponse; -import ${basePackage}.module.system.rsp.DeptGetResponse; -import ${basePackage}.module.system.rsp.DeptUpdateResponse; -import ${basePackage}.frame.base.Token; +import xyz.wbsite.frame.base.Token; +import xyz.wbsite.module.system.req.DeptCreateRequest; +import xyz.wbsite.module.system.req.DeptDeleteRequest; +import xyz.wbsite.module.system.req.DeptFindRequest; +import xyz.wbsite.module.system.req.DeptGetRequest; +import xyz.wbsite.module.system.req.DeptTreeRequest; +import xyz.wbsite.module.system.req.DeptUpdateRequest; +import xyz.wbsite.module.system.rsp.DeptCreateResponse; +import xyz.wbsite.module.system.rsp.DeptDeleteResponse; +import xyz.wbsite.module.system.rsp.DeptFindResponse; +import xyz.wbsite.module.system.rsp.DeptGetResponse; +import xyz.wbsite.module.system.rsp.DeptTreeResponse; +import xyz.wbsite.module.system.rsp.DeptUpdateResponse; /** * 部门 @@ -65,4 +67,13 @@ public interface DeptManager { * @return */ DeptGetResponse get(DeptGetRequest request, Token token); + + /** + * 获得机构树对象 + * + * @param request 请求对象 + * @param token 令牌 + * @return + */ + DeptTreeResponse tree(DeptTreeRequest request, Token token); } diff --git a/src/main/resources/modules/SpringBoot/java/module/system/mgr/DeptManagerImpl.java b/src/main/resources/modules/SpringBoot/java/module/system/mgr/DeptManagerImpl.java index 1e909ba2..93de8d76 100644 --- a/src/main/resources/modules/SpringBoot/java/module/system/mgr/DeptManagerImpl.java +++ b/src/main/resources/modules/SpringBoot/java/module/system/mgr/DeptManagerImpl.java @@ -1,29 +1,41 @@ -package ${basePackage}.module.system.mgr; - -import ${basePackage}.frame.utils.IDgenerator; -import ${basePackage}.frame.utils.Message; -import ${basePackage}.frame.base.ErrorType; -import ${basePackage}.frame.base.Token; -import ${basePackage}.frame.utils.MapperUtil; -import ${basePackage}.frame.utils.ValidationUtil; -import ${basePackage}.module.system.ent.Dept; -import ${basePackage}.module.system.mpr.DeptMapper; -import ${basePackage}.module.system.req.DeptCreateRequest; -import ${basePackage}.module.system.req.DeptDeleteRequest; -import ${basePackage}.module.system.req.DeptFindRequest; -import ${basePackage}.module.system.req.DeptGetRequest; -import ${basePackage}.module.system.req.DeptUpdateRequest; -import ${basePackage}.module.system.rsp.DeptCreateResponse; -import ${basePackage}.module.system.rsp.DeptDeleteResponse; -import ${basePackage}.module.system.rsp.DeptFindResponse; -import ${basePackage}.module.system.rsp.DeptGetResponse; -import ${basePackage}.module.system.rsp.DeptUpdateResponse; +package xyz.wbsite.module.system.mgr; + import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; -import com.github.pagehelper.util.StringUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import xyz.wbsite.frame.base.ErrorType; +import xyz.wbsite.frame.base.SortType; +import xyz.wbsite.frame.base.Token; +import xyz.wbsite.frame.utils.IDgenerator; +import xyz.wbsite.frame.utils.MapperUtil; +import xyz.wbsite.frame.utils.Message; +import xyz.wbsite.frame.utils.StringUtil; +import xyz.wbsite.frame.utils.ValidationUtil; +import xyz.wbsite.module.system.ent.Dept; +import xyz.wbsite.module.system.ent.DeptTree; +import xyz.wbsite.module.system.ent.Resource; +import xyz.wbsite.module.system.ent.ResourceTree; +import xyz.wbsite.module.system.mpr.DeptMapper; +import xyz.wbsite.module.system.req.DeptCreateRequest; +import xyz.wbsite.module.system.req.DeptDeleteRequest; +import xyz.wbsite.module.system.req.DeptFindRequest; +import xyz.wbsite.module.system.req.DeptGetRequest; +import xyz.wbsite.module.system.req.DeptTreeRequest; +import xyz.wbsite.module.system.req.DeptUpdateRequest; +import xyz.wbsite.module.system.rsp.DeptCreateResponse; +import xyz.wbsite.module.system.rsp.DeptDeleteResponse; +import xyz.wbsite.module.system.rsp.DeptFindResponse; +import xyz.wbsite.module.system.rsp.DeptGetResponse; +import xyz.wbsite.module.system.rsp.DeptTreeResponse; +import xyz.wbsite.module.system.rsp.DeptUpdateResponse; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; /** * DEPT - 部门 @@ -54,9 +66,34 @@ public class DeptManagerImpl implements DeptManager { return response; } + //检查是否重复 + { + DeptFindRequest deptFindRequest = new DeptFindRequest(); + deptFindRequest.setDeptCode(request.getDeptCode()); + DeptFindResponse deptFindResponse = this.find(deptFindRequest, token); + if (deptFindResponse.getResult().size() > 0) { + response.addError(ErrorType.BUSINESS_ERROR, "[" + request.getDeptCode() + "]机构代码重复"); + return response; + } + } + long id = IDgenerator.nextId(); Dept entity = MapperUtil.map(request, Dept.class); entity.setId(id); + if (StringUtil.isEmpty(entity.getSupCode()) || StringUtil.isEmpty(entity.getSupName())) { + String deptCode = entity.getDeptCode(); + while (deptCode.contains("_")) { + deptCode = deptCode.substring(0, deptCode.lastIndexOf("_")); + DeptFindRequest deptFindRequest = new DeptFindRequest(); + deptFindRequest.setDeptCode(deptCode); + DeptFindResponse deptFindResponse = this.find(deptFindRequest, token); + if (deptFindResponse.getResult().size() > 0) { + Dept supDept = deptFindResponse.getResult().get(0); + entity.setSupCode(supDept.getDeptCode()); + entity.setSupName(supDept.getDeptName()); + } + } + } long result = deptMapper.insert(entity, token); if (1L != result) { @@ -172,4 +209,70 @@ public class DeptManagerImpl implements DeptManager { return response; } + + + @Override + public DeptTreeResponse tree(DeptTreeRequest request, Token token) { + DeptTreeResponse response = new DeptTreeResponse(); + + ValidationUtil.validate(request, response); + if (response.hasError()) { + return response; + } + + DeptFindRequest deptFindRequest = new DeptFindRequest(); + deptFindRequest.setPageSize(0); + deptFindRequest.setValid(true); + deptFindRequest.setSortType(SortType.ASC); + deptFindRequest.setSortKey("DEPT_CODE"); + DeptFindResponse deptFindResponse = this.find(deptFindRequest, token); + + if (deptFindResponse.hasError()) { + response.addErrors(deptFindResponse.getErrors()); + return response; + } + + List data = new ArrayList<>(); + Map allMap = new HashMap<>(); + List result = deptFindResponse.getResult(); + // 获取最顶级资源 + int level = 1; + while (result.size() > 0) { + Iterator iterator = result.iterator(); + while (iterator.hasNext()) { + Dept next = iterator.next(); + DeptTree tree = new DeptTree(); + tree.setId(next.getId()); + tree.setDeptCode(next.getDeptCode()); + tree.setDeptName(next.getDeptName()); + tree.setSupCode(next.getSupCode()); + tree.setSupName(next.getSupName()); + tree.setValid(next.getValid()); + tree.setRowVersion(next.getRowVersion()); + + if (next.getDeptCode() != null && next.getDeptCode().split("_").length == level) { + allMap.put(tree.getDeptCode(), tree); + iterator.remove(); + //尝试获取上级资源 + String code = next.getDeptCode(); + DeptTree Ptree = null; + while (code.contains("_")) { + code = code.substring(0, code.lastIndexOf("_")); + Ptree = allMap.get(code); + if (Ptree != null) break; + } + if (Ptree != null) { + tree.setpId(Ptree.getId()); + Ptree.getChildren().add(tree); + } else { + data.add(tree); + } + } + } + level++; + } + + response.setResult(data); + return response; + } } diff --git a/src/main/resources/modules/SpringBoot/java/module/system/req/DeptCreateRequest.java b/src/main/resources/modules/SpringBoot/java/module/system/req/DeptCreateRequest.java index 7babe42a..7a4570bb 100644 --- a/src/main/resources/modules/SpringBoot/java/module/system/req/DeptCreateRequest.java +++ b/src/main/resources/modules/SpringBoot/java/module/system/req/DeptCreateRequest.java @@ -34,18 +34,6 @@ public class DeptCreateRequest extends BaseRequest { @Length(min = 0, max = 50, message = "[deptAlias]部门别名长度不合法(0-50)") private String deptAlias; - /** - * 上级代码 - */ - @Length(min = 0, max = 50, message = "[supCode]上级代码长度不合法(0-50)") - private String supCode; - - /** - * 上级名称 - */ - @Length(min = 0, max = 100, message = "[supName]上级名称长度不合法(0-100)") - private String supName; - /** * 是否有效 */ @@ -76,22 +64,6 @@ public class DeptCreateRequest extends BaseRequest { this.deptAlias = deptAlias; } - public String getSupCode() { - return this.supCode; - } - - public void setSupCode(String supCode) { - this.supCode = supCode; - } - - public String getSupName() { - return this.supName; - } - - public void setSupName(String supName) { - this.supName = supName; - } - public Boolean getValid() { return this.valid; } diff --git a/src/main/resources/modules/SpringBoot/java/module/system/req/DeptTreeRequest.java b/src/main/resources/modules/SpringBoot/java/module/system/req/DeptTreeRequest.java new file mode 100644 index 00000000..3f0ab776 --- /dev/null +++ b/src/main/resources/modules/SpringBoot/java/module/system/req/DeptTreeRequest.java @@ -0,0 +1,14 @@ +package xyz.wbsite.module.system.req; + +import xyz.wbsite.frame.base.BaseRequest; + +/** + * ResourceTreeRequest - 资源获取 + * + * @author wangbing + * @version 0.0.1 + * @since 2019-12-26 + */ +public class DeptTreeRequest extends BaseRequest { + +} diff --git a/src/main/resources/modules/SpringBoot/java/module/system/rsp/DeptTreeResponse.java b/src/main/resources/modules/SpringBoot/java/module/system/rsp/DeptTreeResponse.java new file mode 100644 index 00000000..be1d65aa --- /dev/null +++ b/src/main/resources/modules/SpringBoot/java/module/system/rsp/DeptTreeResponse.java @@ -0,0 +1,15 @@ +package xyz.wbsite.module.system.rsp; + +import xyz.wbsite.frame.base.BaseFindResponse; +import xyz.wbsite.module.system.ent.DeptTree; + +/** + * ResourceTreeResponse - 机构树 + * + * @author author + * @version 0.0.1 + * @since 2017-01-01 + */ +public class DeptTreeResponse extends BaseFindResponse { + +} diff --git a/src/main/resources/modules/SpringBoot/resources/templates/screen/module/system/dept.ftl b/src/main/resources/modules/SpringBoot/resources/templates/screen/module/system/dept.ftl index 43624bc8..19b96ed2 100644 --- a/src/main/resources/modules/SpringBoot/resources/templates/screen/module/system/dept.ftl +++ b/src/main/resources/modules/SpringBoot/resources/templates/screen/module/system/dept.ftl @@ -151,7 +151,8 @@ - + @@ -176,7 +177,7 @@ :filter-node-method="filterTree" :expand-on-click-node="false">
- {{ data.comment }}[{{data.code}}] + {{ data.deptName }}[{{data.deptCode}}] - - - - - - @@ -228,6 +223,16 @@
+ diff --git a/src/main/resources/modules/SpringBoot/resources/templates/screen/module/system/resource.ftl b/src/main/resources/modules/SpringBoot/resources/templates/screen/module/system/resource.ftl index dadffe37..50b7a152 100644 --- a/src/main/resources/modules/SpringBoot/resources/templates/screen/module/system/resource.ftl +++ b/src/main/resources/modules/SpringBoot/resources/templates/screen/module/system/resource.ftl @@ -367,6 +367,37 @@ } }.bind(this)) }, + onSave: function () { + this.$refs['form'].validate(function (valid) { + if (valid) { + if (this.form.id) { + this.$ajax.update(this.module, this.target, this.form).then(function (response) { + if (response.errors.length > 0) { + nav.e(response.errors[0].message); + } else { + this.onFind(); + this.onLoadTree(); + this.$refs['form'].resetFields(); + this.form.dialog = false; + } + }.bind(this)) + } else { + this.$ajax.create(this.module, this.target, this.form).then(function (response) { + if (response.errors.length > 0) { + nav.e(response.errors[0].message); + } else { + this.onFind(); + this.onLoadTree(); + this.$refs['form'].resetFields(); + this.form.dialog = false; + } + }.bind(this)) + } + } else { + return false; + } + }.bind(this)); + }, }, watch: { treeKeyword:function(val) { diff --git a/src/main/resources/modules/SpringBoot/test/system/DeptTest.java b/src/main/resources/modules/SpringBoot/test/system/DeptTest.java index 56d550d0..6e6823c8 100644 --- a/src/main/resources/modules/SpringBoot/test/system/DeptTest.java +++ b/src/main/resources/modules/SpringBoot/test/system/DeptTest.java @@ -1,15 +1,16 @@ -package ${basePackage}.system; +package xyz.wbsite.system; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.Rollback; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.transaction.annotation.Transactional; -import ${basePackage}.frame.base.Token; -import ${basePackage}.module.system.mgr.DeptManager; -import ${basePackage}.module.system.req.*; -import ${basePackage}.module.system.rsp.*; +import xyz.wbsite.frame.base.Token; +import xyz.wbsite.module.system.mgr.DeptManager; +import xyz.wbsite.module.system.req.*; +import xyz.wbsite.module.system.rsp.*; import static org.junit.Assert.assertTrue; @@ -33,18 +34,26 @@ public class DeptTest { private DeptManager deptManager; @Test + @Rollback(false) public void testCreate() { - DeptCreateRequest request = new DeptCreateRequest(); - request.setDeptCode("部门代码"); - request.setDeptName("部门名称"); - request.setDeptAlias("部门别名"); - request.setSupCode("上级代码"); - request.setSupName("上级名称"); - request.setValid(true); - - DeptCreateResponse response = deptManager.create(request,token); - - assertTrue(!response.hasError()); + { + DeptCreateRequest request = new DeptCreateRequest(); + request.setDeptCode("A00"); + request.setDeptName("开发部"); + request.setDeptAlias("开发部"); + request.setValid(true); + DeptCreateResponse response = deptManager.create(request,token); + assertTrue(!response.hasError()); + } + { + DeptCreateRequest request = new DeptCreateRequest(); + request.setDeptCode("A00_B00"); + request.setDeptName("开发部开发一组"); + request.setDeptAlias("开发一组"); + request.setValid(true); + DeptCreateResponse response = deptManager.create(request,token); + assertTrue(!response.hasError()); + } } @Test @@ -52,11 +61,9 @@ public class DeptTest { //创建数据 DeptCreateRequest createRequest = new DeptCreateRequest(); - createRequest.setDeptCode("部门代码"); - createRequest.setDeptName("部门名称"); - createRequest.setDeptAlias("部门别名"); - createRequest.setSupCode("上级代码"); - createRequest.setSupName("上级名称"); + createRequest.setDeptCode("A00"); + createRequest.setDeptName("开发部"); + createRequest.setDeptAlias("开发部"); createRequest.setValid(true); DeptCreateResponse createResponse = deptManager.create(createRequest,token); @@ -75,11 +82,9 @@ public class DeptTest { public void testUpdate() { //创建数据 DeptCreateRequest createRequest = new DeptCreateRequest(); - createRequest.setDeptCode("部门代码"); - createRequest.setDeptName("部门名称"); - createRequest.setDeptAlias("部门别名"); - createRequest.setSupCode("上级代码"); - createRequest.setSupName("上级名称"); + createRequest.setDeptCode("A00"); + createRequest.setDeptName("开发部"); + createRequest.setDeptAlias("开发部"); createRequest.setValid(true); DeptCreateResponse createResponse = deptManager.create(createRequest, token); @@ -89,11 +94,9 @@ public class DeptTest { //更新数据 DeptUpdateRequest request = new DeptUpdateRequest(); request.setId(createResponse.getId()); - request.setDeptCode("部门代码"); - request.setDeptName("部门名称"); - request.setDeptAlias("部门别名"); - request.setSupCode("上级代码"); - request.setSupName("上级名称"); + request.setDeptCode("A00"); + request.setDeptName("开发部"); + request.setDeptAlias("开发部"); request.setValid(true); DeptUpdateResponse response = deptManager.update(request,token); @@ -104,11 +107,9 @@ public class DeptTest { @Test public void testFind() { DeptFindRequest request = new DeptFindRequest(); - request.setDeptCode("部门代码"); - request.setDeptName("部门名称"); - request.setDeptAlias("部门别名"); - request.setSupCode("上级代码"); - request.setSupName("上级名称"); + request.setDeptCode("A00"); + request.setDeptName("开发部"); + request.setDeptAlias("开发部"); request.setValid(true); DeptFindResponse response = deptManager.find(request,token); @@ -120,11 +121,9 @@ public class DeptTest { public void testGet() { //创建数据 DeptCreateRequest createRequest = new DeptCreateRequest(); - createRequest.setDeptCode("部门代码"); - createRequest.setDeptName("部门名称"); - createRequest.setDeptAlias("部门别名"); - createRequest.setSupCode("上级代码"); - createRequest.setSupName("上级名称"); + createRequest.setDeptCode("A00"); + createRequest.setDeptName("开发部"); + createRequest.setDeptAlias("开发部"); createRequest.setValid(true); DeptCreateResponse createResponse = deptManager.create(createRequest, token);