package com.heyu.api.controller.medical;


import com.heyu.api.baidu.handle.medical.BMedicalOutpatientHandle;
import com.heyu.api.baidu.request.medical.BMedicalOutpatientRequest;
import com.heyu.api.baidu.response.medical.BMedicalOutpatientResp;
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 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=1740582423627#/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_outpatient&method=post
 *门诊病历识别
 *
 *接口描述
 * 支持识别全国各地各医院门诊病历的姓名、日期、诊断、检查、主诉、现病史 6个关键字段，及表格区清单项目名称、规格、单价、数量、金额、项目类型等字段。
 */


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


    @Autowired
    private BMedicalOutpatientHandle bMedicalOutpatientHandle;


    @RequestMapping("/outpatient")
    @CacheResult
    public R outpatient(BMedicalOutpatientRequest req) {

        MedicalOutpatientResp resp = new MedicalOutpatientResp();

        ApiR<BMedicalOutpatientResp> bR = bMedicalOutpatientHandle.handle(req);
        if (bR.isSuccess()) {

            BMedicalOutpatientResp bMedicalOutpatientResp =   bR.getData();
            BMedicalOutpatientResp.WordsResultDTO wordsResultDTO = bMedicalOutpatientResp.getWordsResult();


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

            if(CollectionUtils.isNotEmpty(wordsResultDTO.getCommonData())){
                for (BMedicalOutpatientResp.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<BMedicalOutpatientResp.WordsResultDTO.CostDetailDTO> costDetailDTOS : wordsResultDTO.getCostDetail()) {
                    List<MedicalOutpatientResp.DataDTO> dataDTO = new ArrayList<>();
                    for (BMedicalOutpatientResp.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());
    }

}
