package com.yihu.wlyy.service.app.team; import com.yihu.wlyy.entity.doctor.profile.Doctor; import com.yihu.wlyy.entity.doctor.team.admin.AdminTeam; import com.yihu.wlyy.entity.doctor.team.admin.AdminTeamMember; import com.yihu.wlyy.entity.patient.Patient; import com.yihu.wlyy.repository.doctor.DoctorAdminTeamDao; import com.yihu.wlyy.repository.doctor.DoctorAdminTeamMemberDao; import com.yihu.wlyy.service.BaseService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import java.util.*; /** * 医生行政团队服务。 * * @author Sand */ @Component @Transactional public class AdminTeamService extends BaseService { @Autowired DoctorAdminTeamDao teamDao; @Autowired DoctorAdminTeamMemberDao memberDao; //-----------------Team Operation---------------------- public long getTeamCount() { return teamDao.count(); } public AdminTeam createTeam(String teamName, String leaderCode, String orgCode) { AdminTeam team = new AdminTeam(); team.setName(teamName); team.setLeaderCode(leaderCode); team.setOrgCode(orgCode); team.setAvailable(true); team.setCreateTime(new Date()); saveTeam(team); return team; } public AdminTeam getTeam(long id) { AdminTeam team = teamDao.findOne(id); return team; } public AdminTeam updateTeamName(long teamId, String teamName) { AdminTeam team = getTeam(teamId); if (team != null) { if (teamName != null) team.setName(teamName); saveTeam(team); } return team; } public AdminTeam updateTeamLeader(long teamId, String doctorCode) { AdminTeam team = getTeam(teamId); if (team != null) { if (doctorCode != null) team.setLeaderCode(doctorCode); saveTeam(team); } return team; } public List getDoctorTeams(String doctorCode){ return memberDao.findDoctorTeams(doctorCode); } //-----------------Member Operation---------------------- public void addMember(long teamId, String doctorCode){ Integer count = memberDao.isMemberExist(teamId, doctorCode); if(count == null || count == 0) { AdminTeamMember member = new AdminTeamMember(); member.setTeamId(teamId); member.setDoctorCode(doctorCode); member.setJoinTime(new Date()); saveMember(member); } } public void removeMember(long teamId, String doctorCode){ AdminTeamMember member = memberDao.findByTeamIdAndDoctorCodeOrderByDoctorCodeAsc(teamId, doctorCode); if(member != null) memberDao.delete(member); } public List getMembers(long teamId){ return memberDao.findAllMembers(teamId); } public List getExcludedMembers(String orgCode, long teamId, int page, int size){ Pageable pageable = new PageRequest(page, size); Page doctors = memberDao.findTeamExcludedMembers(orgCode, teamId, pageable); if (doctors == null) return null; return doctors.getContent(); } public Map getMemberSigningCount(List doctors){ Map counts = new HashMap<>(); for (Doctor doctor : doctors){ if (!(doctor.getLevel() == 2 || doctor.getLevel() == 3)) continue; boolean isHealthDoctor = doctor.getLevel() == 3; List partners = new ArrayList<>(); for (Doctor partner : doctors){ if(partner == doctor) continue; if (isHealthDoctor && partner.getLevel() == 2){ partners.add(partner.getCode()); } else if (!isHealthDoctor && partner.getLevel() == 3) { partners.add(partner.getCode()); } } Integer signingCount = 0; if(partners.isEmpty()){ signingCount = 0; } else if (isHealthDoctor){ signingCount = teamDao.getHealthDoctorSignCount(doctor.getCode(), partners); } else { signingCount = teamDao.getDoctorSignCount(doctor.getCode(), partners); } counts.put(doctor.getCode(), signingCount); } return counts; } public List getMemberSigningPatients(long teamId, String doctorCode, int page, int size){ List healthDoctors = new ArrayList<>(); List doctors = getMembers(teamId); for (Doctor doctor : doctors){ if (doctor.getLevel() != 3) continue; healthDoctors.add(doctor.getCode()); } Page result = teamDao.getHealthDoctorSigningPatients( doctorCode, healthDoctors, new PageRequest(page, size)); return result.getContent(); } private void saveTeam(AdminTeam team) { teamDao.save(team); } private void saveMember(AdminTeamMember member){ memberDao.save(member); } }