//3.找出这些测试人员中需要进行的待测试的测试任务最少的
CondSetBean taskAndCSB = new CondSetBeanJustAnd();
taskAndCSB.addCondBean(new CondBeanEqual("CR_STATUS", statusId));
String columnName = null;
if (roleId.equals(TaskDefinition.ROLE_DEVELOP_TESTER)) {
columnName = "CR_DEVELOP_TESTER";
} else if (roleId.equals(TaskDefinition.ROLE_INTEGRATION_TESTER)) {
columnName = "CR_INTEGRATION_TESTER";
}
CondBeanIn taskTesterCBIforTaskAndCSB = new CondBeanIn(columnName);
taskAndCSB.addCondBean(taskTesterCBIforTaskAndCSB);
for (int i = 0; i < userRoleTB.size(); i++) {
RowBean userRoleRowBean = userRoleTB.get(i);
String userId = userRoleRowBean.getCellBeanValue("CR_USER_ID");
taskTesterCBIforTaskAndCSB.addValue(userId);
if (!testerIdArray.contains(userId)) {
testerIdArray.add(userId);//把所有测试人员全部放入集合中,通过后面的算法移除不符合要求的;
}
}
TableBean taskTB = this.baseDAO.queryForTableBean(new ClassPOJO("TN_TASK"), taskAndCSB);
Bag testerBag = new HashBag();
if (taskTB.size() > 0) {
for (int i = 0; i < taskTB.size(); i++) {
testerBag.add(taskTB.get(i).getCellBeanValue(columnName));//开发测试人员放入HashBag中统计数量
}
}
int lastTesterTaskCount = -1;
Iterator<String> iterator = testerIdArray.iterator();
ArrayList<String> theLeastTesterIdList = new ArrayList<String>();
while (iterator.hasNext()) {
String tempTesterId = (String) iterator.next();
int testerTaskCount = testerBag.getCount(tempTesterId);//读取当前测试人的待测试task数量
if (lastTesterTaskCount == -1) {
lastTesterTaskCount = testerTaskCount;
theLeastTesterIdList.add(tempTesterId);
continue;
}
//如果当前开发测试人的待测试task数量小于之前开发测试人员的待测试task数量,将之前的测试人员清空,当前测试人加入集合
if (testerTaskCount < lastTesterTaskCount) {
theLeastTesterIdList.clear();
theLeastTesterIdList.add(tempTesterId);
lastTesterTaskCount = testerTaskCount;
}else if(testerTaskCount == lastTesterTaskCount){
theLeastTesterIdList.add(tempTesterId);
}else if(testerTaskCount > lastTesterTaskCount){
}
}
//如果有多个测试人员有相同的待测试task数量,则为当前task在这些测试人员中随机分配一个
if (theLeastTesterIdList.size() > 1) {
Random random = new Random(System.currentTimeMillis());
testUserId = theLeastTesterIdList.get(random.nextInt(theLeastTesterIdList.size()));
}else if (theLeastTesterIdList.size() == 1) {
testUserId = theLeastTesterIdList.get(0);
}
CondSetBean taskAndCSB = new CondSetBeanJustAnd();
taskAndCSB.addCondBean(new CondBeanEqual("CR_STATUS", statusId));
String columnName = null;
if (roleId.equals(TaskDefinition.ROLE_DEVELOP_TESTER)) {
columnName = "CR_DEVELOP_TESTER";
} else if (roleId.equals(TaskDefinition.ROLE_INTEGRATION_TESTER)) {
columnName = "CR_INTEGRATION_TESTER";
}
CondBeanIn taskTesterCBIforTaskAndCSB = new CondBeanIn(columnName);
taskAndCSB.addCondBean(taskTesterCBIforTaskAndCSB);
for (int i = 0; i < userRoleTB.size(); i++) {
RowBean userRoleRowBean = userRoleTB.get(i);
String userId = userRoleRowBean.getCellBeanValue("CR_USER_ID");
taskTesterCBIforTaskAndCSB.addValue(userId);
if (!testerIdArray.contains(userId)) {
testerIdArray.add(userId);//把所有测试人员全部放入集合中,通过后面的算法移除不符合要求的;
}
}
TableBean taskTB = this.baseDAO.queryForTableBean(new ClassPOJO("TN_TASK"), taskAndCSB);
Bag testerBag = new HashBag();
if (taskTB.size() > 0) {
for (int i = 0; i < taskTB.size(); i++) {
testerBag.add(taskTB.get(i).getCellBeanValue(columnName));//开发测试人员放入HashBag中统计数量
}
}
int lastTesterTaskCount = -1;
Iterator<String> iterator = testerIdArray.iterator();
ArrayList<String> theLeastTesterIdList = new ArrayList<String>();
while (iterator.hasNext()) {
String tempTesterId = (String) iterator.next();
int testerTaskCount = testerBag.getCount(tempTesterId);//读取当前测试人的待测试task数量
if (lastTesterTaskCount == -1) {
lastTesterTaskCount = testerTaskCount;
theLeastTesterIdList.add(tempTesterId);
continue;
}
//如果当前开发测试人的待测试task数量小于之前开发测试人员的待测试task数量,将之前的测试人员清空,当前测试人加入集合
if (testerTaskCount < lastTesterTaskCount) {
theLeastTesterIdList.clear();
theLeastTesterIdList.add(tempTesterId);
lastTesterTaskCount = testerTaskCount;
}else if(testerTaskCount == lastTesterTaskCount){
theLeastTesterIdList.add(tempTesterId);
}else if(testerTaskCount > lastTesterTaskCount){
}
}
//如果有多个测试人员有相同的待测试task数量,则为当前task在这些测试人员中随机分配一个
if (theLeastTesterIdList.size() > 1) {
Random random = new Random(System.currentTimeMillis());
testUserId = theLeastTesterIdList.get(random.nextInt(theLeastTesterIdList.size()));
}else if (theLeastTesterIdList.size() == 1) {
testUserId = theLeastTesterIdList.get(0);
}