package com.malk.lingmingguangzi;

import com.alibaba.fastjson.JSONObject;
import com.malk.lingmingguangzi.service.LingmingguangziService;
import com.malk.server.aliwork.YDConf;
import com.malk.server.aliwork.YDParam;
import com.malk.service.aliwork.YDClient;
import com.malk.utils.UtilMap;
import lombok.extern.slf4j.Slf4j;
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.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@SpringBootTest
@RunWith(SpringRunner.class)
public class YdTest {
    @Autowired
    private LingmingguangziService lingmingguangziService;

    @Autowired
    private YDClient ydClient;

    @Test
    public void test() throws UnsupportedEncodingException {

        lingmingguangziService.postWithFile("https://szerp.adapsphotonics.net:28443/pws/sys/common/upload","D:\\20241213 图达通智能科技（苏州）有限公司 PSZ10011077灵明_已签章.pdf","深圳");



    }

    @org.junit.Test
    public void test2(){
        List<String> data = (List<String>) ydClient.queryData(YDParam.builder()
                .formUuid("FORM-C20F12A6BF6145A4A451F35CC2C78AFC7PB8")
                .build(), YDConf.FORM_QUERY.retrieve_search_form_id).getData();

        for (String datum : data) {
            Map map = new HashMap();
            map.put("formInstId",datum);
            lingmingguangziService.addContacts(map);
        }
    }


    @org.junit.Test
    public void test3() {
        String downloadPath = "/home/mc/mjs/lingmingguangzi/";
        int lastIndex = downloadPath.lastIndexOf("/");
        int secondLastIndex = downloadPath.lastIndexOf("/",lastIndex-1);
        String str = downloadPath.substring(secondLastIndex);
        System.out.println(str);
    }

    @org.junit.Test
    public void test4() throws IOException {
        String ydDownloadUrl = "https://www.aliwork.com/ossFileHandle?appType=APP_MOIYIC856NRIBCMXEXVD&fileName=APP_MOIYIC856NRIBCMXEXVD_MTU5OTQ0MTgzOTczNDg1OTlfMDdFNjZJOTFXTDJST1dYWDZKSFBOREFTSDBPSjM4TFJQSlQ0TVZB.pdf&instId=&type=download&originalFileName=%E7%A6%BE%E8%B5%9B%2020240709%20hawk%20%E8%AE%A2%E5%8D%95_%E5%B7%B2%E7%AD%BE%E7%AB%A0.pdf";

        lingmingguangziService.downloadFile(ydDownloadUrl,"d://", "禾赛 20240709 hawk 订单_已签章.pdf");
    }

    @org.junit.Test
    public void test5()  {
        String[] formInstIds= {"191a2130-d61b-4fed-ad08-ccb350bd85a0","4bec2e30-e852-4003-8f0d-772e989635d2","33b40f67-8d4a-4d89-9f34-ff94cb06aeb4","776ff3d0-d249-4f7a-95b2-37e4e292b35a","3edbeaeb-75b1-4c2b-b60f-ae550995984b","3e9bdc41-769a-4d21-80c9-8e4a0b67f45f","853100c7-ef07-485a-b537-35f302b2100a","1a642340-6eae-4931-8bf0-aafe5ec26397","b843ba58-5c76-489c-bb13-e9dcff0de7ac","2dadb0a0-5cc1-4c63-9db9-eb1d94bda3ac","ed927f8c-1276-406a-a5b6-3d13625b76d3","e03e621d-e581-4245-9a61-3ee6b0b0c4dc","23bb6f6b-4c4c-424f-af0d-da90cf0ccce6","fad78eff-7f1a-4330-8b1e-5f60aea0bdf1","d9fea78d-ba3a-413f-ac5e-88dc389fb885","5bd2e906-a781-4042-8a5f-547b84ff94d5","f611b9b7-5f53-4907-81a5-c9cd5cae3c8d","70c6f84f-7b69-477b-9281-429ca9bdf69f","017632b6-11fd-45ec-b45d-aa251d300754","4237314f-0920-49a5-b3c8-d42b7e46641e","416245ae-c1b8-4628-b9fe-acabf64fba65","c7d30871-0bc5-4d1e-be4d-ea1038725d65","eba339c7-ca9a-4197-82fe-79ecb90dfb39","183535bd-b91a-4a81-8bad-27cd5a21c236","79a00320-0151-43e2-913b-8bdfaed380d3","f29b8556-fc8e-4c18-bf2a-b06aff6d64da","1c63c3c0-fb59-46e3-bd12-e2ceeaa2a5a4","9a53077e-7e61-4b7d-9e1d-566a2bedb3ed","f1f10641-f6d9-4d4f-9c40-01a75f66e7f4","86085320-2508-4d83-a5f7-db32c069e22c","c528bcc1-1f5b-4875-891e-7cd0b96193fe"};
        for (String formInstId : formInstIds) {

            ydClient.operateData(YDParam.builder()
                    .formInstId(formInstId)
                    .useLatestVersion(true)
                    .updateFormDataJson(JSONObject.toJSONString(UtilMap.map("selectField_m4uq0n0k","否")))
                    .build(), YDConf.FORM_OPERATION.update);
        }
    }

