package com.malk.mc;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.malk.mc.service.McYdService;
import com.malk.server.aliwork.YDConf;
import com.malk.server.aliwork.YDParam;
import com.malk.server.dingtalk.DDR_New;
import com.malk.service.aliwork.YDClient;
import com.malk.service.dingtalk.DDClient;
import com.malk.service.dingtalk.DDClient_Extension;
import com.malk.service.dingtalk.DDClient_Workflow;
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.slf4j.MDC;
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.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Slf4j
@SpringBootTest
@RunWith(SpringRunner.class)
public class McTest {

    @Autowired
    private DDClient_Extension ddClientExtension;
    @Autowired
    private YDClient ydClient;
    @Autowired
    private DDClient ddClient;

    @Test
    public void test8(){
        MDC.put("MDC_KEY_PID","1041");
        String str="{   \"state\": \"实施中\",   \"color\": \"绿\",   \"projectName\": \"AIOA\",   \"submitter\": \"钉三多\",   \"content\": \"三楼没空调\",   \"finishDate\": \"05月20日 12:00\",   \"redirectUrl\": \"https://www.dingtalk.com/\" }";
        Map<String,Object> data = new HashMap();
        data.put("imGroupOpenDeliverModel",UtilMap.map("robotCode","dingrlahvtc530jgfati"));

        Map map = ddClientExtension.createAndDeliverCards(ddClient.getAccessToken(),"a2ea5959-0307-4e7e-9f1a-f86614b772ba.schema","1212",
                UtilMap.map("cardParamMap",JSONObject.parseObject(str)),"dtv1.card//IM_GROUP.cidSkF0KW3Z18zr2/lGPqCk0g==",data);

        System.out.println(map);
    }

    @Test
    public void test9(){
        MDC.put("MDC_KEY_PID","1041");
        List<Map> data = (List<Map>) ydClient.queryData(YDParam.builder().formUuid("FORM-3EFF01F12DFF4CA0B0A8ADD735C664B1GY5L")
                .createFromTimeGMT(LocalDate.now().minusDays(1).toString()+" 02:00:00")
                .createToTimeGMT(LocalDate.now().toString()+" 01:59:59").build(), YDConf.FORM_QUERY.retrieve_list).getData();
        String text="### 交付部今日工作安排\n";
        List<String> userIds=new ArrayList<>();
        for(Map map:data){
            Map formData = (Map) map.get("formData");
            userIds.add(String.valueOf(map.get("creatorUserId")));
            text=text+"@"+String.valueOf(map.get("creatorUserId"))+"核心事项" + strFormat("\n"+String.valueOf(formData.get("textareaField_m8479h6d")))+"\n\n";
        }
        System.out.println(text);
//        JSONObject object=new JSONObject();
//        object.put("msgtype","text");
//        object.put("text",UtilMap.map("content",text));
//        object.put("at",UtilMap.map("atUserIds, isAtAll",userIds,false));
//        String result=HttpUtil.createPost("https://oapi.dingtalk.com/robot/send?access_token=8717934232803fed4496ae5e64fa7f3c7a3db58c786e6517ce74af5208664e07")
//                .body(JSONObject.toJSONString(object)).execute().body();
//        System.out.println(result);
    }

    private String strFormat(String input){
        StringBuilder output = new StringBuilder(input);
        int count = 0;
        int index = 0;

        while ((index = output.indexOf("\n", index)) != -1) {
            count++; // 计数器增加
            output.insert(index + 1, "工作"+count+" "); // 在 \n 后插入数字
            index += 2; // 跳过插入的数字，继续查找下一个 \n
        }
        return output.toString();
    }

    @Autowired
    private DDClient_Workflow ddClientWorkflow;

    @Test
    public void test10(){
        ddClientWorkflow.createTBTask(ddClient.getAccessToken(),"","标题","描述",
                UtilDateTime.getLocalDateTimeTimeStamp(LocalDate.now().plusDays(7).atTime(LocalTime.MAX)), Arrays.asList(""),Arrays.asList("")
        ,UtilMap.map("appUrl, pcUrl","www","www"),false,30,UtilMap.map("dingNotify","1"));
    }

    @Test
    public void test1(){
        log.info("test1");
    }
}
