package weaver.interfaces.htsc.workflow.odoc;

import com.engine.common.util.ServiceUtil;
import com.engine.htsc.collaboration.service.WorkflowTaskService;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.Map;

/**
 * 创建协作事项(改成任务了)
 * 公司收文主办部门-主办人员-节点后操作
 * create by PTY
 */
public class ReceiptCoWorkAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        writeLog("----ReceiptCoWorkAction-------" + requestid);
        try{
            String notAgree = requestInfo.getRequestManager().getRequest().getParameter("notAgree");
            String src = requestInfo.getRequestManager().getSrc();
            writeLog("notAgree:"+notAgree);
            writeLog("src:"+src);
            if("submit".equals(src)&&!"1".equals(notAgree)){
                RecordSet rs = new RecordSet();
                //获取流程表名
                String tableName = JAVATools.getMaintable(requestid);

                String sfyjxzsx = "";//是否已建协作事项 0-否 1-是
                String sfcfxz = "";//是否触发协作 0-是 1-否
                String sql = "select sfyjxzsx,sfcfxz from "+tableName+" where requestid= '"+requestid+"'";
                writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    sfyjxzsx = Util.null2String(rs.getString("sfyjxzsx"));
                    sfcfxz = Util.null2String(rs.getString("sfcfxz"));
                }

                if(!"1".equals(sfyjxzsx)&&"0".equals(sfcfxz)){
                    WorkflowTaskService workflowTaskService = ServiceUtil.getService(WorkflowTaskService.class);
                    Map<String, Object>result = workflowTaskService.createFromReceipt(requestid);
                    if("1".equals(result.get("status"))){
                        String update_sql = "update "+tableName+" set sfyjxzsx = '1' where requestid = '"+requestid+"'";
                        writeLog(update_sql);
                        rs.executeUpdate(update_sql);
                    }
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
            writeLog("ReceiptCoWorkAction error:" + e);
            requestInfo.getRequestManager().setMessageid( "222222" );
            requestInfo.getRequestManager().setMessagecontent( "流程提交异常,请联系管理员!" );
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }
}
