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.
144 lines
7.0 KiB
144 lines
7.0 KiB
package com.fr.plugin.handers; |
|
|
|
import com.fanruan.api.log.LogKit; |
|
import com.fr.collections.FineCollections; |
|
import com.fr.collections.api.FineLock; |
|
import com.fr.decision.authority.AuthorityContext; |
|
import com.fr.decision.authority.base.constant.type.operation.ManualOperationType; |
|
import com.fr.decision.authority.controller.DepartmentController; |
|
import com.fr.decision.authority.controller.PostController; |
|
import com.fr.decision.authority.controller.UserController; |
|
import com.fr.decision.authority.data.Department; |
|
import com.fr.decision.authority.data.Post; |
|
import com.fr.decision.authority.data.User; |
|
import com.fr.decision.fun.impl.BaseHttpHandler; |
|
import com.fr.json.JSONObject; |
|
import com.fr.log.FineLoggerFactory; |
|
import com.fr.plugin.HttpUtils; |
|
import com.fr.plugin.SafeCheckUtils; |
|
import com.fr.plugin.transform.ExecuteFunctionRecord; |
|
import com.fr.plugin.transform.FunctionRecorder; |
|
import com.fr.stable.StringUtils; |
|
import com.fr.stable.query.QueryFactory; |
|
import com.fr.stable.query.data.DataList; |
|
import com.fr.stable.query.restriction.RestrictionFactory; |
|
import com.fr.third.springframework.web.bind.annotation.RequestMethod; |
|
import com.fr.web.utils.WebUtils; |
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
import javax.servlet.http.HttpServletResponse; |
|
import java.util.List; |
|
import java.util.concurrent.TimeUnit; |
|
|
|
@FunctionRecorder |
|
public class SynPositionHander extends BaseHttpHandler { |
|
@Override |
|
public RequestMethod getMethod() { |
|
return null; |
|
} |
|
|
|
@Override |
|
public String getPath() { |
|
return "/positions"; |
|
} |
|
|
|
@Override |
|
public boolean isPublic() { |
|
return true; |
|
} |
|
|
|
@Override |
|
@ExecuteFunctionRecord |
|
public void handle(HttpServletRequest req, HttpServletResponse httpServletResponse) throws Exception { |
|
String body = HttpUtils.inputStream2String(req.getInputStream()); |
|
if (StringUtils.isNotBlank(body)) { |
|
String xtime = req.getHeader("xtime"); |
|
String xsign = req.getHeader("xkey"); |
|
// if (!SafeCheckUtils.checkSafe(body, "", xtime, xsign)) { |
|
// FineLoggerFactory.getLogger().error("SynPositionHander: 验证失败"); |
|
// JSONObject successJSONObject = HttpUtils.getError("签名验证失败", 101); |
|
// WebUtils.printAsJSON(httpServletResponse, successJSONObject); |
|
// return; |
|
// } |
|
FineLoggerFactory.getLogger().info("SynPositionHander: 请求参数 {}", body); |
|
PostController postController = AuthorityContext.getInstance().getPostController(); |
|
JSONObject jsonObject = new JSONObject(body); |
|
if (jsonObject.has("position_info")) { |
|
JSONObject entries = jsonObject.getJSONObject("position_info"); |
|
String postionId = entries.getString("id"); |
|
FineLock lock = FineCollections.getInstance().getClient().getLock("syncuser", "lockedpostion" + postionId); |
|
try { |
|
if (lock.tryLock(1L, 20, TimeUnit.SECONDS)) { |
|
String status = entries.getString("status"); |
|
Post post = postController.findOne(QueryFactory.create().addRestriction(RestrictionFactory.eq("id", postionId))); |
|
boolean enable = true; |
|
if (StringUtils.equals(status, "1")) { |
|
enable = false; |
|
} |
|
String dep_id = entries.getString("dep_id"); |
|
if (post == null) { |
|
post = new Post(); |
|
post.setName(entries.getString("name")); |
|
post.setId(postionId); |
|
post.setEnable(enable); |
|
post.setCreationType(ManualOperationType.KEY); |
|
try { |
|
postController.add(post); |
|
postController.addPostToDepartment(postionId, dep_id); |
|
} catch (Exception e) { |
|
FineLoggerFactory.getLogger().error("添加职位到部门失败:", e); |
|
WebUtils.printAsJSON(httpServletResponse, HttpUtils.getErrorByCreateRelation()); |
|
return; |
|
} |
|
} else { |
|
post.setName(entries.getString("name")); |
|
post.setId(postionId); |
|
post.setEnable(enable); |
|
post.setCreationType(ManualOperationType.KEY); |
|
postController.update(post); |
|
DepartmentController departmentController = AuthorityContext.getInstance().getDepartmentController(); |
|
List<Department> departments = departmentController.findByPost(post.getId(), QueryFactory.create()); |
|
UserController userController = AuthorityContext.getInstance().getUserController(); |
|
boolean notneedadd = false; |
|
for (Department de : departments) { |
|
if (StringUtils.equals(de.getId(), dep_id)) { |
|
notneedadd = true; |
|
} else { |
|
DataList<User> users = userController.findByDepartmentAndPost(de.getId(), postionId, QueryFactory.create()); |
|
List<User> list = users.getList(); |
|
for (User user : list) { |
|
userController.removeUserFromDepartmentAndPost(user.getId(), de.getId(), postionId); |
|
} |
|
postController.removePostFromDepartment(postionId, de.getId()); |
|
} |
|
} |
|
if (!notneedadd) { |
|
try { |
|
postController.addPostToDepartment(postionId, dep_id); |
|
} catch (Exception e) { |
|
FineLoggerFactory.getLogger().error("添加职位到部门失败:", e); |
|
WebUtils.printAsJSON(httpServletResponse, HttpUtils.getErrorByCreateRelation()); |
|
return; |
|
} |
|
} |
|
} |
|
} else { |
|
WebUtils.printAsJSON(httpServletResponse, HttpUtils.getError("获取职位同步锁失败", 111)); |
|
return; |
|
} |
|
}catch (Exception e){ |
|
LogKit.error("同步异常:{}",e); |
|
}finally { |
|
try { |
|
lock.unlock(); |
|
}catch (Exception e){ |
|
} |
|
} |
|
|
|
|
|
} |
|
} |
|
WebUtils.printAsJSON(httpServletResponse, HttpUtils.getSuccessJSONObject()); |
|
} |
|
|
|
}
|
|
|