package com.malk.pake;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONObject;
import com.malk.pake.service.PkDingProcService;
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_Alibtrip;
import com.malk.service.dingtalk.DDClient_Workflow;
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.io.File;
import java.util.*;

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

    @Autowired
    private DDClient ddClient;
    @Autowired
    private DDClient_Workflow ddClient_workflow;
    @Autowired
    private PkDingProcService pkDingProcService;
    @Autowired
    private YDClient ydClient;

    @Autowired
    private DDClient_Alibtrip ddClient_alibtrip;

    @Test
    public void tes2t(){
        String result=ddClient_alibtrip.addProject(ddClient.getAccessToken(),"测试项目","PK-YF-2025033");
        System.out.println(result);
    }

    @Test
    public void test(){
        pkDingProcService.syncAmt("Agt9MobvR-ubwYeGd14fmA06821717753263");
//        System.out.println("hello");
    }






    private String code="PROC-27EE84F7-EA7E-460C-B9F4-21D54DFD11AA";

    @Test
    public void syncProject(){
//        List<String> list= FileUtil.readUtf8Lines("/home/ids.txt");
//        for (int i = 0; i < list.size(); i++) {
//            log.info("处理进度：{}/{}",i+1,list.size());
//            String[] data=list.get(i).split("###");
//            create(data[0],data[1],data[2]);
//        }

        int i=0,t=0;
        do {
            i++;
            DDR_New projectData = ydClient.queryData(YDParam.builder()
                    .formUuid("FORM-NO96679107GDYGUK9SM8T42G95KA2KZOMUELL4")
                    .pageNumber(i)
                    .build(), YDConf.FORM_QUERY.retrieve_list);
            if(i==1){
                t=Integer.parseInt(String.valueOf((projectData.getTotalCount()/YDConf.PAGE_SIZE_LIMIT)))+1;
            }
            List<Map> dataList = (List<Map>) projectData.getData();
            for (Map map:dataList){
                Map formData=UtilMap.getMap(map,"formData");
                try {
                    create(UtilMap.getString(formData,"textField_llemcq7b"),UtilMap.getString(formData,"textField_llemcq7d"),UtilMap.getString(formData,"employeeField_llemcq8c_id"));
                }catch (Exception e){
                    log.info("数据：{}，{}，{}",UtilMap.getString(formData,"textField_llemcq7b"),UtilMap.getString(formData,"textField_llemcq7d"),UtilMap.getString(formData,"employeeField_llemcq8c_id"));
                    e.printStackTrace();
                }
            }
        }while (i<t);
    }

    @Test
    public void  ttt(){
        String aa="{\"projectName\":\"安徽电信华视网聚内容运营支撑\",\"projectId\":\"XY-DY-2024024\",\"userId\":[\"01254038076338840748\"]}";

        JSONObject param=JSONObject.parseObject(aa);
        // {"projectName":"安徽电信华视网聚内容运营支撑","projectId":"XY-DY-2024024","userId":["01254038076338840748"]}
        create(param.getString("projectName"),param.getString("projectId"),param.getString("userId"));
    }

    private void create(String name,String id,String userId){
        ddClient_workflow.doProcessInstances(ddClient.getAccessToken(),"17104651870654620","PROC-27EE84F7-EA7E-460C-B9F4-21D54DFD11AA", Arrays.asList(
                dataName(name),dataId(id),dataPeople(userId)
                ),
                UtilMap.map("dept_id","-1"));
    }

    private Map dataName(String name){
        return UtilMap.map("name, value","项目名称",name);
    }

    private Map dataId(String id){
        return UtilMap.map("name, value","项目编号",id);
    }

    private Map dataPeople(String userId){
        return UtilMap.map("name, value","项目经理",userId);
    }

}
