package com.docus.server.service.handler;

import com.docus.core.util.Func;
import com.docus.log.annotation.TrackRetryListener;
import com.docus.server.api.ocr.OcrApi;
import com.docus.server.entity.segmentation.OcrBasicTest;
import com.docus.server.entity.segmentation.OcrFileInfoTest;
import com.docus.server.entity.segmentation.OcrRuleResult;
import com.docus.server.entity.segmentation.OcrSpecialResult;
import com.docus.server.enums.OcrStatusEnum;
import com.docus.server.infrastructure.dao.IOcrBasicTestDao;
import com.docus.server.infrastructure.dao.IOcrFileInfoTestDao;
import com.docus.server.infrastructure.dao.IOcrUrlConfigTestDao;
import com.docus.server.service.IOcrRuleTestService;
import com.docus.server.service.IOcrSpecialRuleTestService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 开始分段处理器
 */
@Component
public class StartSegmentTestHandler {
    @Resource
    private OcrApi ocrApi;
    @Resource
    private IOcrFileInfoTestDao iOcrFileInfoTestDao;
    @Resource
    private IOcrSpecialRuleTestService iOcrSpecialRuleTestService;
    @Resource
    private IOcrRuleTestService iOcrRuleTestService;
    @Resource
    private IOcrBasicTestDao iOcrBasicTestDao;
    @Resource
    private IOcrUrlConfigTestDao iOcrUrlConfigTestDao;


    @TrackRetryListener("START_SEGMENT_TEST")
    public List<OcrFileInfoTest> startSegment(String patientId) {
        boolean isSpecialFile = false;
        OcrSpecialResult lastSpecialResul = null;

        //根据病案号查询文件列表
        List<OcrBasicTest> ocrBasicTestList = iOcrBasicTestDao.findBy("patientId",patientId);
        List<OcrFileInfoTest> fileInfoTestList = iOcrFileInfoTestDao.findBy("patientId", patientId);
        String url = iOcrUrlConfigTestDao.findAll().get(0).getUrl();
        for (OcrFileInfoTest fileInfoTest : fileInfoTestList) {

            //遍历文件列表 获取ocr识别结果
            List<String> ocrTextList = ocrApi.getText(fileInfoTest.getPicCutUrl(),url);

            fileInfoTest.setOcrText(Func.toJson(ocrTextList));
            fileInfoTest.setOcrStatus(OcrStatusEnum.COMPLETE);
            fileInfoTest.setOcrFinishTime(new Date());
            //判断上次是否特殊文件开始  需要判断是否特殊文件结尾
            if (isSpecialFile) {
                fileInfoTest.setAssortId(lastSpecialResul.getAssortId());
                fileInfoTest.setAssortName(lastSpecialResul.getAssortName());
                fileInfoTest.setRuleId(lastSpecialResul.getRuleId());
                boolean b = iOcrSpecialRuleTestService.handleSpecialRuleEnd(lastSpecialResul, ocrTextList);
                if (b) {
                    //当前分段为特殊分段结尾
                    isSpecialFile = false;
                    lastSpecialResul = null;
                }
                continue;
            }
            //优先判断是否特殊规则开始
            OcrSpecialResult ocrSpecialResult = iOcrSpecialRuleTestService.handleSpecialRule(ocrTextList);
            //判断是否属于某个分段
            if (ocrSpecialResult != null) {
                fileInfoTest.setAssortId(ocrSpecialResult.getAssortId());
                fileInfoTest.setAssortName(ocrSpecialResult.getAssortName());
                fileInfoTest.setRuleId(ocrSpecialResult.getRuleId());
                fileInfoTest.setHitKey(ocrSpecialResult.getHitKey());
                fileInfoTest.setRate(ocrSpecialResult.getRate());

                isSpecialFile = true;
                lastSpecialResul = ocrSpecialResult;
                continue;
            }
            OcrRuleResult ocrRuleResult = iOcrRuleTestService.handleRule(ocrTextList);
            //更新文件数据的分段
            if (ocrRuleResult != null) {
                fileInfoTest.setAssortId(ocrRuleResult.getAssortId());
                fileInfoTest.setAssortName(ocrRuleResult.getAssortName());
                fileInfoTest.setRuleId(ocrRuleResult.getRuleId());
                fileInfoTest.setHitKey(ocrRuleResult.getHitKey());
                fileInfoTest.setRate(ocrRuleResult.getRate());
            }
        }
        //更新数据库表
        List<OcrBasicTest> collect = ocrBasicTestList.stream().peek(p -> {
            p.setOcrFinishTime(new Date());
            p.setOcrStatue(OcrStatusEnum.COMPLETE);
        }).collect(Collectors.toList());

        iOcrFileInfoTestDao.updateBatchById(fileInfoTestList, 1000);
        iOcrBasicTestDao.updateBatchById(collect,1000);

        return fileInfoTestList;
    }
}
