package com.engine.htsc.synorg;

import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.hrm.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


/**
 *
 */
public class SynOrgAction {

    /**
     * 查询人员待办
     * @param request
     * @param response
     * @return
     */
    /*@GET
    @Path("/synall")
    @Produces({MediaType.TEXT_PLAIN})
    public String synall(@Context HttpServletRequest request, @Context HttpServletResponse response){

        JSONObject apidatas = new JSONObject();
        BaseBean bb = new BaseBean();
        //获取用户
        String type = Util.null2String(request.getParameter("type"));
        try {
            bb.writeLog("OrganizationSyn1111111");
            Map<String,String> headMap = new HashMap<String,String>();
            OrganizationSyn organizationsyn = new OrganizationSyn();
            String token = organizationsyn.getToken();
            bb.writeLog("token:"+token);
            headMap.put("JTXT-AUTH-TOKEN",token);
            headMap.put("JTXT-AUTH-TYPE","APP");

            updateAll(type,headMap);
            updateInc(type,headMap);
            organizationsyn.removeCache();
            apidatas.put("flag", "0");

//            JSONObject dataJson = new JSONObject();
//            dataJson.put("dept",map1);
//            dataJson.put("hrm",map2);
//            dataJson.put("job",map3);
//            dataJson.put("hrmleader",map4);
//            JSONObject jsonObject = new JSONObject();
//            jsonObject.put("flag","0");
//            jsonObject.put("data",dataJson);

        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("status", "0");
            apidatas.put("resultMsg","数据异常");
        }
        return apidatas.toJSONString();
    }


    public void updateAll(String type,Map<String,String> headMap)
    {
        PropBean pb = new PropBean();
        OrganizationSyn organizationsyn = new OrganizationSyn();
        BaseBean bb = new BaseBean();
        int isTrue = 0;

        String dept_message = "";
        if("1".equals(type) || "5".equals(type))
        {
            String deptUrl = pb.getPropName("deptUrl_All");
            try{
                bb.writeLog("开始同步部门信息");
                organizationsyn.doDeptSyn(deptUrl,headMap);
                bb.writeLog("开始同步部门信息");

                bb.writeLog("再开始同步部门信息");
                Map<String,String> map = organizationsyn.doDeptSyn(deptUrl,headMap);
                bb.writeLog("结束同步部门信息");

                String updatesuccesscount = map.get("updatesuccesscount");
                String updateerrorcount = map.get("updateerrorcount");
                String insertsuccesscount = map.get("insertsuccesscount");
                String inserterrorcount = map.get("inserterrorcount");
                String exceptioncount = map.get("exceptioncount");
                String allcount = map.get("allcount");
                dept_message = "部门同步总数:"+allcount+",新增成功:"+insertsuccesscount+",新增失败:"+inserterrorcount+",更新成功:"+updatesuccesscount+",更新失败:"+updateerrorcount+",更新异常:"+exceptioncount;

            }catch (Exception e){
                bb.writeLog("OrganizationSyn-e:"+e);
            }

            String deptAbsUrl = pb.getPropName("deptAbsUrl_All");
            try{
                organizationsyn.doDeptAbSyn(deptAbsUrl,headMap);
            }catch (Exception e){
                bb.writeLog("OrganizationSyn-e:"+e);
            }

            try{
                bb.writeLog("deptUrl:"+deptUrl);
                bb.writeLog("开始同步部门领导信息信息");
                organizationsyn.doDeptSynToLeader(deptUrl,headMap);
                bb.writeLog("结束同步部门领导信息信息");
            }catch (Exception e){
                bb.writeLog("OrganizationSyn-e:"+e);
            }

            try{
                bb.writeLog("deptAbsUrl:"+deptAbsUrl);
                bb.writeLog("开始同步虚拟部门领导信息信息");
                organizationsyn.doDeptSynToLeader(deptAbsUrl,headMap);
                bb.writeLog("结束同步虚拟部门领导信息信息");
            }catch (Exception e){
                bb.writeLog("OrganizationSyn-e:"+e);
            }
            isTrue++;
        }

        String res_message = "";
        if("2".equals(type) || "5".equals(type))
        {
            try{

                bb.writeLog("开始同步人员信息");
                String hrmUrl = pb.getPropName("hrmUrl_All");

                organizationsyn.updateBelongto();
                Map<String,String> map = organizationsyn.doHrmSyn(hrmUrl,headMap);

                String updatesuccesscount = map.get("updatesuccesscount");
                String updateerrorcount = map.get("updateerrorcount");
                String insertsuccesscount = map.get("insertsuccesscount");
                String inserterrorcount = map.get("inserterrorcount");
                String exceptioncount = map.get("exceptioncount");
                String allcount = map.get("allcount");
                String dataerrorcount = map.get("dataerrorcount");
                res_message = "部门人员(主岗)总数:"+allcount+",新增成功:"+insertsuccesscount+",新增失败:"+inserterrorcount+",更新成功:"+updatesuccesscount+",更新失败:"+updateerrorcount+",更新异常:"+exceptioncount+",异常数据:"+dataerrorcount;
                bb.writeLog("结束同步人员信息");

            }catch (Exception e){
                bb.writeLog("OrganizationSyn-e:"+e);
            }

            try{
                String deptUrl = pb.getPropName("deptUrl_All");
                bb.writeLog("deptUrl:"+deptUrl);
                bb.writeLog("开始同步部门领导信息信息");
                organizationsyn.doDeptSynToLeader(deptUrl,headMap);
                bb.writeLog("结束同步部门领导信息信息");
            }catch (Exception e){
                bb.writeLog("OrganizationSyn-e:"+e);
            }

            try{
                bb.writeLog("开始设置控件不检测");
                organizationsyn.setSysActivexCheck();
                bb.writeLog("结束设置控件不检测");
            }catch (Exception e){
                bb.writeLog("OrganizationSyn-e:"+e);
            }

            try{
                bb.writeLog("开始设置控件不检测");
                organizationsyn.setWorkflowUserDefault();
                bb.writeLog("结束设置控件不检测");
            }catch (Exception e){
                bb.writeLog("OrganizationSyn-e:"+e);
            }

            isTrue++;
        }

        String pos_message = "";
        if("3".equals(type) || "5".equals(type))
        {
            try{
                bb.writeLog("开始同步岗位信息");
                String jobUrl = pb.getPropName("jobUrl_All");
                organizationsyn.updateBelongto();
                Map<String,String> map = organizationsyn.doPositionSyn(jobUrl,headMap);
                String updatesuccesscount = map.get("updatesuccesscount");
                String updateerrorcount = map.get("updateerrorcount");
                String insertsuccesscount = map.get("insertsuccesscount");
                String inserterrorcount = map.get("inserterrorcount");
                String exceptioncount = map.get("exceptioncount");
                String allcount = map.get("allcount");
                String dataerrorcount = map.get("dataerrorcount");

                pos_message = "部门人员(兼岗)总数:"+allcount+",新增成功:"+insertsuccesscount+",新增失败:"+inserterrorcount+",更新成功:"+updatesuccesscount+",更新失败:"+updateerrorcount+",更新异常:"+exceptioncount+",异常数据:"+dataerrorcount;
                bb.writeLog("结束同步岗位信息");

            }catch (Exception e){
                bb.writeLog("OrganizationSyn-e:"+e);
            }

            try{
                bb.writeLog("开始同步岗位信息");
                String jobAbsUrl = pb.getPropName("jobAbsUrl_All");
                organizationsyn.doAbsPositionSyn(jobAbsUrl,headMap);
                bb.writeLog("结束同步岗位信息");
            }catch (Exception e){
                bb.writeLog("OrganizationSyn-e:"+e);
            }

            try{
                String deptUrl = pb.getPropName("deptUrl_All");
                bb.writeLog("deptUrl:"+deptUrl);
                bb.writeLog("开始同步部门领导信息信息");
                organizationsyn.doDeptSynToLeader(deptUrl,headMap);
                bb.writeLog("结束同步部门领导信息信息");
            }catch (Exception e){
                bb.writeLog("OrganizationSyn-e:"+e);
            }

            try{
                bb.writeLog("开始设置控件不检测");
                organizationsyn.setSysActivexCheck();
                bb.writeLog("结束设置控件不检测");
            }catch (Exception e){
                bb.writeLog("OrganizationSyn-e:"+e);
            }

            isTrue++;

        }

        if("4".equals(type) || "5".equals(type)){
            try{
                bb.writeLog("开始同步部门矩阵信息");
                organizationsyn.doMatrixInfo();
                bb.writeLog("结束同步部门矩阵信息");
            }catch (Exception e){
                bb.writeLog("OrganizationSyn-e:"+e);
            }

            isTrue++;
        }

        if("10".equals(type)){
            try{
                bb.writeLog("开始同步人员头像");
                String hrmUrl = pb.getPropName("hrmUrl_All");
                organizationsyn.doHrmSynPhoto(hrmUrl,headMap);
                bb.writeLog("结束同步人员头像");
            }catch (Exception e){
                bb.writeLog("OrganizationSyn-e:"+e);
            }

            isTrue++;

        }

        if("11".equals(type))
        {
            try{
                bb.writeLog("开始同步待办显示定制列");
                organizationsyn.updateColList("");
                bb.writeLog("结束同步待办显示定制列");
            }catch (Exception e){
                bb.writeLog("OrganizationSyn-e:"+e);
            }

            isTrue++;

        }

        if(isTrue >0)
        {
            RecordSet rs = new RecordSet();
            SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm");//设置日期格式
            String modedatacreatedate = sdfDate.format(new Date());
            String modedatacreatetime = sdfTime.format(new Date());
            String modedatacreatertype = "0";

            String syn_hrmmode_cubeuuid = pb.getPropName("UF_ORGSYNTIME_CUBEUUID");
            JSONObject jsonObject = pb.getModeInfoByUUID(syn_hrmmode_cubeuuid);
            int formmodeid = Util.getIntValue(jsonObject.getString("modeid")) ;
            bb.writeLog("formmodeid:"+formmodeid);

            int userid = 1;
            String modeuuid = UUID.randomUUID().toString();
            String sql = " insert into uf_orgsyntime(tbrq,tbsj,formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime,modeuuid,bmtbsl,ryzgtbsl,ryjgtbsl)values\n" +
                    " ('"+modedatacreatedate+"','"+modedatacreatetime+"',"+formmodeid+","+userid+","+modedatacreatertype+",'"+modedatacreatedate+"','"+modedatacreatetime+"','"+modeuuid+"','"+dept_message+"','"+res_message+"','"+pos_message+"') ";
            bb.writeLog("sql:"+sql);
            boolean bool = rs.executeUpdate(sql);
            bb.writeLog("bool:"+bool);
            if(bool)
            {
                rs.executeQuery( "select id from uf_orgsyntime where modeuuid='" + modeuuid + "'" );
                while (rs.next())
                {
                    int id = Util.getIntValue( rs.getString( "id" ) );
                    ModeRightInfo moderight = new ModeRightInfo();
                    moderight.editModeDataShare(userid, formmodeid, id );
                }
            }
        }
    }

    public void updateInc(String type,Map<String,String> headMap)
    {
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        PropBean pb = new PropBean();
        int isTrue = 0;
        OrganizationSynTime OrganizationSynTime = new OrganizationSynTime();
        String createTimeGreater = "";
        String sql = "select to_char(sysdate-30,'yyyy-mm-dd') as nowdate from dual";
        rs.execute(sql);
        if(rs.next()){
            createTimeGreater = Util.null2String(rs.getString("nowdate"));
        }
        bb.writeLog("createTimeGreater:"+createTimeGreater);
        if(!"".equalsIgnoreCase(createTimeGreater))
        {
            createTimeGreater = createTimeGreater+"T00:00:00" ;

            String dept_message = "";
            if("6".equals(type) || "9".equals(type)){
                try{
                    bb.writeLog("开始同步部门信息");
                    String deptUrl = pb.getPropName("deptUrl_Inc");
                    Map<String,String> map = OrganizationSynTime.doDeptSyn(deptUrl,createTimeGreater,headMap);
                    String updatesuccesscount = map.get("updatesuccesscount");
                    String updateerrorcount = map.get("updateerrorcount");
                    String insertsuccesscount = map.get("insertsuccesscount");
                    String inserterrorcount = map.get("inserterrorcount");
                    String exceptioncount = map.get("exceptioncount");
                    String allcount = map.get("allcount");
                    dept_message = "部门同步总数:"+allcount+",新增成功:"+insertsuccesscount+",新增失败:"+inserterrorcount+",更新成功:"+updatesuccesscount+",更新失败:"+updateerrorcount+",更新异常:"+exceptioncount;
                    bb.writeLog("开始同步部门信息");
                }catch (Exception e){
                    bb.writeLog("OrganizationSyn-e:"+e);
                }
                isTrue++;
            }

            String res_message = "";
            if("7".equals(type) || "9".equals(type))
            {
                try {
                    bb.writeLog("开始同步人员信息");
                    String hrmUrl = pb.getPropName("hrmUrl_Inc");
                    OrganizationSynTime.updateBelongto();
                    Map<String,String> map = OrganizationSynTime.doHrmSyn(hrmUrl,createTimeGreater,headMap);
                    String updatesuccesscount = map.get("updatesuccesscount");
                    String updateerrorcount = map.get("updateerrorcount");
                    String insertsuccesscount = map.get("insertsuccesscount");
                    String inserterrorcount = map.get("inserterrorcount");
                    String exceptioncount = map.get("exceptioncount");
                    String allcount = map.get("allcount");
                    String dataerrorcount = map.get("dataerrorcount");
                    res_message = "部门人员(主岗)总数:"+allcount+",新增成功:"+insertsuccesscount+",新增失败:"+inserterrorcount+",更新成功:"+updatesuccesscount+",更新失败:"+updateerrorcount+",更新异常:"+exceptioncount+",异常数据:"+dataerrorcount;
                    bb.writeLog("结束同步人员信息");
                }catch (Exception e){
                    bb.writeLog("e:"+e);
                }

                try{
                    String deptUrl = pb.getPropName("deptUrl_Inc");
                    bb.writeLog("开始同步部门领导信息信息");
                    OrganizationSynTime.doDeptSynToLeader(deptUrl,createTimeGreater,headMap);
                    bb.writeLog("结束同步部门领导信息信息");
                }catch (Exception e){
                    bb.writeLog("e:"+e);
                }

                isTrue++;
            }

            String pos_message = "";
            if("8".equals(type) || "9".equals(type))
            {
                try{
                    bb.writeLog("开始同步岗位信息");
                    OrganizationSynTime.updateBelongto();

                    String jobUrl = pb.getPropName("jobUrl_Inc");
                    Map<String,String> map = OrganizationSynTime.doPositionSyn(jobUrl,createTimeGreater,headMap);
                    String updatesuccesscount = map.get("updatesuccesscount");
                    String updateerrorcount = map.get("updateerrorcount");
                    String insertsuccesscount = map.get("insertsuccesscount");
                    String inserterrorcount = map.get("inserterrorcount");
                    String exceptioncount = map.get("exceptioncount");
                    String allcount = map.get("allcount");
                    String dataerrorcount = map.get("dataerrorcount");
                    pos_message = "部门人员(兼岗)总数:"+allcount+",新增成功:"+insertsuccesscount+",新增失败:"+inserterrorcount+",更新成功:"+updatesuccesscount+",更新失败:"+updateerrorcount+",更新异常:"+exceptioncount+",异常数据:"+dataerrorcount;
                    bb.writeLog("结束同步岗位信息");
                }catch (Exception e){
                    bb.writeLog("e:"+e);
                }

                try{
                    String deptUrl = pb.getPropName("deptUrl_Inc");
                    bb.writeLog("开始同步部门领导信息信息");
                    OrganizationSynTime.doDeptSynToLeader(deptUrl,createTimeGreater,headMap);
                    bb.writeLog("结束同步部门领导信息信息");
                }catch (Exception e){
                    bb.writeLog("e:"+e);
                }

                isTrue++;
            }

            if(isTrue > 0)
            {
                SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
                SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm");//设置日期格式
                String modedatacreatedate = sdfDate.format(new Date());
                String modedatacreatetime = sdfTime.format(new Date());
                String modedatacreatertype = "0";

                String syn_hrmmode_cubeuuid = pb.getPropName("UF_ORGSYNTIME_CUBEUUID");
                JSONObject jsonObject = pb.getModeInfoByUUID(syn_hrmmode_cubeuuid);
                int formmodeid = Util.getIntValue(jsonObject.getString("modeid")) ;
                bb.writeLog("formmodeid:"+formmodeid);

                int userid = 1;
                String modeuuid = UUID.randomUUID().toString();
                sql = " insert into uf_orgsyntime(tbrq,tbsj,formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime,modeuuid,bmtbsl,ryzgtbsl,ryjgtbsl)values\n" +
                        " ('"+modedatacreatedate+"','"+modedatacreatetime+"',"+formmodeid+","+userid+","+modedatacreatertype+",'"+modedatacreatedate+"','"+modedatacreatetime+"','"+modeuuid+"','"+dept_message+"','"+res_message+"','"+pos_message+"') ";
                bb.writeLog("sql:"+sql);
                boolean bool = rs.executeUpdate(sql);
                bb.writeLog("bool:"+bool);
                if(bool)
                {
                    rs.executeQuery( "select id from uf_orgsyntime where modeuuid='" + modeuuid + "'" );
                    while (rs.next())
                    {
                        int id = Util.getIntValue( rs.getString( "id" ) );
                        ModeRightInfo moderight = new ModeRightInfo();
                        moderight.editModeDataShare(userid, formmodeid, id );
                    }
                }
            }
        }
    }*/


