package weaver.interfaces.htsc.workflow.action;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.*;

/**
 * Created by K1810007 on 2020/10/27.
 */

public class UpdateZbzysxBshqAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        String requestid = requestInfo.getRequestid();
        writeLog( "----UpdateZbzysxBshqAction-------" + requestid );

        try {
            String tablename = requestInfo.getRequestManager().getBillTableName();//主表
            String sblb ="";
            String sblx = "";
            String mainid = "" ;
            String sql = "select id,sblb,sblx,hqbms from " + tablename + " where requestid='" + requestid + "'";
            writeLog( sql );
            rs.executeQuery( sql );
            if(rs.next()){
                sblb = Util.null2String( rs.getString( "sblb" ) );//申报类别
                sblx = Util.null2String( rs.getString( "sblx" ) );//申报类型
                mainid = Util.null2String( rs.getString( "id" ) );//mainid
            }

            writeLog("sblb:"+sblb);
            writeLog("sblx:"+sblx);
            writeLog("mainid:"+mainid);

            String bjhqbm = "";
            sql = " select wm_concat(bjhqbm) as bjhqbm from uf_zysxsyfw where sblb ='" + sblb + "' and sblx='" + sblx + "'";
            rs.executeQuery( sql);
            writeLog("sql:"+sql);
            if (rs.next())
            {
                bjhqbm = Util.null2String( rs.getString( "bjhqbm" ) );//必经会签部门
            }
            writeLog("必经会签部门："+bjhqbm);
            if (!"".equals(bjhqbm)) {
                List<Map<String,String>> list = new ArrayList<Map<String,String>>();
                Map<String,String> map = null;
                sql = " select id,hqbm from "+tablename+"_dt2 where mainid="+mainid;
                writeLog("sql:"+sql);
                rs.execute(sql);
                while(rs.next()){
                    map = new HashMap<String,String>();
                    map.put("id",Util.null2String(rs.getString("id")));
                    map.put("hqbm",Util.null2String(rs.getString("hqbm")));
                    list.add(map);
                }
                int errcount = 0;
                for(int i=0;i<list.size();i++)
                {
                    Map<String,String> datamap = list.get(i);
                    String id = Util.null2String(datamap.get("id"));
                    String hqbm = Util.null2String(datamap.get("hqbm"));
                    int isBjhqbm = 0;
                    if (bjhqbm.contains(hqbm)) {
                        isBjhqbm = 1;
                    }
                    sql = " update " + tablename + "_dt2 set bshq="+isBjhqbm+" where id="+id;
                    writeLog("sql:"+sql);
                    boolean bool = rs.execute(sql);
                    if(!bool){
                        errcount++;
                    }
                }
                writeLog("errcount:"+errcount);
                if(errcount > 0)
                {
                    requestInfo.getRequestManager().setMessageid( "1111" );
                    requestInfo.getRequestManager().setMessagecontent( "更新数据异常,请连续管理员!" );
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            writeLog( "异常参考:" + e );
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent("流程提交异常,请联系管理员!");
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }
}
