package com.muzhi.dingtalk.test.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiSmartworkHrmEmployeeListRequest;
import com.dingtalk.api.request.OapiSmartworkHrmEmployeeQuerydimissionRequest;
import com.dingtalk.api.request.OapiSmartworkHrmEmployeeQueryonjobRequest;
import com.dingtalk.api.response.OapiSmartworkHrmEmployeeListResponse;
import com.dingtalk.api.response.OapiSmartworkHrmEmployeeQuerydimissionResponse;
import com.dingtalk.api.response.OapiSmartworkHrmEmployeeQueryonjobResponse;
import com.muzhi.dingtalk.test.entity.EmployeeFangDe;
import com.muzhi.dingtalk.test.entity.vo.EmployeeFangDeDataVo;
import com.muzhi.dingtalk.test.entity.vo.EmployeeFangDeVo;
import com.muzhi.dingtalk.test.mapper.EmployeeFangDeMapper;
import com.muzhi.dingtalk.test.service.FangDeTongbuService;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;


@Service
//@WebService
public class FangDeTongbuServiceImpl implements FangDeTongbuService {

    @Autowired
    private FangDeDingAccessTokenServiceImpl fangDeDingAccessTokenService;

    @Autowired
    private EmployeeFangDeMapper employeeFangDeMapper;

    //在职员工花名册变量
    public static String huamingce = null;
    //离职员工花名册变量
    public static String huamingce2 = null;
    //离职员工信息变量【根据离职员工id列表接口获取的数据放到离职员工信息接口查询获取到相关离职员工信息，而非花名册字段信息】
    public static String huamingce3 = null;

    //在职员工id变量
    public static String employee1 = null;
    //离职员工id变量【从离职员工id列表接口中获取】
    public static String employee2 = null;


    //【测试！！！】获取房德钉钉中的花名册数据并解析成json格式数据返回【在职】
    @Override
    public EmployeeFangDeDataVo<EmployeeFangDeVo> getFangDeHuamingceTest(){

        //---*** 执行同步在职员工信息 ***---//
        //---*** 执行同步在职员工信息 ***---//
        //---*** 执行同步在职员工信息 ***---//

        //在职员工id列表的循环获取
        DingTalkClient client1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/queryonjob");
        OapiSmartworkHrmEmployeeQueryonjobRequest request1 = new OapiSmartworkHrmEmployeeQueryonjobRequest();

        //企业在职员工userid列表
        int offset = 0;//分页游标，从0开始
        int size = 50;//分页大小，最大50

        JSONObject result1;

        EmployeeFangDeDataVo employeeFangDeDataVo = new EmployeeFangDeDataVo();

        List<EmployeeFangDeVo> employeeFangDeVoList = new ArrayList<>();

//        do {
            List<String> list = new ArrayList<>();

            System.out.println("测试分页值：" + offset);

            try {
                request1.setStatusList("2,3,5,-1");
                request1.setOffset(0L);
                request1.setSize(2L);
                OapiSmartworkHrmEmployeeQueryonjobResponse response1 = client1.execute(request1, fangDeDingAccessTokenService.getFangDeAccessToken());
                employee1 = response1.getBody();
                System.out.println(response1.getBody());
            } catch (ApiException e) {
                e.printStackTrace();
            }

            //通过fastjson工具转换成json对象,逐层解析
            JSONObject jsonObject1 = JSON.parseObject(employee1);
            result1 = jsonObject1.getJSONObject("result");

            String dataList = result1.getString("data_list");

            //将String数组进行处理，去掉所有的双引号（""）和中括号（[]）
            String dataRes = dataList.replace("\"", "").
                    replaceAll("\\[", "").
                    replaceAll("\\]", "");

            list.add(dataRes);

            String userid1 = list.toString().replace("\"", "").
                    replaceAll("\\[", "").
                    replaceAll("\\]", "");


            //TODO ---原始同步花名册数据至数据库--- //
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/list");
            OapiSmartworkHrmEmployeeListRequest request = new OapiSmartworkHrmEmployeeListRequest();

            try {
//                request.setUseridList(userid1);
                request.setUseridList("01021809444938499292,124860395138328065,304728645226168038,045256626236441308,01021809444027753566,01021809443838698843,01021809444536724928");

                //需要获取的花名册字段信息
//                request.setFieldFilterList("sys00-name,145d3425-9dea-42a6-88ae-1e4e3590046c,473f9667-d220-426f-8431-56be1e8a027d," +
//                        "395840c1-2cbe-4f36-b3fb-249d7ddf42f2,sys00-confirmJoinTime,sys02-sexType,sys02-certNo,sys01-employeeType," +
//                        "sys04-accountBank,sys04-bankAccountNo,ed21c116-efcb-49f3-a507-f8474450260c,sys01-employeeStatus");

                OapiSmartworkHrmEmployeeListResponse response = client.execute(request, fangDeDingAccessTokenService.getFangDeAccessToken());
                huamingce = response.getBody();
                System.out.println(response.getBody());
            }catch (ApiException e){
                e.printStackTrace();
            }

            //通过fastjson工具转换成json对象,逐层解析
            //将String类型的花名册json数据转换为Object数据对象
            JSONObject jsonObject = JSON.parseObject(huamingce);

            //直接将花名册对象通过Object转换成Array数组格式
            JSONArray resultArray = jsonObject.getJSONArray("result");





            for (int i = 0; i<resultArray.size(); i++){
                JSONArray fieldListArray = resultArray
                        .getJSONObject(i).getJSONArray("field_list");

                String userid = resultArray.getJSONObject(i).getString("userid");
                System.out.println("员工id：" + userid);

                //new一个employeeFangDeVo对象
                EmployeeFangDeVo employeeFangDeVo = new EmployeeFangDeVo();

                //将result的每个对象的id放入employee实体类的code字段中
                employeeFangDeVo.setCode(userid);
                employeeFangDeVo.setIdtype("身份证");
                employeeFangDeVo.setBanktype("工资卡");
                employeeFangDeVo.setStatus(1);

                for (int a = 0; a<fieldListArray.size(); a++){
                    String fieldCode = fieldListArray
                            .getJSONObject(a)
                            .getString("field_code");

                    switch (fieldCode){
                        //姓名
                        case "sys00-name":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***《《《姓名》》》***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employeeFangDeVo.setName(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employeeFangDeVo.setName("");
                            }
                            break;

                        //合同公司（业务单元名称）
                        case "145d3425-9dea-42a6-88ae-1e4e3590046c":
                            if (fieldListArray.getJSONObject(a).getString("label") != null || fieldListArray.getJSONObject(a).getString("label") != ""){
                                System.out.println("***合同公司（业务单元名称）***-->" + fieldListArray.getJSONObject(a).getString("label"));

                                employeeFangDeVo.setOrgname(fieldListArray.getJSONObject(a).getString("label"));
                                employeeFangDeVo.setIndutyorgname(fieldListArray.getJSONObject(a).getString("label"));
                            }
                            else{
                                employeeFangDeVo.setOrgname("");
                                employeeFangDeVo.setIndutyorgname("");
                            }
                            break;

                        //人员成本分摊一级部门
                        case "473f9667-d220-426f-8431-56be1e8a027d":
                            if (fieldListArray.getJSONObject(a).getString("label") != null){
                                System.out.println("***人员成本分摊一级部门***-->" + fieldListArray.getJSONObject(a).getString("label"));

                                employeeFangDeVo.setDef1(fieldListArray.getJSONObject(a).getString("label"));
                            }
                            else{
                                employeeFangDeVo.setDef1("");
                            }
                            break;

                        //人员成本分摊二级部门
                        case "395840c1-2cbe-4f36-b3fb-249d7ddf42f2":
                            if (fieldListArray.getJSONObject(a).getString("label") != null){
                                System.out.println("***人员成本分摊二级部门***-->" + fieldListArray.getJSONObject(a).getString("label"));

                                employeeFangDeVo.setDeptname(fieldListArray.getJSONObject(a).getString("label"));
                            }
                            else{
                                employeeFangDeVo.setDeptname("");
                            }
                            break;

                        //入职时间
                        case "sys00-confirmJoinTime":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***入职时间***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employeeFangDeVo.setIndutydate(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employeeFangDeVo.setIndutydate("");
                            }
                            break;

                        //性别
                        case "sys02-sexType":
                            if (fieldListArray.getJSONObject(a).getString("label") != null){
                                System.out.println("***性别***-->" + fieldListArray.getJSONObject(a).getString("label"));
//                            String sex = fieldListArray.getJSONObject(a).getInteger("value") == 0 ?"男":"女";
//                            employee.setSys02SexType(sex);
                                employeeFangDeVo.setSex(fieldListArray.getJSONObject(a).getString("label"));
                            }
                            else{
                                employeeFangDeVo.setSex(null);
                            }
                            break;

                        //证件号码
                        case "sys02-certNo":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***证件号码***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employeeFangDeVo.setId(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employeeFangDeVo.setId("");
                            }
                            break;

                        //员工类别
                        case "8b655cff-d575-4742-bbd4-ddc9fd4b5acf":
                            if (fieldListArray.getJSONObject(a).getString("label") != null){
                                System.out.println("***员工类型***-->" + fieldListArray.getJSONObject(a).getString("label"));

                                employeeFangDeVo.setPsnclname(fieldListArray.getJSONObject(a).getString("label"));
                            }
                            else{
                                employeeFangDeVo.setPsnclname(null);
                            }
                            break;

                        //开户行
                        case "sys04-accountBank":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***开户行***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employeeFangDeVo.setBank(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employeeFangDeVo.setBank("");
                            }
                            break;

                        //银行卡号
                        case "sys04-bankAccountNo":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***银行卡号***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employeeFangDeVo.setBanknum(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employeeFangDeVo.setBanknum("");
                            }
                            break;

                        //户名
                        case "ed21c116-efcb-49f3-a507-f8474450260c":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***户名***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employeeFangDeVo.setBankname(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employeeFangDeVo.setBankname("");
                            }
                            break;
                    }
                }
                employeeFangDeVoList.add(employeeFangDeVo);
            }
            //TODO ---原始同步花名册数据至数据库--- //


            employeeFangDeDataVo.setData(employeeFangDeVoList);

//            if (result1.getString("next_cursor") != null || result1.getString("data_list") != "") {
//
//                offset = offset + size;
//
//            }

//        } while (result1.getString("next_cursor") != null);


        return employeeFangDeDataVo;
    }


