package weaver.interfaces.zjrb.job;

import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSetDataSource;
import weaver.general.BaseBean;

/**
 * @Author: shil
 * @Date: 2024/8/21
 * @Time: 17:23
 * @Desc: 浙江人保组织架构同步，从E9同步到E7
 *        需要注意的是所有的表的外键都是id而不是code，在每次同步结束后
 *        要根据原来的id转成code，再到当前E9中转成现在的id
 * @Version: 1.0
 */

public class HrmSynDaoTime_E92E7 {

    public void syn(int days,String dataResource){

        // 备份数据

        BaseBean bb = new BaseBean();
        HrmSynSubcompanyUtil hrmSynSubcompanyUtil = new HrmSynSubcompanyUtil();
        HrmSynDepartmentUtil hrmSynDepartmentUtil = new HrmSynDepartmentUtil();
        HrmSynJobGroupUtil hrmSynJobGroupUtil = new HrmSynJobGroupUtil();
        HrmSynJobActivitiesUtil hrmSynJobActivitiesUtil = new HrmSynJobActivitiesUtil();
        HrmSynJobTitlesUtil hrmSynJobTitlesUtil = new HrmSynJobTitlesUtil();
        HrmSynResourceUtil hrmSynResourceUtil = new HrmSynResourceUtil();

        try {
            backDataBaseData(dataResource,"1");
            String subcompanyWhere = " and to_char(created,'yyyy-mm-dd')>=to_char(sysdate-"+days+",'yyyy-mm-dd') or to_char(modified,'yyyy-mm-dd')>=to_char(sysdate-"+days+",'yyyy-mm-dd') ";
            String mainid = hrmSynSubcompanyUtil.SynHrmSubcompanyData(dataResource,subcompanyWhere);
            if(StringUtils.isNotBlank(mainid)){
                backDataBaseData(dataResource,"2");
                String departmentwhere = " and to_char(created,'yyyy-mm-dd')>=to_char(sysdate-"+days+",'yyyy-mm-dd') or to_char(modified,'yyyy-mm-dd')>=to_char(sysdate-"+days+",'yyyy-mm-dd') ";
                hrmSynDepartmentUtil.SynHrmDepartmentData(dataResource,mainid,departmentwhere);

                backDataBaseData(dataResource,"3");
                String jobgroupWhere = " and to_char(created,'yyyy-mm-dd')>=to_char(sysdate-"+days+",'yyyy-mm-dd') or to_char(modified,'yyyy-mm-dd')>=to_char(sysdate-"+days+",'yyyy-mm-dd') ";
                hrmSynJobGroupUtil.SynHrmJobGroupData(dataResource,mainid,jobgroupWhere);

                backDataBaseData(dataResource,"4");
                String jobactivitiesWhere  = " and to_char(created,'yyyy-mm-dd')>=to_char(sysdate-"+days+",'yyyy-mm-dd') or to_char(modified,'yyyy-mm-dd')>=to_char(sysdate-"+days+",'yyyy-mm-dd') ";
                hrmSynJobActivitiesUtil.SynHrmJobActivitiesData(dataResource,mainid,jobactivitiesWhere);

                backDataBaseData(dataResource,"5");
                String jobTitlesWhere = " and to_char(created,'yyyy-mm-dd')>=to_char(sysdate-"+days+",'yyyy-mm-dd') or to_char(modified,'yyyy-mm-dd')>=to_char(sysdate-"+days+",'yyyy-mm-dd') ";
                hrmSynJobTitlesUtil.SynHrmJobTitlesData(dataResource,mainid,jobTitlesWhere);

                backDataBaseData(dataResource,"6");
                String hrmresourceWhere = " and to_char(created,'yyyy-mm-dd')>=to_char(sysdate-"+days+",'yyyy-mm-dd') or to_char(modified,'yyyy-mm-dd')>=to_char(sysdate-"+days+",'yyyy-mm-dd') ";
                hrmSynResourceUtil.SynHrmresourceData(dataResource,mainid,hrmresourceWhere);
            }
        }catch (Exception e){
            bb.writeLog(e);
        }
    }


    public void allSyn(String dataResource){

        // 备份数据
        BaseBean bb = new BaseBean();
        HrmSynSubcompanyUtil hrmSynSubcompanyUtil = new HrmSynSubcompanyUtil();
        HrmSynDepartmentUtil hrmSynDepartmentUtil = new HrmSynDepartmentUtil();
        HrmSynJobGroupUtil hrmSynJobGroupUtil = new HrmSynJobGroupUtil();
        HrmSynJobActivitiesUtil hrmSynJobActivitiesUtil = new HrmSynJobActivitiesUtil();
        HrmSynJobTitlesUtil hrmSynJobTitlesUtil = new HrmSynJobTitlesUtil();
        HrmSynResourceUtil hrmSynResourceUtil = new HrmSynResourceUtil();

        try {
            backDataBaseData(dataResource,"1");
            String mainid = hrmSynSubcompanyUtil.SynHrmSubcompanyData(dataResource,"");
            if(StringUtils.isNotBlank(mainid)){
                backDataBaseData(dataResource,"2");
                hrmSynDepartmentUtil.SynHrmDepartmentData(dataResource,mainid,"");
                backDataBaseData(dataResource,"3");
                hrmSynJobGroupUtil.SynHrmJobGroupData(dataResource,mainid,"");
                backDataBaseData(dataResource,"4");
                hrmSynJobActivitiesUtil.SynHrmJobActivitiesData(dataResource,mainid,"");
                backDataBaseData(dataResource,"5");
                hrmSynJobTitlesUtil.SynHrmJobTitlesData(dataResource,mainid,"");
                backDataBaseData(dataResource,"6");
                hrmSynResourceUtil.SynHrmresourceData(dataResource,mainid,"");
            }
        }catch (Exception e){
            bb.writeLog(e);
        }
    }


