package com.malk.lianan.service.impl;

import com.alibaba.fastjson.JSON;
import com.malk.server.aliwork.YDConf;
import com.malk.server.aliwork.YDParam;
import com.malk.service.aliwork.YDClient;
import com.malk.utils.UtilMap;
import com.malk.lianan.service.IvYdService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class IvYdServiceImpl implements IvYdService {

    @Autowired
    private YDClient ydClient;
    @Autowired
    private YDConf ydConf;

    @Async
    @SneakyThrows
    @Override
    public Object operateData(Map data, Map update, YDParam param, YDConf.FORM_OPERATION type) {
        if (data.containsKey("aUuid")) {
            Thread.sleep(3000);
            List<Map> process = (List<Map>) ydClient.queryData(YDParam.builder()
                    .formUuid(data.get("aFormUuid").toString())
                    .searchFieldJson(JSON.toJSONString(UtilMap.map("textField_lmewsobs", data.get("aUuid"))))
                    .build(), YDConf.FORM_QUERY.retrieve_search_form).getData();

            update.put(data.get("aCompId"),
                    Arrays.asList(UtilMap.map("appType, formType, instanceId, formUuid, title",
                            ydConf.getAppType(), "process", process.get(0).get("formInstanceId"), data.get("aFormUuid"), process.get(0).get("title"))));
            param.setUpdateFormDataJson(JSON.toJSONString(update));
        }
        return ydClient.operateData(param, type);
    }

    public static void main(String[] args) {
        int i=0;
        do {
            System.out.println(i+1);
            i++;
        }while (true);
    }
}
