package com.malk.aiwei;

import com.alibaba.fastjson.JSONObject;
import com.malk.aiwei.server.AWServer;
import com.malk.server.aliwork.YDConf;
import com.malk.server.aliwork.YDParam;
import com.malk.server.teambition.TBConf;
import com.malk.service.aliwork.YDClient;
import com.malk.service.aliwork.YDService;
import com.malk.service.teambition.TBClient;
import com.malk.utils.UtilMap;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.platform.commons.util.StringUtils;
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.util.*;
import java.util.stream.Collectors;

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

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

    // 处理数据
    @Test
    public void test(){
        List<Map> data = ydService.queryFormData_all(YDParam.builder().formUuid("FORM-6E2C0D1197264B8AA23EB3FECAE7344B00BN").build());
        List<Map> errorList = new ArrayList<>();
        for (Map item:data){
            String instanceId=UtilMap.getString(item,("instanceId"));
            String tbTaskId=UtilMap.getString(item,("textField_lr3dlwsa"));
            if(StringUtils.isBlank(tbTaskId)){
                continue;
            }
            try {
                Map taskData = _getTaskFieldMap(tbTaskId, "wafer");
                Map rTask = UtilMap.getMap(taskData, "task");
                Map updateMap=new HashMap();
                updateMap.put("textField_m0yo7tsj",taskData.get("wafer"));
                updateMap.put("textField_m0yo7tsk",_getWorkFlowStatusList(UtilMap.getString(rTask,"projectId"),UtilMap.getString(rTask,"tfsId"))); // 任务状态
                ydClient.operateData(YDParam.builder().formInstId(instanceId).updateFormDataJson(JSONObject.toJSONString(updateMap)).build(), YDConf.FORM_OPERATION.update);
            }catch (Exception e){
                e.printStackTrace();
                errorList.add(UtilMap.map("instanceId, tbTaskId",instanceId,tbTaskId));
            }
        }
        System.out.println(JSONObject.toJSONString(errorList));
    }

    private Map _getTaskFieldMap(String taskId, String... fieldNames) {
        // 查询任务详情
        Map rTask = tbClient.queryTaskDetail(taskId, null, null).get(0);
        // 查询项目任务ID
        List<Map> customField = tbClient.queryProjectCustomField(String.valueOf(rTask.get("projectId")), null);
        return __getTaskMap(rTask, customField, fieldNames);
    }

    // 组装任务数据
    private Map __getTaskMap(Map task, List<Map> projectCustomField, String... fieldNames) {
        Map result = UtilMap.map("task", task);
        List<Map> taskCustomField = (List<Map>) task.get("customfields");
        for (String filed : fieldNames) {
            Optional optional = projectCustomField.stream().filter(item -> filed.equals(item.get("name"))).findAny();
            if (optional.isPresent()) {
                Map map = (Map) optional.get();
                result.put(filed, TBConf.getTaskFieldValue_First(taskCustomField, String.valueOf(map.get("id"))));
                result.put(filed + "_id", map.get("id")); // ppExt: 返回字段ID
            }
        }
        return result;
    }

    private String _getWorkFlowStatusList(String projectId,String tfsId) {
        List<Map> customFlowStatus = tbClient.queryProjectCustomFlowStatus(projectId, UtilMap.map("tfsIds",tfsId));
        return UtilMap.getString(customFlowStatus.get(0),"name");
    }

}
