package com.engine.workflow.cmd.agent ;
import com.alibaba.fastjson.JSONArray ;
import com.alibaba.fastjson.JSONObject ;
import com.api.workflow.util.ServiceUtil ;
import com.customization.dito.sendtodo.HttpRequestUtil ;
import com.customization.dito.sendtodo.SendMailUtil ;
import com.customization.dito.sendtodo.SendPortalErrorUtil ;
import com.engine.common.biz.AbstractCommonCommand ;
import com.engine.common.entity.BizLogContext ;
import com.engine.core.interceptor.CommandContext ;
import com.engine.workflow.biz.AgentBiz ;
import com.time.util.DateUtil ;
import org.apache.commons.lang.StringUtils ;
import weaver.conn.RecordSet ;
import weaver.general.Util ;
import weaver.hrm.User ;
import weaver.hrm.resource.ResourceComInfo ;
import weaver.interfaces.dito.comInfo.PropBean ;
import weaver.workflow.agent.AgentManager ;
import javax.servlet.http.HttpServletRequest ;
import java.net.URLEncoder ;
import java.util.ArrayList ;
import java.util.HashMap ;
import java.util.List ;
import java.util.Map ;
import java.util.regex.Pattern ;
public class TakeBackAgentCmd extends AbstractCommonCommand < Map < String , Object > > {
private HttpServletRequest request ;
public TakeBackAgentCmd ( HttpServletRequest request , User user ) {
this . request = request ;
this . user = user ;
}
public Map < String , Object > execute ( CommandContext commandContext ) {
boolean flag = true ;
try {
AgentManager agentManager = new AgentManager ( user ) ;
String symbol = Util . null2String ( request . getParameter ( "symbol" ) ) ;
boolean needBackRunning = "1" . equals ( Util . null2String ( request . getParameter ( "backRunning" ) ) ) ; //收回流转中数据
List < String > range = new ArrayList < String > ( ) ;
if ( "it" . equals ( symbol ) | | "mt" . equals ( symbol ) ) { //单个、批量收回代理
String agentids = Util . null2String ( request . getParameter ( "agentid" ) ) ;
agentids = this . getAuthByUser ( agentids ) ;
for ( String keyid : agentids . split ( "," ) ) {
if ( ! "" . equals ( keyid ) )
range . add ( keyid ) ;
}
agentManager . takeBackAgent ( range , needBackRunning ) ;
if ( needBackRunning ) {
SendPortalTodoAndDone ( range ) ;
}
} else if ( "pt" . equals ( symbol ) ) { // 全部收回逻辑
int agentid = Util . getIntValue ( request . getParameter ( "agentid" ) ) ;
int bagentuid = Util . getIntValue ( request . getParameter ( "bagentuid" ) ) ;
String agentuid = agentManager . getAgentuid ( agentid , bagentuid ) ;
if ( Util . getIntValue ( agentuid ) > 0 ) {
range = agentManager . getAgentRangeByUser ( Util . getIntValue ( agentuid ) , bagentuid ) ;
agentManager . takeBackAgent ( range , needBackRunning ) ;
}
}
// 推送门户处理待办和已办
if ( needBackRunning ) {
SendPortalTodoAndDone ( range ) ;
}
} catch ( Exception e ) {
flag = false ;
e . printStackTrace ( ) ;
}
Map < String , Object > apidatas = new HashMap < String , Object > ( ) ;
apidatas . put ( "flag" , flag ) ;
return apidatas ;
}
private String getAuthByUser ( String agentids ) {
boolean haveAgentAllRight = AgentBiz . judgeHaveAgentAllRight ( user ) ;
if ( haveAgentAllRight ) {
return agentids ;
}
if ( agentids . startsWith ( "," ) ) {
agentids = agentids . substring ( 1 ) ;
}
if ( agentids . endsWith ( "," ) ) {
agentids = agentids . substring ( 0 , agentids . length ( ) - 1 ) ;
}
String allUserid = ServiceUtil . getAllUserid ( user ) ; //所有用户id, 包括主从账号
String agentids_temp = "" ;
RecordSet rs = new RecordSet ( ) ;
/ *
原 有 代 码 : rs . executeQuery ( "select agentid from workflow_agent where beagenterid in (" + allUserid + ") and agentid IN(" + agentids + ")" ) ;
* /
String sql_in = Util . getSubINClause ( agentids , "agentid" , "IN" ) ;
String sql = "select agentid from workflow_agent where beagenterid in (" + allUserid + ") and " + sql_in ;
rs . executeQuery ( sql ) ;
while ( rs . next ( ) ) {
agentids_temp + = "," + rs . getString ( 1 ) ;
}
return "" . equals ( agentids_temp ) ? agentids_temp : agentids_temp . substring ( 1 ) ;
}
@Override
public BizLogContext getLogContext ( ) {
return null ;
}
public void SendPortalTodoAndDone ( List < String > range ) {
RecordSet rs = new RecordSet ( ) ;
int agenterid = 1 ;
int beagenterid = 1 ;
String workflowids = "" ;
for ( String agentid : range ) {
rs . executeQuery ( "select * from workflow_agent where agentid=?" , agentid ) ;
rs . next ( ) ;
agenterid = Util . getIntValue ( rs . getString ( "agenterid" ) ) ;
beagenterid = Util . getIntValue ( rs . getString ( "beagenterid" ) ) ;
workflowids = Util . null2String ( rs . getString ( "workflowid" ) ) ;
DealPortalTodoAndDone ( agenterid , beagenterid , workflowids ) ;
}
}
/ * *
* 先 查 询 出 被 代 理 人 的 待 办 , 然 后 一 一 推 送 给 门 户 , 代 理 人 待 办 增 加 , 被 代 理 人 待 办 删 除
* @param agenterid 代 理 人
* @param beagenterid 被 代 理 人
* @param workflowids 指 定 流 程 id
* /
public void DealPortalTodoAndDone ( int agenterid , int beagenterid , String workflowids ) {
RecordSet rs = new RecordSet ( ) ;
JSONArray taskObjectList = new JSONArray ( ) ;
String EMAIL_REGEX_DEFAULT = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$" ;
Pattern regex = Pattern . compile ( EMAIL_REGEX_DEFAULT ) ;
JSONObject taskObject = new JSONObject ( ) ;
rs . writeLog ( "==================== TakeBackAgentCmd DealPortalTodoAndDone ===================" ) ;
rs . writeLog ( "==================== TakeBackAgentCmd DealPortalTodoAndDone agenterid ===================" + agenterid ) ;
rs . writeLog ( "==================== TakeBackAgentCmd DealPortalTodoAndDone beagenterid ===================" + beagenterid ) ;
rs . writeLog ( "==================== TakeBackAgentCmd DealPortalTodoAndDone workflowids ===================" + workflowids ) ;
try {
taskObject . put ( "objectAction" , "0" ) ;
taskObject . put ( "objectType" , "0" ) ;
taskObject . put ( "objectCode" , new ResourceComInfo ( ) . getWorkcode ( String . valueOf ( beagenterid ) ) ) ;
taskObject . put ( "objectId" , getStaffId ( beagenterid ) ) ;
taskObject . put ( "operatorCode" , new ResourceComInfo ( ) . getWorkcode ( String . valueOf ( beagenterid ) ) ) ;
taskObjectList . add ( taskObject ) ;
HttpRequestUtil httpRequestUtil = new HttpRequestUtil ( ) ;
SendPortalErrorUtil sendPortalErrorUtil = new SendPortalErrorUtil ( ) ;
SendMailUtil sendMailUtil = new SendMailUtil ( ) ;
String scopeid = "-1" ;
String scope = "HrmCustomFieldByInfoType" ;
String bpm_app_workflowurl = PropBean . getUfPropValue ( "bpm_app_workflowurl" ) ;
String bpm_app_requesturl = PropBean . getUfPropValue ( "bpm_app_requesturl" ) ;
String portal_todourl = PropBean . getUfPropValue ( "portal_todourl" ) ;
String username = PropBean . getUfPropValue ( "username" ) ;
String passwd = PropBean . getUfPropValue ( "passwd" ) ;
String center = PropBean . getUfPropValue ( "center" ) ;
String bpm_workflowurl = PropBean . getUfPropValue ( "bpm_workflowurl" ) ;
String mobileJumpUrl = PropBean . getUfPropValue ( "mobileJumpUrl" ) ;
String cus_staff = PropBean . getUfPropValue ( "cus_staff" ) ;
String processTime = DateUtil . getCurrentTime ( "yyyy-MM-dd HH:mm:ss" ) ;
String sql = " select distinct t1.requestid,t1.requestname,t1.createdate,h1.lastname as creatorname,t2.receivedate,t2.receivetime " +
" from workflow_base t " +
" left join workflow_requestbase t1 on t.id=t1.workflowid " +
" left join workflow_currentoperator t2 on t1.requestid = t2.requestid " +
" left join hrmresource h1 on h1.id = t1.creater " +
" where t2.usertype=0 and t2.userid in (" + beagenterid + ") and t.id in (" + workflowids + ") " +
" and ((t2.isremark =0 and (t2.takisremark is null or t2.takisremark ='0')) or t2.isremark in('1','5','9','7','8','9','11') ) " +
" and t2.islasttimes=1 " +
" and (t.isvalid=3 or t.isvalid=1) " ;
String zhjkbs = "com.engine.workflow.cmd.agent.TakeBackAgentCmd.DealPortalTodoAndDone" ;
String actionType = "1" ;
String terminal = "1" ;
String taskType = "0" ;
rs . executeQuery ( sql ) ;
while ( rs . next ( ) ) {
String requestid = Util . null2String ( rs . getString ( "requestid" ) ) ;
String workflowname = "" ;
String nodeId = "" ;
String nodeName = "" ;
JSONObject requestObject = new JSONObject ( ) ;
String urlPc = "[newtab]" + bpm_workflowurl + "/workflow/request/ViewRequestForwardSPA.jsp?requestid=" + requestid ;
String replace = bpm_app_workflowurl . replace ( "/bpm" , "" ) ;
String urlDing = replace + bpm_app_requesturl + requestid ;
String urlApp = replace + bpm_app_requesturl + requestid ;
if ( StringUtils . isNotEmpty ( mobileJumpUrl ) ) {
urlDing + = "&returnUrl=" + URLEncoder . encode ( mobileJumpUrl , "UTF-8" ) ;
urlApp + = "&returnUrl=" + URLEncoder . encode ( mobileJumpUrl , "UTF-8" ) ;
}
String msgdata = "" ;
String requestname = "" ;
String lastname = "" ;
String userids = "" ;
String loginid = "" ;
String emailArray = "" ;
sql = " select t1.requestname,t1.currentnodeid as nodeid,d1.nodename,t3.workflowname,h2.lastname,h2.loginid " +
" from workflow_requestbase t1\n" +
" inner join workflow_base t3 on t1.workflowid = t3.id\n" +
" left join workflow_nodebase d1 on d1.id = t1.currentnodeid " +
" left join (select id,lastname,loginid from hrmresource where status = 1 union all select id,lastname,loginid from hrmresourcemanager ) h2 on h2.id = t1.creater \n" +
" where (t1.deleted <> 1 or t1.deleted is null or t1.deleted = '')\n" +
" and t1.requestid = " + requestid ;
rs . writeLog ( "TakeBackAgentCmd DealPortalTodoAndDone -- sql:" + sql ) ;
rs . executeQuery ( sql ) ;
if ( rs . next ( ) ) {
requestname = Util . null2String ( rs . getString ( "requestname" ) ) ;
workflowname = Util . null2String ( rs . getString ( "workflowname" ) ) ;
nodeId = Util . null2String ( rs . getString ( "nodeid" ) + user . getUID ( ) ) ;
nodeName = Util . null2String ( rs . getString ( "nodeName" ) ) ;
lastname = Util . null2String ( rs . getString ( "lastname" ) ) ;
loginid = Util . null2String ( rs . getString ( "loginid" ) ) ;
}
requestObject . put ( "center" , center ) ;
requestObject . put ( "title" , requestname ) ;
requestObject . put ( "creator" , loginid ) ;
requestObject . put ( "taskCode" , "weaver" + requestid ) ;
requestObject . put ( "nodeId" , nodeId ) ;
requestObject . put ( "taskType" , taskType ) ;
requestObject . put ( "actionType" , actionType ) ;
requestObject . put ( "createDate" , processTime ) ;
requestObject . put ( "messageTitle" , getShortMessageTitle ( requestname ) ) ;
requestObject . put ( "messageContent" , requestname ) ;
requestObject . put ( "terminal" , terminal ) ;
requestObject . put ( "urlPc" , urlPc ) ;
requestObject . put ( "urlApp" , urlApp ) ;
requestObject . put ( "urlDing" , urlDing ) ;
requestObject . put ( "nodeName" , nodeName ) ;
requestObject . put ( "ticketType" , workflowname ) ;
JSONObject taskObject1 = new JSONObject ( ) ;
taskObject1 . put ( "objectAction" , "1" ) ;
taskObject1 . put ( "objectType" , "0" ) ;
taskObject1 . put ( "objectCode" , new ResourceComInfo ( ) . getWorkcode ( String . valueOf ( agenterid ) ) ) ;
taskObject1 . put ( "objectId" , getStaffId ( agenterid ) ) ;
taskObject1 . put ( "operatorCode" , new ResourceComInfo ( ) . getWorkcode ( String . valueOf ( agenterid ) ) ) ;
taskObjectList . add ( taskObject1 ) ;
if ( taskObjectList . size ( ) > 0 ) {
requestObject . put ( "taskObjectList" , taskObjectList ) ;
} else {
requestObject . put ( "actionType" , "3" ) ;
}
String auth = username + ":" + passwd ;
rs . writeLog ( "================== TakeBackAgentCmd DealPortalTodoAndDone ======================== " + requestObject . toJSONString ( ) ) ;
msgdata = httpRequestUtil . doPostByAuth ( portal_todourl , requestObject . toJSONString ( ) , auth ) ;
if ( ! "" . equals ( msgdata ) ) {
JSONObject msgObject = JSONObject . parseObject ( msgdata ) ;
if ( msgObject . containsKey ( "resultCode" ) ) {
String resultCode = msgObject . getString ( "resultCode" ) ;
if ( "0" . equals ( resultCode ) ) {
rs . writeLog ( "TakeBackAgentCmd emailArray:" + emailArray ) ;
if ( StringUtils . isNotEmpty ( emailArray ) ) {
Map < String , String > templateMap = getEmailTemplate ( requestname , lastname , processTime ) ;
String notificationSubject = templateMap . get ( "notificationSubject" ) ;
String notificationContent = templateMap . get ( "notificationContent" ) ;
sendMailUtil . sendMail ( requestid , emailArray , notificationSubject , notificationContent ) ;
}
sendPortalErrorUtil . doRecordPortalErrorInfo4Phase2 ( requestid , nodeId , nodeName , userids , requestObject . toJSONString ( ) , resultCode , "" , zhjkbs ) ;
} else {
String resultmsg = msgObject . getString ( "resultMsg" ) ;
sendPortalErrorUtil . doRecordPortalErrorInfo4Phase2 ( requestid , nodeId , nodeName , userids , requestObject . toJSONString ( ) , resultCode , resultmsg , zhjkbs ) ;
}
}
}
}
} catch ( Exception e ) {
e . printStackTrace ( ) ;
rs . writeLog ( e ) ;
}
}
public String getShortMessageTitle ( String messageTitle ) {
if ( messageTitle . length ( ) > 20 ) {
messageTitle = messageTitle . substring ( 0 , 17 ) + "..." ;
}
return messageTitle ;
}
/ * * *
*
* @param requestname
* @param operator
* @param operatorTime
* @return
* /
public Map < String , String > getEmailTemplate ( String requestname , String operator , String operatorTime ) {
Map < String , String > dataMap = new HashMap < String , String > ( ) ;
String notificationSubject = "You have a new To-Read - <" + requestname + "> " ;
String notificationContent = operator + " created this task on " + operatorTime + "\n" +
" Please handle it in time." ;
dataMap . put ( "notificationSubject" , notificationSubject ) ;
dataMap . put ( "notificationContent" , notificationContent ) ;
return dataMap ;
}
public int getStaffId ( int userid ) {
int result = - 1 ;
String cus_staff = PropBean . getUfPropValue ( "cus_staff" ) ;
RecordSet rs = new RecordSet ( ) ;
rs . executeQuery ( "select " + cus_staff + " from cus_fielddata where scopeid=-1 and scope = 'HrmCustomFieldByInfoType' and id=?" , userid ) ;
rs . next ( ) ;
result = Util . getIntValue ( rs . getString ( cus_staff ) ) ;
return result ;
}
}