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.OapiDepartmentListRequest;
import com.dingtalk.api.request.OapiSmartworkHrmEmployeeListRequest;
import com.dingtalk.api.request.OapiSmartworkHrmEmployeeQueryonjobRequest;
import com.dingtalk.api.response.OapiDepartmentListResponse;
import com.dingtalk.api.response.OapiSmartworkHrmEmployeeListResponse;
import com.dingtalk.api.response.OapiSmartworkHrmEmployeeQueryonjobResponse;
import com.muzhi.dingtalk.test.entity.Department;
import com.muzhi.dingtalk.test.entity.Employee;
import com.muzhi.dingtalk.test.mapper.DepartmentMapper;
import com.muzhi.dingtalk.test.mapper.EmployeeMapper;
import com.muzhi.dingtalk.test.service.DingAccessTokenService;
import com.muzhi.dingtalk.test.service.EmployeeService;
import com.muzhi.dingtalk.test.service.TongbuService;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class TongbuServiceImpl implements TongbuService{

    @Autowired
    private DingAccessTokenService dingAccessTokenService;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private DepartmentMapper departmentMapper;

    public static String huamingce = null;
    public static String departmentList = null;
    public static String departmentXQ = null;

    public static String employee1 = null;

    //查询数据库中employee表的所有数据
    @Override
    public List<Employee> getEmployee(){
        List<Employee> employeeList = employeeMapper.selectList(null);
        return employeeList;
    }

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

    //同步花名册数据至数据库
    @Override
    public String addHuamingce(){

        //在职员工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 = 20;//分页大小，最大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, dingAccessTokenService.getAccessToken());
                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,sys02-sexType,sys01-employeeStatus,sys00-email,sys03-graduateSchool,sys03-highestEdu,sys00-dept,e3d6dbbd-9265-4f1e-8c58-ac3602e83960,sys03-graduationTime,sys00-mobile,sys02-address,sys02-marriage,sys00-confirmJoinTime,sys00-workPlace,sys00-position,sys01-positionLevel,sys02-politicalStatus,sys03-major,sys02-certNo,sys02-birthTime,sys02-certAddress,sys02-certEndTime,sys02-residenceType,sys01-employeeType,sys04-bankAccountNo,sys04-accountBank,sys09-personalHf,sys05-contractType,sys05-contractCompanyName,ff63191a-45cf-4106-ade0-54f1def7cc55");

                OapiSmartworkHrmEmployeeListResponse response = client.execute(request, dingAccessTokenService.getAccessToken());
                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一个employee对象
            Employee employee = new Employee();

            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实体类的id字段中
                employee.setId(userid);

                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"));

                                //放入employee表的姓名字段中
                                employee.setSys00Name(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                //如果没有则放入空值
                                employee.setSys00Name("");
                            }
                            break;

//                  //邮箱
                        case "sys00-email":
                            if (fieldListArray.getJSONObject(a).getString("value") != null || fieldListArray.getJSONObject(a).getString("value") != ""){
                                System.out.println("***邮箱***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys00Email(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys00Email("");
                            }
                            break;

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

                                employee.setSys00Tel(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys00Tel("");
                            }
                            break;

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

                                employee.setSys00Mobile(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys00Mobile("");
                            }
                            break;

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

                                employee.setSys00JobNumber(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys00JobNumber("");
                            }
                            break;

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

                                employee.setSys00Remark(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys00Remark("");
                            }
                            break;

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

                                employee.setSys00ConfirmJoinTime(fieldListArray.getJSONObject(a).getDate("value"));
                            }
                            else{
                                employee.setSys00ConfirmJoinTime(null);
                            }
                            break;

                        //办公地点
                        case "sys00-workPlace":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***办公地点***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys00WorkPlace(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys00WorkPlace("");
                            }
                            break;

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

                                employee.setSys00Position(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys00Position("");
                            }
                            break;

                        //岗位职级
                        case "sys01-positionLevel":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***岗位职级***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys01PositionLevel(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys01PositionLevel("");
                            }
                            break;

                        //政治面貌
                        case "sys02-politicalStatus":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***政治面貌***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys02PoliticalStatus(fieldListArray.getJSONObject(a).getInteger("value"));
                            }
                            else{
                                employee.setSys02PoliticalStatus(null);
                            }
                            break;

                        //所学专业
                        case "sys03-major":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***所学专业***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys03Major(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys03Major("");
                            }
                            break;

                        //现合同到期日
                        case "sys05-nowContractEndTime":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***现合同到期日***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys05NowContractEndTime(fieldListArray.getJSONObject(a).getDate("value"));
                            }
                            else{
                                employee.setSys05NowContractEndTime(null);
                            }
                            break;

                        //转正日期
                        case "sys01-regularTime":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***转正日期***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys01RegularTime(fieldListArray.getJSONObject(a).getDate("value"));
                            }
                            else{
                                employee.setSys01RegularTime(null);
                            }
                            break;

                        //身份证地址
                        case "sys02-certAddress":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***身份证地址***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys02CertAddress(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys02CertAddress("");
                            }
                            break;

                        //子女出生日期
                        case "sys07-childBirthDate":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***子女出生日期***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys07ChildBirthDate(fieldListArray.getJSONObject(a).getDate("value"));
                            }
                            else{
                                employee.setSys07ChildBirthDate(null);
                            }
                            break;

                        //户籍类型
                        case "sys02-residenceType":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***户籍类型***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys02ResidenceType(fieldListArray.getJSONObject(a).getInteger("value"));
                            }
                            else{
                                employee.setSys02ResidenceType(null);
                            }
                            break;

                        //有无子女
                        case "sys07-haveChild":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***有无子女***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys07HaveChild(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys07HaveChild("");
                            }
                            break;

                        //身份证姓名
                        case "sys02-realName":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***身份证姓名***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys02RealName(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys02RealName("");
                            }
                            break;

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

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

                                employee.setSys01EmployeeType(fieldListArray.getJSONObject(a).getInteger("value"));
                            }
                            else{
                                employee.setSys01EmployeeType(null);
                            }
                            break;

                        //合同期限
                        case "sys05-contractPeriodType":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***合同期限***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys05ContractPeriodType(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys05ContractPeriodType("");
                            }
                            break;

                        //试用期
                        case "sys01-probationPeriodType":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***试用期***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys01ProbationPeriodType(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys01ProbationPeriodType("");
                            }
                            break;

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

                                employee.setSys02CertEndTime(fieldListArray.getJSONObject(a).getDate("value"));
                            }
                            else{
                                employee.setSys02CertEndTime(null);
                            }
                            break;

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

                                employee.setSys04BankAccountNo(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys04BankAccountNo("");
                            }
                            break;

                        //续签次数
                        case "sys05-contractRenewCount":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***续签次数***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys05ContractRenewCount(fieldListArray.getJSONObject(a).getInteger("value"));
                            }
                            else{
                                employee.setSys05ContractRenewCount(null);
                            }
                            break;

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

                                employee.setSys04AccountBank(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys04AccountBank("");
                            }
                            break;

                        //个人公积金账号
                        case "sys09-personalHf":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***个人公积金账号***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys09PersonalHf(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys09PersonalHf("");
                            }
                            break;

                        //合同类型
                        case "sys05-contractType":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***合同类型***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys05ContractType(fieldListArray.getJSONObject(a).getInteger("value"));
                            }
                            else{
                                employee.setSys05ContractType(null);
                            }
                            break;

                        //首次合同到期日
                        case "sys05-firstContractEndTime":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***首次合同到期日***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys05FirstContractEndTime(fieldListArray.getJSONObject(a).getDate("value"));
                            }
                            else{
                                employee.setSys05FirstContractEndTime(null);
                            }
                            break;

                        //合同公司
                        case "sys05-contractCompanyName":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***合同公司***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys05ContractCompanyName(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys05ContractCompanyName("");
                            }
                            break;

                        //紧急联系人姓名
                        case "sys06-urgentContactsName":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***紧急联系人姓名***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys06UrgentContactsName(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys06UrgentContactsName("");
                            }
                            break;

                        //子女姓名
                        case "sys07-childName":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***子女姓名***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys07ChildName(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys07ChildName("");
                            }
                            break;

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

                                employee.setSys02CertNo(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys02CertNo("");
                            }
                            break;

                        //出生日期
                        case "sys02-birthTime":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***出生日期***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys02BirthTime(fieldListArray.getJSONObject(a).getDate("value"));
                            }
                            else{
                                employee.setSys02BirthTime(null);
                            }
                            break;

                        //首次合同起始日
                        case "sys05-firstContractStartTime":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***首次合同起始日***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys05FirstContractStartTime(fieldListArray.getJSONObject(a).getDate("value"));
                            }
                            else{
                                employee.setSys05FirstContractStartTime(null);
                            }
                            break;

                        //现合同起始日
                        case "sys05-nowContractStartTime":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***现合同起始日***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys05NowContractStartTime(fieldListArray.getJSONObject(a).getDate("value"));
                            }
                            else{
                                employee.setSys05NowContractStartTime(null);
                            }
                            break;

                        //毕业院校
                        case "sys03-graduateSchool":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***毕业院校***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys03GraduateSchool(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys03GraduateSchool("");
                            }
                            break;

                        //住址
                        case "sys02-address":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***住址***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys02Address(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys02Address("");
                            }
                            break;

                        //联系人电话
                        case "sys06-urgentContactsPhone":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***联系人电话***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys06UrgentContactsPhone(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys06UrgentContactsPhone("");
                            }
                            break;

                        //联系人关系
                        case "sys06-urgentContactsRelation":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***联系人关系***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys06UrgentContactsRelation(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys06UrgentContactsRelation("");
                            }
                            break;

                        //婚姻状况
                        case "sys02-marriage":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***婚姻状况***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys02Marriage(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys02Marriage("");
                            }
                            break;

                        //子女性别
                        case "sys07-childSex":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***子女性别***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys07ChildSex(fieldListArray.getJSONObject(a).getInteger("value"));
                            }
                            else{
                                employee.setSys07ChildSex(null);
                            }
                            break;

                        //首次参加工作时间
                        case "sys02-joinWorkingTime":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***首次参加工作时间***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys02JoinWorkingTime(fieldListArray.getJSONObject(a).getDate("value"));
                            }
                            else{
                                employee.setSys02JoinWorkingTime(null);
                            }
                            break;

                        //毕业时间
                        case "sys03-graduationTime":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***毕业时间***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys03GraduationTime(fieldListArray.getJSONObject(a).getDate("value"));
                            }
                            else{
                                employee.setSys03GraduationTime(null);
                            }
                            break;

                        //员工状态
                        case "sys01-employeeStatus":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***员工状态***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys01EmployeeStatus(fieldListArray.getJSONObject(a).getInteger("value"));
                            }
                            else{
                                employee.setSys01EmployeeStatus(null);
                            }
                            break;

                        //民族
                        case "sys02-nationType":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***民族***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys02NationType(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys02NationType("");
                            }
                            break;

                        //学历
                        case "sys03-highestEdu":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***学历***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys03HighestEdu(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys03HighestEdu("");
                            }
                            break;

                        //部门id
                        case "sys00-deptIds":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***部门id***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys00DeptIds(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys00DeptIds("");
                            }
                            break;

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

                                employee.setSys00Dept(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys00Dept("");
                            }
                            break;

                        //主部门id
                        case "sys00-mainDeptId":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***主部门id***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys00MainDeptId(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys00MainDeptId("");
                            }
                            break;

                        //主部门
                        case "sys00-mainDept":
                            if (fieldListArray.getJSONObject(a).getString("value") != null){
                                System.out.println("***主部门***-->" + fieldListArray.getJSONObject(a).getString("value"));

                                employee.setSys00MainDept(fieldListArray.getJSONObject(a).getString("value"));
                            }
                            else{
                                employee.setSys00MainDept("");
                            }
                            break;
                    }
                }
                employeeMapper.insert(employee);
            }
            //TODO ---原始同步花名册数据至数据库--- //


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

                offset = offset + size;

            }

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


        return "同步花名册数据成功！";
    }

    //删除department表中的所有数据
    @Override
    public String removeDepartment(){
        departmentMapper.delete(null);
        return "清空部门数据成功！";
    }

    //同步部门数据至数据库
    @Override
    public String addDepartment(){
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/department/list");
        OapiDepartmentListRequest request = new OapiDepartmentListRequest();
        try {
            request.setHttpMethod("GET");
            OapiDepartmentListResponse response = client.execute(request, dingAccessTokenService.getAccessToken());
            departmentList = response.getBody();
            System.out.println(response.getBody());
        } catch (ApiException e) {
            e.printStackTrace();
        }

        //通过fastjson工具转换成json对象,逐层解析
        //将String类型的部门列表json数据转换为Object数据对象
        JSONObject jsonObject = JSON.parseObject(departmentList);

        //直接将部门列表对象通过Object转换成Array数组格式
        JSONArray departmentArray = jsonObject.getJSONArray("department");

        //new一个department对象
        Department department = new Department();

        for (int i = 0; i<departmentArray.size(); i++) {

            //部门id
            if (departmentArray.getJSONObject(i).getString("id") != null) {
                System.out.println("***《《《部门id》》》***-->" + departmentArray.getJSONObject(i).getString("id"));

                //放入部门id字段中
                department.setId(departmentArray.getJSONObject(i).getString("id"));
            } else {
                //如果没有则放入空值
                department.setId("");
            }

            //部门名称
            if (departmentArray.getJSONObject(i).getString("name") != null) {
                System.out.println("***《《《部门名称》》》***-->" + departmentArray.getJSONObject(i).getString("name"));

                department.setName(departmentArray.getJSONObject(i).getString("name"));
            } else {
                department.setName("");
            }

            //父部门id
            if (departmentArray.getJSONObject(i).getString("parentid") != null) {
                System.out.println("***《《《父部门id》》》***-->" + departmentArray.getJSONObject(i).getString("parentid"));

                department.setParentid(departmentArray.getJSONObject(i).getString("parentid"));
            } else {
                department.setParentid("");
            }

            //是否同步创建一个关联此部门的企业群
            if (departmentArray.getJSONObject(i).getString("createDeptGroup") != null) {
                System.out.println("***《《《是否同步创建一个关联此部门的企业群》》》***-->" + departmentArray.getJSONObject(i).getString("createDeptGroup"));

                department.setCreateDeptGroup(departmentArray.getJSONObject(i).getString("createDeptGroup"));
            } else {
                department.setCreateDeptGroup("");
            }

            //是否有新人加入部门会自动加入该群
            if (departmentArray.getJSONObject(i).getString("autoAddUser") != null) {
                System.out.println("***《《《是否有新人加入部门会自动加入该群》》》***-->" + departmentArray.getJSONObject(i).getString("autoAddUser"));

                department.setAutoAddUser(departmentArray.getJSONObject(i).getString("autoAddUser"));
            } else {
                department.setAutoAddUser("");
            }

            //部门自定义字段
            if (departmentArray.getJSONObject(i).getString("ext") != null) {
                System.out.println("***《《《部门自定义字段》》》***-->" + departmentArray.getJSONObject(i).getString("ext"));

                department.setExt(departmentArray.getJSONObject(i).getString("ext"));
            } else {
                department.setExt("");
            }

            departmentMapper.insert(department);
            //System.out.println("展示！！！" + department);
        }

        return "同步部门列表数据成功！";
    }


    //删除user表中的所有数据
    @Override
    public String removeUser() {
        return null;
    }

    //同步用户通讯录数据至数据库
    @Override
    public String addUser() {
        return null;
    }
}
