SettingsService.java 17 KB

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