package thyyxxk.webserver.service.settings; import cn.hutool.core.io.file.FileNameUtil; import cn.hutool.crypto.SecureUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import thyyxxk.webserver.config.exception.ExceptionEnum; import thyyxxk.webserver.constants.Message; import thyyxxk.webserver.dao.his.settings.SettingsDao; import thyyxxk.webserver.entity.ResultVo; import thyyxxk.webserver.entity.dictionary.EmployeeMi; import thyyxxk.webserver.entity.login.PersonnelQueryConditions; import thyyxxk.webserver.entity.login.UserInfo; import thyyxxk.webserver.entity.settings.deptphones.DeptPhones; import thyyxxk.webserver.entity.settings.permissions.*; import thyyxxk.webserver.entity.settings.users.ChangePwdParam; import thyyxxk.webserver.entity.settings.users.WorkIntegrationPlatformAdvice; import thyyxxk.webserver.entity.socketmessage.ApiMessageBody; import thyyxxk.webserver.entity.socketmessage.SendUserList; import thyyxxk.webserver.service.PublicServer; import thyyxxk.webserver.service.hutoolcache.RedisServer; import thyyxxk.webserver.service.externalhttp.CorpWxSrvc; import thyyxxk.webserver.service.externalhttp.WebSocketService; import thyyxxk.webserver.service.hutoolcache.RedisLikeService; import thyyxxk.webserver.utils.*; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.util.*; /** * @author dj */ @Slf4j @Service public class SettingsService { private final SettingsDao dao; private final CorpWxSrvc srvc; private final PublicServer publicServer; private final RedisLikeService redis; private final WebSocketService socketService; @Value("${is-prod}") private boolean isProd; @Autowired public SettingsService(SettingsDao dao, CorpWxSrvc srvc, PublicServer publicServer, RedisLikeService redis, WebSocketService socketService) { this.dao = dao; this.srvc = srvc; this.publicServer = publicServer; this.redis = redis; this.socketService = socketService; } public ResultVo getUserInfo() { final String code = TokenUtil.getInstance().getTokenUserId(); final UserInfo user = dao.getUserInfo(code); if (null == user) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有找到此用户的在职信息,请重新登录!"); } JSONObject json = srvc.getUserinfo(socketService.getWeComAddressBookToken(), user.getCodeRs()); user.setAvatar(json.getString("avatar")); user.setRoles(dao.getUserRoles(code)); return ResultVoUtil.success(user); } @Transactional(rollbackFor = Exception.class) public ResultVo changePassword(ChangePwdParam param) { String code = TokenUtil.getInstance().getTokenUserId(); String password = dao.getPassword(code); String newPwd = SecureUtil.md5(param.getNewPassword()); String old = SecureUtil.md5(param.getOldPassword()); if (!old.equals(password)) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "旧密码错误,请检查!"); } if (param.getNewPassword().trim().isEmpty()) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "新密码不能包含空格,请检查!"); } dao.updatePassword(code, newPwd); redis.updateUserInfo(code); return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE, "密码修改成功。"); } public ResultVo> getDeptTree() { List list = new ArrayList<>(); DeptTree deptTree = new DeptTree(); deptTree.setCode(""); deptTree.setName("长沙泰和医院"); String code = TokenUtil.getInstance().getTokenUserId(); List roles = dao.getUserRoles(code); if (roles.contains(1) || roles.contains(2)) { deptTree.setChildren(dao.selectAllDepts()); } else { deptTree.setChildren(dao.selectDeptsByUserCode(code)); } list.add(deptTree); return ResultVoUtil.success(list); } public ResultVo> getAllUsers(PersonnelQueryConditions param) { IPage page = new Page<>(param.getCurrentPage(), param.getPageSize(), param.getTotal() == 0); if (publicServer.needRule(1, 2, 62, 19)) { return ResultVoUtil.success(dao.selectAllUsers(page, personnelQuery(param))); } return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "你没有查看的权限."); } private QueryWrapper personnelQuery(PersonnelQueryConditions param) { QueryWrapper qw = new QueryWrapper<>(); if (StringUtil.notBlank(param.getDeptCode())) { qw.eq("dept_code", param.getDeptCode()); } if (StringUtil.notBlank(param.getName())) { qw.and(QueryWrapper -> QueryWrapper.like("name", param.getName()).or().eq("code_rs", param.getName())); } if (StringUtil.notBlank(param.getYbCode())) { qw.eq("yb_code", param.getYbCode()); } if (param.getWhetherToDisable() != 9) { qw.eq("isnull(del_flag,0)", param.getWhetherToDisable()); } if (param.getIsThereAMedicalInsuranceCode() == 1) { qw.isNotNull("yb_code"); } else if (param.getIsThereAMedicalInsuranceCode() == 2) { qw.isNull("yb_code"); } return qw; } public ResultVo> getAllRoles() { List roles = dao.getUserRoles(TokenUtil.getInstance().getTokenUserId()); if (roles.contains(1)) { return ResultVoUtil.success(dao.getAllRoles()); } else { return ResultVoUtil.success(dao.getAllRolesExceptAdmin()); } } public ResultVo> getUserRoles(String code) { return ResultVoUtil.success(dao.getUserRoles(code)); } @Transactional(rollbackFor = Exception.class) public ResultVo saveUserRoles(SaveUserRolesParam param) { dao.deleteUserRoles(param.getCode()); if (null == param.getRoles() || param.getRoles().isEmpty()) { return ResultVoUtil.success(); } dao.insertUserRoles(param.getCode(), param.getRoles()); redis.updateUserInfo(param.getCode()); log.info("更改人员角色 => 操作员:{},数据:{}", TokenUtil.getInstance().getTokenUserId(), param); return ResultVoUtil.success(); } public ResultVo addNewRole(String name) { if (dao.isRoleExist(name) > 0) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "该角色已存在,请匆重复添加!"); } Role role = new Role(); role.setName(name); role.setCreateStaff(TokenUtil.getInstance().getTokenUserId()); dao.addNewRole(role); return ResultVoUtil.success(); } public ResultVo> getDeptPhones() { return ResultVoUtil.success(dao.getDeptPhones()); } public ResultVo saveDeptPhone(DeptPhones param) { log.info("修改科室电话 => 操作员:{},数据:{}", TokenUtil.getInstance().getTokenUserId(), param); dao.saveDeptPhone(param); return ResultVoUtil.success(); } public ResultVo> getMyAdvices() { String userId = TokenUtil.getInstance().getTokenUserId(); List userRoles = dao.getUserRoles(userId); List list; if (null != userRoles && userRoles.contains(1)) { list = dao.selectAllAdvices(); } else { list = dao.selectMyAdvices(userId); } return ResultVoUtil.success(list); } public ResultVo submitNewAdvice(WorkIntegrationPlatformAdvice advice) { if (StringUtil.isBlank(advice.getSubmitContent())) { return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "请填写建议内容!"); } String userId = TokenUtil.getInstance().getTokenUserId(); dao.insertNewAdvice(userId, advice.getSubmitContent()); List admins = dao.selectAdmins(); JSONObject obj = new JSONObject(); obj.put("message", String.format("用户【%s】提交了新的建议,请前往个人中心查看。", userId)); String message = SocketMsg.socketVo(Message.SYSTEM_NOTIFICATION, obj); socketService.sendUserListMessage(new SendUserList(admins, message)); return ResultVoUtil.success(); } public ResultVo checkAdvice(Integer id) { dao.checkAdvice(id); return ResultVoUtil.success(); } public ResultVo dismissUserBadge(Integer id) { dao.dismissUserBadge(id); return ResultVoUtil.success(); } public ResultVo replyAdvice(WorkIntegrationPlatformAdvice advice) { if (StringUtil.isBlank(advice.getReply())) { return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "请填写回复内容!"); } dao.updateReply(advice.getId(), advice.getReply(), TokenUtil.getInstance().getTokenUserId()); JSONObject obj = new JSONObject(); obj.put("message", "您提交的建议已有新的回复,请前往个人中心查看。"); String message = SocketMsg.socketVo(Message.SYSTEM_NOTIFICATION, obj); ApiMessageBody messageBody = new ApiMessageBody(advice.getSubmitStaff(), message); socketService.sendMessageByUserCode(messageBody); return ResultVoUtil.success(); } public ResultVo> getRoleVueMenus(Integer id) { return ResultVoUtil.success(dao.getRoleVueMenus(id)); } @Transactional(rollbackFor = Exception.class) public ResultVo saveRoleVueMenus(SaveRoleMenusParam param) { dao.deleteRoleVueMenus(param.getId()); if (null == param.getMenus() || param.getMenus().isEmpty()) { return ResultVoUtil.success(); } log.info("修改角色菜单 => 操作员:{},数据:{}", TokenUtil.getInstance().getTokenUserId(), param); dao.insertRoleVueMenus(param.getId(), param.getMenus()); redis.setIntergrationRoleMenu(); return ResultVoUtil.success(); } public ResultVo modifyEmployeeInfo(UserInfo userInfo) { UserInfo info = getEmployeeOriginalInformation(userInfo.getCode()); if (null == info) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有查询到原来信息"); } String changedField = CampareObject.getChangedFieldsOfSql(info, userInfo); if (StringUtil.notBlank(changedField)) { if (changedField.contains("name")) { changedField += String.format(",py_code = '%s',d_code = '%s' ", PingYinUtils.pyShouZiMuDaXie(userInfo.getName()), PingYinUtils.getWBCode(userInfo.getName())); } dao.modifyPersonnelInformation(changedField, userInfo.getCode()); } dao.delPartTimeDeptByCode(userInfo.getCode()); if (ListUtil.notBlank(userInfo.getPartTimeDept())) { dao.insertPartTimeDept(userInfo.getCode(), userInfo.getPartTimeDept()); } redis.updateUserInfo(userInfo.getCode()); log.info("修改员工信息==>操作人:{},数据:{}", TokenUtil.getInstance().getTokenUserId(), JSON.toJSONString(userInfo)); return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE); } public ResultVo saveEmployeeInfo(UserInfo userInfo) { if (dao.doesThePersonnelNumberExist(userInfo.getCodeRs()) > 0) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "人事工号已存在,换一个."); } userInfo.setCode("0" + publicServer.getPersonnelCode()) .setPyCode(PingYinUtils.pyShouZiMuDaXie(userInfo.getName())) .setDCode(PingYinUtils.getWBCode(userInfo.getName())); dao.saveEmployeeInfo(userInfo); dao.delPartTimeDeptByCode(userInfo.getCode()); if (ListUtil.notBlank(userInfo.getPartTimeDept())) { dao.insertPartTimeDept(userInfo.getCode(), userInfo.getPartTimeDept()); } redis.updateUserInfo(userInfo.getCode()); log.info("添加员工信息==>操作人:{},数据:{}", TokenUtil.getInstance().getTokenUserId(), JSON.toJSONString(userInfo)); return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE); } public ResultVo resetPasswordByCode(String code, Integer nextTime) { dao.resetPassword(code); return ResultVoUtil.fail(ExceptionEnum.SUCCESS_AND_EL_MESSAGE); } private UserInfo getEmployeeOriginalInformation(String code) { IPage page = new Page<>(1, 1, false); QueryWrapper qw = new QueryWrapper<>(); qw.eq("a.code", code); return dao.selectAllUsers(page, qw).getRecords().get(0); } /** * @param file 签名 code 人员编码 * @return map * @Description 更新医生的签名 * @Author hsh * @Date 2024/5/14 10:36 */ public ResultVo> setAutographImage(MultipartFile file, String code) { if (StringUtil.isBlank(code)) { return ResultVoUtil.fail(ExceptionEnum.INVALID_PARAM); } String img = base64Encode(file); Map m = dao.selectAutographImageByCode(code); int count; if (null == m || m.isEmpty()) { EmployeeMi mi = dao.selectEmployeeMiByCode(code); if (null != mi) { count = dao.intoAutographImage(code, img, mi.getSexCode() == null ? "9" : mi.getSexCode()); } else { count = dao.intoAutographImage(code, img, "9"); } } else { count = dao.updateAutographImage(code, img); } if (count > 0) { Map map = new HashMap<>(); Map imgMap = dao.selectAutographImageByCode(code); map.put("data", imgMap); return ResultVoUtil.success(map); } else { return ResultVoUtil.fail(ExceptionEnum.NEED_PROOFREAD); } } /** * @param code 医生编码 * @return map * @Description 查询医生签名图片 * @Author hsh * @Date 2024/5/15 9:47 */ public ResultVo> selectAutographImageByCode(String code) { return ResultVoUtil.success(dao.selectAutographImageByCode(code)); } private String base64Encode(MultipartFile file) { String ret = null; if (file == null) { return null; } try { Base64.Encoder encoder = Base64.getEncoder(); ret = encoder.encodeToString(file.getBytes()); } catch (Exception e) { log.error("将图片文件转base64出错", e); } return ret; } public ResultVo putAutographImage(MultipartFile file, String code) { String codeRs = ""; if (code == null) { codeRs = FileNameUtil.mainName(file.getOriginalFilename()); } if (StringUtil.isBlank(code) && StringUtil.notBlank(codeRs)) { code = dao.selectCodeByCodeRs(codeRs); if (StringUtil.isBlank(code)) { return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "工号不存在"); } } String uploadDir; if (isProd) { uploadDir = "/home/doctorSignature"; } else { uploadDir = "D:\\doctorSignature"; } JSONObject js = new JSONObject(); String name = FileNameUtil.extName(file.getOriginalFilename()); String fileName = code + "." + name; Path uploadPath = Paths.get(uploadDir); try { Files.createDirectories(uploadPath); Path filePath = uploadPath.resolve(fileName); Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING); log.info("文件:{}, code:{}", file.getName(), code); String url = "http://172.16.32.167:8077/doctorSignatureImage/" + fileName; js.put("url", url); js.put("name", fileName); return ResultVoUtil.success(js); } catch (IOException e) { log.error("上传错误:{}", e.getMessage()); return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "上传错误请重新上传。"); } } public void setUserConfig(String js) { dao.updateUserConfig(js, TokenUtil.getInstance().getTokenUserId()); redis.updateUserInfo(TokenUtil.getInstance().getTokenUserId()); } }