    //【测试！！！】获取房德钉钉中的离职人员信息属于最后工作日为：2021-02-18 00:00:00的人员id【离职】
    @Override
    public String getFangDeHuamingceLiZhiIdTest(){

        //---*** 执行同步离职员工信息 ***---//
        //---*** 执行同步离职员工信息 ***---//
        //---*** 执行同步离职员工信息 ***---//

        //在职员工id列表的循环获取
        DingTalkClient client1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/querydimission");
        OapiSmartworkHrmEmployeeQueryonjobRequest request1 = new OapiSmartworkHrmEmployeeQueryonjobRequest();

        //企业在职员工userid列表
        int offset = 0;//分页游标，从0开始
        int size = 50;//分页大小，最大50

        JSONObject result1;

        List<String> list111 = new ArrayList<>();


        do {
            List<String> list = new ArrayList<>();

            System.out.println("测试分页值：" + offset);

            try {
                request1.setStatusList("2,3,5,-1");
                request1.setOffset((long) offset);
                request1.setSize((long) size);
                OapiSmartworkHrmEmployeeQueryonjobResponse response1 = client1.execute(request1, fangDeDingAccessTokenService.getFangDeAccessToken());
                employee2 = response1.getBody();
                System.out.println(response1.getBody());
            } catch (ApiException e) {
                e.printStackTrace();
            }

            //通过fastjson工具转换成json对象,逐层解析
            JSONObject jsonObject1 = JSON.parseObject(employee2);
            result1 = jsonObject1.getJSONObject("result");

            String dataList = result1.getString("data_list");

            //将String数组进行处理，去掉所有的双引号（""）和中括号（[]）
            String dataRes = dataList.replace("\"", "").
                    replaceAll("\\[", "").
                    replaceAll("\\]", "");

            list.add(dataRes);

            String userid1 = list.toString().replace("\"", "").
                    replaceAll("\\[", "").
                    replaceAll("\\]", "");



            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/listdimission");
            OapiSmartworkHrmEmployeeListRequest request = new OapiSmartworkHrmEmployeeListRequest();

            try {
                request.setUseridList(userid1);

                OapiSmartworkHrmEmployeeListResponse response = client.execute(request, fangDeDingAccessTokenService.getFangDeAccessToken());
                huamingce3 = response.getBody();
                System.out.println(response.getBody());
            }catch (ApiException e){
                e.printStackTrace();
            }

            //通过fastjson工具转换成json对象,逐层解析
            //将String类型的花名册json数据转换为Object数据对象
            JSONObject jsonObject = JSON.parseObject(huamingce3);

            //直接将花名册对象通过Object转换成Array数组格式
            JSONArray resultArray = jsonObject.getJSONArray("result");

//            String aaa = jsonObject.getString("result");
//
//            list111.add(aaa);


            for (int a = 0; a<resultArray.size(); a++){
                String lastWorkDay = resultArray
                        .getJSONObject(a)
                        .getString("last_work_day");

                try {

                    //循环获取json数据中的最后工作日，并转换成long型
                    Long aaa = Long.parseLong(lastWorkDay);
                    //从房德钉钉的离职人员信息接口中循环抓取出来的最后工作日转换成时间戳
                    Date date = new Date(aaa);
                    //设置的时间节点（时间戳转换[毫秒]：2021-02-18 00:00:00）
                    Date date1 = new Date(1613577600000L);

                    if(lastWorkDay != null && date.getTime() > date1.getTime()){

                        list111.add(resultArray.getJSONObject(a).getString("userid"));

                    }
                }catch (Exception e){

                }
            }

            if (result1.getString("next_cursor") != null || result1.getString("data_list") != "") {

                offset = offset + size;

            }

        } while (result1.getString("next_cursor") != null);

        String userid1 = list111.toString().replace("\"", "").
                replaceAll("\\[", "").
                replaceAll("\\]", "");

        return userid1;
    }


