package weaver.interfaces.htsc.itcssx;


import com.weaver.general.BaseBean;
import org.apache.commons.lang3.StringUtils;
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.Arrays;
import java.util.LinkedHashSet;
import java.util.List;

public class ITTestOnlineDetailAction extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        PropBean pb = new PropBean();
        RecordSet rs = new RecordSet();
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();//主表
        writeLog("----ITTestOnlineDetailAction-------" + requestid);
        try {
            String mainid = "";
            String bbxgbm = "";//版本相关部门
            String kgsldsp = "";//跨公司领导审批
            String xtmcIdStrs = "";//系统名称
            String bshqdeptcode = converString(pb.getPropName("ITXQ_BSHQ2"));//系统名称含有电子印章管理平台时必送的会签部门的部门编码
            String dzyzXtmcId = pb.getPropName("xqxgxt2");//系统名称:电子印章管理平台 的id
            List<String> bshqdeptIdList = new ArrayList<>();
            // 获取部门id
            String sql = "select id from HRMDEPARTMENT where DEPARTMENTCODE in("+bshqdeptcode+")";
            writeLog(sql);
            rs.execute(sql);
            while (rs.next()){
                bshqdeptIdList.add(Util.null2String(rs.getString("id")));
            }
            writeLog("bshqdeptIdList-->"+bshqdeptIdList);
            sql = " select id,bbxgbm,kgsldsp,XTMC from "+tablename+" where requestid='"+requestid+"'";
            writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                mainid = Util.null2String(rs.getString("id"));
                bbxgbm = Util.null2String(rs.getString("bbxgbm"));
                kgsldsp = Util.null2String(rs.getString("kgsldsp"));
                xtmcIdStrs = Util.null2String(rs.getString("XTMC"));
            }

            //会签
            rs.executeUpdate("delete from "+tablename+"_dt2 where mainid='"+mainid+"' and (yhq is null or yhq = 0)");
            List<String> currenthqbmList = new ArrayList<String>();
            sql = " select hqbm from "+tablename+"_dt2 where mainid="+mainid+" and yhq=1 ";
            rs.execute(sql);
            while(rs.next()){
                String id = Util.null2String(rs.getString("hqbm"));
                if(!"".equals(id))
                {
                    currenthqbmList.add(id);
                }
            }
            if(StringUtils.isNotEmpty(bbxgbm)){
                List<String> hqbmList = Arrays.asList(bbxgbm.split(","));
                for(String deptid:hqbmList){
                    boolean occur = false;
                    String bshq = "0";
                    if(currenthqbmList.size()>0&&currenthqbmList.contains(deptid)){
                        occur = true;
                    }
                    if(!occur)
                    {
                        rs.executeQuery( "select deptdirectors from hrmdepartmentdefined where deptid='"+deptid+"'");
                        if(rs.next()){
                            // 如果包含电子印章管理平台
                            if (xtmcIdStrs.indexOf(dzyzXtmcId) != -1){
                                // 如果会签部门包含必送的部门，则必送bshq=1
                                if (bshqdeptIdList.contains(deptid)){
                                    bshq = "1";
                                }
                            }
                            String deptdirectors = Util.null2String(rs.getString("deptdirectors"));
                            String insert_sql = "";
                            insert_sql = "insert into "+tablename+"_dt2 (mainid,hqbm,bmfzr,bshq)values('"+mainid+"','"+deptid+"','"+deptdirectors+"','"+bshq+"')";
                            writeLog(insert_sql);
                            rs.executeUpdate(insert_sql);
                        }
                    }

                }
            }

            //跨公司会签
            rs.executeUpdate("delete from "+tablename+"_dt3 where mainid='"+mainid+"' and (yhq is null or yhq = 0)");
            List<String> currentkgsldspList = new ArrayList<String>();
            sql = " select bmfzr from "+tablename+"_dt3 where mainid="+mainid+" and yhq=1 ";
            rs.execute(sql);
            while(rs.next()){
                String id = Util.null2String(rs.getString("bmfzr"));
                if(!"".equals(id))
                {
                    currentkgsldspList.add(id);
                }
            }
            if(StringUtils.isNotEmpty(kgsldsp)){
                List<String> kgsldspList = Arrays.asList(kgsldsp.split(","));
                for(String userid:kgsldspList){
                    boolean occur = false;
                    if(currentkgsldspList.size()>0&&currentkgsldspList.contains(userid)){
                        occur = true;
                    }
                    if(!occur)
                    {
                        String insert_sql = "insert into "+tablename+"_dt3 (mainid,bmfzr,bshq)values('"+mainid+"','"+userid+"','0')";
                        writeLog(insert_sql);
                        rs.executeUpdate(insert_sql);
                    }

                }
            }

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

    /**
     * List<String> 去重
     * @param list
     * @return
     */
    public static List<String> delRepeat1(List<String> list) {
        List<String> listNew2 = new ArrayList<String>(new LinkedHashSet<String>(list));
        return listNew2;
    }

    /**
     * 字符串转换成list
     * @param strs
     * @return
     */
    public List<String> convertStringToList(String strs){
        if (StringUtils.isNotBlank(strs)){
            String[] idStrs = strs.trim().split(",");
            if (idStrs.length > 0){
                List<String> strsList = new ArrayList<String>();
                for (String str : idStrs) {
                    if (StringUtils.isNotBlank(str)){
                        strsList.add(str.trim());
                    }
                }
                if (strsList.size() > 0){
                    return strsList;
                }
            }
        }
        return  null;
    }

    /**
     * 将"1,2,3,.."格式的字符串，转换成"'1','2','3',..."格式的字符串
     * @param strs
     * @return
     */
    public static String converString(String strs){
        if (StringUtils.isNotBlank(strs)){
            String[] idStrs = strs.trim().split(",");
            if (idStrs.length > 0){
                StringBuffer sbf = new StringBuffer();
                for (String str : idStrs) {
                    if (StringUtils.isNotBlank(str)){
                        sbf.append("'").append(str.trim()).append("'").append(",");

                    }
                }
                if (sbf.length() > 0){
                    sbf = sbf.deleteCharAt(sbf.length() - 1);
                    return sbf.toString();
                }
            }
        }
        return "";
    }

}
