package com.malk.mc.service.impl;

import com.alibaba.fastjson.JSONObject;
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.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[]> data = 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");
                data.put(UtilMap.getString(formData,"serialNumberField_lxlfw7nn"),new String[]{UtilMap.getString(formData,"textField_lxlfw7no"),UtilMap.getString(formData,"textField_lxlfw7np")});
            }
        }while (i<t);
        McProject.insertYida(data);
        log.info("项目加载完毕：{}",data.keySet());
    }
}
