package com.actionsoft.bpms.org.event;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.actionsoft.bpms.api.OpenApiClient;
import com.actionsoft.bpms.api.common.ApiResponse;
import com.actionsoft.bpms.schedule.IJob;
import com.actionsoft.sdk.service.model.DepartmentModel;
import com.actionsoft.sdk.service.response.IntResponse;
import com.actionsoft.sdk.service.response.ListMapResponse;
import com.actionsoft.sdk.service.response.StringResponse;
import com.actionsoft.sdk.service.response.org.DepartmentResponse;
import com.actionsoft.sdk.service.response.org.UsersGetResponse;
import com.actionsoft.sdk.service.response.process.ProcessInstResponse;
public class Rexamine implements IJob {
//服务器地址
private String apiServer = "http://localhost:8088/portal/openapi";
//api方法名
private String apiMethod;
//连接服务
private String accessKey = "sasc_api";
private String secret = "sasc_api";
//业务bo表名
private String boName="BO_EU_REEXAMINE_PLANS";
private String caName="BO_EU_CA";
//log日志表名
private String logName="BO_EU_LOG";
//插入bo日志 使用admin身份
private String logUid="admin";
//流程启动uid
private String uid="admin";
//供应商任务接受者
private String taskaccepor="admin";
//复审评价流程id
private String processDefId="obj_73d3a8d2bbe84d4f8a98e3eff26d7c3a";
//流程标题
String title="年度复审";
//发证单位书的所属部门id字段
private String CA_COM_DEPTID="CA_COM_DEPTID";
//bindid
private String processInsid;
//参数
private Map<String, Object> args = new HashMap<String, Object>();
OpenApiClient client = new OpenApiClient(apiServer, accessKey, secret);
/*
* 实际执行
*/
public void execute(JobExecutionContext arg0) throws JobExecutionException {
//查询数据
//select * from BO_EU_CA where CA_TYPE='准入' or CA_TYPE='资质评价' and IS_FREEZEN=1 and IS_DELETE=0 and IS_CHOSED=0 and datediff(CA_END_TIME, now())<=30 ;
String sql="select * from "+caName+" where CA_TYPE='准入' or CA_TYPE='资质评价' and IS_FREEZEN=1 and IS_DELETE=0 and IS_CHOSED=0 and datediff(CA_END_TIME, now())<=30 ";
apiMethod = "bo.query";
args.clear();
args.put("boName", caName);
args.put("selectClause", sql);
args.put("orderBy", "CREATEDATE");
ListMapResponse r4 = client.exec(apiMethod, args, ListMapResponse.class);
List<Map<String, Object>> result=r4.getData();
int size=result.size();
System.out.println("----------需要复审的供应商有"+size+"个!");
//考虑到数据量大的情况 8000次循环别死到半路
if(size>0){
for(int i=0;i<size;i++){
boolean res=startActivity(result.get(i));
//没有执行成功 因为中心没有管理者
if(!res){
//因供应商部门没有管理者uid 启动失败 写入日志
String logType="中心部门没有管理者";
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String dateTime = df.format(date); // Formats a Date into a date/time string.
String logContent="年度复审中:"+result.get(i).get("CA_TYPE")+"没有找到部门管理者,从而无法下发任务。";
String module="年度复审";
String deptid=""+result.get(i).get(CA_COM_DEPTID);
insertBo(logName,logType,dateTime,logContent,module,deptid);
}
continue;
}
}
}
/*
* 启动一个流程
* 参数recordNum 是bo记录
*/
private boolean startActivity(Map<String, Object> recordNum){
//获该证书的部门id 以便找到部门管理者 接收待办任务
String deptid=""+recordNum.get(CA_COM_DEPTID);
String su_deptid=""+recordNum.get("CA_SUPPLIER_ID");
//获该证书的部门id部门管理者 接收待办任务
if(null==deptid||("".equals(deptid))){
return false;
}
uid=getUid_from_dept(deptid);
if(null==uid||("".endsWith(uid))){
return false;
}
if(null==su_deptid||("".equals(su_deptid))){
return false;
}
taskaccepor=getUid_from_dept(su_deptid);
if(null==taskaccepor||("".endsWith(taskaccepor))){
return false;
}
// 创建流程
apiMethod = "process.create";
args.clear();
args.put("processDefId", processDefId);
args.put("uid",uid);
args.put("title",title);
ProcessInstResponse r = client.exec(apiMethod, args, ProcessInstResponse.class);
processInsid=r.getData().getId();
// 流程启动
apiMethod = "process.start";
args.clear();
args.put("processInstId", processInsid);
ApiResponse r1 = client.exec(apiMethod, args, ApiResponse.class);
//插入数据
Map<String, Object> boMap = new HashMap<String, Object>();
//为复审计划bo赋值开始
boMap.clear();
boMap.put("SUPPLIER_ID", recordNum.get("CA_SUPPLIER_ID"));
boMap.put("SUPPLIER_NAME", recordNum.get("CA_SUPPLIER_NAME"));
boMap.put("COM_CREDIT_CODE", recordNum.get("CA_SUPPLIER_CREDIT"));
boMap.put("CA_NO", recordNum.get("CA_NO"));
boMap.put("CA_ID", recordNum.get("ID"));
boMap.put("CA_NAME", recordNum.get("CA_NAME"));
boMap.put("CA_BEGIN_TIME", recordNum.get("CA_BEGIN_TIME"));
boMap.put("CA_END_TIME", recordNum.get("CA_END_TIME"));
boMap.put("CA_COM", recordNum.get("CA_COM"));
boMap.put("CA_COM_DEPTID", recordNum.get("CA_COM_DEPTID"));
boMap.put("CA_FILE", recordNum.get("CA_FILE"));
boMap.put("CA_TYPE", recordNum.get("CA_TYPE"));
boMap.put("TASK_ACCEPTOR",taskaccepor);
//为bo赋值结束
apiMethod = "bo.create";
args.clear();
args.put("boName",boName );
args.put("recordData",boMap);
args.put("bindId", processInsid);
args.put("uid", uid);
StringResponse r3= client.exec(apiMethod, args, StringResponse.class);
//获得流程的开始节点
apiMethod = "process.inst.get";
args.clear();
args.put("processInstId", processInsid);
ProcessInstResponse r2 = client.exec(apiMethod, args, ProcessInstResponse.class);
String StratActivity = r2.getData().getStartTaskInstId();
if(null!=StratActivity||(!"".equals(StratActivity.trim()))){
int count=updateCa(""+recordNum.get("ID"));
if(count<1){
System.out.println("证书更新字段是否被选中为1失败");
}
}
return true;
}
/*
*根据部门ID查找部门管理者 接受任务
*参数 部门id
*返回值 部门管理者 uid
*/
private String getUid_from_dept(String departmentId){
String apiMethod = "org.users.manager.get.department";
args.put("departmentId", departmentId);
//可能没有管理者
try{
UsersGetResponse r = client.exec(apiMethod, args, UsersGetResponse.class);
if(null!=r.getData().get(0)){
return r.getData().get(0).getUid();
}else{
return "";
}
}catch (Exception e) {
// TODO: handle exception
//没有
return "";
}
}
/*
* 更新证书以选中状态
* 参数 表的id
* 返回 影响的行数
*/
private int updateCa(String id){
String apiMethod = "bo.update";
args.clear();
Map<String, Object> boMap = new HashMap<String, Object>();
//为bo赋值开始
boMap.put("ID",id);
//证书被选中
boMap.put("IS_CHOSED",1);
args.put("boName",caName );
args.put("recordData",boMap );
IntResponse r = client.exec(apiMethod, args, IntResponse.class);
//影响的行数
return r.getData();
}
/*
* 插入日志bo数据
*/
private void insertBo(String logName, String logType, String dateTime,
String logContent, String module, String deptid) {
//插入数据
Map<String, Object> boMap = new HashMap<String, Object>();
//为bo赋值开始
boMap.put("LOGTYPE",logType);
boMap.put("LOGDATE",dateTime);
boMap.put("LOGCONTENT",logContent);
boMap.put("MODULE",module);
boMap.put("DEPARTMENTID",deptid);
//为bo赋值结束
apiMethod = "bo.data.create";
args.clear();
args.put("boName",logName );
args.put("recordData",boMap);
args.put("uid", logUid);
StringResponse r3= client.exec(apiMethod, args, StringResponse.class);
}
/*
* 暂时没有用到
*根据部门ID获取部门对象
*参数 部门id
*返回值 部门对象
*/
private DepartmentModel getDept(String departmentId){
String apiMethod = "org.department.get";
args.clear();
args.put("id", departmentId);
DepartmentResponse r = client.exec(apiMethod, args, DepartmentResponse.class);
return r.getData();
}
}
网友评论