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.
406 lines
14 KiB
406 lines
14 KiB
package com.nov.KgLowDurable.service.Impl; |
|
import com.alibaba.fastjson.JSON; |
|
import com.alibaba.fastjson.JSONObject; |
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
import com.nov.KgLowDurable.mapper.UserMapper; |
|
import com.nov.KgLowDurable.pojo.entity.*; |
|
import com.nov.KgLowDurable.pojo.vo.*; |
|
import com.nov.KgLowDurable.service.*; |
|
import com.nov.KgLowDurable.util.HttpUtils; |
|
import lombok.AllArgsConstructor; |
|
import lombok.extern.slf4j.Slf4j; |
|
import org.apache.http.util.EntityUtils; |
|
import org.springframework.beans.BeanUtils; |
|
import org.springframework.beans.factory.annotation.Autowired; |
|
import org.springframework.context.annotation.Lazy; |
|
import org.springframework.stereotype.Service; |
|
import org.springframework.util.CollectionUtils; |
|
import org.springframework.util.StringUtils; |
|
|
|
import java.time.LocalDateTime; |
|
import java.util.ArrayList; |
|
import java.util.HashMap; |
|
import java.util.List; |
|
import java.util.Map; |
|
import java.util.stream.Collectors; |
|
|
|
/** |
|
* 服务实现类 |
|
* @author liweidong |
|
*/ |
|
@Service |
|
@AllArgsConstructor |
|
@Slf4j |
|
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService { |
|
|
|
@Autowired |
|
UserMapper userMapper; |
|
|
|
@Autowired |
|
ILdApproveService ldApproveService; |
|
|
|
@Autowired |
|
IDepartmentService departmentService; |
|
|
|
|
|
@Override |
|
public UserInfoVO getUserInfo() throws Exception { |
|
// Map<String, String> headers = new HashMap<String, String>() {{ |
|
// put("Content-Type", "application/json"); |
|
// }}; |
|
// |
|
// Map<String, String> querys = new HashMap<String, String>() {{ |
|
// put("taskId", "1"); |
|
// }}; |
|
// |
|
// String resp = EntityUtils.toString( |
|
// HttpUtils.doGet( |
|
// "http://10.90.100.231:8132", |
|
// "/AES/decryptAndGetUser", |
|
// "GET", |
|
// headers, |
|
// querys |
|
// ).getEntity() |
|
// ); |
|
// |
|
// JSONObject obj = JSON.parseObject(resp); |
|
// |
|
// if (!"200".equals(obj.getString("code"))) { |
|
// throw new RuntimeException("失败: " + obj.getString("message")); |
|
// } |
|
// |
|
// UserInfoVO data = JSON.toJavaObject(obj.getJSONObject("data"), UserInfoVO.class); |
|
|
|
UserInfoVO data = new UserInfoVO(); |
|
data.setUserId("AnXinghe"); |
|
data.setName("安星河"); |
|
data.setDepartment("中国民用航空青岛空中交通管理站/实业公司/IT事业部"); |
|
data.setGender("男"); |
|
data.setPhone("13526109603"); |
|
data.setRoleNames("普通用户,超级管理员"); |
|
data.setMainErDepartment("12"); |
|
data.setMainErDepartmentName("实业公司"); |
|
Role role = new Role(); |
|
role.setId(1L); |
|
role.setCode("admin"); |
|
role.setName("超级管理员"); |
|
List<Role> roleList = new ArrayList<>(); |
|
roleList.add(role); |
|
data.setRolelist(roleList); |
|
return data; |
|
} |
|
|
|
|
|
|
|
/** |
|
* 模拟数据 - 直接使用JSON字符串 |
|
*/ |
|
private static final String MOCK_APPROVAL_DATA = "{\n" + |
|
" \"success\": true,\n" + |
|
" \"message\": \"获取审批列表成功\",\n" + |
|
" \"code\": 200,\n" + |
|
" \"result\": {\n" + |
|
" \"1\": [\n" + |
|
" {\n" + |
|
" \"userId\": \"LiuXu\",\n" + |
|
" \"name\": \"刘旭\",\n" + |
|
" \"department\": \"青岛空管站/实业公司/IT事业部/产品研发室\",\n" + |
|
" \"mainDepartment\": \"82\",\n" + |
|
" \"mainErDepartment\": \"20\",\n" + |
|
" \"leaderInDept\": \",\",\n" + |
|
" \"gender\": \"女\",\n" + |
|
" \"phone\": \"17853572157\",\n" + |
|
" \"mail\": null,\n" + |
|
" \"roleList\": null,\n" + |
|
" \"roleNames\": null,\n" + |
|
" \"checked\": false,\n" + |
|
" \"manageNum\": \"1\",\n" + |
|
" \"deptId\": null,\n" + |
|
" \"deptList\": null,\n" + |
|
" \"postName\": null,\n" + |
|
" \"virPostName\": null,\n" + |
|
" \"apprStatus\": null,\n" + |
|
" \"isCanAbnormal\": false,\n" + |
|
" \"userSort\": 1000,\n" + |
|
" \"isDel\": false,\n" + |
|
" \"mainErDepartmentName\": null\n" + |
|
" }\n" + |
|
" ],\n" + |
|
" \"2\": [\n" + |
|
" {\n" + |
|
" \"userId\": \"AnXingHe\",\n" + |
|
" \"name\": \"安星河\",\n" + |
|
" \"department\": \"青岛空管站/实业公司/IT事业部/产品研发室\",\n" + |
|
" \"mainDepartment\": \"82\",\n" + |
|
" \"mainErDepartment\": \"20\",\n" + |
|
" \"leaderInDept\": \",20,\",\n" + |
|
" \"gender\": \"男\",\n" + |
|
" \"phone\": \"13526109603\",\n" + |
|
" \"mail\": null,\n" + |
|
" \"roleList\": null,\n" + |
|
" \"roleNames\": null,\n" + |
|
" \"checked\": false,\n" + |
|
" \"manageNum\": \"1\",\n" + |
|
" \"deptId\": null,\n" + |
|
" \"deptList\": null,\n" + |
|
" \"postName\": null,\n" + |
|
" \"virPostName\": null,\n" + |
|
" \"apprStatus\": null,\n" + |
|
" \"isCanAbnormal\": false,\n" + |
|
" \"userSort\": 1000,\n" + |
|
" \"isDel\": false,\n" + |
|
" \"mainErDepartmentName\": null\n" + |
|
" }\n" + |
|
" ]\n" + |
|
" },\n" + |
|
" \"timestamp\": 1767767650684\n" + |
|
"}"; |
|
|
|
@Override |
|
public String getApprover(String mainErDepartment, String type,Long formId) { |
|
// 参数校验 |
|
if (!StringUtils.hasText(type)) { |
|
throw new IllegalArgumentException("审批类型不能为空"); |
|
} |
|
if (formId == null || formId <= 0) { |
|
throw new IllegalArgumentException("表单ID必须大于0"); |
|
} |
|
Map<String, String> headers = new HashMap<String, String>() {{ |
|
put("Content-Type", "application/json"); |
|
}}; |
|
|
|
Map<String, String> querys = new HashMap(); |
|
querys. put("type",type); |
|
if ("1".equals(type)) { |
|
querys.put("deptId", mainErDepartment); |
|
} |
|
|
|
String resp = null; |
|
try { |
|
resp = EntityUtils.toString( |
|
HttpUtils.doGet( |
|
"http://10.90.100.231:8132", |
|
"/ld-person-form/getApprove", |
|
"GET", |
|
headers, |
|
querys |
|
).getEntity() |
|
); |
|
} catch (Exception e) { |
|
e.printStackTrace(); |
|
} |
|
|
|
try { |
|
// 1.解析模拟数据 |
|
JSONObject obj = JSON.parseObject(resp); |
|
|
|
if (!"200".equals(obj.getString("code")) || !obj.getBooleanValue("success")) { |
|
String message = obj.getString("message"); |
|
throw new RuntimeException("获取审批人失败: " + message); |
|
} |
|
|
|
// 2.获取审批结果数据 |
|
JSONObject result = obj.getJSONObject("result"); |
|
if (result == null || result.isEmpty()) { |
|
throw new RuntimeException("未找到审批人数据"); |
|
} |
|
|
|
// 3.保存审批人信息到数据库 |
|
List<LdApprove> approversToSave = new ArrayList<>(); |
|
// 构建审批人json结构 [{"level":1,"ids":"LiuXu"},{"level":2,"ids":"AnXingHe"},{"level":3,"ids":"LiuXu"}] |
|
List<JSONObject> approvers = new ArrayList<>(); |
|
|
|
// 遍历所有审批层级(1, 2, 3...) |
|
for (String level : result.keySet()) { |
|
// 获取该层级的所有用户 |
|
List<User> users = result.getJSONArray(level).toJavaList(User.class); |
|
if (CollectionUtils.isEmpty(users)) { |
|
log.warn("层级 {} 没有审批人", level); |
|
continue; |
|
} |
|
|
|
for (int i = 0; i < users.size(); i++) { |
|
User user = users.get(i); |
|
LdApprove ldApprove = createLdApprove(user, level, formId, type); |
|
approversToSave.add(ldApprove); |
|
|
|
JSONObject approver = new JSONObject(); |
|
approver.put("level", level); |
|
approver.put("ids", user.getUserId()); |
|
approvers.add(approver); |
|
} |
|
} |
|
|
|
// 5. 批量保存审批人 |
|
boolean saveSuccess = false; |
|
if (!CollectionUtils.isEmpty(approversToSave)) { |
|
saveSuccess = ldApproveService.saveBatch(approversToSave); |
|
} else { |
|
throw new RuntimeException("未找到审批人信息"); |
|
} |
|
|
|
return JSONObject.toJSONString(approvers); |
|
|
|
} catch (Exception e) { |
|
throw new RuntimeException("处理审批人数据失败: " + e.getMessage(), e); |
|
} |
|
|
|
} |
|
|
|
/** |
|
* 创建审批记录实体 |
|
*/ |
|
private LdApprove createLdApprove(User user, String level, Long formId, String type) { |
|
LdApprove ldApprove = new LdApprove(); |
|
ldApprove.setFormId(formId.intValue()); |
|
ldApprove.setUserId(user.getUserId()); |
|
ldApprove.setUserName(user.getName()); |
|
ldApprove.setLevel(level); |
|
ldApprove.setStatus("0"); |
|
ldApprove.setOptTime(LocalDateTime.now()); |
|
ldApprove.setType("1".equals(type)?"3":type); |
|
return ldApprove; |
|
} |
|
|
|
@Override |
|
public User getDeptApprove(String department) { |
|
|
|
String topDepartmentId = getTopDepartmentId(department); |
|
|
|
User user = userMapper.selectByLeaderInDept(topDepartmentId); |
|
|
|
if(null == user){ |
|
throw new RuntimeException(department+":部门负责人获取失败"); |
|
} |
|
|
|
return user; |
|
} |
|
|
|
public String getTopDepartmentId(String departmentId) { |
|
int maxDepth = 20; |
|
int currentDepth = 0; |
|
String currentId = departmentId; |
|
while (currentDepth < maxDepth) { |
|
Department dept = departmentService.getByDepartmentId(currentId); |
|
if (dept == null) { |
|
throw new RuntimeException("部门不存在: " + currentId); |
|
} |
|
String parentId = dept.getParentid(); |
|
//如果是顶级部门 返回当前部门ID |
|
if ("0".equals(parentId) || "1".equals(parentId)) { |
|
return dept.getId(); |
|
} |
|
//否则继续查询父部门 |
|
currentId = parentId; |
|
currentDepth++; |
|
} |
|
|
|
throw new RuntimeException("部门负责人获取失败,查询深度超过限制: " + departmentId); |
|
} |
|
|
|
@Override |
|
public List<User> getUserByDeptId(String departmentId) { |
|
return userMapper.selectByDepartmentId(departmentId); |
|
} |
|
|
|
@Override |
|
public void submitApproval(String json) { |
|
Map<String, String> headers = new HashMap<String, String>() {{ |
|
put("Content-Type", "application/json"); |
|
}}; |
|
|
|
Map<String, String> querys = new HashMap<String, String>() {{ |
|
}}; |
|
|
|
String resp = null; |
|
try { |
|
resp = EntityUtils.toString( |
|
HttpUtils.doPost( |
|
"http://10.90.100.231:8132", |
|
"/qywx/applySubmit", |
|
"POST", |
|
headers, |
|
querys, |
|
json |
|
).getEntity() |
|
); |
|
} catch (Exception e) { |
|
e.printStackTrace(); |
|
} |
|
} |
|
|
|
@Override |
|
public List<GroupVO> getOutGroupName() { |
|
Map<String, String> headers = new HashMap<String, String>() {{ |
|
put("Content-Type", "application/json"); |
|
}}; |
|
|
|
Map<String, String> querys = new HashMap<String, String>() {{ |
|
}}; |
|
|
|
String resp = null; |
|
try { |
|
resp = EntityUtils.toString( |
|
HttpUtils.doGet( |
|
"http://10.90.100.231:8132", |
|
"/ld-person-form/getOutGroupName", |
|
"GET", |
|
headers, |
|
querys |
|
).getEntity() |
|
); |
|
} catch (Exception e) { |
|
e.printStackTrace(); |
|
} |
|
JSONObject obj = JSON.parseObject(resp); |
|
|
|
if (!"200".equals(obj.getString("code"))) { |
|
throw new RuntimeException("失败: " + obj.getString("message")); |
|
} |
|
|
|
List<GroupVO> list = obj.getJSONArray("result").toJavaList(GroupVO.class); |
|
if(CollectionUtils.isEmpty(list)){ |
|
throw new RuntimeException("解析失败: " + obj.getString("message")); |
|
} |
|
return list; |
|
} |
|
|
|
@Override |
|
public List<GroupMaterialVO> getGroupMaterial(String groupName, String departmentId) { |
|
Map<String, String> headers = new HashMap<String, String>() {{ |
|
put("Content-Type", "application/json"); |
|
}}; |
|
|
|
Map<String, String> querys = new HashMap<String, String>() {{ |
|
put("groupName",groupName); |
|
}}; |
|
|
|
String resp = null; |
|
try { |
|
resp = EntityUtils.toString( |
|
HttpUtils.doGet( |
|
"http://10.90.100.231:8132", |
|
"/ld-person-form/getGroupMaterial", |
|
"GET", |
|
headers, |
|
querys |
|
).getEntity() |
|
); |
|
} catch (Exception e) { |
|
e.printStackTrace(); |
|
} |
|
JSONObject obj = JSON.parseObject(resp); |
|
if (!"200".equals(obj.getString("code"))) { |
|
throw new RuntimeException("失败: " + obj.getString("message")); |
|
} |
|
|
|
GroupResultVO result = JSON.parseObject( |
|
obj.getJSONObject("result").toString(), |
|
GroupResultVO.class |
|
); |
|
|
|
List<GroupMaterialVO> materialVOList = result.getMaterials(); |
|
|
|
return materialVOList; |
|
} |
|
}
|
|
|