    @org.junit.Test
    public void test6() {

        List<Map> data = (List<Map>) ydClient.queryData(YDParam.builder()
                .formUuid("FORM-6F1E2DCFB3F5448EBDD4994551EBF690DVIT")
                .pageNumber(2)
                .pageSize(100)
                .build(), YDConf.FORM_QUERY.retrieve_list).getData();

        for (Map datum : data) {
            Map formData = (Map) datum.get("formData");
            List<String> employeeFieldM3mce7ox = (List<String>) formData.get("employeeField_m3mce7ox_id");
            if (employeeFieldM3mce7ox == null) {

                List<String> employeeFieldM2cz3vbtId = (List<String>) formData.get("employeeField_m2cz3vbt_id");
                if (employeeFieldM2cz3vbtId != null){
                    String value = employeeFieldM2cz3vbtId.get(0);

                    Map formData2 = new HashMap();
                    formData2.put("employeeField_m3mce7ox",new String[]{value});

                    ydClient.operateData(YDParam.builder()
                            .formInstId(datum.get("formInstanceId").toString())
                            .updateFormDataJson(JSONObject.toJSONString(formData2))
                            .build(), YDConf.FORM_OPERATION.update);
                }

            }


        }
    }

    @org.junit.Test
    public void test7() {
        List<Map> data = (List<Map>) ydClient.queryData(YDParam.builder()
                .formUuid("FORM-6F1E2DCFB3F5448EBDD4994551EBF690DVIT")
                .pageNumber(1)
                .pageSize(100)
                .searchFieldJson(JSONObject.toJSONString(UtilMap.map("selectField_m4uq0n0k","是")))
                .build(), YDConf.FORM_QUERY.retrieve_list).getData();


        for (Map datum : data) {
            Map formData = (Map) datum.get("formData");
            String serialNumberFieldM4rv2j4i = formData.get("serialNumberField_m4rv2j4i").toString();
            List<Map> data1 = (List<Map>) ydClient.queryData(YDParam.builder()
                    .formUuid("FORM-C20F12A6BF6145A4A451F35CC2C78AFC7PB8")
                    .searchFieldJson(JSONObject.toJSONString(UtilMap.map("textField_m4rxq12o", serialNumberFieldM4rv2j4i)))
                    .build(), YDConf.FORM_QUERY.retrieve_list).getData();

            Map map2 = data1.get(0);

            String updateFormInstId = map2.get("formInstanceId").toString();


            Map formData2 = new HashMap();
            List<Map> employeeFieldM2cz3vbtId = (List<Map>) formData.get("employeeField_m2cz3vbt_id");
            List<Map> employeeFieldM3mce7oxId = (List<Map>) formData.get("employeeField_m3mce7ox_id");
            formData2.put("employeeField_m2cz3vbt",employeeFieldM2cz3vbtId);//关联人员
            formData2.put("employeeField_m4w80djq",employeeFieldM3mce7oxId);//提交人
            formData2.put("employeeField_m25inxlq",employeeFieldM3mce7oxId);//对接销售

            ydClient.operateData(YDParam.builder()
                    .formInstId(updateFormInstId)
                    .updateFormDataJson(JSONObject.toJSONString(formData2))
                    .build(), YDConf.FORM_OPERATION.update);
        }
    }

