SettingsService.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. package thyyxxk.webserver.service.settings;
  2. import cn.hutool.core.io.file.FileNameUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import cn.hutool.crypto.SecureUtil;
  5. import com.alibaba.fastjson.JSON;
  6. import com.alibaba.fastjson.JSONObject;
  7. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  8. import com.baomidou.mybatisplus.core.metadata.IPage;
  9. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  10. import lombok.RequiredArgsConstructor;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.springframework.stereotype.Service;
  13. import org.springframework.transaction.annotation.Transactional;
  14. import org.springframework.web.multipart.MultipartFile;
  15. import thyyxxk.webserver.config.exception.ExceptionEnum;
  16. import thyyxxk.webserver.constants.Message;
  17. import thyyxxk.webserver.dao.his.settings.SettingsDao;
  18. import thyyxxk.webserver.entity.ResultVo;
  19. import thyyxxk.webserver.entity.login.PersonnelQueryConditions;
  20. import thyyxxk.webserver.entity.login.UserInfo;
  21. import thyyxxk.webserver.entity.settings.deptphones.DeptPhones;
  22. import thyyxxk.webserver.entity.settings.permissions.*;
  23. import thyyxxk.webserver.entity.settings.users.ChangePwdParam;
  24. import thyyxxk.webserver.entity.settings.users.WorkIntegrationPlatformAdvice;
  25. import thyyxxk.webserver.http.websocket.SocketV2;
  26. import thyyxxk.webserver.http.websocket.dto.WebSocketByListUserCode;
  27. import thyyxxk.webserver.http.websocket.dto.WebSocketByUserCode;
  28. import thyyxxk.webserver.service.PublicServer;
  29. import thyyxxk.webserver.service.TokenService;
  30. import thyyxxk.webserver.service.archive.ArchiveServer;
  31. import thyyxxk.webserver.service.externalhttp.CorpWxSrvc;
  32. import thyyxxk.webserver.service.hutoolcache.CacheEnums;
  33. import thyyxxk.webserver.service.hutoolcache.ExtraCache;
  34. import thyyxxk.webserver.service.hutoolcache.UserCache;
  35. import thyyxxk.webserver.utils.*;
  36. import java.io.File;
  37. import java.io.IOException;
  38. import java.util.*;
  39. /**
  40. * @author dj
  41. */
  42. @Slf4j
  43. @Service
  44. @RequiredArgsConstructor
  45. public class SettingsService {
  46. private final SettingsDao dao;
  47. private final CorpWxSrvc srvc;
  48. private final PublicServer publicServer;
  49. private final ExtraCache extraCache;
  50. private final UserCache userCache;
  51. private final TokenService tokenService;
  52. private final SocketV2 intergrationPlatformSocket;
  53. private final ArchiveServer archiveServer;
  54. public ResultVo<UserInfo> getUserInfo() {
  55. final String code = TokenUtil.getInstance().getTokenUserId();
  56. final UserInfo user = dao.getUserInfo(code);
  57. if (null == user) {
  58. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有找到此用户的在职信息,请重新登录!");
  59. }
  60. user.setRoles(dao.getUserRoles(code));
  61. String weComToken = tokenService.getWeComAddressBookToken();
  62. if (StrUtil.isNotBlank(weComToken)) {
  63. JSONObject json = srvc.getUserinfo(weComToken, user.getCodeRs());
  64. user.setAvatar(json.getString("avatar"));
  65. }
  66. return ResultVoUtil.success(user);
  67. }
  68. @Transactional(rollbackFor = Exception.class)
  69. public ResultVo<String> changePassword(ChangePwdParam param) {
  70. String code = TokenUtil.getInstance().getTokenUserId();
  71. String password = dao.getPassword(code);
  72. String newPwd = SecureUtil.md5(param.getNewPassword());
  73. String old = SecureUtil.md5(param.getOldPassword());
  74. if (!old.equals(password)) {
  75. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "旧密码错误,请检查!");
  76. }
  77. if (param.getNewPassword().trim().isEmpty()) {
  78. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "新密码不能包含空格,请检查!");
  79. }
  80. dao.updatePassword(code, newPwd);
  81. userCache.refreshCache(code);
  82. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE, "密码修改成功。");
  83. }
  84. public ResultVo<List<DeptTree>> getDeptTree() {
  85. List<DeptTree> list = new ArrayList<>();
  86. DeptTree deptTree = new DeptTree();
  87. deptTree.setCode("");
  88. deptTree.setName("沭阳铭和医院");
  89. String code = TokenUtil.getInstance().getTokenUserId();
  90. List<Integer> roles = dao.getUserRoles(code);
  91. if (roles.contains(1) || roles.contains(2)) {
  92. deptTree.setChildren(dao.selectAllDepts());
  93. } else {
  94. deptTree.setChildren(dao.selectDeptsByUserCode(code));
  95. }
  96. list.add(deptTree);
  97. return ResultVoUtil.success(list);
  98. }
  99. public ResultVo<IPage<UserInfo>> getAllUsers(PersonnelQueryConditions param) {
  100. IPage<UserInfo> page = new Page<>(param.getCurrentPage(), param.getPageSize(), param.getTotal() == 0);
  101. if (publicServer.needRule(1, 2, 62, 19)) {
  102. return ResultVoUtil.success(dao.selectAllUsers(page, personnelQuery(param)));
  103. }
  104. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "你没有查看的权限.");
  105. }
  106. private QueryWrapper<?> personnelQuery(PersonnelQueryConditions param) {
  107. QueryWrapper<?> qw = new QueryWrapper<>();
  108. if (StringUtil.notBlank(param.getDeptCode())) {
  109. qw.eq("dept_code", param.getDeptCode());
  110. }
  111. if (StringUtil.notBlank(param.getName())) {
  112. qw.and(QueryWrapper -> QueryWrapper.like("name", param.getName()).or().eq("code_rs", param.getName()));
  113. }
  114. if (StringUtil.notBlank(param.getYbCode())) {
  115. qw.eq("yb_code", param.getYbCode());
  116. }
  117. if (param.getWhetherToDisable() != 9) {
  118. qw.eq("isnull(del_flag,0)", param.getWhetherToDisable());
  119. }
  120. if (param.getIsThereAMedicalInsuranceCode() == 1) {
  121. qw.isNotNull("yb_code");
  122. } else if (param.getIsThereAMedicalInsuranceCode() == 2) {
  123. qw.isNull("yb_code");
  124. }
  125. return qw;
  126. }
  127. public ResultVo<List<Role>> getAllRoles() {
  128. List<Integer> roles = dao.getUserRoles(TokenUtil.getInstance().getTokenUserId());
  129. if (roles.contains(1)) {
  130. return ResultVoUtil.success(dao.getAllRoles());
  131. } else {
  132. return ResultVoUtil.success(dao.getAllRolesExceptAdmin());
  133. }
  134. }
  135. public ResultVo<List<Integer>> getUserRoles(String code) {
  136. return ResultVoUtil.success(dao.getUserRoles(code));
  137. }
  138. @Transactional(rollbackFor = Exception.class)
  139. public ResultVo<String> saveUserRoles(SaveUserRolesParam param) {
  140. dao.deleteUserRoles(param.getCode());
  141. if (null == param.getRoles() || param.getRoles().isEmpty()) {
  142. return ResultVoUtil.success();
  143. }
  144. dao.insertUserRoles(param.getCode(), param.getRoles());
  145. userCache.refreshCache(param.getCode());
  146. log.info("更改人员角色 => 操作员:{},数据:{}", TokenUtil.getInstance().getTokenUserId(), param);
  147. return ResultVoUtil.success();
  148. }
  149. public ResultVo<String> addNewRole(String name) {
  150. if (dao.isRoleExist(name) > 0) {
  151. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "该角色已存在,请匆重复添加!");
  152. }
  153. Role role = new Role();
  154. role.setName(name);
  155. role.setCreateStaff(TokenUtil.getInstance().getTokenUserId());
  156. dao.addNewRole(role);
  157. return ResultVoUtil.success();
  158. }
  159. public ResultVo<List<DeptPhones>> getDeptPhones() {
  160. return ResultVoUtil.success(dao.getDeptPhones());
  161. }
  162. public ResultVo<String> saveDeptPhone(DeptPhones param) {
  163. log.info("修改科室电话 => 操作员:{},数据:{}", TokenUtil.getInstance().getTokenUserId(), param);
  164. dao.saveDeptPhone(param);
  165. return ResultVoUtil.success();
  166. }
  167. public ResultVo<List<WorkIntegrationPlatformAdvice>> getMyAdvices() {
  168. String userId = TokenUtil.getInstance().getTokenUserId();
  169. List<Integer> userRoles = dao.getUserRoles(userId);
  170. List<WorkIntegrationPlatformAdvice> list;
  171. if (null != userRoles && userRoles.contains(1)) {
  172. list = dao.selectAllAdvices();
  173. } else {
  174. list = dao.selectMyAdvices(userId);
  175. }
  176. return ResultVoUtil.success(list);
  177. }
  178. public ResultVo<String> submitNewAdvice(WorkIntegrationPlatformAdvice advice) {
  179. if (StringUtil.isBlank(advice.getSubmitContent())) {
  180. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "请填写建议内容!");
  181. }
  182. String userId = TokenUtil.getInstance().getTokenUserId();
  183. dao.insertNewAdvice(userId, advice.getSubmitContent());
  184. List<String> admins = dao.selectAdmins();
  185. JSONObject obj = new JSONObject();
  186. obj.put("message", String.format("用户【%s】提交了新的建议,请前往个人中心查看。", userId));
  187. String message = SocketMsg.socketVo(Message.SYSTEM_NOTIFICATION, obj);
  188. intergrationPlatformSocket.sendUserCodeListMessage(
  189. WebSocketByListUserCode.create(admins, message)
  190. );
  191. return ResultVoUtil.success();
  192. }
  193. public ResultVo<String> checkAdvice(Integer id) {
  194. dao.checkAdvice(id);
  195. return ResultVoUtil.success();
  196. }
  197. public ResultVo<String> dismissUserBadge(Integer id) {
  198. dao.dismissUserBadge(id);
  199. return ResultVoUtil.success();
  200. }
  201. public ResultVo<String> replyAdvice(WorkIntegrationPlatformAdvice advice) {
  202. if (StringUtil.isBlank(advice.getReply())) {
  203. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "请填写回复内容!");
  204. }
  205. dao.updateReply(advice.getId(), advice.getReply(), TokenUtil.getInstance().getTokenUserId());
  206. String message = SocketMsg.socketVo(Message.SYSTEM_NOTIFICATION, "您提交的建议已有新的回复,请前往个人中心查看。");
  207. intergrationPlatformSocket.sendMessageByUserCode(WebSocketByUserCode.create(advice.getSubmitStaff(), message));
  208. return ResultVoUtil.success();
  209. }
  210. public ResultVo<List<Integer>> getRoleVueMenus(Integer id) {
  211. return ResultVoUtil.success(dao.getRoleVueMenus(id));
  212. }
  213. @Transactional(rollbackFor = Exception.class)
  214. public ResultVo<String> saveRoleVueMenus(SaveRoleMenusParam param) {
  215. dao.deleteRoleVueMenus(param.getId());
  216. if (null == param.getMenus() || param.getMenus().isEmpty()) {
  217. return ResultVoUtil.success();
  218. }
  219. log.info("修改角色菜单 => 操作员:{},数据:{}", TokenUtil.getInstance().getTokenUserId(), param);
  220. dao.insertRoleVueMenus(param.getId(), param.getMenus());
  221. extraCache.del(CacheEnums.ROLE_MENU);
  222. return ResultVoUtil.success();
  223. }
  224. public ResultVo<String> modifyEmployeeInfo(UserInfo userInfo) {
  225. UserInfo info = getEmployeeOriginalInformation(userInfo.getCode());
  226. if (null == info) {
  227. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有查询到原来信息");
  228. }
  229. String changedField = CampareObject.getChangedFieldsOfSql(info, userInfo);
  230. if (StringUtil.notBlank(changedField)) {
  231. if (changedField.contains("name")) {
  232. changedField += String.format(",py_code = '%s',d_code = '%s' ",
  233. PingYinUtils.pyShouZiMuDaXie(userInfo.getName()), PingYinUtils.getWBCode(userInfo.getName()));
  234. }
  235. dao.modifyPersonnelInformation(changedField, userInfo.getCode());
  236. }
  237. if(userInfo.getDoctorYpZl() == null) {
  238. dao.setDoctorYpZlNull(userInfo.getCode());
  239. }
  240. dao.delPartTimeDeptByCode(userInfo.getCode());
  241. if (ListUtil.notBlank(userInfo.getPartTimeDept())) {
  242. dao.insertPartTimeDept(userInfo.getCode(), userInfo.getPartTimeDept());
  243. }
  244. userCache.refreshCache(userInfo.getCode());
  245. log.info("修改员工信息==>操作人:{},数据:{}", TokenUtil.getInstance().getTokenUserId(), JSON.toJSONString(userInfo));
  246. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE);
  247. }
  248. public ResultVo<String> saveEmployeeInfo(UserInfo userInfo) {
  249. if (dao.doesThePersonnelNumberExist(userInfo.getCodeRs()) > 0) {
  250. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "人事工号已存在,换一个.");
  251. }
  252. userInfo.setCode("0" + publicServer.getPersonnelCode());
  253. userInfo.setPyCode(PingYinUtils.pyShouZiMuDaXie(userInfo.getName()));
  254. userInfo.setDCode(PingYinUtils.getWBCode(userInfo.getName()));
  255. dao.saveEmployeeInfo(userInfo);
  256. dao.delPartTimeDeptByCode(userInfo.getCode());
  257. if (ListUtil.notBlank(userInfo.getPartTimeDept())) {
  258. dao.insertPartTimeDept(userInfo.getCode(), userInfo.getPartTimeDept());
  259. }
  260. userCache.refreshCache(userInfo.getCode());
  261. log.info("添加员工信息==>操作人:{},数据:{}", TokenUtil.getInstance().getTokenUserId(), JSON.toJSONString(userInfo));
  262. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE);
  263. }
  264. public ResultVo<String> resetPasswordByCode(String code, Integer nextTime) {
  265. dao.resetPassword(code);
  266. return ResultVoUtil.fail(ExceptionEnum.SUCCESS_AND_EL_MESSAGE);
  267. }
  268. private UserInfo getEmployeeOriginalInformation(String code) {
  269. IPage<UserInfo> page = new Page<>(1, 1, false);
  270. QueryWrapper<?> qw = new QueryWrapper<>();
  271. qw.eq("a.code", code);
  272. return dao.selectAllUsers(page, qw).getRecords().get(0);
  273. }
  274. /**
  275. * @param code 医生编码
  276. * @return map
  277. * @Description 查询医生签名图片
  278. * @Author hsh
  279. * @Date 2024/5/15 9:47
  280. */
  281. public ResultVo<Map<String, Object>> selectAutographImageByCode(String code) {
  282. return ResultVoUtil.success(dao.selectAutographImageByCode(code));
  283. }
  284. public ResultVo<JSONObject> putAutographImage(MultipartFile file, String code) {
  285. String codeRs = "";
  286. if (code == null) {
  287. codeRs = FileNameUtil.mainName(file.getOriginalFilename());
  288. }
  289. if (StringUtil.isBlank(code) && StringUtil.notBlank(codeRs)) {
  290. code = dao.selectCodeByCodeRs(codeRs);
  291. if (StringUtil.isBlank(code)) {
  292. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "工号不存在");
  293. }
  294. }
  295. String createFilePath = StrUtil.format("/{}/doctorSignature/{}.png", "archive", code);
  296. archiveServer.createFile(file, createFilePath);
  297. JSONObject js = new JSONObject();
  298. String name = FileNameUtil.extName(file.getOriginalFilename());
  299. String fileName = code + "." + name;
  300. js.put("url", StrUtil.format("/doctorSignatureImage/{}.png", code));
  301. js.put("name", fileName);
  302. return ResultVoUtil.success(js);
  303. }
  304. public void setUserConfig(String js) {
  305. String id = TokenUtil.getInstance().getTokenUserId();
  306. dao.updateUserConfig(js, id);
  307. userCache.refreshCache(id);
  308. }
  309. public void importUser() throws IOException {
  310. File file = new File("D:\\his\\基础数据\\铭和\\上级专家(2).xlsx");
  311. List<List<String>> data = ExcelReaderUtil.readExcel(file);
  312. // 打印读取到的数据
  313. for (List<String> row : data) {
  314. if("科室".equals(row.get(0))) {
  315. continue;
  316. }
  317. try {
  318. UserInfo userInfo = new UserInfo();
  319. userInfo.setCodeRs(row.get(7));
  320. userInfo.setName(row.get(1));
  321. userInfo.setDeptCode(row.get(0));
  322. userInfo.setSocialNo(row.get(3));
  323. userInfo.setYbCode(row.get(6));
  324. userInfo.setPhoneNo(row.get(4));
  325. userInfo.setYbName(row.get(5));
  326. userInfo.setDoctorXzYp("0");
  327. userInfo.setOrderYn("1");
  328. userInfo.setCode("0" + publicServer.getPersonnelCode());
  329. userInfo.setPyCode(PingYinUtils.pyShouZiMuDaXie(userInfo.getName()));
  330. userInfo.setDCode(PingYinUtils.getWBCode(userInfo.getName()));
  331. dao.saveEmployeeInfo(userInfo);
  332. dao.delPartTimeDeptByCode(userInfo.getCode());
  333. if (ListUtil.notBlank(userInfo.getPartTimeDept())) {
  334. dao.insertPartTimeDept(userInfo.getCode(), userInfo.getPartTimeDept());
  335. }
  336. } catch (Exception e) {
  337. e.printStackTrace();
  338. }
  339. }
  340. }
  341. }