AdminTeamService.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package com.yihu.wlyy.service.app.team;
  2. import com.yihu.wlyy.entity.doctor.profile.Doctor;
  3. import com.yihu.wlyy.entity.doctor.team.admin.AdminTeam;
  4. import com.yihu.wlyy.entity.doctor.team.admin.AdminTeamMember;
  5. import com.yihu.wlyy.entity.patient.Patient;
  6. import com.yihu.wlyy.repository.doctor.DoctorAdminTeamDao;
  7. import com.yihu.wlyy.repository.doctor.DoctorAdminTeamMemberDao;
  8. import com.yihu.wlyy.repository.doctor.DoctorDao;
  9. import com.yihu.wlyy.repository.doctor.DoctorTeamMemberDao;
  10. import com.yihu.wlyy.service.BaseService;
  11. import org.apache.commons.lang3.tuple.ImmutablePair;
  12. import org.apache.commons.lang3.tuple.Pair;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.data.domain.Page;
  15. import org.springframework.data.domain.PageRequest;
  16. import org.springframework.data.domain.Pageable;
  17. import org.springframework.stereotype.Component;
  18. import org.springframework.transaction.annotation.Transactional;
  19. import org.springframework.util.StringUtils;
  20. import java.util.*;
  21. /**
  22. * 医生行政团队服务。
  23. *
  24. * @author Sand
  25. */
  26. @Component
  27. @Transactional
  28. public class AdminTeamService extends BaseService {
  29. @Autowired
  30. DoctorDao doctorDao;
  31. @Autowired
  32. DoctorAdminTeamDao teamDao;
  33. @Autowired
  34. DoctorAdminTeamMemberDao memberDao;
  35. @Autowired
  36. DoctorTeamMemberDao signingTeamMemberDao;
  37. /**
  38. * 获取团队数量。
  39. *
  40. * @return
  41. */
  42. public long getTeamCount() {
  43. return teamDao.count();
  44. }
  45. /**
  46. * 创建团队。
  47. *
  48. * @param teamName
  49. * @param leaderCode
  50. * @param orgCode
  51. * @return
  52. */
  53. public AdminTeam createTeam(String teamName, String leaderCode, String orgCode) {
  54. AdminTeam team = new AdminTeam();
  55. team.setName(teamName);
  56. team.setLeaderCode(leaderCode);
  57. team.setOrgCode(orgCode);
  58. team.setAvailable(true);
  59. team.setCreateTime(new Date());
  60. saveTeam(team);
  61. return team;
  62. }
  63. /**
  64. * 获取团队信息。
  65. *
  66. * @param id
  67. */
  68. public AdminTeam getTeam(long id) {
  69. AdminTeam team = teamDao.findOne(id);
  70. return team;
  71. }
  72. public Map<String, Object> getPatientSigningTeam(String patientCode){
  73. Object result[] = (Object[]) teamDao.getPatientSigningTeam(patientCode);
  74. if (result == null) return null;
  75. Map<String, Object> team = new HashMap<>();
  76. team.put("teamId", result[0]);
  77. team.put("teamName", result[1]);
  78. team.put("doctorCode", result[2]);
  79. team.put("doctorName", result[3]);
  80. team.put("healthDoctorCode", result[4]);
  81. team.put("healthDoctorName", result[5]);
  82. return team;
  83. }
  84. /**
  85. * 更新团队名称。
  86. *
  87. * @param teamId
  88. * @param teamName
  89. * @return
  90. */
  91. public AdminTeam updateTeamName(long teamId, String teamName) {
  92. AdminTeam team = getTeam(teamId);
  93. if (team != null) {
  94. if (teamName != null) team.setName(teamName);
  95. saveTeam(team);
  96. }
  97. return team;
  98. }
  99. /**
  100. * 更新团队领导。
  101. *
  102. * @param teamId
  103. * @param doctorCode
  104. * @return
  105. */
  106. public AdminTeam updateTeamLeader(long teamId, String doctorCode) {
  107. AdminTeam team = getTeam(teamId);
  108. if (team != null) {
  109. if (doctorCode != null) team.setLeaderCode(doctorCode);
  110. saveTeam(team);
  111. }
  112. return team;
  113. }
  114. /**
  115. * 获取此医生所在的团队列表。
  116. *
  117. * @param doctorCode
  118. * @return
  119. */
  120. public List<AdminTeam> getDoctorTeams(String doctorCode) {
  121. return memberDao.findDoctorTeams(doctorCode);
  122. }
  123. /**
  124. * 添加成员。
  125. *
  126. * @param teamId
  127. * @param doctorCode
  128. */
  129. public void addMember(long teamId, String doctorCode) {
  130. if (!memberDao.isMemberExist(teamId, doctorCode)) {
  131. AdminTeamMember member = new AdminTeamMember();
  132. member.setTeamId(teamId);
  133. member.setDoctorCode(doctorCode);
  134. member.setJoinTime(new Date());
  135. saveMember(member);
  136. }
  137. }
  138. /**
  139. * 移除成员逻辑:
  140. * 1、判断团队内是否有三师签约关系,如果有不能删除。
  141. * 2、若没有三师签约关系,判断是否为专科医生,如果是,直接删除
  142. * 3、若不是,则判断健康管理师或全科医生是否有签约人,有的话提示先转移居民。
  143. *
  144. * @param teamId
  145. * @param doctorCode
  146. */
  147. public Pair<Integer, String> removeMember(long teamId, String doctorCode) {
  148. if(signingTeamMemberDao.isSanShiSigning(doctorCode)){
  149. return new ImmutablePair<>(403, "三师签约医生,不能删除");
  150. }
  151. Doctor doctor = doctorDao.findByCode(doctorCode);
  152. if (!doctor.isProfessionalDoctor()){
  153. if (doctor.isGeneralDoctor()){
  154. List<Doctor> doctorList = new ArrayList<>();
  155. doctorList.add(doctor);
  156. Integer count = getMemberSigningCount(teamId, doctorList).get(doctorCode);
  157. if (count != null || count > 0){
  158. if (doctor.isGeneralDoctor()){
  159. return new ImmutablePair<>(403, "全科医生有签约关系,不可删除");
  160. } else if (doctor.isHealthDoctor()){
  161. return new ImmutablePair<>(403, "健康管理师仍有签约居民,请先转移签约居民");
  162. }
  163. }
  164. }
  165. }
  166. AdminTeamMember member = memberDao.findByTeamIdAndDoctorCodeOrderByDoctorCodeAsc(teamId, doctorCode);
  167. if (member != null) memberDao.delete(member);
  168. return new ImmutablePair<>(200, "OK");
  169. }
  170. public List<Doctor> getMembers(long teamId) {
  171. return memberDao.findAllMembers(teamId);
  172. }
  173. /**
  174. * 获取不在当前团队内的机构成员。
  175. *
  176. * @param orgCode
  177. * @param teamId
  178. * @param page
  179. * @param size
  180. * @return
  181. */
  182. public List<Doctor> getExcludedMembers(String orgCode, String doctorName, long teamId, int page, int size) {
  183. Pageable pageable = new PageRequest(page, size);
  184. if (StringUtils.isEmpty(doctorName)) {
  185. doctorName = "%";
  186. } else {
  187. doctorName = "%" + doctorName + "%";
  188. }
  189. Page<Doctor> doctors = memberDao.findTeamExcludedMembers(orgCode, doctorName, teamId, pageable);
  190. if (doctors == null) return null;
  191. return doctors.getContent();
  192. }
  193. /**
  194. * 团队内所有成员的签约人数。
  195. *
  196. * @param doctors
  197. * @return
  198. */
  199. public Map<String, Integer> getMemberSigningCount(long teamId, List<Doctor> doctors) {
  200. Map<String, Integer> counts = new HashMap<>();
  201. for (Doctor doctor : doctors) {
  202. if (doctor.isProfessionalDoctor()) continue;
  203. Integer signingCount;
  204. if (doctor.isHealthDoctor()) {
  205. signingCount = teamDao.getHealthDoctorSignCount(doctor.getCode(), teamId);
  206. } else {
  207. signingCount = teamDao.getDoctorSignCount(doctor.getCode(), teamId);
  208. }
  209. counts.put(doctor.getCode(), signingCount);
  210. }
  211. return counts;
  212. }
  213. /**
  214. * 团队内所有成员的签约患者列表。以健康管理师为中心,与全科医生为准的交集。
  215. *
  216. * @param teamId
  217. * @param healthDoctorCode
  218. * @param page
  219. * @param size
  220. * @return
  221. */
  222. public List<Patient> getMemberSigningPatients(long teamId, String healthDoctorCode, int page, int size) {
  223. Page<Patient> result = teamDao.getHealthDoctorSigningPatients(healthDoctorCode, teamId, new PageRequest(page, size));
  224. return result.getContent();
  225. }
  226. /**
  227. * 保存团队。
  228. *
  229. * @param team
  230. */
  231. private void saveTeam(AdminTeam team) {
  232. teamDao.save(team);
  233. }
  234. /**
  235. * 保存成员。
  236. *
  237. * @param member
  238. */
  239. private void saveMember(AdminTeamMember member) {
  240. memberDao.save(member);
  241. }
  242. }