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.OapiAttendanceGetsimplegroupsRequest;
import com.dingtalk.api.request.OapiAttendanceGroupQueryRequest;
import com.dingtalk.api.request.OapiAttendanceListRecordRequest;
import com.dingtalk.api.request.OapiAttendanceListRequest;
import com.dingtalk.api.response.OapiAttendanceGetsimplegroupsResponse;
import com.dingtalk.api.response.OapiAttendanceGroupQueryResponse;
import com.dingtalk.api.response.OapiAttendanceListRecordResponse;
import com.dingtalk.api.response.OapiAttendanceListResponse;
import com.muzhi.dingtalk.test.service.DingAccessTokenService;
import com.muzhi.dingtalk.test.service.DingdingLianJieQiService;
import com.muzhi.dingtalk.test.service.EmployeeService;
import com.muzhi.dingtalk.test.service.KaoqinService;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;


/**
 * @Author ZhangKan
 * @Data 20:32
 */

@Service
public class KaoqinServiceImpl implements KaoqinService {

    @Autowired
    private DingAccessTokenService dingAccessTokenService;

    @Autowired
    private DingdingLianJieQiService dingdingLianJieQiService;

    @Autowired
    private EmployeeService employeeService;

    public static String kaoqinXQ = null;
    public static String kaoqinJG = null;
    public static String kaoqinzuList = null;
    public static String kaoqinzuXQ = null;


    //获取考勤结果
    @Override
    public String getKaoqinJG(){

        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/attendance/list");
        OapiAttendanceListRequest request = new OapiAttendanceListRequest();
        try {

            //打卡记录起始工作日时间
            request.setWorkDateFrom("2021-03-25 00:00:00");
            //打卡记录结束工作日时间
            request.setWorkDateTo("2021-03-25 00:00:00");

//            request.setUserIdList(Arrays.asList(employeeService.getEmployeeId()));
//            request.setUserIdList(employeeService.getEmployeeIdAll());

            //员工userid
            request.setUserIdList(Arrays.asList("manager7290"));

            //分页值
            request.setOffset(0L);
            //最大条数
            request.setLimit(50L);

            OapiAttendanceListResponse response = client.execute(request,dingAccessTokenService.getMuZhiAccessToken());
            kaoqinJG = response.getBody();
            System.out.println(response.getBody());
        } catch (ApiException e) {
            e.printStackTrace();
        }

        //通过fastjson工具转换成json对象,逐层解析
        JSONObject jsonObject = JSON.parseObject(kaoqinJG);
        JSONArray recordresult = jsonObject.getJSONArray("recordresult");

        String recordresult1 = jsonObject.getString("recordresult");
        System.out.println("打卡结果：" + recordresult1);

        int resultSize = recordresult.size();
        System.out.println("打卡结果数据总数：" + resultSize);

        double d = 0;

        for (int a = 0; a < recordresult.size(); a++){
            String checkType = recordresult.getJSONObject(a).getString("checkType");
            System.out.println("上下班类型：" + checkType);

            switch (checkType){
                case "OffDuty":
                    String baseCheckTime = recordresult.getJSONObject(a).getString("baseCheckTime");
                    System.out.println("下班标准时间：" + baseCheckTime);

                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                    String sd = simpleDateFormat.format(new Date(Long.parseLong(String.valueOf(baseCheckTime))));
                    System.out.println("下班日期格式：" + sd);


                    //实际打卡时间
                    String aa = "2021-03-25 17:44:00";

                    try {
                        Date date = simpleDateFormat.parse(sd);
                        System.out.println("时间1" + date);
                        Date date1 = simpleDateFormat.parse(aa);
                        System.out.println("时间2" + date1);

                        //下班标准时间 - 实际打卡时间（时间戳相减）
                        long time = date1.getTime() - date.getTime();
                        //分钟
                        long time1 = (time / 1000) / 60;
                        System.out.println("下班标准时间（毫秒级）：" + time);
                        System.out.println("计算后的实际时间（分钟）：" + time1);

                        //计算后的实际时间（小时）
                        d = (double)time1 / 60;

                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
            }

        }

//        String a = String.format("%.1f", d);
//        System.out.println("保留1位小数：" + a);

//        DecimalFormat df = new DecimalFormat("0.0");
//        System.out.println("测试double小数位：" + df.format(666.123456789456));

        DecimalFormat df = new DecimalFormat("0.0");
        String jisuanTime = df.format(d);

        double jisuanTime1 = Double.parseDouble(jisuanTime);

        System.out.println("计算后的实际时间（小时）：" + jisuanTime1);

        return null;
    }


    //获取考勤详情
    @Override
    public String getKaoqinXQ(){
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/attendance/listRecord");
        OapiAttendanceListRecordRequest request = new OapiAttendanceListRecordRequest();
        try {
            request.setCheckDateFrom("2020-11-02 00:00:00");
            request.setCheckDateTo("2020-11-08 00:00:00");

//            request.setUserIds(Arrays.asList("16003030121002248","15997556700605983"));
//            request.setUserIds(Arrays.asList(employeeService.getEmployeeId()));

            request.setUserIds(employeeService.getEmployeeIdAll());

            OapiAttendanceListRecordResponse response = client.execute(request,dingAccessTokenService.getAccessToken());
            kaoqinXQ = response.getBody();
            System.out.println(response.getBody());
        } catch (ApiException e) {
            e.printStackTrace();
        }

        return kaoqinXQ;
    }



    //获取考勤组列表详情
    @Override
    public String getKaoqinzuList(){
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/getsimplegroups");
        OapiAttendanceGetsimplegroupsRequest request = new OapiAttendanceGetsimplegroupsRequest();
        try {
            request.setOffset(0L);
            request.setSize(10L);
            OapiAttendanceGetsimplegroupsResponse response = client.execute(request, dingAccessTokenService.getAccessToken());
            kaoqinzuList = response.getBody();
            System.out.println(response.getBody());
        }catch (ApiException e){
            e.printStackTrace();
        }

        return kaoqinzuList;
    }

    //获取考勤组详情
    @Override
    public String getKaoqinzuXQ(){
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/group/query");
        OapiAttendanceGroupQueryRequest request = new OapiAttendanceGroupQueryRequest();
        try {
            request.setOpUserId("16051955788282051");
            request.setGroupId(105097853L);
            OapiAttendanceGroupQueryResponse response = client.execute(request, dingAccessTokenService.getAccessToken());
            kaoqinzuXQ = response.getBody();
            System.out.println(response.getBody());
        }catch (ApiException e){
            e.printStackTrace();
        }

        return kaoqinzuXQ;
    }

}
