package com.gz;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.gz.dto.archive.ArchiveDTO;
import com.gz.dto.archive.SecondaryArchiveDTO;
import com.gz.dto.system.ArchiveFileDTO;
import com.gz.dto.system.ArchivesTreeDTO;
import com.gz.mapper.archive.ArchiveFileMapper;
import com.gz.mapper.archive.ArchiveMapper;
import com.gz.mapper.archive.SecondaryArchiveMapper;
import com.gz.rvo.archive.ArchiveRVO;
import com.gz.vo.rabbitmq.OcrEtlVO;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author LiuchangLan
 * @date 2021/4/26 18:50
 */
@Slf4j
@SpringBootTest
public class OcrTest {

    @Resource
    private ArchiveMapper archiveMapper;

    @Resource
    private SecondaryArchiveMapper secondaryArchiveMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Value("${upload-file.server-file-url-prefix}")
    private String serverFileUrlPrefix;

    @Value("${rabbitmq.data-exchange}")
    private String dataExchangeName;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Resource
    private ArchiveFileMapper archiveFileMapper;

    @Value("${rabbitmq.ocr.queue-binding-key}")
    private String ocrBindingKey;


    @Test
    public void test(){
        Example example = new Example(ArchiveFileDTO.class);
        example.setOrderByClause("id ASC");
        List<ArchiveFileDTO> archiveFileDTOS = archiveFileMapper.selectByExample(example);
        for (int i = 0; i < archiveFileDTOS.size(); i++) {
            ArchiveFileDTO archiveFileDTO = archiveFileDTOS.get(i);
            ArchiveRVO archiveRVO = archiveMapper.selectByPk(archiveFileDTO.getArchiveId());
            OcrEtlVO ocrBean = BeanUtil.copyProperties(archiveRVO, OcrEtlVO.class);
            ocrBean.setSecondaryArchiveId(archiveFileDTO.getSecondaryArchiveId());
            ocrBean.setFileUrl(serverFileUrlPrefix + archiveFileDTO.getFilePath() + "/" + archiveFileDTO.getFileName());
            log.info("【ocr识别】发起识别队列，参数[{}]", JSON.toJSONString(ocrBean));
            rabbitTemplate.convertAndSend(dataExchangeName, ocrBindingKey, JSON.toJSONString(ocrBean));
            return;
        }
//        Integer i = Integer.valueOf(stringRedisTemplate.opsForValue().get("sdfsdf"));
//        ArchiveFileDTO archiveFileDTO = archiveFileDTOS.get(0);
//        ArchiveRVO archiveRVO = archiveMapper.selectByPk(archiveFileDTO.getArchiveId());
//        OcrEtlVO ocrBean = BeanUtil.copyProperties(archiveRVO, OcrEtlVO.class);
//        ocrBean.setSecondaryArchiveId(archiveFileDTO.getSecondaryArchiveId());
//        ocrBean.setFileUrl(serverFileUrlPrefix + archiveFileDTO.getFilePath() + "/" + archiveFileDTO.getFileName());
//        log.info("【ocr识别】发起识别队列，参数[{}]", JSON.toJSONString(ocrBean));
//        rabbitTemplate.convertAndSend(dataExchangeName, ocrBindingKey, JSON.toJSONString(ocrBean));
//        while (i < archiveFileDTOS.size()){
//            ArchiveFileDTO archiveFileDTO = archiveFileDTOS.get(i);
//            ArchiveRVO archiveRVO = archiveMapper.selectByPk(archiveFileDTO.getArchiveId());
//            OcrEtlVO ocrBean = BeanUtil.copyProperties(archiveRVO, OcrEtlVO.class);
//            ocrBean.setSecondaryArchiveId(archiveFileDTO.getSecondaryArchiveId());
//            ocrBean.setFileUrl(serverFileUrlPrefix + archiveFileDTO.getFilePath() + "/" + archiveFileDTO.getFileName());
//            log.info("【ocr识别】发起识别队列，参数[{}]", JSON.toJSONString(ocrBean));
//            rabbitTemplate.convertAndSend(dataExchangeName, ocrBindingKey, JSON.toJSONString(ocrBean));
//            i ++;
//            stringRedisTemplate.opsForValue().set("sdfsdf",i.toString());
//            return;
//        }
    }
}
