|  | @ -13,6 +13,7 @@ import org.springframework.data.domain.PageRequest;
 | 
	
		
			
				|  |  | import org.springframework.data.domain.Pageable;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Component;
 | 
	
		
			
				|  |  | import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  | import org.springframework.util.StringUtils;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.util.*;
 | 
	
		
			
				|  |  | 
 | 
	
	
		
			
				|  | @ -30,11 +31,23 @@ public class AdminTeamService extends BaseService {
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     DoctorAdminTeamMemberDao memberDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     //-----------------Team Operation----------------------
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 获取团队数量。
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public long getTeamCount() {
 | 
	
		
			
				|  |  |         return teamDao.count();
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 创建团队。
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param teamName
 | 
	
		
			
				|  |  |      * @param leaderCode
 | 
	
		
			
				|  |  |      * @param orgCode
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public AdminTeam createTeam(String teamName, String leaderCode, String orgCode) {
 | 
	
		
			
				|  |  |         AdminTeam team = new AdminTeam();
 | 
	
		
			
				|  |  |         team.setName(teamName);
 | 
	
	
		
			
				|  | @ -48,11 +61,23 @@ public class AdminTeamService extends BaseService {
 | 
	
		
			
				|  |  |         return team;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 获取团队信息。
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param id
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public AdminTeam getTeam(long id) {
 | 
	
		
			
				|  |  |         AdminTeam team = teamDao.findOne(id);
 | 
	
		
			
				|  |  |         return team;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 更新团队名称。
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param teamId
 | 
	
		
			
				|  |  |      * @param teamName
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public AdminTeam updateTeamName(long teamId, String teamName) {
 | 
	
		
			
				|  |  |         AdminTeam team = getTeam(teamId);
 | 
	
		
			
				|  |  |         if (team != null) {
 | 
	
	
		
			
				|  | @ -64,6 +89,13 @@ public class AdminTeamService extends BaseService {
 | 
	
		
			
				|  |  |         return team;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 更新团队领导。
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param teamId
 | 
	
		
			
				|  |  |      * @param doctorCode
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public AdminTeam updateTeamLeader(long teamId, String doctorCode) {
 | 
	
		
			
				|  |  |         AdminTeam team = getTeam(teamId);
 | 
	
		
			
				|  |  |         if (team != null) {
 | 
	
	
		
			
				|  | @ -75,14 +107,25 @@ public class AdminTeamService extends BaseService {
 | 
	
		
			
				|  |  |         return team;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public List<AdminTeam> getDoctorTeams(String doctorCode){
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 获取此医生所在的团队列表。
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param doctorCode
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public List<AdminTeam> getDoctorTeams(String doctorCode) {
 | 
	
		
			
				|  |  |         return memberDao.findDoctorTeams(doctorCode);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     //-----------------Member Operation----------------------
 | 
	
		
			
				|  |  |     public void addMember(long teamId, String doctorCode){
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 添加成员。
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param teamId
 | 
	
		
			
				|  |  |      * @param doctorCode
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public void addMember(long teamId, String doctorCode) {
 | 
	
		
			
				|  |  |         Integer count = memberDao.isMemberExist(teamId, doctorCode);
 | 
	
		
			
				|  |  |         if(count == null || count == 0) {
 | 
	
		
			
				|  |  |         if (count == null || count == 0) {
 | 
	
		
			
				|  |  |             AdminTeamMember member = new AdminTeamMember();
 | 
	
		
			
				|  |  |             member.setTeamId(teamId);
 | 
	
		
			
				|  |  |             member.setDoctorCode(doctorCode);
 | 
	
	
		
			
				|  | @ -91,46 +134,76 @@ public class AdminTeamService extends BaseService {
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public void removeMember(long teamId, String doctorCode){
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 移除成员。
 | 
	
		
			
				|  |  |      * <p>
 | 
	
		
			
				|  |  |      * 如果成员已经有跟团队内的某医生合作与患者签约,那么不能移除,必须等签约患者交接后才可以。
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param teamId
 | 
	
		
			
				|  |  |      * @param doctorCode
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public void removeMember(long teamId, String doctorCode) {
 | 
	
		
			
				|  |  |         AdminTeamMember member = memberDao.findByTeamIdAndDoctorCodeOrderByDoctorCodeAsc(teamId, doctorCode);
 | 
	
		
			
				|  |  |         if(member != null) memberDao.delete(member);
 | 
	
		
			
				|  |  |         if (member != null) memberDao.delete(member);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public List<Doctor> getMembers(long teamId){
 | 
	
		
			
				|  |  |     public List<Doctor> getMembers(long teamId) {
 | 
	
		
			
				|  |  |         return memberDao.findAllMembers(teamId);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public List<Doctor> getExcludedMembers(String orgCode, long teamId, int page, int size){
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 获取不在当前团队内的机构成员。
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param orgCode
 | 
	
		
			
				|  |  |      * @param teamId
 | 
	
		
			
				|  |  |      * @param page
 | 
	
		
			
				|  |  |      * @param size
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public List<Doctor> getExcludedMembers(String orgCode, String doctorName, long teamId, int page, int size) {
 | 
	
		
			
				|  |  |         Pageable pageable = new PageRequest(page, size);
 | 
	
		
			
				|  |  |         Page<Doctor> doctors = memberDao.findTeamExcludedMembers(orgCode, teamId, pageable);
 | 
	
		
			
				|  |  |         Page<Doctor> doctors;
 | 
	
		
			
				|  |  |         if (StringUtils.isEmpty(doctorName)) {
 | 
	
		
			
				|  |  |             doctors = memberDao.findTeamExcludedMembers(orgCode, teamId, pageable);
 | 
	
		
			
				|  |  |         } else {
 | 
	
		
			
				|  |  |             doctors = memberDao.findTeamExcludedMembersWithDoctorName(orgCode,
 | 
	
		
			
				|  |  |                     "%" + doctorName + "%", teamId, pageable);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         if (doctors == null) return null;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return doctors.getContent();
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public Map<String, Integer> getMemberSigningCount(List<Doctor> doctors){
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 团队内所有成员的签约人数。
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param doctors
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public Map<String, Integer> getMemberSigningCount(List<Doctor> doctors) {
 | 
	
		
			
				|  |  |         Map<String, Integer> counts = new HashMap<>();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         for (Doctor doctor : doctors){
 | 
	
		
			
				|  |  |             if (!(doctor.getLevel() == 2 || doctor.getLevel() == 3)) continue;
 | 
	
		
			
				|  |  |         for (Doctor doctor : doctors) {
 | 
	
		
			
				|  |  |             if (doctor.isProfessionalDoctor()) continue;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             boolean isHealthDoctor = doctor.getLevel() == 3;
 | 
	
		
			
				|  |  |             boolean isHealthDoctor = doctor.isHealthDoctor();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             List<String> partners = new ArrayList<>();
 | 
	
		
			
				|  |  |             for (Doctor partner : doctors){
 | 
	
		
			
				|  |  |                 if(partner == doctor) continue;
 | 
	
		
			
				|  |  |             for (Doctor partner : doctors) {
 | 
	
		
			
				|  |  |                 if (partner == doctor) continue;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                 if (isHealthDoctor && partner.getLevel() == 2){
 | 
	
		
			
				|  |  |                 if (isHealthDoctor && partner.isGeneralDoctor()) {
 | 
	
		
			
				|  |  |                     partners.add(partner.getCode());
 | 
	
		
			
				|  |  |                 } else if (!isHealthDoctor && partner.getLevel() == 3) {
 | 
	
		
			
				|  |  |                 } else if (!isHealthDoctor && partner.isHealthDoctor()) {
 | 
	
		
			
				|  |  |                     partners.add(partner.getCode());
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             Integer signingCount = 0;
 | 
	
		
			
				|  |  |             if(partners.isEmpty()){
 | 
	
		
			
				|  |  |             Integer signingCount;
 | 
	
		
			
				|  |  |             if (partners.isEmpty()) {
 | 
	
		
			
				|  |  |                 signingCount = 0;
 | 
	
		
			
				|  |  |             } else if (isHealthDoctor){
 | 
	
		
			
				|  |  |             } else if (isHealthDoctor) {
 | 
	
		
			
				|  |  |                 signingCount = teamDao.getHealthDoctorSignCount(doctor.getCode(), partners);
 | 
	
		
			
				|  |  |             } else {
 | 
	
		
			
				|  |  |                 signingCount = teamDao.getDoctorSignCount(doctor.getCode(), partners);
 | 
	
	
		
			
				|  | @ -142,27 +215,46 @@ public class AdminTeamService extends BaseService {
 | 
	
		
			
				|  |  |         return counts;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public List<Patient> getMemberSigningPatients(long teamId, String doctorCode, int page, int size){
 | 
	
		
			
				|  |  |         List<String> healthDoctors = new ArrayList<>();
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 团队内所有成员的签约患者列表。以健康管理师为中心,与全科医生为准的交集。
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param teamId
 | 
	
		
			
				|  |  |      * @param healthDoctorCode
 | 
	
		
			
				|  |  |      * @param page
 | 
	
		
			
				|  |  |      * @param size
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public List<Patient> getMemberSigningPatients(long teamId, String healthDoctorCode, int page, int size) {
 | 
	
		
			
				|  |  |         List<String> generalDoctors = new ArrayList<>();
 | 
	
		
			
				|  |  |         List<Doctor> doctors = getMembers(teamId);
 | 
	
		
			
				|  |  |         for (Doctor doctor : doctors){
 | 
	
		
			
				|  |  |             if (doctor.getLevel() != 3) continue;
 | 
	
		
			
				|  |  |         for (Doctor doctor : doctors) {
 | 
	
		
			
				|  |  |             if (doctor.getLevel() != 2) continue;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             healthDoctors.add(doctor.getCode());
 | 
	
		
			
				|  |  |             generalDoctors.add(doctor.getCode());
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         Page<Patient> result = teamDao.getHealthDoctorSigningPatients(
 | 
	
		
			
				|  |  |                 doctorCode, healthDoctors,
 | 
	
		
			
				|  |  |                 healthDoctorCode, generalDoctors,
 | 
	
		
			
				|  |  |                 new PageRequest(page, size));
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return result.getContent();
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 保存团队。
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param team
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     private void saveTeam(AdminTeam team) {
 | 
	
		
			
				|  |  |         teamDao.save(team);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private void saveMember(AdminTeamMember member){
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 保存成员。
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param member
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     private void saveMember(AdminTeamMember member) {
 | 
	
		
			
				|  |  |         memberDao.save(member);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | }
 |