    //【测试！！！】获取房德钉钉中的花名册数据并解析成json格式数据返回【离职】
    @Override
    public EmployeeFangDeDataVo<EmployeeFangDeVo> getFangDeHuamingceLiZhiTest(){

        //---*** 执行同步离职员工信息 ***---//
        //---*** 执行同步离职员工信息 ***---//
        //---*** 执行同步离职员工信息 ***---//

        //在职员工id列表的循环获取
        DingTalkClient client1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/querydimission");
        OapiSmartworkHrmEmployeeQueryonjobRequest request1 = new OapiSmartworkHrmEmployeeQueryonjobRequest();

        //企业在职员工userid列表
        int offset = 0;//分页游标，从0开始
        int size = 50;//分页大小，最大50

        JSONObject result1;


        EmployeeFangDeDataVo employeeFangDeDataVo = new EmployeeFangDeDataVo();

        List<EmployeeFangDeVo> employeeFangDeVoList = new ArrayList<>();

        do {
            //离职人员id
            List<String> list = new ArrayList<>();

            //符合条件的离职人员id
            List<String> list111 = new ArrayList<>();

            System.out.println("测试分页值：" + offset);

            try {
                request1.setStatusList("2,3,5,-1");
                request1.setOffset((long) offset);
                request1.setSize((long) size);
                OapiSmartworkHrmEmployeeQueryonjobResponse response1 = client1.execute(request1, fangDeDingAccessTokenService.getFangDeAccessToken());
                employee2 = response1.getBody();
                System.out.println(response1.getBody());
            } catch (ApiException e) {
                e.printStackTrace();
            }

            //通过fastjson工具转换成json对象,逐层解析
            JSONObject jsonObject1 = JSON.parseObject(employee2);
            result1 = jsonObject1.getJSONObject("result");

            String dataList = result1.getString("data_list");
            System.out.println("id数组：" + dataList);

            JSONArray data_list = result1.getJSONArray("data_list");

            //判断钉钉花名册中在职人员id循环的时候json数据中data_list数组是否为空
            if (data_list.isEmpty() || data_list.size() < 1) {

                break;

            } else {

                //将String数组进行处理，去掉所有的双引号（""）和中括号（[]）
                String dataRes = dataList.replace("\"", "").
                        replaceAll("\\[", "").
                        replaceAll("\\]", "");

                list.add(dataRes);

                //离职人员id
                String userid1 = list.toString().replace("\"", "").
                        replaceAll("\\[", "").
                        replaceAll("\\]", "");


                DingTalkClient client3 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/listdimission");
                OapiSmartworkHrmEmployeeListRequest request3 = new OapiSmartworkHrmEmployeeListRequest();

                try {
                    request3.setUseridList(userid1);

                    OapiSmartworkHrmEmployeeListResponse response = client3.execute(request3, fangDeDingAccessTokenService.getFangDeAccessToken());
                    huamingce3 = response.getBody();
                    System.out.println(response.getBody());
                }catch (ApiException e){
                    e.printStackTrace();
                }

                //通过fastjson工具转换成json对象,逐层解析
                //将String类型的花名册json数据转换为Object数据对象
                JSONObject jsonObject3 = JSON.parseObject(huamingce3);

                //直接将花名册对象通过Object转换成Array数组格式
                JSONArray resultArray3 = jsonObject3.getJSONArray("result");

//            String aaa = jsonObject.getString("result");
//
//            list111.add(aaa);


                for (int c = 0; c<resultArray3.size(); c++){
                    String lastWorkDay = resultArray3
                            .getJSONObject(c)
                            .getString("last_work_day");

                    try {

                        //循环获取json数据中的最后工作日，并转换成long型
                        Long aaa = Long.parseLong(lastWorkDay);
                        //从房德钉钉的离职人员信息接口中循环抓取出来的最后工作日转换成时间戳
                        Date date = new Date(aaa);
                        //设置的时间节点（时间戳转换[毫秒]：2021-02-18 00:00:00）
                        Date date1 = new Date(1613577600000L);

                        if(date.getTime() > date1.getTime()) {

                            list111.add(resultArray3.getJSONObject(c).getString("userid"));


                            //从离职信息中循环获取符合最后工作日的离职人员id
                            String userid111 = list111.toString().replace("\"", "").
                                    replaceAll("\\[", "").
                                    replaceAll("\\]", "");

                            System.out.println("符合最后工作日的离职人员id：" + userid111);


                            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/list");
                            OapiSmartworkHrmEmployeeListRequest request = new OapiSmartworkHrmEmployeeListRequest();

                            try {
                                request.setUseridList(userid111);

                                //需要获取的花名册字段信息
//                request.setFieldFilterList("sys00-name,145d3425-9dea-42a6-88ae-1e4e3590046c,473f9667-d220-426f-8431-56be1e8a027d," +
//                        "395840c1-2cbe-4f36-b3fb-249d7ddf42f2,sys00-confirmJoinTime,sys02-sexType,sys02-certNo,sys01-employeeType," +
//                        "sys04-accountBank,sys04-bankAccountNo,ed21c116-efcb-49f3-a507-f8474450260c,sys01-employeeStatus");

                                OapiSmartworkHrmEmployeeListResponse response = client.execute(request, fangDeDingAccessTokenService.getFangDeAccessToken());
                                huamingce2 = response.getBody();
                                System.out.println(response.getBody());
                            } catch (ApiException e) {
                                e.printStackTrace();
                            }

                            //通过fastjson工具转换成json对象,逐层解析
                            //将String类型的花名册json数据转换为Object数据对象
                            JSONObject jsonObject = JSON.parseObject(huamingce2);

                            //直接将花名册对象通过Object转换成Array数组格式
                            JSONArray resultArray = jsonObject.getJSONArray("result");


                            for (int i = 0; i < resultArray.size(); i++) {
                                JSONArray fieldListArray = resultArray
                                        .getJSONObject(i).getJSONArray("field_list");

                                String userid = resultArray.getJSONObject(i).getString("userid");
                                System.out.println("员工id：" + userid);

                                //new一个employeeFangDeVo对象
                                EmployeeFangDeVo employeeFangDeVo = new EmployeeFangDeVo();

                                //将result的每个对象的id放入employee实体类的code字段中
                                employeeFangDeVo.setCode(userid);
                                employeeFangDeVo.setIdtype("身份证");
                                employeeFangDeVo.setBanktype("工资卡");
                                employeeFangDeVo.setStatus(2);

                                for (int a = 0; a < fieldListArray.size(); a++) {
                                    String fieldCode = fieldListArray
                                            .getJSONObject(a)
                                            .getString("field_code");

                                    switch (fieldCode) {
                                        //姓名
                                        case "sys00-name":
                                            if (fieldListArray.getJSONObject(a).getString("value") != null) {
                                                System.out.println("***《《《姓名》》》***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                                employeeFangDeVo.setName(fieldListArray.getJSONObject(a).getString("value"));
                                            } else {
                                                employeeFangDeVo.setName("");
                                            }
                                            break;

                                        //合同公司（业务单元名称）
                                        case "145d3425-9dea-42a6-88ae-1e4e3590046c":
                                            if (fieldListArray.getJSONObject(a).getString("label") != null || fieldListArray.getJSONObject(a).getString("label") != "") {
                                                System.out.println("***合同公司（业务单元名称）***-->" + fieldListArray.getJSONObject(a).getString("label"));

                                                employeeFangDeVo.setOrgname(fieldListArray.getJSONObject(a).getString("label"));
                                                employeeFangDeVo.setIndutyorgname(fieldListArray.getJSONObject(a).getString("label"));
                                            } else {
                                                employeeFangDeVo.setOrgname("");
                                                employeeFangDeVo.setIndutyorgname("");
                                            }
                                            break;

                                        //人员成本分摊一级部门
                                        case "473f9667-d220-426f-8431-56be1e8a027d":
                                            if (fieldListArray.getJSONObject(a).getString("label") != null) {
                                                System.out.println("***人员成本分摊一级部门***-->" + fieldListArray.getJSONObject(a).getString("label"));

                                                employeeFangDeVo.setDef1(fieldListArray.getJSONObject(a).getString("label"));
                                            } else {
                                                employeeFangDeVo.setDef1("");
                                            }
                                            break;

                                        //人员成本分摊二级部门
                                        case "395840c1-2cbe-4f36-b3fb-249d7ddf42f2":
                                            if (fieldListArray.getJSONObject(a).getString("label") != null) {
                                                System.out.println("***人员成本分摊二级部门***-->" + fieldListArray.getJSONObject(a).getString("label"));

                                                employeeFangDeVo.setDeptname(fieldListArray.getJSONObject(a).getString("label"));
                                            } else {
                                                employeeFangDeVo.setDeptname("");
                                            }
                                            break;

                                        //入职时间
                                        case "sys00-confirmJoinTime":
                                            if (fieldListArray.getJSONObject(a).getString("value") != null) {
                                                System.out.println("***入职时间***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                                employeeFangDeVo.setIndutydate(fieldListArray.getJSONObject(a).getString("value"));
                                            } else {
                                                employeeFangDeVo.setIndutydate("");
                                            }
                                            break;

                                        //性别
                                        case "sys02-sexType":
                                            if (fieldListArray.getJSONObject(a).getString("label") != null) {
                                                System.out.println("***性别***-->" + fieldListArray.getJSONObject(a).getString("label"));
                                                employeeFangDeVo.setSex(fieldListArray.getJSONObject(a).getString("label"));
                                            } else {
                                                employeeFangDeVo.setSex(null);
                                            }
                                            break;

                                        //证件号码
                                        case "sys02-certNo":
                                            if (fieldListArray.getJSONObject(a).getString("value") != null) {
                                                System.out.println("***证件号码***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                                employeeFangDeVo.setId(fieldListArray.getJSONObject(a).getString("value"));
                                            } else {
                                                employeeFangDeVo.setId("");
                                            }
                                            break;

                                        //员工类别
                                        case "8b655cff-d575-4742-bbd4-ddc9fd4b5acf":
                                            if (fieldListArray.getJSONObject(a).getString("label") != null) {
                                                System.out.println("***员工类型***-->" + fieldListArray.getJSONObject(a).getString("label"));

                                                employeeFangDeVo.setPsnclname(fieldListArray.getJSONObject(a).getString("label"));
                                            } else {
                                                employeeFangDeVo.setPsnclname(null);
                                            }
                                            break;

                                        //开户行
                                        case "sys04-accountBank":
                                            if (fieldListArray.getJSONObject(a).getString("value") != null) {
                                                System.out.println("***开户行***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                                employeeFangDeVo.setBank(fieldListArray.getJSONObject(a).getString("value"));
                                            } else {
                                                employeeFangDeVo.setBank("");
                                            }
                                            break;

                                        //银行卡号
                                        case "sys04-bankAccountNo":
                                            if (fieldListArray.getJSONObject(a).getString("value") != null) {
                                                System.out.println("***银行卡号***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                                employeeFangDeVo.setBanknum(fieldListArray.getJSONObject(a).getString("value"));
                                            } else {
                                                employeeFangDeVo.setBanknum("");
                                            }
                                            break;

                                        //户名
                                        case "ed21c116-efcb-49f3-a507-f8474450260c":
                                            if (fieldListArray.getJSONObject(a).getString("value") != null) {
                                                System.out.println("***户名***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                                employeeFangDeVo.setBankname(fieldListArray.getJSONObject(a).getString("value"));
                                            } else {
                                                employeeFangDeVo.setBankname("");
                                            }
                                            break;
                                    }
                                }
                                employeeFangDeVoList.add(employeeFangDeVo);
                            }
                            employeeFangDeDataVo.setData(employeeFangDeVoList);
                        }
//                        else break;
                        list111.clear();


                    }catch (Exception e){

                    }

                }


                if (result1.getString("next_cursor") != null || result1.getString("data_list") != "") {

                    offset = offset + size;

                }

            }

        } while (result1.getString("next_cursor") != null);


        return employeeFangDeDataVo;
    }







    //***以下为正式使用的json数据方法***//
    //***以下为正式使用的json数据方法***//
    //***以下为正式使用的json数据方法***//


    //获取房德钉钉中的花名册数据并解析成json格式数据返回【在职】
    @Override
    public EmployeeFangDeDataVo<EmployeeFangDeVo> getFangDeHuamingce(){

        //---*** 执行同步在职员工信息 ***---//
        //---*** 执行同步在职员工信息 ***---//
        //---*** 执行同步在职员工信息 ***---//

        //在职员工id列表的循环获取
        DingTalkClient client1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/queryonjob");
        OapiSmartworkHrmEmployeeQueryonjobRequest request1 = new OapiSmartworkHrmEmployeeQueryonjobRequest();

        //企业在职员工userid列表
        int offset = 0;//分页游标，从0开始
        int size = 50;//分页大小，最大50

        JSONObject result1;

        EmployeeFangDeDataVo employeeFangDeDataVo = new EmployeeFangDeDataVo();

        List<EmployeeFangDeVo> employeeFangDeVoList = new ArrayList<>();

        do {
            List<String> list = new ArrayList<>();

            System.out.println("测试分页值：" + offset);

            try {
                request1.setStatusList("2,3,5,-1");
                request1.setOffset((long) offset);
                request1.setSize((long) size);
                OapiSmartworkHrmEmployeeQueryonjobResponse response1 = client1.execute(request1, fangDeDingAccessTokenService.getFangDeAccessToken());
                employee1 = response1.getBody();
                System.out.println(response1.getBody());
            } catch (ApiException e) {
                e.printStackTrace();
            }

            //通过fastjson工具转换成json对象,逐层解析
            JSONObject jsonObject1 = JSON.parseObject(employee1);
            result1 = jsonObject1.getJSONObject("result");

            String dataList = result1.getString("data_list");
            System.out.println("id数组：" + dataList);

            JSONArray data_list = result1.getJSONArray("data_list");

            //判断钉钉花名册中在职人员id循环的时候json数据中data_list数组是否为空
            if (data_list.isEmpty() || data_list.size() < 1) {

                break;

            } else {

                //将String数组进行处理，去掉所有的双引号（""）和中括号（[]）
                String dataRes = dataList.replace("\"", "").
                        replaceAll("\\[", "").
                        replaceAll("\\]", "");

                list.add(dataRes);

                String userid1 = list.toString().replace("\"", "").
                        replaceAll("\\[", "").
                        replaceAll("\\]", "");


                DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/list");
                OapiSmartworkHrmEmployeeListRequest request = new OapiSmartworkHrmEmployeeListRequest();

                try {
                    request.setUseridList(userid1);

                    //需要获取的花名册字段信息
//                request.setFieldFilterList("sys00-name,145d3425-9dea-42a6-88ae-1e4e3590046c,473f9667-d220-426f-8431-56be1e8a027d," +
//                        "395840c1-2cbe-4f36-b3fb-249d7ddf42f2,sys00-confirmJoinTime,sys02-sexType,sys02-certNo,sys01-employeeType," +
//                        "sys04-accountBank,sys04-bankAccountNo,ed21c116-efcb-49f3-a507-f8474450260c,sys01-employeeStatus");

                    OapiSmartworkHrmEmployeeListResponse response = client.execute(request, fangDeDingAccessTokenService.getFangDeAccessToken());
                    huamingce = response.getBody();
                    System.out.println(response.getBody());
                } catch (ApiException e) {
                    e.printStackTrace();
                }

                //通过fastjson工具转换成json对象,逐层解析
                //将String类型的花名册json数据转换为Object数据对象
                JSONObject jsonObject = JSON.parseObject(huamingce);

                //直接将花名册对象通过Object转换成Array数组格式
                JSONArray resultArray = jsonObject.getJSONArray("result");


                for (int i = 0; i < resultArray.size(); i++) {
                    JSONArray fieldListArray = resultArray
                            .getJSONObject(i).getJSONArray("field_list");

                    String userid = resultArray.getJSONObject(i).getString("userid");
                    System.out.println("员工id：" + userid);

                    //new一个employeeFangDeVo对象
                    EmployeeFangDeVo employeeFangDeVo = new EmployeeFangDeVo();

                    //将result的每个对象的id放入employee实体类的code字段中
//                    employeeFangDeVo.setCode(userid);
                    employeeFangDeVo.setIdtype("身份证");
                    employeeFangDeVo.setBanktype("工资卡");
                    employeeFangDeVo.setStatus(1);


                    System.out.println("循环的值111--->" + fieldListArray.size());


                    for (int a = 0; a < fieldListArray.size(); a++) {
                        String fieldCode = fieldListArray
                                .getJSONObject(a)
                                .getString("field_code");



                        /*
                         *获取当前代码进度并取值放入session中供前端调用
                         */
//                        // 接收 HttpServletRequest request
//                        HttpSession session = request111.getSession();
//                        //定义double类型，用于保存当前百分比
//                        double currentPercent;
//                        //int类型，进度条显示的百分比
//                        int percent = 0;
//                        for (int aa = 20 ; aa < fieldListArray.size(); aa += 20) {
//                            currentPercent=((double)aa/fieldListArray.size())*100;
//                            //因为百分比定义的为整数型，所以每次当前百分比和进度条显示的百分比之间差值大于1才更新
//                            if((int) Math.floor(currentPercent) - percent >= 1) {
//                                percent = (int)Math.floor(currentPercent);
//                                if (percent>=99){
//                                    percent=100;
//                                }
//                                //"write,数值形式"方便显示
//                                session.setAttribute("processvalue", "Loading···" + percent);
//                            }
//                        }
                        /*
                         *获取当前代码进度并取值放入session中供前端调用
                         */



                        switch (fieldCode) {
                            //姓名
                            case "sys00-name":
                                if (fieldListArray.getJSONObject(a).getString("value") != null) {
                                    System.out.println("***《《《姓名》》》***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                    employeeFangDeVo.setName(fieldListArray.getJSONObject(a).getString("value"));
                                } else {
                                    employeeFangDeVo.setName("");
                                }
                                break;

                            //工号
                            case "sys00-jobNumber":
                                if (fieldListArray.getJSONObject(a).getString("value") != null){
                                    System.out.println("***工号***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                    employeeFangDeVo.setCode(fieldListArray.getJSONObject(a).getString("value"));
                                }
                                else{
                                    employeeFangDeVo.setCode("");
                                }
                                break;

                            //合同公司（业务单元名称）
                            case "145d3425-9dea-42a6-88ae-1e4e3590046c":
                                if (fieldListArray.getJSONObject(a).getString("label") != null || fieldListArray.getJSONObject(a).getString("label") != "") {
                                    System.out.println("***合同公司（业务单元名称）***-->" + fieldListArray.getJSONObject(a).getString("label"));

                                    employeeFangDeVo.setOrgname(fieldListArray.getJSONObject(a).getString("label"));
                                    employeeFangDeVo.setIndutyorgname(fieldListArray.getJSONObject(a).getString("label"));
                                } else {
                                    employeeFangDeVo.setOrgname("");
                                    employeeFangDeVo.setIndutyorgname("");
                                }
                                break;

                            //人员成本分摊一级部门
                            case "473f9667-d220-426f-8431-56be1e8a027d":
                                if (fieldListArray.getJSONObject(a).getString("label") != null) {
                                    System.out.println("***人员成本分摊一级部门***-->" + fieldListArray.getJSONObject(a).getString("label"));

                                    employeeFangDeVo.setDef1(fieldListArray.getJSONObject(a).getString("label"));
                                } else {
                                    employeeFangDeVo.setDef1("");
                                }
                                break;

                            //人员成本分摊二级部门
                            case "395840c1-2cbe-4f36-b3fb-249d7ddf42f2":
                                if (fieldListArray.getJSONObject(a).getString("label") != null) {
                                    System.out.println("***人员成本分摊二级部门***-->" + fieldListArray.getJSONObject(a).getString("label"));

                                    employeeFangDeVo.setDeptname(fieldListArray.getJSONObject(a).getString("label"));
                                } else {
                                    employeeFangDeVo.setDeptname("");
                                }
                                break;

                            //入职时间
                            case "sys00-confirmJoinTime":
                                if (fieldListArray.getJSONObject(a).getString("value") != null) {
                                    System.out.println("***入职时间***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                    employeeFangDeVo.setIndutydate(fieldListArray.getJSONObject(a).getString("value"));
                                } else {
                                    employeeFangDeVo.setIndutydate("");
                                }
                                break;

                            //性别
                            case "sys02-sexType":
                                if (fieldListArray.getJSONObject(a).getString("label") != null) {
                                    System.out.println("***性别***-->" + fieldListArray.getJSONObject(a).getString("label"));
//                            String sex = fieldListArray.getJSONObject(a).getInteger("value") == 0 ?"男":"女";
//                            employee.setSys02SexType(sex);
                                    employeeFangDeVo.setSex(fieldListArray.getJSONObject(a).getString("label"));
                                } else {
                                    employeeFangDeVo.setSex(null);
                                }
                                break;

                            //证件号码
                            case "sys02-certNo":
                                if (fieldListArray.getJSONObject(a).getString("value") != null) {
                                    System.out.println("***证件号码***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                    employeeFangDeVo.setId(fieldListArray.getJSONObject(a).getString("value"));
                                } else {
                                    employeeFangDeVo.setId("");
                                }
                                break;

//                        //合同公司（任职业务单元名称）
//                        case "145d3425-9dea-42a6-88ae-1e4e3590046c":
//                            if (fieldListArray.getJSONObject(a).getString("label") != null || fieldListArray.getJSONObject(a).getString("label") != ""){
//                                System.out.println("***合同公司（任职业务单元名称）***-->" + fieldListArray.getJSONObject(a).getString("label"));
//
//                                employeeFangDeVo.setIndutyorgname(fieldListArray.getJSONObject(a).getString("label"));
//                            }
//                            else{
//                                employeeFangDeVo.setIndutyorgname("");
//                            }
//                            break;

                            //员工类别
                            case "8b655cff-d575-4742-bbd4-ddc9fd4b5acf":
                                if (fieldListArray.getJSONObject(a).getString("label") != null) {
                                    System.out.println("***员工类型***-->" + fieldListArray.getJSONObject(a).getString("label"));

                                    employeeFangDeVo.setPsnclname(fieldListArray.getJSONObject(a).getString("label"));
                                } else {
                                    employeeFangDeVo.setPsnclname(null);
                                }
                                break;

                            //开户行
                            case "sys04-accountBank":
                                if (fieldListArray.getJSONObject(a).getString("value") != null) {
                                    System.out.println("***开户行***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                    employeeFangDeVo.setBank(fieldListArray.getJSONObject(a).getString("value"));
                                } else {
                                    employeeFangDeVo.setBank("");
                                }
                                break;

                            //银行卡号
                            case "sys04-bankAccountNo":
                                if (fieldListArray.getJSONObject(a).getString("value") != null) {
                                    System.out.println("***银行卡号***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                    employeeFangDeVo.setBanknum(fieldListArray.getJSONObject(a).getString("value"));
                                } else {
                                    employeeFangDeVo.setBanknum("");
                                }
                                break;

                            //户名
                            case "ed21c116-efcb-49f3-a507-f8474450260c":
                                if (fieldListArray.getJSONObject(a).getString("value") != null) {
                                    System.out.println("***户名***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                    employeeFangDeVo.setBankname(fieldListArray.getJSONObject(a).getString("value"));
                                } else {
                                    employeeFangDeVo.setBankname("");
                                }
                                break;

                            //员工状态
//                        case "sys01-employeeStatus":
//                            if (fieldListArray.getJSONObject(a).getString("value") != null){
//                                System.out.println("***员工状态***-->" + fieldListArray.getJSONObject(a).getString("value"));
//
//                                employeeFangDeVo.setStatus(1);
//                            }
//                            else{
//                                employeeFangDeVo.setStatus(null);
//                            }
//                            break;
                        }
                    }
                    employeeFangDeVoList.add(employeeFangDeVo);
                }
                //TODO ---原始同步花名册数据至数据库--- //


                employeeFangDeDataVo.setData(employeeFangDeVoList);

                if (result1.getString("next_cursor") != null || result1.getString("data_list") != "") {

                    offset = offset + size;

                }

            }
        } while (result1.getString("next_cursor") != null);


        return employeeFangDeDataVo;
    }



    //获取房德钉钉中的花名册数据并解析成json格式数据返回【离职】
    @Override
    public EmployeeFangDeDataVo<EmployeeFangDeVo> getFangDeHuamingceLiZhi(){

        //---*** 执行同步离职员工信息 ***---//
        //---*** 执行同步离职员工信息 ***---//
        //---*** 执行同步离职员工信息 ***---//

        //在职员工id列表的循环获取
        DingTalkClient client1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/querydimission");
        OapiSmartworkHrmEmployeeQueryonjobRequest request1 = new OapiSmartworkHrmEmployeeQueryonjobRequest();

        //企业在职员工userid列表
        int offset = 0;//分页游标，从0开始
        int size = 50;//分页大小，最大50

        JSONObject result1;


        EmployeeFangDeDataVo employeeFangDeDataVo = new EmployeeFangDeDataVo();

        List<EmployeeFangDeVo> employeeFangDeVoList = new ArrayList<>();

        do {
            //离职人员id
            List<String> list = new ArrayList<>();

            //符合条件的离职人员id
            List<String> list111 = new ArrayList<>();

            System.out.println("测试分页值：" + offset);

            try {
                request1.setStatusList("2,3,5,-1");
                request1.setOffset((long) offset);
                request1.setSize((long) size);
                OapiSmartworkHrmEmployeeQueryonjobResponse response1 = client1.execute(request1, fangDeDingAccessTokenService.getFangDeAccessToken());
                employee2 = response1.getBody();
                System.out.println(response1.getBody());
            } catch (ApiException e) {
                e.printStackTrace();
            }

            //通过fastjson工具转换成json对象,逐层解析
            JSONObject jsonObject1 = JSON.parseObject(employee2);
            result1 = jsonObject1.getJSONObject("result");

            String dataList = result1.getString("data_list");
            System.out.println("id数组：" + dataList);

            JSONArray data_list = result1.getJSONArray("data_list");

            //判断钉钉花名册中在职人员id循环的时候json数据中data_list数组是否为空
            if (data_list.isEmpty() || data_list.size() < 1) {

                break;

            } else {

                //将String数组进行处理，去掉所有的双引号（""）和中括号（[]）
                String dataRes = dataList.replace("\"", "").
                        replaceAll("\\[", "").
                        replaceAll("\\]", "");

                list.add(dataRes);

                //离职人员id
                String userid1 = list.toString().replace("\"", "").
                        replaceAll("\\[", "").
                        replaceAll("\\]", "");


                DingTalkClient client3 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/listdimission");
                OapiSmartworkHrmEmployeeListRequest request3 = new OapiSmartworkHrmEmployeeListRequest();

                try {
                    request3.setUseridList(userid1);

                    OapiSmartworkHrmEmployeeListResponse response = client3.execute(request3, fangDeDingAccessTokenService.getFangDeAccessToken());
                    huamingce3 = response.getBody();
                    System.out.println(response.getBody());
                }catch (ApiException e){
                    e.printStackTrace();
                }

                //通过fastjson工具转换成json对象,逐层解析
                //将String类型的花名册json数据转换为Object数据对象
                JSONObject jsonObject3 = JSON.parseObject(huamingce3);

                //直接将花名册对象通过Object转换成Array数组格式
                JSONArray resultArray3 = jsonObject3.getJSONArray("result");

//            String aaa = jsonObject.getString("result");
//
//            list111.add(aaa);


                for (int c = 0; c<resultArray3.size(); c++){
                    String lastWorkDay = resultArray3
                            .getJSONObject(c)
                            .getString("last_work_day");

                    try {

                        //循环获取json数据中的最后工作日，并转换成long型
                        Long aaa = Long.parseLong(lastWorkDay);
                        //从房德钉钉的离职人员信息接口中循环抓取出来的最后工作日转换成时间
                        Date date = new Date(aaa);
                        //设置的时间节点（时间戳转换[毫秒]：2021-02-18 00:00:00）
                        Date date1 = new Date(1613577600000L);

                        if(date.getTime() > date1.getTime()) {

                            list111.add(resultArray3.getJSONObject(c).getString("userid"));


                            //从离职信息中循环获取符合最后工作日的离职人员id
                            String userid111 = list111.toString().replace("\"", "").
                                    replaceAll("\\[", "").
                                    replaceAll("\\]", "");

                            System.out.println("符合最后工作日的离职人员id：" + userid111);


                            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/list");
                            OapiSmartworkHrmEmployeeListRequest request = new OapiSmartworkHrmEmployeeListRequest();

                            try {
                                request.setUseridList(userid111);

                                //需要获取的花名册字段信息
//                request.setFieldFilterList("sys00-name,145d3425-9dea-42a6-88ae-1e4e3590046c,473f9667-d220-426f-8431-56be1e8a027d," +
//                        "395840c1-2cbe-4f36-b3fb-249d7ddf42f2,sys00-confirmJoinTime,sys02-sexType,sys02-certNo,sys01-employeeType," +
//                        "sys04-accountBank,sys04-bankAccountNo,ed21c116-efcb-49f3-a507-f8474450260c,sys01-employeeStatus");

                                OapiSmartworkHrmEmployeeListResponse response = client.execute(request, fangDeDingAccessTokenService.getFangDeAccessToken());
                                huamingce2 = response.getBody();
                                System.out.println(response.getBody());
                            } catch (ApiException e) {
                                e.printStackTrace();
                            }

                            //通过fastjson工具转换成json对象,逐层解析
                            //将String类型的花名册json数据转换为Object数据对象
                            JSONObject jsonObject = JSON.parseObject(huamingce2);

                            //直接将花名册对象通过Object转换成Array数组格式
                            JSONArray resultArray = jsonObject.getJSONArray("result");


                            for (int i = 0; i < resultArray.size(); i++) {
                                JSONArray fieldListArray = resultArray
                                        .getJSONObject(i).getJSONArray("field_list");

                                String userid = resultArray.getJSONObject(i).getString("userid");
                                System.out.println("员工id：" + userid);

                                //new一个employeeFangDeVo对象
                                EmployeeFangDeVo employeeFangDeVo = new EmployeeFangDeVo();

                                //将result的每个对象的id放入employee实体类的code字段中
                                employeeFangDeVo.setCode(userid);
                                employeeFangDeVo.setIdtype("身份证");
                                employeeFangDeVo.setBanktype("工资卡");
                                employeeFangDeVo.setStatus(2);

                                for (int a = 0; a < fieldListArray.size(); a++) {
                                    String fieldCode = fieldListArray
                                            .getJSONObject(a)
                                            .getString("field_code");

                                    switch (fieldCode) {
                                        //姓名
                                        case "sys00-name":
                                            if (fieldListArray.getJSONObject(a).getString("value") != null) {
                                                System.out.println("***《《《姓名》》》***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                                employeeFangDeVo.setName(fieldListArray.getJSONObject(a).getString("value"));
                                            } else {
                                                employeeFangDeVo.setName("");
                                            }
                                            break;

                                        //合同公司（业务单元名称）
                                        case "145d3425-9dea-42a6-88ae-1e4e3590046c":
                                            if (fieldListArray.getJSONObject(a).getString("label") != null || fieldListArray.getJSONObject(a).getString("label") != "") {
                                                System.out.println("***合同公司（业务单元名称）***-->" + fieldListArray.getJSONObject(a).getString("label"));

                                                employeeFangDeVo.setOrgname(fieldListArray.getJSONObject(a).getString("label"));
                                                employeeFangDeVo.setIndutyorgname(fieldListArray.getJSONObject(a).getString("label"));
                                            } else {
                                                employeeFangDeVo.setOrgname("");
                                                employeeFangDeVo.setIndutyorgname("");
                                            }
                                            break;

                                        //人员成本分摊一级部门
                                        case "473f9667-d220-426f-8431-56be1e8a027d":
                                            if (fieldListArray.getJSONObject(a).getString("label") != null) {
                                                System.out.println("***人员成本分摊一级部门***-->" + fieldListArray.getJSONObject(a).getString("label"));

                                                employeeFangDeVo.setDef1(fieldListArray.getJSONObject(a).getString("label"));
                                            } else {
                                                employeeFangDeVo.setDef1("");
                                            }
                                            break;

                                        //人员成本分摊二级部门
                                        case "395840c1-2cbe-4f36-b3fb-249d7ddf42f2":
                                            if (fieldListArray.getJSONObject(a).getString("label") != null) {
                                                System.out.println("***人员成本分摊二级部门***-->" + fieldListArray.getJSONObject(a).getString("label"));

                                                employeeFangDeVo.setDeptname(fieldListArray.getJSONObject(a).getString("label"));
                                            } else {
                                                employeeFangDeVo.setDeptname("");
                                            }
                                            break;

                                        //入职时间
                                        case "sys00-confirmJoinTime":
                                            if (fieldListArray.getJSONObject(a).getString("value") != null) {
                                                System.out.println("***入职时间***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                                employeeFangDeVo.setIndutydate(fieldListArray.getJSONObject(a).getString("value"));
                                            } else {
                                                employeeFangDeVo.setIndutydate("");
                                            }
                                            break;

                                        //性别
                                        case "sys02-sexType":
                                            if (fieldListArray.getJSONObject(a).getString("label") != null) {
                                                System.out.println("***性别***-->" + fieldListArray.getJSONObject(a).getString("label"));
                                                employeeFangDeVo.setSex(fieldListArray.getJSONObject(a).getString("label"));
                                            } else {
                                                employeeFangDeVo.setSex(null);
                                            }
                                            break;

                                        //证件号码
                                        case "sys02-certNo":
                                            if (fieldListArray.getJSONObject(a).getString("value") != null) {
                                                System.out.println("***证件号码***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                                employeeFangDeVo.setId(fieldListArray.getJSONObject(a).getString("value"));
                                            } else {
                                                employeeFangDeVo.setId("");
                                            }
                                            break;

                                        //员工类别
                                        case "8b655cff-d575-4742-bbd4-ddc9fd4b5acf":
                                            if (fieldListArray.getJSONObject(a).getString("label") != null) {
                                                System.out.println("***员工类型***-->" + fieldListArray.getJSONObject(a).getString("label"));

                                                employeeFangDeVo.setPsnclname(fieldListArray.getJSONObject(a).getString("label"));
                                            } else {
                                                employeeFangDeVo.setPsnclname(null);
                                            }
                                            break;

                                        //开户行
                                        case "sys04-accountBank":
                                            if (fieldListArray.getJSONObject(a).getString("value") != null) {
                                                System.out.println("***开户行***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                                employeeFangDeVo.setBank(fieldListArray.getJSONObject(a).getString("value"));
                                            } else {
                                                employeeFangDeVo.setBank("");
                                            }
                                            break;

                                        //银行卡号
                                        case "sys04-bankAccountNo":
                                            if (fieldListArray.getJSONObject(a).getString("value") != null) {
                                                System.out.println("***银行卡号***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                                employeeFangDeVo.setBanknum(fieldListArray.getJSONObject(a).getString("value"));
                                            } else {
                                                employeeFangDeVo.setBanknum("");
                                            }
                                            break;

                                        //户名
                                        case "ed21c116-efcb-49f3-a507-f8474450260c":
                                            if (fieldListArray.getJSONObject(a).getString("value") != null) {
                                                System.out.println("***户名***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                                employeeFangDeVo.setBankname(fieldListArray.getJSONObject(a).getString("value"));
                                            } else {
                                                employeeFangDeVo.setBankname("");
                                            }
                                            break;
                                    }
                                }
                                employeeFangDeVoList.add(employeeFangDeVo);
                            }
                            employeeFangDeDataVo.setData(employeeFangDeVoList);
                        }

                        //在循环的时候将每一批（即每50个）符合某个最后工作日时间条件的离职人员id取出来放入list111集合中
                        //并且放入花名册信息接口中使用后，进行清空list111集合。
                        list111.clear();


                    }catch (Exception e){

                    }

                }


                if (result1.getString("next_cursor") != null || result1.getString("data_list") != "") {

                    offset = offset + size;

                }

            }

        } while (result1.getString("next_cursor") != null);


        return employeeFangDeDataVo;
    }





//    //获取房德钉钉中的花名册数据并解析成json格式数据返回【所有的离职人员信息】
//    @Override
//    public EmployeeFangDeDataVo<EmployeeFangDeVo> getFangDeHuamingceLiZhi(){
//
//        //---*** 执行同步离职员工信息 ***---//
//        //---*** 执行同步离职员工信息 ***---//
//        //---*** 执行同步离职员工信息 ***---//
//
//        //在职员工id列表的循环获取
//        DingTalkClient client1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/querydimission");
//        OapiSmartworkHrmEmployeeQueryonjobRequest request1 = new OapiSmartworkHrmEmployeeQueryonjobRequest();
//
//        //企业在职员工userid列表
//        int offset = 0;//分页游标，从0开始
//        int size = 50;//分页大小，最大50
//
//        JSONObject result1;
//
//        EmployeeFangDeDataVo employeeFangDeDataVo = new EmployeeFangDeDataVo();
//
//        List<EmployeeFangDeVo> employeeFangDeVoList = new ArrayList<>();
//
//        do {
//            List<String> list = new ArrayList<>();
//
//            System.out.println("测试分页值：" + offset);
//
//            try {
//                request1.setStatusList("2,3,5,-1");
//                request1.setOffset((long) offset);
//                request1.setSize((long) size);
//                OapiSmartworkHrmEmployeeQueryonjobResponse response1 = client1.execute(request1, fangDeDingAccessTokenService.getFangDeAccessToken());
//                employee2 = response1.getBody();
//                System.out.println(response1.getBody());
//            } catch (ApiException e) {
//                e.printStackTrace();
//            }
//
//            //通过fastjson工具转换成json对象,逐层解析
//            JSONObject jsonObject1 = JSON.parseObject(employee2);
//            result1 = jsonObject1.getJSONObject("result");
//
//            String dataList = result1.getString("data_list");
//            System.out.println("id数组：" + dataList);
//
//            JSONArray data_list = result1.getJSONArray("data_list");
//
//            //判断钉钉花名册中在职人员id循环的时候json数据中data_list数组是否为空
//            if (data_list.isEmpty() || data_list.size() < 1) {
//
//                break;
//
//            } else {
//
//                //将String数组进行处理，去掉所有的双引号（""）和中括号（[]）
//                String dataRes = dataList.replace("\"", "").
//                        replaceAll("\\[", "").
//                        replaceAll("\\]", "");
//
//                list.add(dataRes);
//
//                String userid1 = list.toString().replace("\"", "").
//                        replaceAll("\\[", "").
//                        replaceAll("\\]", "");
//
//
//                DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/list");
//                OapiSmartworkHrmEmployeeListRequest request = new OapiSmartworkHrmEmployeeListRequest();
//
//                try {
//                    request.setUseridList(userid1);
//
//                    //需要获取的花名册字段信息
////                request.setFieldFilterList("sys00-name,145d3425-9dea-42a6-88ae-1e4e3590046c,473f9667-d220-426f-8431-56be1e8a027d," +
////                        "395840c1-2cbe-4f36-b3fb-249d7ddf42f2,sys00-confirmJoinTime,sys02-sexType,sys02-certNo,sys01-employeeType," +
////                        "sys04-accountBank,sys04-bankAccountNo,ed21c116-efcb-49f3-a507-f8474450260c,sys01-employeeStatus");
//
//                    OapiSmartworkHrmEmployeeListResponse response = client.execute(request, fangDeDingAccessTokenService.getFangDeAccessToken());
//                    huamingce2 = response.getBody();
//                    System.out.println(response.getBody());
//                } catch (ApiException e) {
//                    e.printStackTrace();
//                }
//
//                //通过fastjson工具转换成json对象,逐层解析
//                //将String类型的花名册json数据转换为Object数据对象
//                JSONObject jsonObject = JSON.parseObject(huamingce2);
//
//                //直接将花名册对象通过Object转换成Array数组格式
//                JSONArray resultArray = jsonObject.getJSONArray("result");
//
//
//                for (int i = 0; i < resultArray.size(); i++) {
//                    JSONArray fieldListArray = resultArray
//                            .getJSONObject(i).getJSONArray("field_list");
//
//                    String userid = resultArray.getJSONObject(i).getString("userid");
//                    System.out.println("员工id：" + userid);
//
//                    //new一个employeeFangDeVo对象
//                    EmployeeFangDeVo employeeFangDeVo = new EmployeeFangDeVo();
//
//                    //将result的每个对象的id放入employee实体类的code字段中
//                    employeeFangDeVo.setCode(userid);
//                    employeeFangDeVo.setIdtype("身份证");
//                    employeeFangDeVo.setBanktype("工资卡");
//                    employeeFangDeVo.setStatus(2);
//
//                    for (int a = 0; a < fieldListArray.size(); a++) {
//                        String fieldCode = fieldListArray
//                                .getJSONObject(a)
//                                .getString("field_code");
//
//                        switch (fieldCode) {
//                            //姓名
//                            case "sys00-name":
//                                if (fieldListArray.getJSONObject(a).getString("value") != null) {
//                                    System.out.println("***《《《姓名》》》***-->" + fieldListArray.getJSONObject(a).getString("value"));
//
//                                    employeeFangDeVo.setName(fieldListArray.getJSONObject(a).getString("value"));
//                                } else {
//                                    employeeFangDeVo.setName("");
//                                }
//                                break;
//
//                            //合同公司（业务单元名称）
//                            case "145d3425-9dea-42a6-88ae-1e4e3590046c":
//                                if (fieldListArray.getJSONObject(a).getString("label") != null || fieldListArray.getJSONObject(a).getString("label") != "") {
//                                    System.out.println("***合同公司（业务单元名称）***-->" + fieldListArray.getJSONObject(a).getString("label"));
//
//                                    employeeFangDeVo.setOrgname(fieldListArray.getJSONObject(a).getString("label"));
//                                    employeeFangDeVo.setIndutyorgname(fieldListArray.getJSONObject(a).getString("label"));
//                                } else {
//                                    employeeFangDeVo.setOrgname("");
//                                    employeeFangDeVo.setIndutyorgname("");
//                                }
//                                break;
//
//                            //人员成本分摊一级部门
//                            case "473f9667-d220-426f-8431-56be1e8a027d":
//                                if (fieldListArray.getJSONObject(a).getString("label") != null) {
//                                    System.out.println("***人员成本分摊一级部门***-->" + fieldListArray.getJSONObject(a).getString("label"));
//
//                                    employeeFangDeVo.setDef1(fieldListArray.getJSONObject(a).getString("label"));
//                                } else {
//                                    employeeFangDeVo.setDef1("");
//                                }
//                                break;
//
//                            //人员成本分摊二级部门
//                            case "395840c1-2cbe-4f36-b3fb-249d7ddf42f2":
//                                if (fieldListArray.getJSONObject(a).getString("label") != null) {
//                                    System.out.println("***人员成本分摊二级部门***-->" + fieldListArray.getJSONObject(a).getString("label"));
//
//                                    employeeFangDeVo.setDeptname(fieldListArray.getJSONObject(a).getString("label"));
//                                } else {
//                                    employeeFangDeVo.setDeptname("");
//                                }
//                                break;
//
//                            //入职时间
//                            case "sys00-confirmJoinTime":
//                                if (fieldListArray.getJSONObject(a).getString("value") != null) {
//                                    System.out.println("***入职时间***-->" + fieldListArray.getJSONObject(a).getString("value"));
//
//                                    employeeFangDeVo.setIndutydate(fieldListArray.getJSONObject(a).getString("value"));
//                                } else {
//                                    employeeFangDeVo.setIndutydate("");
//                                }
//                                break;
//
//                            //性别
//                            case "sys02-sexType":
//                                if (fieldListArray.getJSONObject(a).getString("label") != null) {
//                                    System.out.println("***性别***-->" + fieldListArray.getJSONObject(a).getString("label"));
////                            String sex = fieldListArray.getJSONObject(a).getInteger("value") == 0 ?"男":"女";
////                            employee.setSys02SexType(sex);
//                                    employeeFangDeVo.setSex(fieldListArray.getJSONObject(a).getString("label"));
//                                } else {
//                                    employeeFangDeVo.setSex(null);
//                                }
//                                break;
//
//                            //证件号码
//                            case "sys02-certNo":
//                                if (fieldListArray.getJSONObject(a).getString("value") != null) {
//                                    System.out.println("***证件号码***-->" + fieldListArray.getJSONObject(a).getString("value"));
//
//                                    employeeFangDeVo.setId(fieldListArray.getJSONObject(a).getString("value"));
//                                } else {
//                                    employeeFangDeVo.setId("");
//                                }
//                                break;
//
////                        //合同公司（任职业务单元名称）
////                        case "145d3425-9dea-42a6-88ae-1e4e3590046c":
////                            if (fieldListArray.getJSONObject(a).getString("label") != null || fieldListArray.getJSONObject(a).getString("label") != ""){
////                                System.out.println("***合同公司（任职业务单元名称）***-->" + fieldListArray.getJSONObject(a).getString("label"));
////
////                                employeeFangDeVo.setIndutyorgname(fieldListArray.getJSONObject(a).getString("label"));
////                            }
////                            else{
////                                employeeFangDeVo.setIndutyorgname("");
////                            }
////                            break;
//
//                            //员工类别
//                            case "8b655cff-d575-4742-bbd4-ddc9fd4b5acf":
//                                if (fieldListArray.getJSONObject(a).getString("label") != null) {
//                                    System.out.println("***员工类型***-->" + fieldListArray.getJSONObject(a).getString("label"));
//
//                                    employeeFangDeVo.setPsnclname(fieldListArray.getJSONObject(a).getString("label"));
//                                } else {
//                                    employeeFangDeVo.setPsnclname(null);
//                                }
//                                break;
//
//                            //开户行
//                            case "sys04-accountBank":
//                                if (fieldListArray.getJSONObject(a).getString("value") != null) {
//                                    System.out.println("***开户行***-->" + fieldListArray.getJSONObject(a).getString("value"));
//
//                                    employeeFangDeVo.setBank(fieldListArray.getJSONObject(a).getString("value"));
//                                } else {
//                                    employeeFangDeVo.setBank("");
//                                }
//                                break;
//
//                            //银行卡号
//                            case "sys04-bankAccountNo":
//                                if (fieldListArray.getJSONObject(a).getString("value") != null) {
//                                    System.out.println("***银行卡号***-->" + fieldListArray.getJSONObject(a).getString("value"));
//
//                                    employeeFangDeVo.setBanknum(fieldListArray.getJSONObject(a).getString("value"));
//                                } else {
//                                    employeeFangDeVo.setBanknum("");
//                                }
//                                break;
//
//                            //户名
//                            case "ed21c116-efcb-49f3-a507-f8474450260c":
//                                if (fieldListArray.getJSONObject(a).getString("value") != null) {
//                                    System.out.println("***户名***-->" + fieldListArray.getJSONObject(a).getString("value"));
//
//                                    employeeFangDeVo.setBankname(fieldListArray.getJSONObject(a).getString("value"));
//                                } else {
//                                    employeeFangDeVo.setBankname("");
//                                }
//                                break;
//
//                            //员工状态
////                        case "sys01-employeeStatus":
////                            if (fieldListArray.getJSONObject(a).getString("value") != null){
////                                System.out.println("***员工状态***-->" + fieldListArray.getJSONObject(a).getString("value"));
////
////                                employeeFangDeVo.setStatus(1);
////                            }
////                            else{
////                                employeeFangDeVo.setStatus(null);
////                            }
////                            break;
//                        }
//                    }
//                    employeeFangDeVoList.add(employeeFangDeVo);
//                }
//                //TODO ---原始同步花名册数据至数据库--- //
//
//
//                employeeFangDeDataVo.setData(employeeFangDeVoList);
//
//                if (result1.getString("next_cursor") != null || result1.getString("data_list") != "") {
//
//                    offset = offset + size;
//
//                }
//
//            }
//        } while (result1.getString("next_cursor") != null);
//
//
//        return employeeFangDeDataVo;
//    }


    //删除employee表中的所有数据
    @Override
    public String removeFangDeEmployee(){
        employeeFangDeMapper.delete(null);
        return "清空花名册数据成功！";
    }

    //同步在职人员花名册数据至房德数据库
    @Override
    public String addFangDeHuamingce(){

        //---*** 执行同步在职员工信息 ***---//
        //---*** 执行同步在职员工信息 ***---//
        //---*** 执行同步在职员工信息 ***---//

        //在职员工id列表的循环获取
        DingTalkClient client1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/queryonjob");
        OapiSmartworkHrmEmployeeQueryonjobRequest request1 = new OapiSmartworkHrmEmployeeQueryonjobRequest();

        //企业在职员工userid列表
        int offset = 0;//分页游标，从0开始
        int size = 50;//分页大小，最大50

        JSONObject result1;

        do {
            List<String> list = new ArrayList<>();

            System.out.println("测试分页值：" + offset);

            try {
                request1.setStatusList("2,3,5,-1");
                request1.setOffset((long) offset);
                request1.setSize((long) size);
                OapiSmartworkHrmEmployeeQueryonjobResponse response1 = client1.execute(request1, fangDeDingAccessTokenService.getFangDeAccessToken());
                employee1 = response1.getBody();
                System.out.println(response1.getBody());
            } catch (ApiException e) {
                e.printStackTrace();
            }

            //通过fastjson工具转换成json对象,逐层解析
            JSONObject jsonObject1 = JSON.parseObject(employee1);
            result1 = jsonObject1.getJSONObject("result");

            String dataList = result1.getString("data_list");

            //将String数组进行处理，去掉所有的双引号（""）和中括号（[]）
            String dataRes = dataList.replace("\"", "").
                    replaceAll("\\[", "").
                    replaceAll("\\]", "");

            list.add(dataRes);

            String userid1 = list.toString().replace("\"", "").
                    replaceAll("\\[", "").
                    replaceAll("\\]", "");


            //TODO ---原始同步花名册数据至数据库--- //
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/list");
            OapiSmartworkHrmEmployeeListRequest request = new OapiSmartworkHrmEmployeeListRequest();

            try {
                request.setUseridList(userid1);

                //需要获取的花名册字段信息
//                request.setFieldFilterList("sys00-name,145d3425-9dea-42a6-88ae-1e4e3590046c,473f9667-d220-426f-8431-56be1e8a027d," +
//                        "395840c1-2cbe-4f36-b3fb-249d7ddf42f2,sys00-confirmJoinTime,sys02-sexType,sys02-certNo,sys01-employeeType," +
//                        "sys04-accountBank,sys04-bankAccountNo,ed21c116-efcb-49f3-a507-f8474450260c,sys01-employeeStatus");

                OapiSmartworkHrmEmployeeListResponse response = client.execute(request, fangDeDingAccessTokenService.getFangDeAccessToken());
                huamingce = response.getBody();
                System.out.println(response.getBody());
            }catch (ApiException e){
                e.printStackTrace();
            }

            //通过fastjson工具转换成json对象,逐层解析
            //将String类型的花名册json数据转换为Object数据对象
            JSONObject jsonObject = JSON.parseObject(huamingce);

            //直接将花名册对象通过Object转换成Array数组格式
            JSONArray resultArray = jsonObject.getJSONArray("result");

            //new一个employeeFangDe对象
            EmployeeFangDe employeeFangDe = new EmployeeFangDe();

            for (int i = 0; i<resultArray.size(); i++){
                JSONArray fieldListArray = resultArray
                        .getJSONObject(i).getJSONArray("field_list");

                String userid = resultArray.getJSONObject(i).getString("userid");
                System.out.println("员工id：" + userid);

                //将result的每个对象的id放入employee实体类的code字段中
                employeeFangDe.setCode(userid);
                employeeFangDe.setIdtype("身份证");
                employeeFangDe.setBanktype("工资卡");
                employeeFangDe.setStatus(1);

                for (int a = 0; a<fieldListArray.size(); a++){
                    String fieldCode = fieldListArray
                            .getJSONObject(a)
                            .getString("field_code");

                    switch (fieldCode){
                        //姓名
                        case "sys00-name":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***《《《姓名》》》***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employeeFangDe.setName(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employeeFangDe.setName("");
                            }
                            break;

                        //合同公司（业务单元名称）
                        case "145d3425-9dea-42a6-88ae-1e4e3590046c":
                            if (fieldListArray.getJSONObject(a).getString("label") != null || fieldListArray.getJSONObject(a).getString("label") != ""){
                                System.out.println("***合同公司（业务单元名称）***-->" + fieldListArray.getJSONObject(a).getString("label"));

                                employeeFangDe.setOrgname(fieldListArray.getJSONObject(a).getString("label"));
                                employeeFangDe.setIndutyorgname(fieldListArray.getJSONObject(a).getString("label"));
                            }
                            else{
                                employeeFangDe.setOrgname("");
                                employeeFangDe.setIndutyorgname("");
                            }
                            break;

                        //人员成本分摊一级部门
                        case "473f9667-d220-426f-8431-56be1e8a027d":
                            if (fieldListArray.getJSONObject(a).getString("label") != null){
                                System.out.println("***人员成本分摊一级部门***-->" + fieldListArray.getJSONObject(a).getString("label"));

                                employeeFangDe.setDef1(fieldListArray.getJSONObject(a).getString("label"));
                            }
                            else{
                                employeeFangDe.setDef1("");
                            }
                            break;

                        //人员成本分摊二级部门
                        case "395840c1-2cbe-4f36-b3fb-249d7ddf42f2":
                            if (fieldListArray.getJSONObject(a).getString("label") != null){
                                System.out.println("***人员成本分摊二级部门***-->" + fieldListArray.getJSONObject(a).getString("label"));

                                employeeFangDe.setDeptname(fieldListArray.getJSONObject(a).getString("label"));
                            }
                            else{
                                employeeFangDe.setDeptname("");
                            }
                            break;

                        //入职时间
                        case "sys00-confirmJoinTime":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***入职时间***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employeeFangDe.setIndutydate(fieldListArray.getJSONObject(a).getDate("value"));
                            }
                            else{
                                employeeFangDe.setIndutydate(null);
                            }
                            break;

                        //性别
                        case "sys02-sexType":
                            if (fieldListArray.getJSONObject(a).getString("label") != null){
                                System.out.println("***性别***-->" + fieldListArray.getJSONObject(a).getString("label"));
//                            String sex = fieldListArray.getJSONObject(a).getInteger("value") == 0 ?"男":"女";
//                            employee.setSys02SexType(sex);
                                employeeFangDe.setSex(fieldListArray.getJSONObject(a).getString("label"));
                            }
                            else{
                                employeeFangDe.setSex(null);
                            }
                            break;

                        //证件号码
                        case "sys02-certNo":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***证件号码***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employeeFangDe.setId1(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employeeFangDe.setId1("");
                            }
                            break;

//                        //合同公司（任职业务单元名称）
//                        case "145d3425-9dea-42a6-88ae-1e4e3590046c":
//                            if (fieldListArray.getJSONObject(a).getString("label") != null || fieldListArray.getJSONObject(a).getString("label") != ""){
//                                System.out.println("***合同公司（任职业务单元名称）***-->" + fieldListArray.getJSONObject(a).getString("label"));
//
//                                employeeFangDe.setIndutyorgname(fieldListArray.getJSONObject(a).getString("label"));
//                            }
//                            else{
//                                employeeFangDe.setIndutyorgname("");
//                            }
//                            break;

                        //员工类别
                        case "8b655cff-d575-4742-bbd4-ddc9fd4b5acf":
                            if (fieldListArray.getJSONObject(a).getString("label") != null){
                                System.out.println("***员工类型***-->" + fieldListArray.getJSONObject(a).getString("label"));

                                employeeFangDe.setPsnclname(fieldListArray.getJSONObject(a).getString("label"));
                            }
                            else{
                                employeeFangDe.setPsnclname(null);
                            }
                            break;

                        //开户行
                        case "sys04-accountBank":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***开户行***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employeeFangDe.setBank(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employeeFangDe.setBank("");
                            }
                            break;

                        //银行卡号
                        case "sys04-bankAccountNo":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***银行卡号***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employeeFangDe.setBanknum(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employeeFangDe.setBanknum("");
                            }
                            break;

                        //户名
                        case "ed21c116-efcb-49f3-a507-f8474450260c":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***户名***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employeeFangDe.setBankname(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employeeFangDe.setBankname("");
                            }
                            break;

                        //员工状态
//                        case "sys01-employeeStatus":
//                            if (fieldListArray.getJSONObject(a).getString("value") != null){
//                                System.out.println("***员工状态***-->" + fieldListArray.getJSONObject(a).getString("value"));
//
//                                employeeFangDe.setStatus(1);
//                            }
//                            else{
//                                employeeFangDe.setStatus(null);
//                            }
//                            break;
                    }
                }
                employeeFangDeMapper.insert(employeeFangDe);
            }
            //TODO ---原始同步花名册数据至数据库--- //


            if (result1.getString("next_cursor") != null || result1.getString("data_list") != "") {

                offset = offset + size;

            }

        } while (result1.getString("next_cursor") != null);

        return "同步在职人员花名册数据成功！";
    }

    //同步离职人员花名册数据至房德数据库
    @Override
    public String addFangDeLiZhiHuamingce(){

        //---*** 执行同步离职员工信息 ***---//
        //---*** 执行同步离职员工信息 ***---//
        //---*** 执行同步离职员工信息 ***---//

        //离职员工id列表的循环获取
        DingTalkClient client1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/querydimission");
        OapiSmartworkHrmEmployeeQuerydimissionRequest request1 = new OapiSmartworkHrmEmployeeQuerydimissionRequest();

        //企业在职员工userid列表
        int offset = 0;//分页游标，从0开始
        int size = 50;//分页大小，最大50

        JSONObject result1;

        do {
            List<String> list = new ArrayList<>();

            System.out.println("测试分页值：" + offset);

            try {
                request1.setOffset((long) offset);
                request1.setSize((long) size);
                OapiSmartworkHrmEmployeeQuerydimissionResponse response = client1.execute(request1, fangDeDingAccessTokenService.getFangDeAccessToken());
                employee2 = response.getBody();
                System.out.println(response.getBody());
            } catch (ApiException e) {
                e.printStackTrace();
            }

            //通过fastjson工具转换成json对象,逐层解析
            JSONObject jsonObject1 = JSON.parseObject(employee2);
            result1 = jsonObject1.getJSONObject("result");

            String dataList = result1.getString("data_list");

            //将String数组进行处理，去掉所有的双引号（""）和中括号（[]）
            String dataRes = dataList.replace("\"", "").
                    replaceAll("\\[", "").
                    replaceAll("\\]", "");

            list.add(dataRes);

            String userid1 = list.toString().replace("\"", "").
                    replaceAll("\\[", "").
                    replaceAll("\\]", "");


            //TODO ---原始同步花名册数据至数据库--- //
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/smartwork/hrm/employee/list");
            OapiSmartworkHrmEmployeeListRequest request = new OapiSmartworkHrmEmployeeListRequest();

            try {
                request.setUseridList(userid1);

                //需要获取的花名册字段信息
//                request.setFieldFilterList("sys00-name,145d3425-9dea-42a6-88ae-1e4e3590046c,473f9667-d220-426f-8431-56be1e8a027d," +
//                        "395840c1-2cbe-4f36-b3fb-249d7ddf42f2,sys00-confirmJoinTime,sys02-sexType,sys02-certNo,sys01-employeeType," +
//                        "sys04-accountBank,sys04-bankAccountNo,ed21c116-efcb-49f3-a507-f8474450260c,sys01-employeeStatus");

                OapiSmartworkHrmEmployeeListResponse response = client.execute(request, fangDeDingAccessTokenService.getFangDeAccessToken());
                huamingce2 = response.getBody();
                System.out.println(response.getBody());
            }catch (ApiException e){
                e.printStackTrace();
            }

            //通过fastjson工具转换成json对象,逐层解析
            //将String类型的花名册json数据转换为Object数据对象
            JSONObject jsonObject = JSON.parseObject(huamingce2);

            //直接将花名册对象通过Object转换成Array数组格式
            JSONArray resultArray = jsonObject.getJSONArray("result");

            //new一个employeeFangDe对象
            EmployeeFangDe employeeFangDe = new EmployeeFangDe();

            for (int i = 0; i<resultArray.size(); i++){
                JSONArray fieldListArray = resultArray
                        .getJSONObject(i).getJSONArray("field_list");

                String userid = resultArray.getJSONObject(i).getString("userid");
                System.out.println("员工id：" + userid);

                //将result的每个对象的id放入employee实体类的code字段中
                employeeFangDe.setCode(userid);
                employeeFangDe.setIdtype("身份证");
                employeeFangDe.setBanktype("工资卡");
                employeeFangDe.setStatus(2);

                for (int a = 0; a<fieldListArray.size(); a++){
                    String fieldCode = fieldListArray
                            .getJSONObject(a)
                            .getString("field_code");

                    switch (fieldCode){
                        //姓名
                        case "sys00-name":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***《《《姓名》》》***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employeeFangDe.setName(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employeeFangDe.setName("");
                            }
                            break;

                        //合同公司（业务单元名称）
                        case "145d3425-9dea-42a6-88ae-1e4e3590046c":
                            if (fieldListArray.getJSONObject(a).getString("label") != null || fieldListArray.getJSONObject(a).getString("label") != ""){
                                System.out.println("***合同公司（业务单元名称）***-->" + fieldListArray.getJSONObject(a).getString("label"));

                                employeeFangDe.setOrgname(fieldListArray.getJSONObject(a).getString("label"));
                                employeeFangDe.setIndutyorgname(fieldListArray.getJSONObject(a).getString("label"));
                            }
                            else{
                                employeeFangDe.setOrgname("");
                                employeeFangDe.setIndutyorgname("");
                            }
                            break;

                        //人员成本分摊一级部门
                        case "473f9667-d220-426f-8431-56be1e8a027d":
                            if (fieldListArray.getJSONObject(a).getString("label") != null){
                                System.out.println("***人员成本分摊一级部门***-->" + fieldListArray.getJSONObject(a).getString("label"));

                                employeeFangDe.setDef1(fieldListArray.getJSONObject(a).getString("label"));
                            }
                            else{
                                employeeFangDe.setDef1("");
                            }
                            break;

                        //人员成本分摊二级部门
                        case "395840c1-2cbe-4f36-b3fb-249d7ddf42f2":
                            if (fieldListArray.getJSONObject(a).getString("label") != null){
                                System.out.println("***人员成本分摊二级部门***-->" + fieldListArray.getJSONObject(a).getString("label"));

                                employeeFangDe.setDeptname(fieldListArray.getJSONObject(a).getString("label"));
                            }
                            else{
                                employeeFangDe.setDeptname("");
                            }
                            break;

                        //入职时间
                        case "sys00-confirmJoinTime":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***入职时间***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employeeFangDe.setIndutydate(fieldListArray.getJSONObject(a).getDate("value"));
                            }
                            else{
                                employeeFangDe.setIndutydate(null);
                            }
                            break;

                        //性别
                        case "sys02-sexType":
                            if (fieldListArray.getJSONObject(a).getString("label") != null){
                                System.out.println("***性别***-->" + fieldListArray.getJSONObject(a).getString("label"));
//                            String sex = fieldListArray.getJSONObject(a).getInteger("value") == 0 ?"男":"女";
//                            employee.setSys02SexType(sex);
                                employeeFangDe.setSex(fieldListArray.getJSONObject(a).getString("label"));
                            }
                            else{
                                employeeFangDe.setSex(null);
                            }
                            break;

                        //证件号码
                        case "sys02-certNo":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***证件号码***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employeeFangDe.setId1(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employeeFangDe.setId1("");
                            }
                            break;

//                        //合同公司（任职业务单元名称）
//                        case "145d3425-9dea-42a6-88ae-1e4e3590046c":
//                            if (fieldListArray.getJSONObject(a).getString("label") != null || fieldListArray.getJSONObject(a).getString("label") != ""){
//                                System.out.println("***合同公司（任职业务单元名称）***-->" + fieldListArray.getJSONObject(a).getString("label"));
//
//                                employeeFangDe.setIndutyorgname(fieldListArray.getJSONObject(a).getString("label"));
//                            }
//                            else{
//                                employeeFangDe.setIndutyorgname("");
//                            }
//                            break;

                        //员工类型
                        case "sys01-employeeType":
                            if (fieldListArray.getJSONObject(a).getString("label") != null){
                                System.out.println("***员工类型***-->" + fieldListArray.getJSONObject(a).getString("label"));

                                employeeFangDe.setPsnclname(fieldListArray.getJSONObject(a).getString("label"));
                            }
                            else{
                                employeeFangDe.setPsnclname(null);
                            }
                            break;

                        //开户行
                        case "sys04-accountBank":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***开户行***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employeeFangDe.setBank(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employeeFangDe.setBank("");
                            }
                            break;

                        //银行卡号
                        case "sys04-bankAccountNo":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***银行卡号***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employeeFangDe.setBanknum(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employeeFangDe.setBanknum("");
                            }
                            break;

                        //户名
                        case "ed21c116-efcb-49f3-a507-f8474450260c":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***户名***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employeeFangDe.setBankname(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employeeFangDe.setBankname("");
                            }
                            break;

                        //员工状态
//                        case "sys01-employeeStatus":
//                            if (fieldListArray.getJSONObject(a).getString("value") != null){
//                                System.out.println("***员工状态***-->" + fieldListArray.getJSONObject(a).getString("value"));
//
//                                employeeFangDe.setStatus(2);
//                            }
//                            else{
//                                employeeFangDe.setStatus(null);
//                            }
//                            break;
                    }
                }
                employeeFangDeMapper.insert(employeeFangDe);
            }
            //TODO ---原始同步花名册数据至数据库--- //


            if (result1.getString("next_cursor") != null || result1.getString("data_list") != "") {

                offset = offset + size;

            }

        } while (result1.getString("next_cursor") != null);

        return "同步离职人员花名册数据成功！";
    }

}
