AdminTeamService.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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, String currentDoctor){
  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("isLeader", currentDoctor != null && result[2].equals(currentDoctor));
  79. team.put("doctorCode", result[3]);
  80. team.put("doctorName", result[4]);
  81. team.put("healthDoctorCode", result[5]);
  82. team.put("healthDoctorName", result[6]);
  83. return team;
  84. }
  85. /**
  86. * 更新团队名称。
  87. *
  88. * @param teamId
  89. * @param teamName
  90. * @return
  91. */
  92. public AdminTeam updateTeamName(long teamId, String teamName) {
  93. AdminTeam team = getTeam(teamId);
  94. if (team != null) {
  95. if (teamName != null) team.setName(teamName);
  96. saveTeam(team);
  97. }
  98. return team;
  99. }
  100. /**
  101. * 更新团队领导。
  102. *
  103. * @param teamId
  104. * @param doctorCode
  105. * @return
  106. */
  107. public AdminTeam updateTeamLeader(long teamId, String doctorCode) {
  108. AdminTeam team = getTeam(teamId);
  109. if (team != null) {
  110. if (doctorCode != null) team.setLeaderCode(doctorCode);
  111. saveTeam(team);
  112. }
  113. return team;
  114. }
  115. /**
  116. * 获取此医生所在的团队列表。
  117. *
  118. * @param doctorCode
  119. * @return
  120. */
  121. public List<AdminTeam> getDoctorTeams(String doctorCode) {
  122. return memberDao.findDoctorTeams(doctorCode);
  123. }
  124. /**
  125. * 添加成员。
  126. *
  127. * @param teamId
  128. * @param doctorCode
  129. */
  130. public void addMember(long teamId, String doctorCode) {
  131. if (!memberDao.isMemberExist(teamId, doctorCode)) {
  132. AdminTeamMember member = new AdminTeamMember();
  133. member.setTeamId(teamId);
  134. member.setDoctorCode(doctorCode);
  135. member.setJoinTime(new Date());
  136. saveMember(member);
  137. }
  138. }
  139. /**
  140. * 移除成员逻辑:
  141. * 1、判断团队内是否有三师签约关系,如果有不能删除。
  142. * 2、若没有三师签约关系,判断是否为专科医生,如果是,直接删除
  143. * 3、若不是,则判断健康管理师或全科医生是否有签约人,有的话提示先转移居民。
  144. *
  145. * @param teamId
  146. * @param doctorCode
  147. */
  148. public Pair<Integer, String> removeMember(long teamId, String doctorCode) {
  149. if(signingTeamMemberDao.isSanShiSigning(doctorCode)){
  150. return new ImmutablePair<>(403, "三师签约医生,不能删除");
  151. }
  152. Doctor doctor = doctorDao.findByCode(doctorCode);
  153. if (!doctor.isProfessionalDoctor()){
  154. if (doctor.isGeneralDoctor()){
  155. List<Doctor> doctorList = new ArrayList<>();
  156. doctorList.add(doctor);
  157. Integer count = getMemberSigningCount(teamId, doctorList).get(doctorCode);
  158. if (count != null || count > 0){
  159. if (doctor.isGeneralDoctor()){
  160. return new ImmutablePair<>(403, "全科医生有签约关系,不可删除");
  161. } else if (doctor.isHealthDoctor()){
  162. return new ImmutablePair<>(403, "健康管理师仍有签约居民,请先转移签约居民");
  163. }
  164. }
  165. }
  166. }
  167. AdminTeamMember member = memberDao.findByTeamIdAndDoctorCodeOrderByDoctorCodeAsc(teamId, doctorCode);
  168. if (member != null) memberDao.delete(member);
  169. return new ImmutablePair<>(200, "OK");
  170. }
  171. public List<Doctor> getMembers(long teamId) {
  172. return memberDao.findAllMembers(teamId);
  173. }
  174. public List<Doctor> getMembers(long teamId,String doctorCode) {
  175. return memberDao.findAllHeathExcludeThis(teamId,doctorCode);
  176. }
  177. /**
  178. * 获取不在当前团队内的机构成员。
  179. *
  180. * @param orgCode
  181. * @param teamId
  182. * @param page
  183. * @param size
  184. * @return
  185. */
  186. public List<Doctor> getExcludedMembers(String orgCode, String doctorName, long teamId, int page, int size) {
  187. Pageable pageable = new PageRequest(page, size);
  188. if (StringUtils.isEmpty(doctorName)) {
  189. doctorName = "%";
  190. } else {
  191. doctorName = "%" + doctorName + "%";
  192. }
  193. Page<Doctor> doctors = memberDao.findTeamExcludedMembers(orgCode, doctorName, teamId, pageable);
  194. if (doctors == null) return null;
  195. return doctors.getContent();
  196. }
  197. /**
  198. * 团队内所有成员的签约人数。
  199. *
  200. * @param doctors
  201. * @return
  202. */
  203. public Map<String, Integer> getMemberSigningCount(long teamId, List<Doctor> doctors) {
  204. Map<String, Integer> counts = new HashMap<>();
  205. for (Doctor doctor : doctors) {
  206. if (doctor.isProfessionalDoctor()) continue;
  207. Integer signingCount;
  208. if (doctor.isHealthDoctor()) {
  209. signingCount = teamDao.getHealthDoctorSignCount(doctor.getCode(), teamId);
  210. } else {
  211. signingCount = teamDao.getDoctorSignCount(doctor.getCode(), teamId);
  212. }
  213. counts.put(doctor.getCode(), signingCount);
  214. }
  215. return counts;
  216. }
  217. /**
  218. * 团队内所有成员的签约患者列表。以健康管理师为中心,与全科医生为准的交集。
  219. *
  220. * @param teamId
  221. * @param healthDoctorCode
  222. * @param page
  223. * @param size
  224. * @return
  225. */
  226. public List<Patient> getMemberSigningPatients(long teamId, String healthDoctorCode, int page, int size) {
  227. Page<Patient> result = teamDao.getHealthDoctorSigningPatients(healthDoctorCode, teamId, new PageRequest(page, size));
  228. return result.getContent();
  229. }
  230. /**
  231. * 保存团队。
  232. *
  233. * @param team
  234. */
  235. private void saveTeam(AdminTeam team) {
  236. teamDao.save(team);
  237. }
  238. /**
  239. * 保存成员。
  240. *
  241. * @param member
  242. */
  243. private void saveMember(AdminTeamMember member) {
  244. memberDao.save(member);
  245. }
  246. public AdminTeam findByLeaderCode(String leaderCode){
  247. return teamDao.findByLeaderCode(leaderCode);
  248. }
  249. }