package weaver.interfaces.htsc.zbtyfk;

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

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

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-09-24
 * @Description: 申请节点提交后删除明细表数据
 * @ModifyLog:
 **/

public class ZbtyfkDt2Action extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        String requestid = requestInfo.getRequestid();
        System.out.println( "----ZbtyfkDt2Action-------" + requestid );
        String tablename = requestInfo.getRequestManager().getBillTableName();//主表

        try {
            String mainid = "";
            String hqbms = "";
            String sql = " select id,hqbm from " + tablename + " where requestid='" + requestid + "'";
            rs.executeQuery( sql );
            if(rs.next()){
                mainid = Util.null2String( rs.getString( "id" ) );//mainid
                hqbms = Util.null2String( rs.getString( "hqbm" ) );//会签部门
            }

            rs.executeUpdate( "delete " + tablename + "_dt2 where mainid='" + mainid + "' and (yhq is null or yhq = 0) " );
            List<String> list_dt2 = new ArrayList<String>();
            sql = " select hqbm from "+tablename+"_dt2 where mainid="+mainid+" and yhq=1 ";
            rs.execute(sql);
            while(rs.next()){
                String hqbm = Util.null2String(rs.getString("hqbm"));
                if(!"".equals(hqbm))
                {
                    list_dt2.add(hqbm);
                }
            }
            if (!"".equals( hqbms ))
            {
                String[] arr = hqbms.split( "," );
                for (String hqbm : arr)
                {
                    boolean flag = false;
                    if(list_dt2.size() > 0){
                        if(!list_dt2.contains(hqbm)){
                            flag = true;
                        }
                    }else{
                        flag = true;
                    }
                    System.out.println("flag:"+flag);
                    if(flag)
                    {
                        String deptdirectors = "";
                        rs.executeQuery( "select deptdirectors from hrmdepartmentdefined where deptid='" + hqbm + "'" );
                        if(rs.next()){
                            deptdirectors = Util.null2String(rs.getString("deptdirectors"));
                        }

                        String insert_sql = "insert into " + tablename + "_dt2 (mainid,hqbm,bmfzr,bshq)values('" + mainid + "','" + hqbm + "','" + deptdirectors + "','0')";
                        rs.executeUpdate(insert_sql);
                        System.out.println( insert_sql );
                    }
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            writeLog( "异常参考:" + e );
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "流程提交失败" );
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}
