package weaver.interfaces.htsc.duss.action;

import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.GCONST;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.gwlc.sendPostRulRegulAction;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

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

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

public class WeaClearDtlTblAction extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        RecordSet rs2 = new RecordSet();
        String requestid = requestInfo.getRequestid();
        System.out.println( "----WeaClearDtlTblAction-------" + requestid );
        String tablename = requestInfo.getRequestManager().getBillTableName();//主表
        String sql = " select id,hqbm,kgsldsp,sblb,sblx,szbm from " + tablename + " where requestid='" + requestid + "'";
        //writeLog( sql );
        try {
            rs.executeQuery( sql );
            rs.next();
            String sblb = Util.null2String( rs.getString( "sblb" ) );//申报类别
            String sblx = Util.null2String( rs.getString( "sblx" ) );//申报类型
            String mainid = Util.null2String( rs.getString( "id" ) );//mainid
            String hqbms = Util.null2String( rs.getString( "hqbm" ) );//会签部门
            String szbm = Util.null2String( rs.getString( "szbm" ) );//会签部门
            String kgsldsps = Util.null2String( rs.getString( "kgsldsp" ) );//跨公司会签
            String sql1 = "select wm_concat_old(bjhqbm) as bjhqbm from uf_zysxsyfw where sblb ='" + sblb + "' and sblx='" + sblx + "'";
            rs2.executeQuery( sql1 );
            String bjhqbm_mr = "";
            if (rs2.next()) {
                bjhqbm_mr = Util.null2String( rs2.getString( "bjhqbm" ) );//必经会签部门
            }

            String bjhqbm = "" ;
            if(!"".equals(bjhqbm_mr) && !"".equals(szbm))
            {
                List<String> arr = Arrays.asList(bjhqbm_mr.split(","));
                if(arr.contains(szbm))
                {
                    for (String bjhqbm_ : arr)
                    {
                        if(!bjhqbm_.equals(szbm))
                        {
                            bjhqbm += bjhqbm == "" ? bjhqbm_ : ","+bjhqbm_ ;
                        }
                    }
                }else{
                    bjhqbm = bjhqbm_mr ;
                }
            }else{
                bjhqbm = bjhqbm_mr ;
            }

            PropBean pb = new PropBean();
            String bsgid = Util.null2String(pb.getPropName("BGSID"));  //办公室ID

            System.out.println("必经会签部门："+bjhqbm);
            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)
                    {
                        int isBjhqbm =0;
                        if((","+bjhqbm+",").contains(","+hqbm+",")){
                            isBjhqbm=1;
                        }
                        rs1.executeQuery( "select deptdirectors from hrmdepartmentdefined where deptid='" + hqbm + "'" );
                        rs1.next();
                        String deptdirectors = Util.null2String( rs1.getString( "deptdirectors" ) );
                        String insert_sql = "";
//                        if (deptdirectors.split( "," ).length >= 2 || bsgid.equals(hqbm)) {
//                            insert_sql = "insert into " + tablename + "_dt2 (mainid,hqbm,bshq)values('" + mainid + "','" + hqbm + "','"+isBjhqbm+"')";
//                        } else {
//                            insert_sql = "insert into " + tablename + "_dt2 (mainid,hqbm,bmfzr,bshq)values('" + mainid + "','" + hqbm + "','" + deptdirectors + "','"+isBjhqbm+"')";
//                        }

                        if (bsgid.equals(hqbm)) {
                            insert_sql = "insert into " + tablename + "_dt2 (mainid,hqbm,bshq)values('" + mainid + "','" + hqbm + "','"+isBjhqbm+"')";
                        } else {
                            insert_sql = "insert into " + tablename + "_dt2 (mainid,hqbm,bmfzr,bshq)values('" + mainid + "','" + hqbm + "','" + deptdirectors + "','"+isBjhqbm+"')";
                        }

                        rs.executeUpdate( insert_sql );
                        System.out.println( insert_sql );
                    }
                }
            }

            rs.executeUpdate( "delete " + tablename + "_dt3 where mainid='" + mainid + "' and (yhq is null or yhq = 0)" );
            List<String> list_dt3 = new ArrayList<String>();
            sql = " select bmfzr from "+tablename+"_dt3 where mainid="+mainid+" and yhq=1 ";
            rs.execute(sql);
            while(rs.next()){
                String bmfzr = Util.null2String(rs.getString("bmfzr"));
                if(!"".equals(bmfzr)){
                    list_dt3.add(bmfzr);
                }
            }
            if (!"".equals( kgsldsps )) {
                String[] arrkgs = kgsldsps.split( "," );
                for (String kgsldsp : arrkgs) {
                    boolean flag = false;
                    if(list_dt3.size() > 0)
                    {
                        if(!list_dt3.contains(kgsldsp)){
                            flag = true;
                        }
                    }else{
                        flag = true;
                    }
                    System.out.println("flag:"+flag);
                    if(flag){
                        String insert_sql = "insert into " + tablename + "_dt3 (mainid,bmfzr,sfhq)values('" + mainid + "','" + kgsldsp + "','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;
        }
    }


    public static void main(String[] args) {
        GCONST.setRootPath("WEB-INF/");
        GCONST.setServerName("ecology");

        WeaClearDtlTblAction WeaClearDtlTblAction = new WeaClearDtlTblAction();

        System.out.println("1111111");
        RequestInfo requestInfo = new RequestInfo();
        requestInfo.setRequestid("2550549");
        //requestInfo.getRequestManager().setBilltablename("formtable_main_48");

        WeaClearDtlTblAction.execute(requestInfo);

    }

}
