package com.yunhai.flowcenter.service; import com.yunhai.flowcenter.entity.FlwAssigneeMap; import com.yunhai.flowcenter.repository.FlwAssigneeMapRepository; import com.yunhai.platform.apistd.BizException; import com.yunhai.platform.utils.exception.ErrorCode; import com.yunhai.platform.utils.model.BoaUser; import com.yunhai.platform.openapi.client.OpenApiUserClient; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; /** * 办理人映射管理(概览 §4.7/§六):DEPT_KEY → 办理人显式映射,成员检索经 * UserBase /oapi/user/byId 补全姓名快照(检索按 id 精确查;按名检索二期接 UserBase 模糊端点)。 */ @Service @RequiredArgsConstructor public class FlowMapAdminService { private final FlwAssigneeMapRepository mapRepository; private final OpenApiUserClient userClient; public List listByDept(Long schId, String deptKey) { if (deptKey == null || deptKey.trim().isEmpty()) { return mapRepository.findEnabledBySchId(schId); } return mapRepository.findBySchIdAndDeptKeyOrderByUserIdAsc(schId, deptKey.trim()); } /** 新增/启用映射(uk: sch+dept+user;已存在则刷新姓名/状态) */ @Transactional public FlwAssigneeMap save(Long schId, String deptKey, String deptName, Integer userId, String userName) { if (deptKey == null || deptKey.trim().isEmpty() || userId == null) { throw new BizException(ErrorCode.PARAM_ERROR, "deptKey/userId 必填"); } if (userName == null || userName.trim().isEmpty()) { userName = fetchName(userId); } FlwAssigneeMap map = mapRepository.findBySchIdAndDeptKeyAndUserId(schId, deptKey.trim(), userId); if (map == null) { map = new FlwAssigneeMap(); map.setSchId(schId); map.setDeptKey(deptKey.trim()); map.setUserId(userId); } map.setDeptName(deptName); map.setUserName(userName); map.setStatus(0); return mapRepository.save(map); } /** 停用映射(保留历史,DEPT_KEY 解析只取 status=0) */ @Transactional public void disable(Long schId, Long id) { FlwAssigneeMap map = mapRepository.findById(id) .orElseThrow(() -> new BizException(ErrorCode.PARAM_ERROR, "映射不存在:" + id)); if (!map.getSchId().equals(schId)) { throw new BizException(ErrorCode.FORBIDDEN, "映射不属于该学校"); } map.setStatus(1); mapRepository.save(map); } /** 成员检索:按用户 id 从 UserBase 取姓名快照(管理端录入校验) */ public BoaUser lookupUser(Integer userId) { try { return userClient.getUserById(String.valueOf(userId)); } catch (Exception e) { throw new BizException(ErrorCode.PARAM_ERROR, "UserBase 用户检索失败(服务不可达)"); } } private String fetchName(Integer userId) { try { BoaUser user = userClient.getUserById(String.valueOf(userId)); return user == null ? "" : (user.getName() == null ? "" : user.getName()); } catch (Exception e) { return ""; } } }