    @GET
    @Path("/synorgall")
    @Produces({MediaType.TEXT_PLAIN})
    public String synOrgAll(@Context HttpServletRequest request, @Context HttpServletResponse response){

        JSONObject apidatas = new JSONObject();
        BaseBean bb = new BaseBean();
        //获取用户
        try {

            PropBean pb = new PropBean();
            OrganizationSyn organizationsynNew = new OrganizationSyn();
            OrganizationSynHrm organizationSynHrm = new OrganizationSynHrm();

            OrganizationSynPhoto organizationSynPhoto = new OrganizationSynPhoto();
            String type = Util.null2String(request.getParameter("type"));
            RecordSet rs = new RecordSet();
            try{
                if("1".equals(type) || "5".equals(type))
                {
                    String sql = " delete from hrmresource_allsyn " ;
                    boolean flag = rs.executeUpdate(sql);
                    if(flag){
                        sql = " insert into hrmresource_allsyn select * from hrmresource ";
                        rs.executeUpdate(sql);
                    }
                }
            }catch (Exception e){
                bb.writeLog("全量更新失败");
            }

            Map<String,String> headMap = new HashMap<String,String>();
            try {
                String token = organizationsynNew.getToken();
                bb.writeLog("token:"+token);
                headMap.put("JTXT-AUTH-TOKEN",token);
                headMap.put("JTXT-AUTH-TYPE","APP");
            }catch (Exception e){
                bb.writeLog("e1:"+e);
            }




            String dept_message = "" ;
            String res_message = "" ;
            String pos_message = "";
            if("1".equals(type) || "5".equals(type))
            {
                bb.writeLog("开始同步部门信息");
                String deptUrl = pb.getPropName("deptUrl_All");

                try{
                    organizationsynNew.doDeptSyn(deptUrl,headMap);
                    bb.writeLog("开始同步部门信息");

                    bb.writeLog("再开始同步部门信息");
                    Map<String,String> map = organizationsynNew.doDeptSyn(deptUrl,headMap);

                    String updatesuccesscount = map.get("updatesuccesscount");
                    String updateerrorcount = map.get("updateerrorcount");
                    String insertsuccesscount = map.get("insertsuccesscount");
                    String inserterrorcount = map.get("inserterrorcount");
                    String exceptioncount = map.get("exceptioncount");
                    String allcount = map.get("allcount");

                    String updatedefinesuccesscount = map.get("updatedefinesuccesscount");
                    String updatedefineerrorcount = map.get("updatedefineerrorcount");
                    String insertdefinesuccesscount = map.get("insertdefinesuccesscount");
                    String insertdefineerrorcount = map.get("insertdefineerrorcount");

                    dept_message = "部门同步总数:"+allcount+",新增成功:"+insertsuccesscount+",新增失败:"+inserterrorcount+",更新成功:"+updatesuccesscount+",新增自定义成功："+insertdefinesuccesscount+",新增自定义失败："+insertdefineerrorcount+",更新自定义成功："+updatedefinesuccesscount+",更新自定义失败："+updatedefineerrorcount+",更新失败:"+updateerrorcount+",更新异常:"+exceptioncount+";";

                    bb.writeLog("结束同步部门信息");
                }catch (Exception e){
                    bb.writeLog("e:"+e);
                }

                String deptAbsUrl = pb.getPropName("deptAbsUrl_All");
                try{
                    Map<String,String> map = organizationsynNew.doDeptAbSyn(deptAbsUrl,headMap);
                    String updatesuccesscount = map.get("updatesuccesscount");
                    String updateerrorcount = map.get("updateerrorcount");
                    String insertsuccesscount = map.get("insertsuccesscount");
                    String inserterrorcount = map.get("inserterrorcount");
                    String exceptioncount = map.get("exceptioncount");
                    String allcount = map.get("allcount");

                    String updatedefinesuccesscount = map.get("updatedefinesuccesscount");
                    String updatedefineerrorcount = map.get("updatedefineerrorcount");
                    String insertdefinesuccesscount = map.get("insertdefinesuccesscount");
                    String insertdefineerrorcount = map.get("insertdefineerrorcount");

                    dept_message += "虚拟部门同步总数:"+allcount+",新增成功:"+insertsuccesscount+",新增失败:"+inserterrorcount+",更新成功:"+updatesuccesscount+",新增自定义成功："+insertdefinesuccesscount+",新增自定义失败："+insertdefineerrorcount+",更新自定义成功："+updatedefinesuccesscount+",更新自定义失败："+updatedefineerrorcount+",更新失败:"+updateerrorcount+",更新异常:"+exceptioncount+";";


                }catch (Exception e){
                    bb.writeLog("e:"+e);
                }

                try{
                    bb.writeLog("deptUrl:"+deptUrl);
                    bb.writeLog("开始同步部门领导信息信息");
                    Map<String,String> map = organizationsynNew.doDeptSynToLeader(deptUrl,headMap);

                    String updatesuccesscount = map.get("updatesuccesscount");
                    String updateerrorcount = map.get("updateerrorcount");
                    String exceptioncount = map.get("exceptioncount");
                    String allcount = map.get("allcount");

                    dept_message += "部门领导同步总数:"+allcount+",更新成功:"+updatesuccesscount+",更新失败:"+updateerrorcount+",更新异常:"+exceptioncount+";";

                    bb.writeLog("结束同步部门领导信息信息");
                }catch (Exception e){
                    bb.writeLog("e:"+e);
                }

                try{
                    bb.writeLog("deptAbsUrl:"+deptAbsUrl);
                    bb.writeLog("开始同步虚拟部门领导信息信息");
                    Map<String,String> map = organizationsynNew.doDeptSynToLeader(deptAbsUrl,headMap);

                    String updatesuccesscount = map.get("updatesuccesscount");
                    String updateerrorcount = map.get("updateerrorcount");
                    String exceptioncount = map.get("exceptioncount");
                    String allcount = map.get("allcount");

                    dept_message += "虚拟部门领导同步总数:"+allcount+",更新成功:"+updatesuccesscount+",更新失败:"+updateerrorcount+",更新异常:"+exceptioncount+";";
                    bb.writeLog("结束同步虚拟部门领导信息信息");
                }catch (Exception e){
                    bb.writeLog("e:"+e);
                }
            }

            if("2".equals(type) || "5".equals(type)) {
                try {
                    bb.writeLog("开始同步人员信息");
                    String hrmUrl = pb.getPropName("hrmUrl_All");
                    organizationsynNew.updateBelongto();
                    Map<String, String> map2 = organizationSynHrm.doHrmSyn(hrmUrl, headMap);

                    String updatesuccesscount = map2.get("updatesuccesscount");
                    String updateerrorcount = map2.get("updateerrorcount");
                    String insertsuccesscount = map2.get("insertsuccesscount");
                    String inserterrorcount = map2.get("inserterrorcount");
                    String exceptioncount = map2.get("exceptioncount");
                    String allcount = map2.get("allcount");
                    String updatedefinesuccesscount = map2.get("updatedefinesuccesscount");
                    String updatedefineerrorcount = map2.get("updatedefineerrorcount");
                    String insertdefinesuccesscount = map2.get("insertdefinesuccesscount");
                    String insertdefineerrorcount = map2.get("insertdefineerrorcount");
                    String dataerrorcount = map2.get("dataerrorcount");
                    String dataiderroecount = map2.get("dataiderroecount");

                    res_message += "部门人员(主岗)总数:" + allcount + ",新增成功:" + insertsuccesscount + ",新增失败:" + inserterrorcount + ",更新成功:" + updatesuccesscount + ",更新失败:" + updateerrorcount + ",更新异常:" + exceptioncount + ",异常数据:" + dataerrorcount +
                            ",新增自定义成功:" + insertdefinesuccesscount + ",新增自定义失败" + insertdefineerrorcount + ",更新自定义成功：" + updatedefinesuccesscount + ",更新自定义失败:" + updatedefineerrorcount + ",更新数据ID不同数量:" + dataiderroecount +
                            ";";
                    bb.writeLog("结束同步人员信息");


                } catch (Exception e) {
                    bb.writeLog("e:" + e);
                }

                try {
                    String deptUrl = pb.getPropName("deptUrl_All");
                    bb.writeLog("deptUrl:" + deptUrl);
                    bb.writeLog("开始同步部门领导信息信息");
                    Map<String, String> map4 = organizationsynNew.doDeptSynToLeader(deptUrl, headMap);
                    String updatesuccesscount = map4.get("updatesuccesscount");
                    String updateerrorcount = map4.get("updateerrorcount");
                    String exceptioncount = map4.get("exceptioncount");
                    String allcount = map4.get("allcount");

                    res_message += "更新部门领导总数:" + allcount + ",更新成功:" + updatesuccesscount + ",更新失败:" + updateerrorcount + ",更新异常:" + exceptioncount + ";";
                    bb.writeLog("结束同步部门领导信息信息");
                } catch (Exception e) {
                    bb.writeLog("e:" + e);
                }

                try {
                    bb.writeLog("开始设置控件不检测");
                    organizationsynNew.setSysActivexCheck();
                    bb.writeLog("结束设置控件不检测");
                } catch (Exception e) {
                    bb.writeLog("e:" + e);
                }

                try {
                    bb.writeLog("结束设置节点未操作者显示类型");
                    organizationsynNew.setWorkflowUserDefault();
                    bb.writeLog("结束设置节点未操作者显示类型");
                } catch (Exception e) {
                    bb.writeLog("e:" + e);
                }

                try {
                    OrganizationSynUtil OrganizationSynUtil = new OrganizationSynUtil();
                    bb.writeLog("开始更新出错的兼岗");
                    OrganizationSynUtil.updatePositionStatus();
                    bb.writeLog("结束更新出错的兼岗");
                } catch (Exception e) {
                    bb.writeLog("SynOrgAction-Exception-:" + e);
                }
            }

            if("3".equals(type) || "5".equals(type))
            {
                try{
                    bb.writeLog("开始同步岗位信息");
                    String jobUrl = pb.getPropName("jobUrl_All");
                    organizationsynNew.updateBelongto();
                    Map<String,String> map3 = organizationSynHrm.doPositionSyn(jobUrl,headMap);
                    String updatesuccesscount = map3.get("updatesuccesscount");
                    String updateerrorcount = map3.get("updateerrorcount");
                    String exceptioncount = map3.get("exceptioncount");
                    String insertsuccesscount = map3.get("insertsuccesscount");
                    String inserterrorcount = map3.get("inserterrorcount");
                    String dataerrorcount = map3.get("dataerrorcount");
                    String updatedefinesuccesscount = map3.get("updatedefinesuccesscount");
                    String updatedefineerrorcount = map3.get("updatedefineerrorcount");
                    String insertdefinesuccesscount = map3.get("insertdefinesuccesscount");
                    String insertdefineerrorcount = map3.get("insertdefineerrorcount");
                    String nullcount = map3.get("nullcount");
                    String databelongtoerrorcount = map3.get("databelongtoerrorcount");
                    String dataiderrorcount = map3.get("dataiderrorcount");

                    pos_message += "部门人员(兼岗)新增成功:"+insertsuccesscount+",新增失败:"+inserterrorcount+",更新成功:"+updatesuccesscount+",更新失败:"+updateerrorcount+",更新异常:"+exceptioncount+",异常数据:"+dataerrorcount+",无效数据:"+nullcount+
                                   ",新增自定义成功:"+insertdefinesuccesscount+",新增自定义失败"+insertdefineerrorcount+",更新自定义成功："+updatedefinesuccesscount+",更新自定义失败:"+updatedefineerrorcount+",获取主岗为空数量:"+databelongtoerrorcount+",更新ID不同数据："+dataiderrorcount+";";


                    try{
                        OrganizationSynUtil OrganizationSynUtil = new OrganizationSynUtil();
                        bb.writeLog("开始更新兼岗从主岗同步的信息");
                        OrganizationSynUtil.updatePositionInfo();
                        bb.writeLog("开始更新兼岗从主岗同步的信息");
                    }catch (Exception e){
                        bb.writeLog("OrganizationSynTime-Exception-:"+e);
                    }

                    bb.writeLog("结束同步岗位信息");
                }catch (Exception e){
                    bb.writeLog("e:"+e);
                }

                try{
                    String deptUrl = pb.getPropName("deptUrl_All");
                    bb.writeLog("deptUrl:"+deptUrl);
                    bb.writeLog("开始同步部门领导信息信息");
                    Map<String,String> map4 = organizationsynNew.doDeptSynToLeader(deptUrl,headMap);

                    String updatesuccesscount = map4.get("updatesuccesscount");
                    String updateerrorcount = map4.get("updateerrorcount");
                    String exceptioncount = map4.get("exceptioncount");
                    String allcount = map4.get("allcount");
                    res_message += "更新部门领导总数:"+allcount+",更新成功:"+updatesuccesscount+",更新失败:"+updateerrorcount+",更新异常:"+exceptioncount+";";
                    bb.writeLog("结束同步部门领导信息信息");
                }catch (Exception e){
                    bb.writeLog("e:"+e);
                }

                try{
                    bb.writeLog("开始设置控件不检测");
                    organizationsynNew.setSysActivexCheck();
                    bb.writeLog("结束设置控件不检测");
                }catch (Exception e){
                    bb.writeLog("e:"+e);
                }

                try {
                    OrganizationSynUtil OrganizationSynUtil = new OrganizationSynUtil();
                    bb.writeLog("开始更新出错的兼岗");
                    OrganizationSynUtil.updatePositionStatus();
                    bb.writeLog("结束更新出错的兼岗");
                } catch (Exception e) {
                    bb.writeLog("SynOrgAction-Exception-:" + e);

                }
            }

            if("12".equals(type))
            {
                try{
                    bb.writeLog("开始同步岗位信息");
                    String jobAbsUrl = pb.getPropName("jobAbsUrl_All");

                    Map<String,String> map3 = organizationSynHrm.doAbsPositionSyn(jobAbsUrl,headMap);
                    String updatesuccesscount = map3.get("updatesuccesscount");
                    String updateerrorcount = map3.get("updateerrorcount");
                    String exceptioncount = map3.get("exceptioncount");
                    String insertsuccesscount = map3.get("insertsuccesscount");
                    String inserterrorcount = map3.get("inserterrorcount");
                    String dataerrorcount = map3.get("dataerrorcount");
                    String updatedefinesuccesscount = map3.get("updatedefinesuccesscount");
                    String updatedefineerrorcount = map3.get("updatedefineerrorcount");
                    String insertdefinesuccesscount = map3.get("insertdefinesuccesscount");
                    String insertdefineerrorcount = map3.get("insertdefineerrorcount");
                    String nullcount = map3.get("nullcount");

                    pos_message += "虚拟部门人员(兼岗)新增成功:"+insertsuccesscount+",新增失败:"+inserterrorcount+",更新成功:"+updatesuccesscount+",更新失败:"+updateerrorcount+",更新异常:"+exceptioncount+",异常数据:"+dataerrorcount+",无效数据:"+nullcount+
                                   ",新增自定义成功:"+insertdefinesuccesscount+",新增自定义失败"+insertdefineerrorcount+",更新自定义成功："+updatedefinesuccesscount+",更新自定义失败:"+updatedefineerrorcount+";";


                }catch (Exception e){
                    bb.writeLog("e:"+e);
                }

            }

            if("4".equals(type) || "5".equals(type)){
                bb.writeLog("开始同步部门矩阵信息");
                organizationsynNew.doMatrixInfo();
                bb.writeLog("结束同步部门矩阵信息");
            }

            if("10".equals(type)){
                bb.writeLog("开始同步人员头像");
                String hrmUrl = pb.getPropName("hrmUrl_All");
                organizationSynPhoto.doHrmSynPhoto(hrmUrl,headMap);
                bb.writeLog("结束同步人员头像");
            }

            if("11".equals(type))
            {
                bb.writeLog("开始同步待办显示定制列");
                organizationsynNew.updateColList("");
                bb.writeLog("结束同步待办显示定制列");
            }


            // 增量同步
            try{
                if("7".equals(type) || "9".equals(type))
                {
                    String sql = " delete from hrmresource_allsyn " ;
                    boolean flag = rs.executeUpdate(sql);
                    if(flag){
                        sql = " insert into hrmresource_allsyn select * from hrmresource ";
                        rs.executeUpdate(sql);
                    }
                }
            }catch (Exception e){
                bb.writeLog("全量更新失败");
            }
            OrganizationSynTime OrganizationSynTime = new OrganizationSynTime();
            OrganizationTimeSynHrm OrganizationTimeSynHrm = new OrganizationTimeSynHrm();
            OrganizationTimeSynDept OrganizationTimeSynDept = new OrganizationTimeSynDept();
            String createTimeGreater = "";
            String sql = "select to_char(sysdate-10,'yyyy-mm-dd') as nowdate from dual";
            rs.execute(sql);
            if(rs.next()){
                createTimeGreater = Util.null2String(rs.getString("nowdate"));
            }
            bb.writeLog("createTimeGreater:"+createTimeGreater);
            if(!"".equalsIgnoreCase(createTimeGreater)){
                createTimeGreater = createTimeGreater+"T00:00:00" ;

                if("6".equals(type) || "9".equals(type)){
                    bb.writeLog("开始同步部门信息");
                    String deptUrl = pb.getPropName("deptUrl_Inc");
                    Map<String,String> map = OrganizationTimeSynDept.doDeptSyn(deptUrl,createTimeGreater,headMap);

                    String updatesuccesscount = map.get("updatesuccesscount");
                    String updateerrorcount = map.get("updateerrorcount");
                    String insertsuccesscount = map.get("insertsuccesscount");
                    String inserterrorcount = map.get("inserterrorcount");
                    String exceptioncount = map.get("exceptioncount");
                    String allcount = map.get("allcount");

                    String updatedefinesuccesscount = map.get("updatedefinesuccesscount");
                    String updatedefineerrorcount = map.get("updatedefineerrorcount");
                    String insertdefinesuccesscount = map.get("insertdefinesuccesscount");
                    String insertdefineerrorcount = map.get("insertdefineerrorcount");
                    dept_message = "部门同步（增量）总数:"+allcount+",新增成功:"+insertsuccesscount+",新增失败:"+inserterrorcount+",更新成功:"+updatesuccesscount+",新增自定义成功："+insertdefinesuccesscount+",新增自定义失败："+insertdefineerrorcount+",更新自定义成功："+updatedefinesuccesscount+",更新自定义失败："+updatedefineerrorcount+",更新失败:"+updateerrorcount+",更新异常:"+exceptioncount+";";

                    bb.writeLog("开始同步部门信息");
                }

                if("7".equals(type) || "9".equals(type))
                {
                    try {
                        bb.writeLog("开始同步人员信息");
                        String hrmUrl = pb.getPropName("hrmUrl_Inc");
                        OrganizationSynTime.updateBelongto();
                        Map<String,String> map = OrganizationTimeSynHrm.doHrmSyn(hrmUrl,createTimeGreater,headMap);
                        String updatesuccesscount = map.get("updatesuccesscount");
                        String updateerrorcount = map.get("updateerrorcount");
                        String insertsuccesscount = map.get("insertsuccesscount");
                        String inserterrorcount = map.get("inserterrorcount");
                        String exceptioncount = map.get("exceptioncount");
                        String allcount = map.get("allcount");
                        String dataerrorcount = map.get("dataerrorcount");

                        res_message = "人员同步(增量)(主岗)总数:"+allcount+",新增成功:"+insertsuccesscount+",新增失败:"+inserterrorcount+",更新成功:"+updatesuccesscount+",更新失败:"+updateerrorcount+",更新异常:"+exceptioncount+",异常数据:"+dataerrorcount;
                        bb.writeLog("结束同步人员信息");

                        bb.writeLog("结束同步人员信息");
                    }catch (Exception e){
                        bb.writeLog("e:"+e);
                    }

                    try{
                        String deptUrl = pb.getPropName("deptUrl_Inc");
                        bb.writeLog("开始同步部门领导信息信息");
                        Map<String,String> map4 = OrganizationSynTime.doDeptSynToLeader(deptUrl,createTimeGreater,headMap);
                        String updatesuccesscount = map4.get("updatesuccesscount");
                        String updateerrorcount = map4.get("updateerrorcount");
                        String exceptioncount = map4.get("exceptioncount");
                        String allcount = map4.get("allcount");

                        res_message += "更新部门领导总数:"+allcount+",更新成功:"+updatesuccesscount+",更新失败:"+updateerrorcount+",更新异常:"+exceptioncount+";";
                        bb.writeLog("结束同步部门领导信息信息");
                    }catch (Exception e){
                        bb.writeLog("e:"+e);
                    }

                    try {
                        OrganizationSynUtil OrganizationSynUtil = new OrganizationSynUtil();
                        bb.writeLog("开始更新出错的兼岗");
                        OrganizationSynUtil.updatePositionStatus();
                        bb.writeLog("结束更新出错的兼岗");
                    } catch (Exception e) {
                        bb.writeLog("SynOrgAction-Exception-:" + e);
                    }

                }

                if("8".equals(type) || "9".equals(type))
                {
                    try{
                        bb.writeLog("开始同步岗位信息");
                        OrganizationSynTime.updateBelongto();
                        String jobUrl = pb.getPropName("jobUrl_Inc");
                        Map<String,String> map = OrganizationTimeSynHrm.doPositionSyn(jobUrl,createTimeGreater,headMap);
                        String updatesuccesscount = map.get("updatesuccesscount");
                        String updateerrorcount = map.get("updateerrorcount");
                        String insertsuccesscount = map.get("insertsuccesscount");
                        String inserterrorcount = map.get("inserterrorcount");
                        String exceptioncount = map.get("exceptioncount");
                        String allcount = map.get("allcount");
                        String dataerrorcount = map.get("dataerrorcount");
                        pos_message = "人员同步(增量)(兼岗)总数:"+allcount+",新增成功:"+insertsuccesscount+",新增失败:"+inserterrorcount+",更新成功:"+updatesuccesscount+",更新失败:"+updateerrorcount+",更新异常:"+exceptioncount+",异常数据:"+dataerrorcount;

                        try{
                            OrganizationSynUtil OrganizationSynUtil = new OrganizationSynUtil();
                            bb.writeLog("开始更新兼岗从主岗同步的信息");
                            OrganizationSynUtil.updatePositionInfo();
                            bb.writeLog("开始更新兼岗从主岗同步的信息");
                        }catch (Exception e){
                            bb.writeLog("OrganizationSynTime-Exception-:"+e);
                        }

                        bb.writeLog("结束同步岗位信息");
                    }catch (Exception e){
                        bb.writeLog("e:"+e);
                    }

                    try{
                        String deptUrl = pb.getPropName("deptUrl_Inc");
                        bb.writeLog("开始同步部门领导信息信息");
                        OrganizationSynTime.doDeptSynToLeader(deptUrl,createTimeGreater,headMap);
                        bb.writeLog("结束同步部门领导信息信息");
                    }catch (Exception e){
                        bb.writeLog("e:"+e);
                    }

                    try {
                        OrganizationSynUtil OrganizationSynUtil = new OrganizationSynUtil();
                        bb.writeLog("开始更新出错的兼岗");
                        OrganizationSynUtil.updatePositionStatus();
                        bb.writeLog("结束更新出错的兼岗");
                    } catch (Exception e) {
                        bb.writeLog("SynOrgAction-Exception-:" + e);
                    }
                }
            }

            organizationsynNew.removeCache();


            SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            SimpleDateFormat sdfTime = new SimpleDateFormat("HH:mm");//设置日期格式
            String modedatacreatedate = sdfDate.format(new Date());
            String modedatacreatetime = sdfTime.format(new Date());
            String modedatacreatertype = "0";

            String syn_hrmmode_cubeuuid = pb.getPropName("UF_ORGSYNTIME_CUBEUUID");
            JSONObject jsonObject = pb.getModeInfoByUUID(syn_hrmmode_cubeuuid);
            int formmodeid = Util.getIntValue(jsonObject.getString("modeid")) ;
            bb.writeLog("formmodeid:"+formmodeid);

            int userid = 1;
            String modeuuid = UUID.randomUUID().toString();
            sql = " insert into uf_orgsyntime(tbrq,tbsj,formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime,modeuuid,bmtbsl,ryzgtbsl,ryjgtbsl)values\n" +
                    " ('"+modedatacreatedate+"','"+modedatacreatetime+"',"+formmodeid+","+userid+","+modedatacreatertype+",'"+modedatacreatedate+"','"+modedatacreatetime+"','"+modeuuid+"','"+dept_message+"','"+res_message+"','"+pos_message+"') ";
            bb.writeLog("sql:"+sql);
            boolean bool = rs.executeUpdate(sql);
            bb.writeLog("bool:"+bool);
            if(bool)
            {
                rs.executeQuery( "select id from uf_orgsyntime where modeuuid='" + modeuuid + "'" );
                while (rs.next()) {
                    int id = Util.getIntValue( rs.getString( "id" ) );
                    ModeRightInfo moderight = new ModeRightInfo();
                    moderight.editModeDataShare(userid, formmodeid, id );
                }
            }

//            JSONObject dataJson = new JSONObject();
//            dataJson.put("dept",map1);
//            dataJson.put("hrm",map2);
//            dataJson.put("job",map3);
//            dataJson.put("hrmleader",map4);

            apidatas.put("flag","0");
//            apidatas.put("data",dataJson);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("status", "0");
            apidatas.put("resultMsg","数据异常");
        }
        return apidatas.toJSONString();
    }


}
