package com.heyu.api.controller.medical;


import com.heyu.api.baidu.handle.medical.BMedicalReportDetectionHandle;
import com.heyu.api.baidu.request.medical.BMedicalReportDetectionRequest;
import com.heyu.api.baidu.response.medical.BMedicalReportDetectionResp;
import com.heyu.api.data.annotation.CacheResult;
import com.heyu.api.data.annotation.NotIntercept;
import com.heyu.api.data.utils.ApiR;
import com.heyu.api.data.utils.R;
import com.heyu.api.resp.medical.MedicalReportDetectionResp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


/**
 * https://console.bce.baidu.com/support/?_=1740575657628&timestamp=1740581217710#/api?product=AI&project=%E6%96%87%E5%AD%97%E8%AF%86%E5%88%AB&parent=%E5%8C%BB%E7%96%97%E7%A5%A8%E6%8D%AEOCR&api=rest%2F2.0%2Focr%2Fv1%2Fmedical_report_detection&method=post
 * <p>
 * <p>
 * 医疗检验报告单识别
 *
 *接口描述
 * 支持识别全国各地医疗检验报告单的姓名、性别、医院名称、报告单名称等关键字段，支持识别检查具体项目的项目名称、结果、单位、参考区间、结果提示等明细字段。
 *
 */
@Slf4j
@RestController
@RequestMapping("/medical")
@NotIntercept
public class MedicalReportDetectionController {

    @Autowired
    private BMedicalReportDetectionHandle bMedicalReportDetectionHandle;


    @RequestMapping("/reportDetection")
    @CacheResult
    public R reportDetection(BMedicalReportDetectionRequest req) {
        MedicalReportDetectionResp resp = new MedicalReportDetectionResp();

        ApiR<BMedicalReportDetectionResp> bR = bMedicalReportDetectionHandle.handle(req);
        if (bR.isSuccess()) {
            BMedicalReportDetectionResp bMedicalReportDetectionResp = bR.getData();
            BMedicalReportDetectionResp.WordsResultDTO wordsResultDTO = bMedicalReportDetectionResp.getWordsResult();


            List<List<MedicalReportDetectionResp.DataDTO>> item = new ArrayList<>();                   // 检查项目

            if (CollectionUtils.isNotEmpty(wordsResultDTO.getItem())) {
                for (List<BMedicalReportDetectionResp.WordsResultDTO.ItemDTO> itemDTOS : wordsResultDTO.getItem()) {
                    List<MedicalReportDetectionResp.DataDTO> dataDTOS = new ArrayList<>();
                    for (BMedicalReportDetectionResp.WordsResultDTO.ItemDTO itemDTO : itemDTOS) {
                        MedicalReportDetectionResp.DataDTO tempDataDTO = new MedicalReportDetectionResp.DataDTO();
                        tempDataDTO.setWord(itemDTO.getWord());
                        tempDataDTO.setWordName(itemDTO.getWordName());
                        dataDTOS.add(tempDataDTO);
                    }
                    item.add(dataDTOS);
                }
            }
            List<MedicalReportDetectionResp.DataDTO> commonData = new ArrayList<>();               // 患者具体信息

            if (CollectionUtils.isNotEmpty(wordsResultDTO.getCommonData())) {
                for (BMedicalReportDetectionResp.WordsResultDTO.CommonDataDTO commonDatum : wordsResultDTO.getCommonData()) {
                    MedicalReportDetectionResp.DataDTO tempDataDTO = new MedicalReportDetectionResp.DataDTO();
                    tempDataDTO.setWord(commonDatum.getWord());
                    tempDataDTO.setWordName(commonDatum.getWordName());
                    commonData.add(tempDataDTO);

                }
            }

            resp.setItem(item);
            resp.setCommonData(commonData);


        }

        return R.error(bR.getErrorMsg());
    }
}
