package com.malk.shantai;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.malk.service.dingtalk.DDClient;
import com.malk.shantai.config.StEkbConfig;
import com.malk.shantai.service.StDingProcService;
import com.malk.utils.UtilMap;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
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 javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;

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

    @Autowired
    private StEkbConfig stEkbConfig;
    @Autowired
    private StDingProcService stDingProcService;

    @Autowired
    private DDClient ddClient;

    @Test
    public void test(){
//        stDingProcService.fkdSync("JaFP7M4bTF-3qUnkZf5hyg06971714033972");
        //获取付款单信息
//        stDingProcService.fkdSync("jtqqkVH0S0-n2Znb39UI2g06971713928245");
//        stDingProcService.fkdSync("_ynuK9h3RYW4oE06B-D44A06971715048029");
//        stDingProcService.fkdSync("lReoTnLiRbSreZKodGsyRg06971715650847");
//        stDingProcService.fkdSync("o2zdfQO-TP6ipxqzZh9cjA06971715405894");
        stDingProcService.fkdSync("x_ZSQjbDSsWjb9xpS25LMw06971736415884");


    }

    @Test
    public void test2(){
        //付款后回写到OA评论
        Map flowInfo = stDingProcService.getFlowInfo("ID01CDZmSxVSFx");
        stDingProcService.commentSync(flowInfo,"ID01CDZmSxVSFx");
//        stDingProcService.commentSync(null,null);
    }

    @Test
    public void test3(){
        //下载网络文件
        stDingProcService.downloadNet("https://img.zcool.cn/community/01241f5d63368fa8012187f4f38801.jpg@1280w_1l_2o_100sh.jpg","d:\\附件\\4.jpg");
    }

    @Test
    public void ekbTest(){
        //根据模板ID获取模板信息
        String id="ID01yVMv53gDSL";// "对外付款单"模板id
        Map map=new HashMap();
        map.put("accessToken",getToken());
        map.put("type","payment");
        map.put("start",0);
        map.put("count",10);
        Map result=JSONObject.parseObject(HttpUtil.get(stEkbConfig.getPreUrl().concat("/api/openapi/v2/specifications/byIds/[").concat(id).concat("]"),map));
        System.out.println(result);

        //创建单据

    }

    private String getToken(){
        String result= HttpUtil.post(stEkbConfig.getPreUrl().concat("/api/openapi/v1/auth/getAccessToken"),JSONObject.toJSONString(UtilMap.map("appKey, appSecurity",stEkbConfig.getAppKey(),stEkbConfig.getAppSecurity())));
        return JSONObject.parseObject(result).getJSONObject("value").getString("accessToken");
    }

}
