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.awe.util.ASResultSet; import com.amarsoft.awe.util.SqlObject; import com.amarsoft.awe.util.Transaction; import com.amarsoft.context.ASUser; 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; 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, nvl(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 to_number(uti.task_weight) > to_number(nvl(utd.cou,0)) order by to_number(uti.task_weight)-to_number(nvl(utd.cou, '0'))")); //获得需要分配的待办 SqlObject sql = new SqlObject("select serialno, objectno, flowno from flow_task where userid = '" + roleId + "' order by to_date(begintime,'yyyy/mm/dd hh24△mi△ss')"); sql.setDebugSql(sql.getDebugSql().replaceAll("△", ":")); sql.setOriginalSql(sql.getOriginalSql().replaceAll("△", ":")); sql.setRunSql(sql.getRunSql().replaceAll("△", ":")); rsFlow = tran.getASResultSet(sql); List userLists = new ArrayList(); 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 where userid = '" + rsUser.getString("userid") + "' and roleid = '" + roleId + "' and status = 'approve_status01' group by userid,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 removeUsers = new ArrayList(); 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 sys_guid(),'" + 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 = nvl(task_number,0) + " + Integer.parseInt(user.split("@")[4]) +" where userid = '" + user.split("@")[3] + "' and roleid = '" + roleId + "'")); } } } 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(); } 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 userLists) { List sortList = new ArrayList(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 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); } }