SettingsService.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. package thyyxxk.webserver.service.settings;
  2. import cn.hutool.core.io.file.FileNameUtil;
  3. import cn.hutool.crypto.SecureUtil;
  4. import com.alibaba.fastjson.JSON;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  7. import com.baomidou.mybatisplus.core.metadata.IPage;
  8. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.beans.factory.annotation.Value;
  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.dictionary.EmployeeMi;
  20. import thyyxxk.webserver.entity.login.PersonnelQueryConditions;
  21. import thyyxxk.webserver.entity.login.UserInfo;
  22. import thyyxxk.webserver.entity.settings.deptphones.DeptPhones;
  23. import thyyxxk.webserver.entity.settings.permissions.*;
  24. import thyyxxk.webserver.entity.settings.users.ChangePwdParam;
  25. import thyyxxk.webserver.entity.settings.users.WorkIntegrationPlatformAdvice;
  26. import thyyxxk.webserver.entity.socketmessage.ApiMessageBody;
  27. import thyyxxk.webserver.entity.socketmessage.SendUserList;
  28. import thyyxxk.webserver.service.PublicServer;
  29. import thyyxxk.webserver.service.hutoolcache.RedisServer;
  30. import thyyxxk.webserver.service.externalhttp.CorpWxSrvc;
  31. import thyyxxk.webserver.service.externalhttp.WebSocketService;
  32. import thyyxxk.webserver.service.hutoolcache.RedisLikeService;
  33. import thyyxxk.webserver.utils.*;
  34. import java.io.IOException;
  35. import java.nio.file.Files;
  36. import java.nio.file.Path;
  37. import java.nio.file.Paths;
  38. import java.nio.file.StandardCopyOption;
  39. import java.util.*;
  40. /**
  41. * @author dj
  42. */
  43. @Slf4j
  44. @Service
  45. public class SettingsService {
  46. private final SettingsDao dao;
  47. private final CorpWxSrvc srvc;
  48. private final PublicServer publicServer;
  49. private final RedisLikeService redis;
  50. private final WebSocketService socketService;
  51. @Value("${is-prod}")
  52. private boolean isProd;
  53. @Autowired
  54. public SettingsService(SettingsDao dao, CorpWxSrvc srvc, PublicServer publicServer, RedisLikeService redis, WebSocketService socketService) {
  55. this.dao = dao;
  56. this.srvc = srvc;
  57. this.publicServer = publicServer;
  58. this.redis = redis;
  59. this.socketService = socketService;
  60. }
  61. public ResultVo<UserInfo> getUserInfo() {
  62. final String code = TokenUtil.getInstance().getTokenUserId();
  63. final UserInfo user = dao.getUserInfo(code);
  64. if (null == user) {
  65. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有找到此用户的在职信息,请重新登录!");
  66. }
  67. JSONObject json = srvc.getUserinfo(socketService.getWeComAddressBookToken(), user.getCodeRs());
  68. user.setAvatar(json.getString("avatar"));
  69. user.setRoles(dao.getUserRoles(code));
  70. return ResultVoUtil.success(user);
  71. }
  72. @Transactional(rollbackFor = Exception.class)
  73. public ResultVo<String> changePassword(ChangePwdParam param) {
  74. String code = TokenUtil.getInstance().getTokenUserId();
  75. String password = dao.getPassword(code);
  76. String newPwd = SecureUtil.md5(param.getNewPassword());
  77. String old = SecureUtil.md5(param.getOldPassword());
  78. if (!old.equals(password)) {
  79. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "旧密码错误,请检查!");
  80. }
  81. if (param.getNewPassword().trim().isEmpty()) {
  82. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "新密码不能包含空格,请检查!");
  83. }
  84. dao.updatePassword(code, newPwd);
  85. redis.updateUserInfo(code);
  86. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE, "密码修改成功。");
  87. }
  88. public ResultVo<List<DeptTree>> getDeptTree() {
  89. List<DeptTree> list = new ArrayList<>();
  90. DeptTree deptTree = new DeptTree();
  91. deptTree.setCode("");
  92. deptTree.setName("长沙泰和医院");
  93. String code = TokenUtil.getInstance().getTokenUserId();
  94. List<Integer> roles = dao.getUserRoles(code);
  95. if (roles.contains(1) || roles.contains(2)) {
  96. deptTree.setChildren(dao.selectAllDepts());
  97. } else {
  98. deptTree.setChildren(dao.selectDeptsByUserCode(code));
  99. }
  100. list.add(deptTree);
  101. return ResultVoUtil.success(list);
  102. }
  103. public ResultVo<IPage<UserInfo>> getAllUsers(PersonnelQueryConditions param) {
  104. IPage<UserInfo> page = new Page<>(param.getCurrentPage(), param.getPageSize(), param.getTotal() == 0);
  105. if (publicServer.needRule(1, 2, 62, 19)) {
  106. return ResultVoUtil.success(dao.selectAllUsers(page, personnelQuery(param)));
  107. }
  108. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "你没有查看的权限.");
  109. }
  110. private QueryWrapper<?> personnelQuery(PersonnelQueryConditions param) {
  111. QueryWrapper<?> qw = new QueryWrapper<>();
  112. if (StringUtil.notBlank(param.getDeptCode())) {
  113. qw.eq("dept_code", param.getDeptCode());
  114. }
  115. if (StringUtil.notBlank(param.getName())) {
  116. qw.and(QueryWrapper -> QueryWrapper.like("name", param.getName()).or().eq("code_rs", param.getName()));
  117. }
  118. if (StringUtil.notBlank(param.getYbCode())) {
  119. qw.eq("yb_code", param.getYbCode());
  120. }
  121. if (param.getWhetherToDisable() != 9) {
  122. qw.eq("isnull(del_flag,0)", param.getWhetherToDisable());
  123. }
  124. if (param.getIsThereAMedicalInsuranceCode() == 1) {
  125. qw.isNotNull("yb_code");
  126. } else if (param.getIsThereAMedicalInsuranceCode() == 2) {
  127. qw.isNull("yb_code");
  128. }
  129. return qw;
  130. }
  131. public ResultVo<List<Role>> getAllRoles() {
  132. List<Integer> roles = dao.getUserRoles(TokenUtil.getInstance().getTokenUserId());
  133. if (roles.contains(1)) {
  134. return ResultVoUtil.success(dao.getAllRoles());
  135. } else {
  136. return ResultVoUtil.success(dao.getAllRolesExceptAdmin());
  137. }
  138. }
  139. public ResultVo<List<Integer>> getUserRoles(String code) {
  140. return ResultVoUtil.success(dao.getUserRoles(code));
  141. }
  142. @Transactional(rollbackFor = Exception.class)
  143. public ResultVo<String> saveUserRoles(SaveUserRolesParam param) {
  144. dao.deleteUserRoles(param.getCode());
  145. if (null == param.getRoles() || param.getRoles().isEmpty()) {
  146. return ResultVoUtil.success();
  147. }
  148. dao.insertUserRoles(param.getCode(), param.getRoles());
  149. redis.updateUserInfo(param.getCode());
  150. log.info("更改人员角色 => 操作员:{},数据:{}", TokenUtil.getInstance().getTokenUserId(), param);
  151. return ResultVoUtil.success();
  152. }
  153. public ResultVo<String> addNewRole(String name) {
  154. if (dao.isRoleExist(name) > 0) {
  155. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "该角色已存在,请匆重复添加!");
  156. }
  157. Role role = new Role();
  158. role.setName(name);
  159. role.setCreateStaff(TokenUtil.getInstance().getTokenUserId());
  160. dao.addNewRole(role);
  161. return ResultVoUtil.success();
  162. }
  163. public ResultVo<List<DeptPhones>> getDeptPhones() {
  164. return ResultVoUtil.success(dao.getDeptPhones());
  165. }
  166. public ResultVo<String> saveDeptPhone(DeptPhones param) {
  167. log.info("修改科室电话 => 操作员:{},数据:{}", TokenUtil.getInstance().getTokenUserId(), param);
  168. dao.saveDeptPhone(param);
  169. return ResultVoUtil.success();
  170. }
  171. public ResultVo<List<WorkIntegrationPlatformAdvice>> getMyAdvices() {
  172. String userId = TokenUtil.getInstance().getTokenUserId();
  173. List<Integer> userRoles = dao.getUserRoles(userId);
  174. List<WorkIntegrationPlatformAdvice> list;
  175. if (null != userRoles && userRoles.contains(1)) {
  176. list = dao.selectAllAdvices();
  177. } else {
  178. list = dao.selectMyAdvices(userId);
  179. }
  180. return ResultVoUtil.success(list);
  181. }
  182. public ResultVo<String> submitNewAdvice(WorkIntegrationPlatformAdvice advice) {
  183. if (StringUtil.isBlank(advice.getSubmitContent())) {
  184. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "请填写建议内容!");
  185. }
  186. String userId = TokenUtil.getInstance().getTokenUserId();
  187. dao.insertNewAdvice(userId, advice.getSubmitContent());
  188. List<String> admins = dao.selectAdmins();
  189. JSONObject obj = new JSONObject();
  190. obj.put("message", String.format("用户【%s】提交了新的建议,请前往个人中心查看。", userId));
  191. String message = SocketMsg.socketVo(Message.SYSTEM_NOTIFICATION, obj);
  192. socketService.sendUserListMessage(new SendUserList(admins, message));
  193. return ResultVoUtil.success();
  194. }
  195. public ResultVo<String> checkAdvice(Integer id) {
  196. dao.checkAdvice(id);
  197. return ResultVoUtil.success();
  198. }
  199. public ResultVo<String> dismissUserBadge(Integer id) {
  200. dao.dismissUserBadge(id);
  201. return ResultVoUtil.success();
  202. }
  203. public ResultVo<String> replyAdvice(WorkIntegrationPlatformAdvice advice) {
  204. if (StringUtil.isBlank(advice.getReply())) {
  205. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "请填写回复内容!");
  206. }
  207. dao.updateReply(advice.getId(), advice.getReply(), TokenUtil.getInstance().getTokenUserId());
  208. JSONObject obj = new JSONObject();
  209. obj.put("message", "您提交的建议已有新的回复,请前往个人中心查看。");
  210. String message = SocketMsg.socketVo(Message.SYSTEM_NOTIFICATION, obj);
  211. ApiMessageBody messageBody = new ApiMessageBody(advice.getSubmitStaff(), message);
  212. socketService.sendMessageByUserCode(messageBody);
  213. return ResultVoUtil.success();
  214. }
  215. public ResultVo<List<Integer>> getRoleVueMenus(Integer id) {
  216. return ResultVoUtil.success(dao.getRoleVueMenus(id));
  217. }
  218. @Transactional(rollbackFor = Exception.class)
  219. public ResultVo<String> saveRoleVueMenus(SaveRoleMenusParam param) {
  220. dao.deleteRoleVueMenus(param.getId());
  221. if (null == param.getMenus() || param.getMenus().isEmpty()) {
  222. return ResultVoUtil.success();
  223. }
  224. log.info("修改角色菜单 => 操作员:{},数据:{}", TokenUtil.getInstance().getTokenUserId(), param);
  225. dao.insertRoleVueMenus(param.getId(), param.getMenus());
  226. redis.setIntergrationRoleMenu();
  227. return ResultVoUtil.success();
  228. }
  229. public ResultVo<String> modifyEmployeeInfo(UserInfo userInfo) {
  230. UserInfo info = getEmployeeOriginalInformation(userInfo.getCode());
  231. if (null == info) {
  232. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有查询到原来信息");
  233. }
  234. String changedField = CampareObject.getChangedFieldsOfSql(info, userInfo);
  235. if (StringUtil.notBlank(changedField)) {
  236. if (changedField.contains("name")) {
  237. changedField += String.format(",py_code = '%s',d_code = '%s' ",
  238. PingYinUtils.pyShouZiMuDaXie(userInfo.getName()), PingYinUtils.getWBCode(userInfo.getName()));
  239. }
  240. dao.modifyPersonnelInformation(changedField, userInfo.getCode());
  241. }
  242. dao.delPartTimeDeptByCode(userInfo.getCode());
  243. if (ListUtil.notBlank(userInfo.getPartTimeDept())) {
  244. dao.insertPartTimeDept(userInfo.getCode(), userInfo.getPartTimeDept());
  245. }
  246. redis.updateUserInfo(userInfo.getCode());
  247. log.info("修改员工信息==>操作人:{},数据:{}", TokenUtil.getInstance().getTokenUserId(), JSON.toJSONString(userInfo));
  248. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE);
  249. }
  250. public ResultVo<String> saveEmployeeInfo(UserInfo userInfo) {
  251. if (dao.doesThePersonnelNumberExist(userInfo.getCodeRs()) > 0) {
  252. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "人事工号已存在,换一个.");
  253. }
  254. userInfo.setCode("0" + publicServer.getPersonnelCode())
  255. .setPyCode(PingYinUtils.pyShouZiMuDaXie(userInfo.getName()))
  256. .setDCode(PingYinUtils.getWBCode(userInfo.getName()));
  257. dao.saveEmployeeInfo(userInfo);
  258. dao.delPartTimeDeptByCode(userInfo.getCode());
  259. if (ListUtil.notBlank(userInfo.getPartTimeDept())) {
  260. dao.insertPartTimeDept(userInfo.getCode(), userInfo.getPartTimeDept());
  261. }
  262. redis.updateUserInfo(userInfo.getCode());
  263. log.info("添加员工信息==>操作人:{},数据:{}", TokenUtil.getInstance().getTokenUserId(), JSON.toJSONString(userInfo));
  264. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE);
  265. }
  266. public ResultVo<String> resetPasswordByCode(String code, Integer nextTime) {
  267. dao.resetPassword(code);
  268. return ResultVoUtil.fail(ExceptionEnum.SUCCESS_AND_EL_MESSAGE);
  269. }
  270. private UserInfo getEmployeeOriginalInformation(String code) {
  271. IPage<UserInfo> page = new Page<>(1, 1, false);
  272. QueryWrapper<?> qw = new QueryWrapper<>();
  273. qw.eq("a.code", code);
  274. return dao.selectAllUsers(page, qw).getRecords().get(0);
  275. }
  276. /**
  277. * @param file 签名 code 人员编码
  278. * @return map
  279. * @Description 更新医生的签名
  280. * @Author hsh
  281. * @Date 2024/5/14 10:36
  282. */
  283. public ResultVo<Map<String, Object>> setAutographImage(MultipartFile file, String code) {
  284. if (StringUtil.isBlank(code)) {
  285. return ResultVoUtil.fail(ExceptionEnum.INVALID_PARAM);
  286. }
  287. String img = base64Encode(file);
  288. Map<String, Object> m = dao.selectAutographImageByCode(code);
  289. int count;
  290. if (null == m || m.isEmpty()) {
  291. EmployeeMi mi = dao.selectEmployeeMiByCode(code);
  292. if (null != mi) {
  293. count = dao.intoAutographImage(code, img, mi.getSexCode() == null ? "9" : mi.getSexCode());
  294. } else {
  295. count = dao.intoAutographImage(code, img, "9");
  296. }
  297. } else {
  298. count = dao.updateAutographImage(code, img);
  299. }
  300. if (count > 0) {
  301. Map<String, Object> map = new HashMap<>();
  302. Map<String, Object> imgMap = dao.selectAutographImageByCode(code);
  303. map.put("data", imgMap);
  304. return ResultVoUtil.success(map);
  305. } else {
  306. return ResultVoUtil.fail(ExceptionEnum.NEED_PROOFREAD);
  307. }
  308. }
  309. /**
  310. * @param code 医生编码
  311. * @return map
  312. * @Description 查询医生签名图片
  313. * @Author hsh
  314. * @Date 2024/5/15 9:47
  315. */
  316. public ResultVo<Map<String, Object>> selectAutographImageByCode(String code) {
  317. return ResultVoUtil.success(dao.selectAutographImageByCode(code));
  318. }
  319. private String base64Encode(MultipartFile file) {
  320. String ret = null;
  321. if (file == null) {
  322. return null;
  323. }
  324. try {
  325. Base64.Encoder encoder = Base64.getEncoder();
  326. ret = encoder.encodeToString(file.getBytes());
  327. } catch (Exception e) {
  328. log.error("将图片文件转base64出错", e);
  329. }
  330. return ret;
  331. }
  332. public ResultVo<JSONObject> putAutographImage(MultipartFile file, String code) {
  333. String codeRs = "";
  334. if (code == null) {
  335. codeRs = FileNameUtil.mainName(file.getOriginalFilename());
  336. }
  337. if (StringUtil.isBlank(code) && StringUtil.notBlank(codeRs)) {
  338. code = dao.selectCodeByCodeRs(codeRs);
  339. if (StringUtil.isBlank(code)) {
  340. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "工号不存在");
  341. }
  342. }
  343. String uploadDir;
  344. if (isProd) {
  345. uploadDir = "/home/doctorSignature";
  346. } else {
  347. uploadDir = "D:\\doctorSignature";
  348. }
  349. JSONObject js = new JSONObject();
  350. String name = FileNameUtil.extName(file.getOriginalFilename());
  351. String fileName = code + "." + name;
  352. Path uploadPath = Paths.get(uploadDir);
  353. try {
  354. Files.createDirectories(uploadPath);
  355. Path filePath = uploadPath.resolve(fileName);
  356. Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
  357. log.info("文件:{}, code:{}", file.getName(), code);
  358. String url = "http://172.16.32.167:8077/doctorSignatureImage/" + fileName;
  359. js.put("url", url);
  360. js.put("name", fileName);
  361. return ResultVoUtil.success(js);
  362. } catch (IOException e) {
  363. log.error("上传错误:{}", e.getMessage());
  364. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "上传错误请重新上传。");
  365. }
  366. }
  367. public void setUserConfig(String js) {
  368. dao.updateUserConfig(js, TokenUtil.getInstance().getTokenUserId());
  369. redis.updateUserInfo(TokenUtil.getInstance().getTokenUserId());
  370. }
  371. }