You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
6.0 KiB
139 lines
6.0 KiB
package com.fr.plugin; |
|
|
|
import com.fr.decision.authority.AuthorityContext; |
|
import com.fr.decision.authority.controller.CustomRoleController; |
|
import com.fr.decision.authority.controller.DepartmentController; |
|
import com.fr.decision.authority.controller.PostController; |
|
import com.fr.decision.authority.data.CustomRole; |
|
import com.fr.decision.authority.data.Department; |
|
import com.fr.decision.authority.data.Post; |
|
import com.fr.decision.fun.impl.BaseHttpHandler; |
|
import com.fr.log.FineLoggerFactory; |
|
import com.fr.stable.StringUtils; |
|
import com.fr.stable.query.QueryFactory; |
|
import com.fr.stable.query.restriction.RestrictionFactory; |
|
import com.fr.third.org.apache.commons.io.IOUtils; |
|
import com.fr.third.springframework.web.bind.annotation.RequestMethod; |
|
import com.fr.web.utils.WebUtils; |
|
import org.json.JSONArray; |
|
import org.json.JSONObject; |
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
public class PostionImportgHander extends BaseHttpHandler { |
|
@Override |
|
public RequestMethod getMethod() { |
|
return RequestMethod.POST; |
|
} |
|
|
|
@Override |
|
public String getPath() { |
|
return "/depAndPosImport"; |
|
} |
|
|
|
@Override |
|
public boolean isPublic() { |
|
return true; |
|
} |
|
|
|
@Override |
|
public void handle(HttpServletRequest req, HttpServletResponse res) throws Exception { |
|
// 传入的json格式 |
|
/** |
|
* [ |
|
* { |
|
* "fInstNo": "", //父级id |
|
* "instNo": "A01", |
|
* "instNm": "部门一", |
|
* "pstNo": "X1", |
|
* "pstNm": "职务一" |
|
* } |
|
* ] |
|
*/ |
|
String body = IOUtils.toString(req.getReader()); |
|
FineLoggerFactory.getLogger().info("批量添加部门信息接口收到:{}", body); |
|
try { |
|
JSONArray jsonArray = new JSONArray(body); |
|
int length = jsonArray.length(); |
|
for (int i = 0; i < length; i++) { |
|
JSONObject jsonObject = jsonArray.getJSONObject(i); |
|
String fInstNo = jsonObject.getString("fInstNo"); |
|
String instNo = jsonObject.getString("instNo");//部门id |
|
String instNm = jsonObject.getString("instNm");//部门名称 |
|
String pstNo = jsonObject.getString("pstNo");//职务编号 |
|
String pstNm = jsonObject.getString("pstNm");//职务名 |
|
saveDep(fInstNo, instNo, instNm); |
|
savePosition(instNo, pstNo, pstNm); |
|
} |
|
} catch (Exception e) { |
|
WebUtils.printAsString(res, "json format error"); |
|
} |
|
WebUtils.printAsString(res, "success"); |
|
} |
|
|
|
private void savePosition(String did, String pid, String name) { |
|
PostController postController = AuthorityContext.getInstance().getPostController(); |
|
Post post; |
|
try { |
|
post = postController.findOne(QueryFactory.create().addRestriction(RestrictionFactory.eq("id", pid))); |
|
if (post == null) { |
|
post = new Post(); |
|
post.setId(pid); |
|
post.setName(name + "(" + pid + ")"); |
|
post.setEnable(true); |
|
postController.add(post); |
|
FineLoggerFactory.getLogger().info("从接口新增职位,post: {}: name {}", post.getId(), post.getName()); |
|
} else { |
|
post.setName(name + "(" + pid + ")"); |
|
post.setEnable(true); |
|
postController.update(post); |
|
FineLoggerFactory.getLogger().info("已存在职位,post: {}: name {}", post.getId(), post.getName()); |
|
} |
|
postController.addPostToDepartment(pid, did); |
|
|
|
CustomRoleController customRoleController = AuthorityContext.getInstance().getCustomRoleController(); |
|
CustomRole customRole = customRoleController.findOne(QueryFactory.create().addRestriction(RestrictionFactory.eq("id", pid))); |
|
if (customRole == null) { |
|
customRole = new CustomRole(); |
|
customRole.id(pid).name(name + "(" + pid + ")").enable(true); |
|
customRoleController.add(customRole); |
|
FineLoggerFactory.getLogger().info("从接口新增角色,id: {}: name {}", customRole.getId(), customRole.getName()); |
|
} else { |
|
customRole.name(name + "(" + pid + ")").enable(true); |
|
customRoleController.update(customRole); |
|
FineLoggerFactory.getLogger().info("已存在角色,id: {}: name {}", customRole.getId(), customRole.getName()); |
|
} |
|
} catch (Exception e) { |
|
FineLoggerFactory.getLogger().info("添加职位异常: 职位id :{} 部门id:{}: name {}", pid, did, name); |
|
} |
|
} |
|
|
|
private void saveDep(String pid, String id, String name) { |
|
try { |
|
DepartmentController departmentController = AuthorityContext.getInstance().getDepartmentController(); |
|
Department department = departmentController.findOne(QueryFactory.create().addRestriction(RestrictionFactory.eq("id", id))); |
|
if (StringUtils.isBlank(pid)) { |
|
pid = null; |
|
} |
|
if (department == null) { |
|
department = new Department(); |
|
department.setName(name + "(" + id + ")"); |
|
department.setId(id); |
|
department.setParentId(pid); |
|
department.setEnable(true); |
|
department.description("通过x-content创建"); |
|
departmentController.add(department); |
|
FineLoggerFactory.getLogger().info("新增部门,dep: {}: name {}", department.getId(), department.getName()); |
|
} else { |
|
department.setName(name + "(" + id + ")"); |
|
department.setParentId(pid); |
|
department.setEnable(true); |
|
departmentController.update(department); |
|
FineLoggerFactory.getLogger().info("已存在部门,dep: {}: name {}", department.getId(), department.getName()); |
|
} |
|
} catch (Exception e) { |
|
FineLoggerFactory.getLogger().error("添加部门:{} 异常", id, e); |
|
} |
|
} |
|
} |