AdminTeamService.java 7.4 KB

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