    @org.junit.Test
    public void test8() {
        List<Map> data = (List<Map>) ydClient.queryData(YDParam.builder()
                .formUuid("FORM-221A9483B28448089ACBA5B4748D6288KMG5")
                .pageNumber(2)
                .pageSize(100)
                .build(), YDConf.FORM_QUERY.retrieve_list).getData();

        for (Map datum : data) {
            Map formData = (Map) datum.get("formData");
            String code = formData.get("textField_m4u3lrdv").toString();

            List<Map> data2 = (List<Map>) ydClient.queryData(YDParam.builder()
                    .formUuid("FORM-6F1E2DCFB3F5448EBDD4994551EBF690DVIT")
                    .searchFieldJson(JSONObject.toJSONString(UtilMap.map("serialNumberField_m4rv2j4i", code)))
                    .build(), YDConf.FORM_QUERY.retrieve_list).getData();
            if (data2 != null && !data2.isEmpty()){
                Map map = data2.get(0);

                Map formData2 = (Map) map.get("formData");


                Map updateFormData = new HashMap();
                updateFormData.put("employeeField_m74d7c2c",formData2.get("employeeField_m3mce7ox_id"));

                ydClient.operateData(YDParam.builder()
                        .formInstId(datum.get("formInstanceId").toString())
                        .updateFormDataJson(JSONObject.toJSONString(updateFormData))
                        .build(), YDConf.FORM_OPERATION.update);
            }
        }
    }

    @org.junit.Test
    public void test9() {
        //获取宜搭附件临时免登地址
        /*Map param = new HashMap();
        param.put("systemToken","G9666BC1BUHLKS20E7YQRATFDE4F39EOK5QWL06");
        param.put("userId","344749020127590108");
        param.put("timeout",3600000);
        param.put("fileUrl","https://yejlwg.aliwork.com/ossFileHandle?appType=APP_MOIYIC856NRIBCMXEXVD&fileName=APP_MOIYIC856NRIBCMXEXVD_MTMxOTAyNjMzNTI3NDAxMDU4X0JENzY2QkMxSkJHVEo0TUQ4UEhUVjRISEtIMU0yMENUWjlZN004Nw$$.pdf&instId=&type=download&originalFileName=20250307%20%E5%A2%9E%E4%BD%A0%E5%BC%BA%EF%BC%88%E4%B8%8A%E6%B5%B7%EF%BC%89%E5%9B%BD%E9%99%85%E8%B4%B8%E6%98%93%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8%20ADSH250304_%E5%B7%B2%E7%AD%BE%E7%AB%A0.pdf");

        Map header = new HashMap();
        header.put("x-acs-dingtalk-access-token","dcb80fc6c2023a6e98459601293e16d3");

        DDR_New ddrNew = (DDR_New) UtilHttp.doGet("https://api.dingtalk.com/v1.0/yida/apps/temporaryUrls/APP_MOIYIC856NRIBCMXEXVD", header, param, DDR_New.class);
        System.out.println(ddrNew);*/

        MDC.put("MDC_KEY_PID","1017");

        ydClient.convertTemporaryUrl("/ossFileHandle?appType=APP_MOIYIC856NRIBCMXEXVD&fileName=APP_MOIYIC856NRIBCMXEXVD_MTMxOTAyNjMzNTI3NDAxMDU4X0JENzY2QkMxSkJHVEo0TUQ4UEhUVjRISEtIMU0yMENUWjlZN004Nw$$.pdf&instId=&type=download&originalFileName=20250307%20%E5%A2%9E%E4%BD%A0%E5%BC%BA%EF%BC%88%E4%B8%8A%E6%B5%B7%EF%BC%89%E5%9B%BD%E9%99%85%E8%B4%B8%E6%98%93%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8%20ADSH250304_%E5%B7%B2%E7%AD%BE%E7%AB%A0.pdf");

        System.out.println("");
    }

    @Test
    public void test10() {
        lingmingguangziService.updateProcess();
    }

    @Test
    public void test11() {
        lingmingguangziService.updateCurrency();
    }

    @Test
    public void test12() {
        lingmingguangziService.updateTaxRate();
    }


}
