package com.malk.pake;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.malk.core.McProject;
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.utils.PublicUtil;
import com.malk.utils.UtilMap;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
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.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

    @Autowired
    private YDClient ydClient;
    @Autowired
    private YDService ydService;

    @Test
    public void te(){
//        MDC.put("MDC_KEY_PID","1001");
//        McProject.addYida("1001","APP_FTJYB2MKWHXQAFBCZB47,K7D66L91A44LPGKP70LJ8DY05VJT262JR47WLQ8C".split(","));
//        ydService.mirrorFormData("4e6d34ba-b297-43cf-8c3e-463d76c16892","FORM-79DD3E6855BA4B7B93807F32DF4ADD076QQV","",UtilMap.map("textField_lxb9mj4b","111"),"");

        List<String> ids=FileUtil.readLines("/home/ids.txt","utf-8");

        int i = 0;
        for (int t = 0; i < ids.size()/300; i++) {
            ydClient.operateData(YDParam.builder().formInstanceIdList(ids.subList(i*300,(i+1)*300)).formUuid("FORM-223E89E230F3488885A7BEB44352A9C06HZ5").build(), YDConf.FORM_OPERATION.delete_batch);
        }
        ydClient.operateData(YDParam.builder().formInstanceIdList(ids.subList(i*300,ids.size())).formUuid("FORM-223E89E230F3488885A7BEB44352A9C06HZ5").build(), YDConf.FORM_OPERATION.delete_batch);
    }

    @Test
    public void nullData(){
        List<String> list=FileUtil.readLines("/home/ids.txt","utf-8");
        for (int i = 0; i < list.size(); i++) {
            log.info("处理进度：{}/{}",i+1,list.size());
            String[] str=list.get(i).split("###");
            String month=str[0].split("-")[1];
            String id=str[1];
            Long date=LocalDate.of(2024,Integer.parseInt(month),1).atStartOfDay().toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
            ydClient.operateData(YDParam.builder()
                    .formInstanceId(id)
                    .updateFormDataJson(JSONObject.toJSONString(UtilMap.map("dateField_ls2x7t40", date)))
                    .build(), YDConf.FORM_OPERATION.update);
        }
    }

    // 处理数据
    @Test
    public void test3(){
        List<String> list=FileUtil.readLines("/home/ids.txt","utf-8");
        int i=1;
        for (String id:list){
            log.info("处理进度：{}/{}",i,list.size());
            i++;
            Map formData =ydClient.queryData(YDParam.builder()
                    .formInstanceId(id)
                    .build(), YDConf.FORM_QUERY.retrieve_id).getFormData();
            Map mainData=new HashMap();
            mainData.put("textField_lvw66i56",id);
            mainData.put("selectField_lvw57eo9","成都慧享工时");
            mainData.put("departmentSelectField_ls2x7t47",formData.get("departmentSelectField_ls2x7t47_id"));
            mainData.put("employeeField_ls2x7t46",formData.get("employeeField_ls2x7t46_id"));
            mainData.put("dateField_ls2x7t40",formData.get("dateField_ls2x7t40"));// 月份
            mainData.put("dateField_lvw40y8e","");// 日期
            // 项目
            List<Map> list1=UtilMap.getList(formData,"tableField_ls30qr4f");
            for (Map map:list1){
                if(PublicUtil.isNull(map,"textField_ls30qr4a")){
                    continue;
                }
                Map data=new HashMap(mainData);
                data.put("selectField_lvw57eo7","项目");
                data.put("textField_lrpt148q",map.get("textField_ls30qr4a"));
                data.put("selectField_lrhiklat",map.get("selectField_luwcwjak"));
                data.put("numberField_lrhiklas",map.get("numberField_ltqveodc"));
                saveData(data);
            }
            // 商机
            List<Map> list2=UtilMap.getList(formData,"tableField_lts9jusn");
            for (Map map:list2){
                if(PublicUtil.isNull(map,"textField_lts9jush")){
                    continue;
                }
                Map data=new HashMap(mainData);
                data.put("selectField_lvw57eo7","商机");
                data.put("textField_lrpt148q",map.get("textField_lts9jush"));
                data.put("selectField_lrhiklat",map.get("selectField_luwcwjal"));
                data.put("numberField_lrhiklas",map.get("numberField_lts9jusl"));
                saveData(data);
            }
            // 产品
            List<Map> list3=UtilMap.getList(formData,"tableField_lvvpxztg");
            for (Map map:list3){
                if(PublicUtil.isNull(map,"textField_lvvpxzt9")){
                    continue;
                }
                Map data=new HashMap(mainData);
                data.put("selectField_lvw57eo7","产品");
                data.put("textField_lrpt148q",map.get("textField_lvvpxzt9"));
                data.put("selectField_lrhiklat",map.get("selectField_lvvpxztb"));
                data.put("numberField_lrhiklas",map.get("numberField_lvvpxzte"));
                saveData(data);
            }
            // 其他
            List<Map> list4=UtilMap.getList(formData,"tableField_ls30qr4m");
            for (Map map:list4){
                if(PublicUtil.isNull(map,"textField_luwcwjap")){
                    continue;
                }
                Map data=new HashMap(mainData);
                data.put("selectField_lvw57eo7","其他");
                data.put("textField_lrpt148q",map.get("textField_luwcwjap"));
                data.put("selectField_lrhiklat",map.get("selectField_luwcwjao"));
                data.put("numberField_lrhiklas",map.get("numberField_ltqveodd"));
                saveData(data);
            }
        }
    }

    // 处理数据
    @Test
    public void test2(){
        List<String> list=FileUtil.readLines("/home/ids.txt","utf-8");
        int i=1;
        for (String id:list){
            log.info("处理进度：{}/{}",i,list.size());
            i++;
            Map formData =ydClient.queryData(YDParam.builder()
                    .formInstanceId(id)
                    .build(), YDConf.FORM_QUERY.retrieve_id).getFormData();
            Map mainData=new HashMap();
            mainData.put("textField_lvw66i56",id);
            mainData.put("selectField_lvw57eo9","运营相关工时");
            mainData.put("departmentSelectField_ls2x7t47",formData.get("departmentSelectField_ls2x7t47_id"));
            mainData.put("employeeField_ls2x7t46",formData.get("employeeField_ls2x7t46_id"));
            mainData.put("dateField_ls2x7t40",formData.get("dateField_ls2x7t40"));// 月份
            mainData.put("dateField_lvw40y8e","");// 日期
            // 项目
            List<Map> list1=UtilMap.getList(formData,"tableField_ls30qr4f");
            for (Map map:list1){
                if(PublicUtil.isNull(map,"textField_ls30qr4a")){
                    continue;
                }
                Map data=new HashMap(mainData);
                data.put("selectField_lvw57eo7","项目");
                data.put("textField_lrpt148q",map.get("textField_ls30qr4a"));
                data.put("selectField_lrhiklat",map.get("selectField_lwaa390e"));
                data.put("numberField_lrhiklas",map.get("numberField_lt6zyzpg"));
                saveData(data);
            }
            // 商机
            List<Map> list2=UtilMap.getList(formData,"tableField_lwaa390n");
            for (Map map:list2){
                if(PublicUtil.isNull(map,"textField_lwaa390g")){
                    continue;
                }
                Map data=new HashMap(mainData);
                data.put("selectField_lvw57eo7","商机");
                data.put("textField_lrpt148q",map.get("textField_lwaa390g"));
                data.put("selectField_lrhiklat",map.get("selectField_lwaa390i"));
                data.put("numberField_lrhiklas",map.get("numberField_lwaa390l"));
                saveData(data);
            }
            // 其他
            List<Map> list3=UtilMap.getList(formData,"tableField_ls30qr4m");
            for (Map map:list3){
                if(PublicUtil.isNull(map,"textField_lrsvczk1")){
                    continue;
                }
                Map data=new HashMap(mainData);
                data.put("selectField_lvw57eo7","其他");
                data.put("textField_lrpt148q",map.get("textField_lwaa3910"));
                data.put("selectField_lrhiklat",map.get("selectField_lwaa390z"));
                data.put("numberField_lrhiklas",map.get("numberField_lt6zyzph"));
                saveData(data);
            }
            // 计费点
            List<Map> list4=UtilMap.getList(formData,"tableField_ls2x7t4g");
            for (Map map:list4){
                if(PublicUtil.isNull(map,"textField_ls2x7t4a")){
                    continue;
                }
                Map data=new HashMap(mainData);
                data.put("selectField_lvw57eo7","计费点");
                data.put("textField_lrpt148q",map.get("textField_ls2x7t4a"));
                data.put("selectField_lrhiklat",map.get("selectField_lwaa3911"));
                data.put("numberField_lrhiklas",map.get("numberField_lt6zyzpf"));
                saveData(data);
            }
        }
    }

    @Test
    public void test1(){
        List<String> list=FileUtil.readLines("/home/ids.txt","utf-8");
        int i=1;
        for (String id:list){
            log.info("处理进度：{}/{}",i,list.size());
            i++;
            Map formData =ydClient.queryData(YDParam.builder()
                    .formInstanceId(id)
                    .build(), YDConf.FORM_QUERY.retrieve_id).getFormData();
            Map mainData=new HashMap();
            mainData.put("textField_lvw66i56",id);
            mainData.put("selectField_lvw57eo9","项目/研发/产品/运维工时");
            mainData.put("departmentSelectField_ls2x7t47",formData.get("departmentSelectField_lrhjd0ep_id"));
            mainData.put("employeeField_ls2x7t46",formData.get("employeeField_lrhjd0eq_id"));
            mainData.put("dateField_ls2x7t40",formData.get("dateField_luuynvj8"));// 月份
            mainData.put("dateField_lvw40y8e",formData.get("dateField_lrhl6dm1"));// 日期
            // 项目
            List<Map> list1=UtilMap.getList(formData,"tableField_lrhiklaq");
            for (Map map:list1){
                if(PublicUtil.isNull(map,"textField_lrpt148q")){
                    continue;
                }
                Map data=new HashMap(mainData);
                data.put("selectField_lvw57eo7","项目");
                data.put("textField_lrpt148q",map.get("textField_lrpt148q"));
                data.put("selectField_lrhiklat",map.get("selectField_lrhiklat"));
                data.put("numberField_lrhiklas",map.get("numberField_lrhiklas"));
                saveData(data);
            }
            // 产品
            List<Map> list2=UtilMap.getList(formData,"tableField_lrhjxup8");
            for (Map map:list2){
                if(PublicUtil.isNull(map,"textField_lrpt148r")){
                    continue;
                }
                Map data=new HashMap(mainData);
                data.put("selectField_lvw57eo7","产品");
                data.put("textField_lrpt148q",map.get("textField_lrpt148r"));
                data.put("selectField_lrhiklat",map.get("selectField_lrhjxup6"));
                data.put("numberField_lrhiklas",map.get("numberField_lrhjxup5"));
                saveData(data);
            }
            // 其他
            List<Map> list3=UtilMap.getList(formData,"tableField_lrhjxupf");
            for (Map map:list3){
                if(PublicUtil.isNull(map,"textField_lrsvczk1")){
                    continue;
                }
                Map data=new HashMap(mainData);
                data.put("selectField_lvw57eo7","其他");
                data.put("textField_lrpt148q",map.get("textField_lrsvczk1"));
                data.put("selectField_lrhiklat",map.get("selectField_lrhjxupd"));
                data.put("numberField_lrhiklas",map.get("numberField_lrhjxupc"));
                saveData(data);
            }
            // 计费点
            List<Map> list4=UtilMap.getList(formData,"tableField_lrhjxupm");
            for (Map map:list4){
                if(PublicUtil.isNull(map,"textField_lrpt148s")){
                    continue;
                }
                Map data=new HashMap(mainData);
                data.put("selectField_lvw57eo7","计费点");
                data.put("textField_lrpt148q",map.get("textField_lrpt148s"));
                data.put("selectField_lrhiklat",map.get("selectField_lrhjxupk"));
                data.put("numberField_lrhiklas",map.get("numberField_lrhjxupj"));
                saveData(data);
            }
        }
    }

    private void saveData(Map data){
        ydClient.operateData(YDParam.builder()
                .formUuid("FORM-223E89E230F3488885A7BEB44352A9C06HZ5")
                .formDataJson(JSONObject.toJSONString(data))
                .userId(String.valueOf(UtilMap.getList(data,"employeeField_ls2x7t46").get(0)))
                .build(), YDConf.FORM_OPERATION.create);
    }
}
