package weaver.interfaces.htsc.cgrwd;

import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
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;

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

public class cgrwdUpdateDt2Dt3Action extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();

        String requestid = requestInfo.getRequestid();
        System.out.println( "----ItcglcUpdateDt2Dt3Action-------" + requestid );
        String tablename = requestInfo.getRequestManager().getBillTableName();//主表

        try {
            int inok = 0;
            String sql = " select id,hqbm,kgsldsp from " + tablename + " where requestid='" + requestid + "'";
            rs.executeQuery( sql );
            rs.next();
            String mainid = Util.null2String( rs.getString( "id" ) );//mainid
            String hqbms = Util.null2String( rs.getString( "hqbm" ) );//会签部门
            String kgsldsps = Util.null2String( rs.getString( "kgsldsp" ) );//跨公司会签

            boolean isdel2 = rs.executeUpdate( "delete " + tablename + "_dt2 where mainid='" + mainid + "' and (yhq is null or yhq = 0) " );
            if(isdel2){

//                List<String> deptList = new ArrayList<String>();
//                PropBean pb = new PropBean();
//                String itlxlx_bshqbm = pb.getPropName("itcglx_bshqbm");
//                if(!"".equals(itlxlx_bshqbm))
//                {
//                    itlxlx_bshqbm = "'"+itlxlx_bshqbm.replaceAll(",","','")+"'";
//
//                    sql = " select id,departmentname from hrmdepartment where departmentcode in ("+itlxlx_bshqbm+") ";
//                    rs.execute(sql);
//                    while (rs.next()){
//                        String deptid = Util.null2String(rs.getString("id")) ;
//                        deptList.add(deptid);
//                    }
//                }

                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)
                        {
                            rs.executeQuery( "select deptdirectors from hrmdepartmentdefined where deptid='" + hqbm + "'" );
                            rs.next();
                            String deptdirectors = Util.null2String( rs.getString( "deptdirectors" ) );
                            String insert_sql = "insert into " + tablename + "_dt2 (mainid,hqbm,bmfzr,sfhq)values('" + mainid + "','" + hqbm + "','" + deptdirectors + "','1')";
                            boolean istrue = rs.executeUpdate( insert_sql );
                            if(!istrue){
                                inok++;
                            }
                        }
                    }
                }
            }else{
                inok++;
            }



            boolean isdel3 = rs.executeUpdate( "delete " + tablename + "_dt3 where mainid='" + mainid + "' and (yhq is null or yhq = 0)" );
            if(isdel3){
                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 + "','1')";
                            boolean istrue = rs.executeUpdate( insert_sql );
                            if(!istrue){
                                inok++;
                            }
                            System.out.println( insert_sql );
                        }
                    }
                }
            }else{
                inok++;
            }

            if(inok >0){
                requestInfo.getRequestManager().setMessageid( "111111" );
                requestInfo.getRequestManager().setMessagecontent( "流程提交失败" );
                return Action.FAILURE_AND_CONTINUE;
            }else{
                return Action.SUCCESS;
            }

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


    public List<String> getOtherDept(){
        PropBean pb = new PropBean();
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        String deptcode = pb.getPropName("ItlxlcQtBm");
        bb.writeLog("deptcode:"+deptcode);
        if(!"".equals(deptcode)){
            deptcode = "'"+deptcode.replaceAll(",","','")+"'";
        }
        bb.writeLog("deptcode:"+deptcode);
        List<String> list = new ArrayList<String>();

        String sql ="  select id,departmentcode from hrmdepartment d\n" +
                " connect by prior d.id = d.supdepid\n" +
                " start with d.departmentcode in ("+deptcode+") ";
        bb.writeLog("sql:"+sql);
        rs.execute(sql);
        while (rs.next()){
            String id = Util.null2String(rs.getString("id"));
            list.add(id);
        }
        bb.writeLog("list:"+list.size());
        return list;
    }


}