    public void backDataBaseData(String dataResource,String type){
        BaseBean bb = new BaseBean();
        RecordSetDataSource rsd = new RecordSetDataSource(dataResource);
        if("1".equals(type)){
            try {
                String sql  = " delete from hrmsubcompany_bakall ";
                boolean isTrue = rsd.execute(sql);
                if(isTrue){
                    bb.writeLog("删除分部备份表数据成功");
                    sql = " INSERT INTO HRMSUBCOMPANY_bakall \n" +
                          " SELECT * FROM HRMSUBCOMPANY ";
                    boolean isTrue2 = rsd.execute(sql);
                    if(isTrue2){
                        bb.writeLog("备份分部表数据成功");
                    }
                }
            }catch (Exception e){
                bb.writeLog("hrmsubcompany_bakall-Exception");
            }
        }

        if("2".equals(type)){
            try {
                String sql  = " delete from HRMDEPARTMENT_bakall ";
                boolean isTrue = rsd.execute(sql);
                if(isTrue){
                    bb.writeLog("删除部门备份表数据成功");
                    sql = " INSERT INTO HRMDEPARTMENT_bakall \n" +
                          " SELECT * FROM HRMDEPARTMENT h  ";
                    boolean isTrue2  = rsd.execute(sql);
                    if(isTrue2){
                        bb.writeLog("备份部门表数据成功");
                    }
                }
            }catch (Exception e){
                bb.writeLog("HRMDEPARTMENT_bakall-Exception");
            }
        }


        if("3".equals(type)){
            try {
                String sql  = " delete from HRMJOBGROUPS_BAKALL ";
                boolean isTrue = rsd.execute(sql);
                if(isTrue){
                    bb.writeLog("删除人员备份表数据成功");
                    sql = " INSERT INTO HRMJOBGROUPS_BAKALL \n" +
                          " SELECT * FROM HRMJOBGROUPS ";
                    boolean isTrue2  = rsd.execute(sql);
                    if(isTrue2){
                        bb.writeLog("备份人员表数据成功");
                    }
                }
            }catch (Exception e){
                bb.writeLog("HRMJOBGROUPS_BAKALL-Exception");
            }
        }


        if("4".equals(type)){
            try {
                String sql  = " delete from HRMJOBACTIVITIES_BAKALL ";
                boolean isTrue = rsd.execute(sql);
                if(isTrue){
                    bb.writeLog("删除人员备份表数据成功");
                    sql = " INSERT INTO HRMJOBACTIVITIES_BAKALL \n" +
                          " SELECT * FROM HRMJOBACTIVITIES ";
                    boolean isTrue2  = rsd.execute(sql);
                    if(isTrue2){
                        bb.writeLog("备份人员表数据成功");
                    }
                }
            }catch (Exception e){
                bb.writeLog("HRMJOBGROUPS_BAKALL-Exception");
            }
        }

        if("5".equals(type)){
            try {
                String sql  = " delete from HRMJOBTITLES_BAKALL ";
                boolean isTrue = rsd.execute(sql);
                if(isTrue){
                    bb.writeLog("删除人员备份表数据成功");
                    sql = " INSERT INTO HRMJOBTITLES_BAKALL \n" +
                          " SELECT * FROM HRMJOBTITLES ";
                    boolean isTrue2  = rsd.execute(sql);
                    if(isTrue2){
                        bb.writeLog("备份人员表数据成功");
                    }
                }
            }catch (Exception e){
                bb.writeLog("HRMJOBTITLES_BAKALL-Exception");
            }
        }

        if("6".equals(type)){
            try {
                String sql  = " delete from HRMRESOURCE_bakall ";
                boolean isTrue = rsd.execute(sql);
                if(isTrue){
                    bb.writeLog("删除人员备份表数据成功");
                    sql = " INSERT INTO HRMRESOURCE_bakall \n" +
                          " SELECT * FROM HRMRESOURCE ";
                    boolean isTrue2  = rsd.execute(sql);
                    if(isTrue2){
                        bb.writeLog("备份人员表数据成功");
                    }
                }
            }catch (Exception e){

            }
        }

    }

}
