package weaver.interfaces.htsc.workflow.odoc;

import com.engine.common.util.ServiceUtil;
import com.engine.htsc.receipt.service.ReceiptWaitService;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 更新主流程明细表数据为已办结
 * 公司收文子流程-办结操作
 * create by PTY
 */
public class ReceiptSubEndAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        PropBean pb = new PropBean();
        RecordSet rs = new RecordSet();
        String requestid = requestInfo.getRequestid();
        writeLog("----ReceiptSubEndAction-------" + requestid);
        try {
            String sql = "";

            //获取主流程id
            String mainRequestid = "";
            sql = "select mainrequestid from WORKFLOW_SUBWFREQUEST where subrequestid = '"+requestid+"'";
            writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                mainRequestid = Util.null2String(rs.getString("mainrequestid"));
            }

            //获取主流程表名
            String mainTableName = "";
            sql = "select t3.tablename from WORKFLOW_REQUESTBASE t1,WORKFLOW_BASE t2,WORKFLOW_BILL t3 "
                    +"where t2.id = t1.workflowid and t3.id = t2.formid and t1.requestid = '"+mainRequestid+"'";
            writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                mainTableName = Util.null2String(rs.getString("tablename"));
            }

            String update_sql = "update "+mainTableName+"_DT11 set sfbj = '1' where lcmc = '"+requestid+"'";
            writeLog(update_sql);
            rs.executeUpdate(update_sql);

            String workflowid = "";
            sql = "select workflowid from WORKFLOW_REQUESTBASE where requestid = '"+requestid+"'";
            writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                workflowid = Util.null2String(rs.getString("workflowid"));
            }

            String gsswzbbm = pb.getPropName("gsswzbbm");
            String gsswzbbm_workflowid = pb.getActiveWorkflowIdByUnNumber(gsswzbbm);
            writeLog("gsswzbbm:"+gsswzbbm);
            writeLog("gsswzbbm_workflowid:"+gsswzbbm_workflowid);

            if(workflowid.equals(gsswzbbm_workflowid)){
                String subTableName = "";
                sql = "select t3.tablename from WORKFLOW_REQUESTBASE t1,WORKFLOW_BASE t2,WORKFLOW_BILL t3 "
                        +"where t2.id = t1.workflowid and t3.id = t2.formid and t1.requestid = '"+requestid+"'";
                writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    subTableName = Util.null2String(rs.getString("tablename"));
                }

                String zbry = "";
                sql = "select zbry from "+subTableName+" where requestid = '"+requestid+"'";
                writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    zbry = Util.null2String(rs.getString("zbry"));
                }
                update_sql = "update "+mainTableName+" set zbry = '"+zbry+"' where requestid = '"+mainRequestid+"'";
                writeLog(update_sql);
                boolean res = rs.executeUpdate(update_sql);
                writeLog("zbry update res:"+res);

                String mainid = "";
                sql = "select id from "+mainTableName+" where requestid = '"+mainRequestid+"'";
                writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    mainid = Util.null2String(rs.getString("id"));
                }

                update_sql = "update "+mainTableName+"_dt6 set zbclr = '"+zbry+"' where mainid = '"+mainid+"'";
                writeLog(update_sql);
                res = rs.executeUpdate(update_sql);
                writeLog("zbclr update res:"+res);
            }
            endWait(mainRequestid,requestid);
        }catch (Exception e) {
            e.printStackTrace();
            writeLog("ReceiptSubEndAction error:" + e);
            requestInfo.getRequestManager().setMessageid("222222");
            requestInfo.getRequestManager().setMessagecontent("流程提交异常,请联系管理员!");
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }

    public void endWait(String mainRequestid,String subRequestid){
        RecordSet rs = new RecordSet();

        List<String> subList = new ArrayList<>();
        String sql = "select t1.subrequestid,t2.currentnodetype from WORKFLOW_SUBWFREQUEST t1,WORKFLOW_REQUESTBASE t2 " +
                "where t2.requestid = t1.subrequestid and t2.currentnodetype <> 3 and t1.mainrequestid = '"+mainRequestid+"'";
        writeLog(sql);
        rs.executeQuery(sql);
        while(rs.next()){
            subList.add(Util.null2String(rs.getString("subrequestid")));
        }
        writeLog("size:"+subList.size());
        writeLog(subList.toString());
        if(subList.size()==1&&subRequestid.equals(subList.get(0))){
            String userid = "";
            sql = "select creater from WORKFLOW_REQUESTBASE where requestid = '"+subRequestid+"'";
            writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                userid = Util.null2String(rs.getString("creater"));
            }
            ReceiptWaitService receiptWaitService = ServiceUtil.getService(ReceiptWaitService.class);
            receiptWaitService.updateWait(mainRequestid,userid,"delete");
        }
    }
}
