package com.heyu.api.controller.medical;

import com.heyu.api.baidu.handle.medical.BMedicalDetailHandle;
import com.heyu.api.baidu.request.medical.BMedicalDetailRequest;
import com.heyu.api.baidu.response.medical.BMedicalDetailResp;
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.MedicalDetailResp;
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=1740580669633#/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_detail&method=post
 * 医疗费用明细识别
 * <p>
 * <p>
 * 接口描述
 * 支持识别全国医疗费用明细的姓名、日期、病人ID、总金额等关键字段，支持识别费用明细项目清单，包含项目类型、项目名称、单价、数量、规格、金额，其中北京地区识别效果最佳。
 */
@Slf4j
@RestController
@RequestMapping("/medical")
@NotIntercept
public class MedicalDetailController extends BaseController {

    @Autowired
    private BMedicalDetailHandle bMedicalDetailHandle;


    @RequestMapping("/detail")
    @CacheResult
    public R detail(BMedicalDetailRequest req) {


        MedicalDetailResp resp = new MedicalDetailResp();
        ApiR<BMedicalDetailResp> bR = bMedicalDetailHandle.handle(req);
        if (bR.isSuccess()) {
            BMedicalDetailResp bMedicalDetailResp = bR.getData();
            BMedicalDetailResp.WordsResultDTO wordsResultDTO = bMedicalDetailResp.getWordsResult();


            resp.setPatient(wordsResultDTO.getPatientID().getWord());                 //病人ID
            resp.setTotalAmount(wordsResultDTO.getTotalAmount().getWord());             //总金额
            resp.setDate(wordsResultDTO.getDate().getWord());                    //日期
            resp.setName(wordsResultDTO.getName().getWord());                //姓名

            if (CollectionUtils.isNotEmpty(wordsResultDTO.getCostDetail())) {
                List<List<MedicalDetailResp.CostDetailDTO>> list = new ArrayList<>();
                for (List<BMedicalDetailResp.WordsResultDTO.CostDetailDTO> costDetailDTOS : wordsResultDTO.getCostDetail()) {
                    List<MedicalDetailResp.CostDetailDTO> list1 = new ArrayList<>();
                    for (BMedicalDetailResp.WordsResultDTO.CostDetailDTO costDetailDTO : costDetailDTOS) {
                        MedicalDetailResp.CostDetailDTO cost = new MedicalDetailResp.CostDetailDTO();
                        cost.setWord(costDetailDTO.getWord());
                        cost.setWordName(costDetailDTO.getWordName());
                        list1.add(cost);
                    }
                    list.add(list1);
                }
                resp.setCostDetail(list);
            }
        }
        return R.error();
    }


}
