class TeamService {

  public boolean updateByPrimaryKeySelective(Team team) throws Exception {
    LOG.info("updateByPrimaryKeySelective({})", team.toString());

    try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
      TeamMapper teamMapper = sqlSession.getMapper(TeamMapper.class);
      teamMapper.updateByPrimaryKeySelective(team);

      TeamMemberMapper teamMemberMapper = sqlSession.getMapper(TeamMemberMapper.class);
      Map<String, Object> where = new HashMap<>();
      where.put("teamId", team.getId());

      // Take two lists of difference
      List<TeamMember> oldTeamMembers = teamMemberMapper.selectAll(where);
      List<String> oldMembers = new ArrayList<>();
      for (TeamMember oldTeamMember : oldTeamMembers) {
        oldMembers.add(oldTeamMember.getMember());
      }

      List<TeamMember> newTeamMembers = team.getCollaborators();
      List<String> newMembers = new ArrayList<>();
      for (TeamMember newTeamMember : newTeamMembers) {
        newMembers.add(newTeamMember.getMember());
      }

      for (TeamMember oldTeamMember : oldTeamMembers) {
        if (!newMembers.contains(oldTeamMember.getMember())) {
          teamMemberMapper.deleteByPrimaryKey(oldTeamMember.getId());
        }
      }

      for (TeamMember newTeamMember : newTeamMembers) {
        if (!oldMembers.contains(newTeamMember.getMember())) {
          // TODO(zhulinhao)：teamId Send it by the front desk, here there is no assignment
          newTeamMember.setTeamId(team.getId());
          newTeamMember.setTeamName(team.getTeamName());
          teamMemberMapper.insert(newTeamMember);
        }
      }

      // Updates all team_name of corresponding members in the teamMember table
      TeamMember teamMember = new TeamMember();
      teamMember.setTeamName(team.getTeamName());
      teamMember.setTeamId(team.getId());
      teamMemberMapper.updateSelective(teamMember);

      sqlSession.commit();
    } catch (Exception e) {
      LOG.error(e.getMessage(), e);
      throw new Exception(e);
    }
    return true;
  }

}