package weaver.interfaces.htsc.seal;

import com.weaver.general.BaseBean;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

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

/**
 * 外部系统触发用印登记单
 * 用印文件id由明细表汇总到主表
 */
public class SealReOutLaunchAction extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();//主表
        writeLog("----SealReOutLaunchAction-------" + requestid);
        try{
            String mainid = "";
            String sfsdfq = "";
            String wbxtly = "";
            String sql = "select id,sfsdfq,wbxtly from "+tablename+" where requestid = '"+requestid+"'";
            writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                mainid = Util.null2String(rs.getString("id"));
                sfsdfq = Util.null2String(rs.getString("sfsdfq"));
                wbxtly = Util.null2String(rs.getString("wbxtly"));
            }

            if("1".equals(sfsdfq)&&"ONE".equals(wbxtly)){
                List<String> wjmcList = new ArrayList<>();
                sql = "select wjmc from "+tablename+"_dt1 where mainid = '"+mainid+"'";
                writeLog(sql);
                rs.executeQuery(sql);
                while (rs.next()){
                    String wjmc = Util.null2String(rs.getString("wjmc"));
                    if(StringUtils.isNotEmpty(wjmc)){
                        wjmcList.add(wjmc);
                    }
                }
                String yywjStr = String.join(",",wjmcList);
                String update_sql = "update "+tablename+" set yywj = '"+yywjStr+"' where requestid = '"+requestid+"'";
                writeLog(update_sql);
                rs.executeUpdate(update_sql);
            }


            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            writeLog( "异常参考:" + e );
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "流程提交失败" );
            return Action.FAILURE_AND_CONTINUE;
        }
    }
}
