package weaver.interfaces.htsc.itxqlc.action;

import com.weaver.general.BaseBean;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.itxqlc.util.ItxqLcUtil;
import weaver.interfaces.htsc.util.HT_WorkflowInfo;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

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

public class ItxqlcUpdateDt2Dt3Action extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        RecordSet rs2 = new RecordSet();
        BaseBean bb = new BaseBean();
        String requestid = requestInfo.getRequestid();
        bb.writeLog("----WeaClearDtlTblAction-------" + requestid );
        String tablename = requestInfo.getRequestManager().getBillTableName();//主表
        ItxqLcUtil ItxqLcUtil = new ItxqLcUtil();
        try {
            String sql = " select id,szbm,hgfxdj,sfxyw,sfcyxxxt,sfsjywxghgfkgn,ywlb,xqxgxt,hqbm,kgsldsp from " + tablename + " where requestid='" + requestid + "'";
            //writeLog( sql );
            rs.executeQuery( sql );
            rs.next();
            String szbm = Util.null2String( rs.getString( "szbm" ) );//申报类别
            String hgfxdj = Util.null2String( rs.getString( "hgfxdj" ) );//申报类型
            String mainid = Util.null2String( rs.getString( "id" ) );//mainid
            String sfxyw = Util.null2String( rs.getString( "sfxyw" ) );//会签部门
            String sfcyxxxt = Util.null2String( rs.getString( "sfcyxxxt" ) );//会签部门
            String sfsjywxghgfkgn = Util.null2String( rs.getString( "sfsjywxghgfkgn" ) );//跨公司会签
            String ywlb = Util.null2String( rs.getString( "ywlb" ) );//跨公司会签
            String xqxgxt = Util.null2String( rs.getString( "xqxgxt" ) );//跨公司会签
            String hqbmzb = Util.null2String( rs.getString( "hqbm" ) );//跨公司会签
            String kgsldspzb = Util.null2String( rs.getString( "kgsldsp" ) );//跨公司会签

            String bjhqbm = ItxqLcUtil.getBshqbmData(szbm,hgfxdj,sfxyw,sfcyxxxt,sfsjywxghgfkgn,ywlb,xqxgxt);
            bb.writeLog("必经会签部门："+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( hqbmzb ))
            {
                String[] arr = hqbmzb.split( "," );
                for (String hqbm : arr)
                {
                    boolean flag = false;
                    if(list_dt2.size() > 0){
                        if(!list_dt2.contains(hqbm)){
                            flag = true;
                        }
                    }else{
                        flag = true;
                    }
                    bb.writeLog("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 = "insert into " + tablename + "_dt2 (mainid,hqbm,bmfzr,bshq)values('" + mainid + "','" + hqbm + "','" + deptdirectors + "','"+isBjhqbm+"')";
                        rs.executeUpdate( insert_sql );
                        bb.writeLog(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( kgsldspzb )) {
                String[] arrkgs = kgsldspzb.split( "," );
                for (String kgsldsp : arrkgs) {
                    boolean flag = false;
                    if(list_dt3.size() > 0)
                    {
                        if(!list_dt3.contains(kgsldsp)){
                            flag = true;
                        }
                    }else{
                        flag = true;
                    }
                    bb.writeLog("flag:"+flag);
                    if(flag){
                        String insert_sql = "insert into " + tablename + "_dt3 (mainid,bmfzr,sfhq)values('" + mainid + "','" + kgsldsp + "','0')";
                        rs.executeUpdate( insert_sql );
                        bb.writeLog(insert_sql);
                    }

                }
            }
            sql = " update " + tablename + " set ldsp2=ldsp where requestid=" + requestid + "";
            rs.executeUpdate(sql);
            bb.writeLog("sql:"+sql);

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