package com.malk.mc.service.impl;

import com.malk.core.McProject;
import com.malk.mc.service.McProjectService;
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.utils.PublicUtil;
import com.malk.utils.UtilMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class McProjectServiceImpl implements McProjectService {

    @Autowired
    private YDClient ydClient;

    @Override
    public void init() {
        int i=0;
        int t=1;
        Map<String, String[]> ydData = new HashMap();
        Map<String, String[]> ddData = new HashMap();
        do {
            i++;
            DDR_New projectData = ydClient.queryData(YDParam.builder()
                    .formUuid("FORM-7ACBE9705A034ED2B19397A883781175LQWH")
                    .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");
                ydData.put(UtilMap.getString(formData,"serialNumberField_lxlfw7nn"),new String[]{UtilMap.getString(formData,"textField_lxlfw7no"),UtilMap.getString(formData,"textField_lxlfw7np")});
                if(!PublicUtil.isNull(formData,"textField_m7abagvx","textField_m7abagvy")){
                    ddData.put(UtilMap.getString(formData,"serialNumberField_lxlfw7nn"),new String[]{UtilMap.getString(formData,"textField_m7abagvx"),UtilMap.getString(formData,"textField_m7abagvy"),UtilMap.getString(formData,"textField_mbyfztyy")});
                }
            }
        }while (i<t);
        McProject.insertYida(ydData);
        McProject.insertDD(ddData);
        log.info("项目加载完毕：{} 钉钉：{}",ydData.keySet(),ddData.keySet());
    }

    public static void main(String[] args) {
        System.out.println(strFormat("\n优尚和越：表单的搭建\n未来节拍：根据客户的需求进行调整\n未来节拍：根据客户的需求进行调整\n未来节拍：根据客户的需求进行调整"));
    }

    private static 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();
    }
}
