SettingsService.java 15 KB

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