package com.malk.huagao;

import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.malk.huagao.entity.YdUserInput;
import com.malk.huagao.mapper.YdUserInputMapper;
import com.malk.huagao.service.HuaGaoService;
import com.malk.server.aliwork.YDConf;
import com.malk.server.aliwork.YDParam;
import com.malk.service.aliwork.YDClient;
import com.malk.service.aliwork.YDService;
import com.malk.service.dingtalk.DDClient;
import com.malk.service.dingtalk.DDClient_Alibtrip;
import com.malk.service.dingtalk.DDClient_Attendance;
import com.malk.service.dingtalk.DDClient_Contacts;
import com.malk.utils.UtilDateTime;
import com.malk.utils.UtilMap;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * 逸耀宜搭
 */
@Slf4j
@SpringBootTest
@RunWith(SpringRunner.class)
public class YyYdTest {

    @Autowired
    private YDClient ydClient;

    @Autowired
    private HuaGaoService huaGaoService;

    @Autowired
    private YdUserInputMapper ydUserInputMapper;

    @Test
    public void test() {
        List<Map> list=(List<Map>) ydClient.queryData(YDParam.builder().formUuid("FORM-0D560B9D8B64464C8C885FE20B7C7C95T4IA")                .pageSize(1)
        .searchFieldJson(JSONObject.toJSONString(UtilMap.map("selectField_m8nud6t3","待处理"))).build(), YDConf.FORM_QUERY.retrieve_list).getData();
        if(list==null||list.size()<1){
            return;
        }
        Map data=list.get(0);
        log.info("{}",data);
        String formInstanceId=String.valueOf(data.get("formInstanceId"));
        processing(formInstanceId);
        Map formData=UtilMap.getMap(data,"formData");
        try {
            List<String> timeList=UtilMap.getList(formData,"cascadeDateField_m8nud6sy");
            LocalDateTime startTime=UtilDateTime.getLocalDateTimeFromTimestamp(Long.parseLong(timeList.get(0)));
            LocalDateTime endTime=UtilDateTime.getLocalDateTimeFromTimestamp(Long.parseLong(timeList.get(1)));
            huaGaoService.syncKqData(startTime,endTime);
            comp(formInstanceId,"同步成功！","");
        }catch (Exception e){
            e.printStackTrace();
            comp(formInstanceId,"异常",e.getMessage());
        }
    }

    private void processing(String id){
        ydClient.operateData(YDParam.builder().formInstanceId(id)
                .updateFormDataJson(JSON.toJSONString(UtilMap.map("selectField_m8nud6t3", "处理中")))
                .build(), YDConf.FORM_OPERATION.update);
    }

    private void comp(String id,String result,String msg){
        ydClient.operateData(YDParam.builder().formInstanceId(id)
                .updateFormDataJson(JSON.toJSONString(UtilMap.map("selectField_m8nud6t3, textField_m8nud6t9, textareaField_m8nud6tb", "已完成",result,msg)))
                .build(), YDConf.FORM_OPERATION.update);
    }

    @Test
    public void test1() {
//        List<Map> attColumns = ddClientAttendance.getAttColumns(ddClient.getAccessToken());
//        System.out.println(attColumns);
    }

    @Test
    public void test2() {
        //同步员工投入历史数据
//        huaGaoService.syncUserInput(LocalDateTime.now(),LocalDateTime.now());
    }

    @Test
    public void test3() {
        LocalDateTime startTime= LocalDate.now().minusDays(3).atTime(LocalTime.MIN);
        LocalDateTime endTime= LocalDate.now().minusDays(2).atTime(LocalTime.MIN);
//        LocalDateTime endTime= LocalDate.now().atTime(LocalTime.MIN);

        huaGaoService.syncKqData(startTime,endTime);
    }

    @Test
    public void test4() {
        LambdaQueryWrapper<YdUserInput> ydUserInputLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ydUserInputLambdaQueryWrapper.eq(YdUserInput::getUserId,"16992517127785448");
        List<YdUserInput> ydUserInputs = ydUserInputMapper.selectList(ydUserInputLambdaQueryWrapper);

    }
}
