215 lines
8.7 KiB
Java
215 lines
8.7 KiB
Java
package com.tenwa.lease.app.quartzmession;
|
|
|
|
import java.sql.SQLException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import org.quartz.Job;
|
|
import org.quartz.JobExecutionContext;
|
|
import org.quartz.JobExecutionException;
|
|
|
|
import com.amarsoft.app.als.sys.tools.DateUtil;
|
|
import com.amarsoft.are.ARE;
|
|
import com.amarsoft.are.jbo.JBOException;
|
|
import com.amarsoft.are.jbo.JBOFactory;
|
|
import com.amarsoft.are.util.StringFunction;
|
|
import com.amarsoft.awe.util.ASResultSet;
|
|
import com.amarsoft.awe.util.SqlObject;
|
|
import com.amarsoft.awe.util.Transaction;
|
|
import com.amarsoft.context.ASUser;
|
|
import com.base.util.QuartzUtil;
|
|
|
|
public class TaskPool implements Job {
|
|
|
|
/**
|
|
* 为规避定时任务的并行,使用存储过程分单
|
|
* @author zhulh
|
|
*//*
|
|
@Override
|
|
public void execute(JobExecutionContext arg0) throws JobExecutionException {
|
|
Transaction tran = null;
|
|
try {
|
|
tran = Transaction.createTransaction(JBOFactory.createJBOTransaction());
|
|
tran.executeSQL(new SqlObject("{call proc_task_pool()}"));
|
|
} catch(Exception e) {
|
|
e.printStackTrace();
|
|
try {
|
|
if(tran != null) tran.rollback();
|
|
} catch (JBOException e1) {
|
|
e1.printStackTrace();
|
|
}
|
|
} finally {
|
|
try {
|
|
if(tran != null) tran.commit();
|
|
} catch (JBOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}*/
|
|
|
|
//防止手动触发时,与自动触发任务冲突导致的数据紊乱
|
|
@Override
|
|
public synchronized void execute(JobExecutionContext arg0) throws JobExecutionException{
|
|
Transaction tran = null;
|
|
ASResultSet rsRole = null;
|
|
ASResultSet rsUser = null;
|
|
ASResultSet rsFlow = null;
|
|
String startime = StringFunction.getTodayNow();
|
|
Object user_Id = arg0.getTrigger().getJobDataMap().get("CurUserId");
|
|
String curUserId = user_Id == null? "system" : user_Id.toString();
|
|
try {
|
|
//开启事务
|
|
tran = Transaction.createTransaction(JBOFactory.createJBOTransaction());
|
|
//搜索所有任务池角色
|
|
rsRole = tran.getASResultSet(new SqlObject("select roleid from role_info where task_pool = 'Y'"));
|
|
while(rsRole.next()) {
|
|
String roleId = rsRole.getString("roleid");
|
|
//获取此角色下上岗用户
|
|
rsUser = tran.getASResultSet(
|
|
new SqlObject("select uti.userid, uti.task_weight, ifnull(utd.cou,0) tasknumber from user_task_info uti left join (select userid,roleid,count(1) cou from user_task_data where status = 'approve_status01' group by userid,roleid) utd on utd.userid=uti.userid and utd.roleid=uti.roleid where uti.roleid = '" +
|
|
roleId + "' and exists(select 1 from user_role where roleid=uti.roleid and userid=uti.userid) and uti.induction_state = 'induction_state01' and cast(uti.task_weight as decimal) > cast(ifnull(utd.cou,0) as decimal) order by cast(uti.task_weight as decimal)-cast(ifnull(utd.cou, '0') as decimal)"));
|
|
|
|
//获得需要分配的待办
|
|
SqlObject sql = new SqlObject("select serialno, objectno, flowno from flow_task where userid = '" + roleId + "' order by str_to_date(begintime,'%Y/%m/%d %H△%i△%s')");
|
|
sql.setDebugSql(sql.getDebugSql().replaceAll("△", ":"));
|
|
sql.setOriginalSql(sql.getOriginalSql().replaceAll("△", ":"));
|
|
sql.setRunSql(sql.getRunSql().replaceAll("△", ":"));
|
|
rsFlow = tran.getASResultSet(sql);
|
|
|
|
List<String> userLists = new ArrayList<String>();
|
|
while(rsUser.next()) {
|
|
double taskWeight = rsUser.getInt("task_weight");//获得权重
|
|
double taskNumber = rsUser.getInt("tasknumber");;//获得已收到待办
|
|
String cou = tran.getString(new SqlObject("select count(1) from user_task_data utd,flow_task ft where utd.taskno=ft.serialno and ft.endtime is null and utd.userid = '" + rsUser.getString("userid") + "' and utd.roleid = '" + roleId + "' and utd.status = 'approve_status01' group by utd.userid,utd.roleid"));
|
|
if(cou == null) {
|
|
cou = "0";
|
|
}
|
|
taskNumber = Double.parseDouble(cou);
|
|
String weight = (taskNumber == 0 ? "0" : taskNumber/taskWeight + "") + "@" + taskNumber + "@" + taskWeight + "@" + rsUser.getString("userid") + "@0";
|
|
|
|
userLists.add(weight);
|
|
}
|
|
|
|
List<String> removeUsers = new ArrayList<String>();
|
|
|
|
while(rsFlow.next()) {
|
|
if(userLists.size() == 0) {
|
|
break;
|
|
}
|
|
//为权重任务池排序
|
|
sortList(userLists);
|
|
//获取可随机用户数
|
|
int number = getRandomNumber(userLists);
|
|
String userWeight = userLists.get(number);
|
|
double taskNumber = Double.parseDouble(userWeight.split("@")[1]);
|
|
double taskWeight = Double.parseDouble(userWeight.split("@")[2]);
|
|
double actualWeight = (taskNumber + 1) / taskWeight;
|
|
String userId = userWeight.split("@")[3];
|
|
ASUser user = ASUser.getUser(userId, tran);
|
|
int n = Integer.parseInt(userWeight.split("@")[4]) + 1;
|
|
//重新打包用户
|
|
userWeight = actualWeight + "@" + (taskNumber + 1) + "@" + taskWeight + "@" + userId + "@" + n;
|
|
if(actualWeight == 1.0) {//任务满了的情况,就从用户任务集合删除
|
|
userLists.remove(number);
|
|
removeUsers.add(userWeight);
|
|
} else {
|
|
userLists.set(number, userWeight);
|
|
}
|
|
|
|
ARE.getLog().info("******************任务池日志*****为" + user.getUserID() + "增加单子:" + rsFlow.getString("serialno"));
|
|
|
|
tran.executeSQL(new SqlObject("update flow_task set "
|
|
+ "phaseopinion4 = '" + roleId + "', userid = '" + user.getUserID() + "',"
|
|
+ "username = '" + user.getUserName() + "', orgid = '" + user.getOrgID() + "',"
|
|
+ "orgname = '" + user.getOrgName() + "' where serialno = '" + rsFlow.getString("serialno") + "'"));
|
|
SqlObject so = new SqlObject("insert into user_task_data(id,roleid,userid,taskno,flowno,flowunid,project_id,contract_id,begintime,endtime,status)"
|
|
+ "select replace(UUID(), '-', ''),'" + roleId + "','" + user.getUserID() + "','" + rsFlow.getString("serialno") + "','" + rsFlow.getString("flowno") + "',flow_unid,proj_id,contract_id,'"
|
|
+ DateUtil.getTodayNow().replaceAll(":", "△") + "','','approve_status01' from flow_bussiness_object where flow_unid = '" + rsFlow.getString("objectno") + "'");
|
|
so.setDebugSql(so.getDebugSql().replaceAll("△", ":"));
|
|
so.setOriginalSql(so.getOriginalSql().replaceAll("△", ":"));
|
|
so.setRunSql(so.getRunSql().replaceAll("△", ":"));
|
|
tran.executeSQL(so);
|
|
}
|
|
userLists.addAll(removeUsers);
|
|
for(String user : userLists) {
|
|
if(Integer.parseInt(user.split("@")[4]) == 0) {
|
|
continue;
|
|
}
|
|
ARE.getLog().info("******************任务池日志*****为" + user.split("@")[3] + "增加任务:" + user.split("@")[4] + "数据为:" + user);
|
|
tran.executeSQL(new SqlObject("update user_task_info set task_number = ifnull(task_number,0) + "
|
|
+ Integer.parseInt(user.split("@")[4]) +" where userid = '" + user.split("@")[3] + "' and roleid = '" + roleId + "'"));
|
|
}
|
|
}
|
|
QuartzUtil.insertLog(startime,"com.tenwa.lease.app.quartzmession.TaskPool", "success", "成功", curUserId);
|
|
} catch (Exception e) {
|
|
QuartzUtil.insertLog(startime,"com.tenwa.lease.app.quartzmession.TaskPool", "error", "失败", curUserId);
|
|
e.printStackTrace();
|
|
try {
|
|
if(tran != null) tran.rollback();
|
|
} catch (JBOException e1) {
|
|
e1.printStackTrace();
|
|
}
|
|
} finally {
|
|
try {
|
|
if(tran != null) tran.commit();
|
|
} catch (JBOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
try {
|
|
if(rsRole != null) rsRole.close();
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
try {
|
|
if(rsUser != null) rsUser.close();
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
try {
|
|
if(rsFlow != null) rsFlow.close();
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void sortList(List<String> userLists) {
|
|
List<String> sortList = new ArrayList<String>(userLists.size());
|
|
for(int i = 0; i < userLists.size(); i ++) {
|
|
double weight = Double.parseDouble(userLists.get(i).split("@")[0]);
|
|
int j = 0;
|
|
for(; j < sortList.size(); j ++) {
|
|
double sweight = Double.parseDouble(sortList.get(j).split("@")[0]);
|
|
if(sweight >= weight) {
|
|
break;
|
|
}
|
|
}
|
|
sortList.add(j, userLists.get(i));
|
|
}
|
|
userLists.clear();
|
|
userLists.addAll(sortList);
|
|
}
|
|
|
|
public static int getRandomNumber(List<String> userLists) {
|
|
int i = 0;
|
|
double weight = Double.parseDouble(userLists.get(0).split("@")[0]);
|
|
for(String userList : userLists) {
|
|
double sweight = Double.parseDouble(userList.split("@")[0]);
|
|
if(weight == sweight) {
|
|
i ++;
|
|
}
|
|
}
|
|
return (int)(Math.random()*(i));
|
|
}
|
|
|
|
public static String modifyStringByIndex(int index,String oriValue, String value) {
|
|
String[] ori = oriValue.split("@");
|
|
ori[index] = value;
|
|
String result = "";
|
|
for(String o : ori) {
|
|
result += "@" + o;
|
|
}
|
|
return result.substring(1);
|
|
}
|
|
}
|