package com.trade.client;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.trade.service.trade.salesorder.dto.GoodsInfo4TL;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import com.trade.common.utils.file.ASopUtil;
import com.trade.service.contract.dto.ContractGenInputItem;
import com.trade.service.contract.dto.ContractSignInput;
import com.trade.service.contract.entity.ESignFlow;
import com.trade.service.contract.queue.MessageSender;
import com.trade.service.contract.service.ContractService;
import com.trade.service.contract.service.ContractTemplateRepository;
import com.trade.service.contract.service.ESignFlowService;

@SpringBootTest
public class ContractServiceTests {

    @Resource
    ContractService contractService;
    @Resource
    private ContractTemplateRepository contractTemplateRepository;
    @Resource
    private MessageSender messageSender;
    @Resource
    private ESignFlowService esignFlowService;

    @Test
    public void contractMapTest() {
        System.out.println(contractTemplateRepository.getContractTemplates().get("123"));
    }

    @Test
    public void contractGenerateTest() {
        // 生成合同
        ContractGenInputItem item = new ContractGenInputItem();
        item.setTemplateCode("123");
        Map<String, Object> params = new HashMap<>();
        params.put("name", "张三");
        item.setParams(params);
        item.setContractName("测试合同");
        List<GoodsInfo4TL> goods = new ArrayList<>();
        GoodsInfo4TL goods1 = new GoodsInfo4TL();
        goods1.setGoodsName("商品1");
        goods1.setHsCode("123456");
        goods.add(goods1);
        GoodsInfo4TL goods2 = new GoodsInfo4TL();
        goods2.setGoodsName("商品2");
        goods2.setHsCode("789012");
        goods.add(goods2);
        GoodsInfo4TL goods3 = new GoodsInfo4TL();
        goods3.setGoodsName("商品3");
        goods3.setHsCode("789012");
        goods.add(goods3);
        params.put("goods", goods);
        item.setParams(params);
        LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
        item.setConfig(Configure.builder().bind("goods", policy).build());
        System.out.println(contractService.generateSingleContract(item, null));
    }

    @Test
    public void contractSignTest() {
        ContractSignInput input = new ContractSignInput();
        input.setSignPlatform(1);
        contractService.signContract(input);
    }

    @Test
    public void convert2PDFTest() {
        String docFile = "C:/Users/dawn7/Desktop/123.docx";
        ASopUtil.doc2pdf(docFile, "C:/Users/dawn7/Desktop/123.pdf");
    }

    @Test
    public void messageTest() {
        String message = "测试消息";
        messageSender.sendESignCallbackMessage(message);
    }

    @Test
    public void eSignFlowAddTest() {
        ESignFlow esignFlow = ESignFlow.builder().bizSysId("A101").flowId("666666").scene("测试场景")
                .expiredTime(LocalDateTime.now()).status(0).build();
        esignFlowService.add(esignFlow);
    }

    @Test
    public void eSignFlowQueryTest() {
        System.out.println(esignFlowService.getByFlowId("666666").getId());
    }
}
