package com.heyu.api.controller.medical;


import com.heyu.api.baidu.handle.medical.BMedicalPrescriptionHandle;
import com.heyu.api.baidu.request.medical.BMedicalPrescriptionRequest;
import com.heyu.api.baidu.response.medical.BMedicalPrescriptionResp;
import com.heyu.api.controller.BaseController;
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.MedicalOutpatientResp;
import com.heyu.api.resp.medical.MedicalPrescriptionResp;
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=1740582532897#/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_prescription&method=post
 *
 *处方笺识别
 *
 *
 * 接口描述
 * 支持识别全国各地各医院处方笺的姓名、日期、病人ID、科别 4个关键字段，及表格区清单项目名称、规格、单价、数量、金额、频率、用量、用法等字段。
 *
 */

@Slf4j
@RestController
@RequestMapping("/medical")
@NotIntercept
public class MedicalPrescriptionController extends BaseController {


    @Autowired
    private BMedicalPrescriptionHandle bMedicalPrescriptionHandle;


    @RequestMapping("/prescription")
    @CacheResult
    public R prescription(BMedicalPrescriptionRequest req) {
        MedicalPrescriptionResp resp = new MedicalPrescriptionResp();

        ApiR<BMedicalPrescriptionResp> bR = bMedicalPrescriptionHandle.handle(req);
        if (bR.isSuccess()) {

            BMedicalPrescriptionResp bMedicalPrescriptionResp = bR.getData();
            BMedicalPrescriptionResp.WordsResultDTO wordsResultDTO = bMedicalPrescriptionResp.getWordsResult();


            List<MedicalOutpatientResp.DataDTO> commonData = new ArrayList<>();

            if (CollectionUtils.isNotEmpty(wordsResultDTO.getCommonData())) {
                for (BMedicalPrescriptionResp.WordsResultDTO.CommonDataDTO commonDatum : wordsResultDTO.getCommonData()) {
                    MedicalOutpatientResp.DataDTO dataDTO = new MedicalOutpatientResp.DataDTO();
                    dataDTO.setWordName(commonDatum.getWordName());
                    dataDTO.setWord(commonDatum.getWord());
                    commonData.add(dataDTO);
                }
            }
            resp.setCommonData(commonData);

            List<List<MedicalOutpatientResp.DataDTO>> costDetail = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(wordsResultDTO.getCostDetail())) {

                for (List<BMedicalPrescriptionResp.WordsResultDTO.CostDetailDTO> costDetailDTOS : wordsResultDTO.getCostDetail()) {

                    List<MedicalOutpatientResp.DataDTO> dataDTO = new ArrayList<>();
                    for (BMedicalPrescriptionResp.WordsResultDTO.CostDetailDTO costDetailDTO : costDetailDTOS) {
                        MedicalOutpatientResp.DataDTO dataDTO1 = new MedicalOutpatientResp.DataDTO();
                        dataDTO1.setWordName(costDetailDTO.getWordName());
                        dataDTO1.setWord(costDetailDTO.getWord());
                        dataDTO.add(dataDTO1);
                    }
                    costDetail.add(dataDTO);
                }
            }
            resp.setCostDetail(costDetail);
        }


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

    }
}