package weaver.interfaces.htxc.receipt;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.cus.HTUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
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)){
                    String response = createTask(requestid);
                    if(StringUtils.isBlank(response)){
                        writeLog("ReceiptCoWorkAction error: response null");
                        requestInfo.getRequestManager().setMessageid( "222222" );
                        requestInfo.getRequestManager().setMessagecontent( "流程提交异常,请联系管理员!" );
                        return Action.FAILURE_AND_CONTINUE;
                    }
                    JSONObject res = JSONObject.parseObject(response);
                    String status = Util.null2String(res.get("status"));//标识200-成功,否则失败
                    String message = Util.null2String(res.get("message"));
                    writeLog("status:"+status);
                    writeLog("message:"+message);
                    if(!"1".equals(status)){
                        writeLog("ReceiptCoWorkAction error:"+message);
                        requestInfo.getRequestManager().setMessageid( "222222" );
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }else{
                        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;
    }

    /**
     * 调用htoa环境触发接口
     * @param requestid
     * @return
     */
    public String createTask(String requestid) {
        BaseBean bs = new BaseBean();
        String response = "";
        bs.writeLog("doCreatTask----->Start");
        try {
            Map<String,Object> requestMap = new HashMap<>();
            requestMap.put("requestid",requestid);
            PropBean pb = new PropBean();
            //触发协作任务地址
            String receiptCreateTaskUrl = Util.null2String(pb.getPropName("receipt.creatTask.url"))+"?requestid="+requestid;
            bs.writeLog("doCreatTask----->" + receiptCreateTaskUrl);
            HTUtil htUtil = new HTUtil();
            response = htUtil.httpPostRaw(receiptCreateTaskUrl, JSON.toJSON(requestMap).toString(), null, null);
        } catch (Exception e) {
            bs.writeLog("doCreatTask捕获异常：", e);
        }
        bs.writeLog("doCreatTask response：" + response);
        bs.writeLog("doCreatTask----->End");
        return response;
    }
}
