package com.heyu.api.controller.medical;


import com.heyu.api.baidu.handle.medical.BMedicalInvoiceHandle;
import com.heyu.api.baidu.request.medical.BMedicalInvoiceRequest;
import com.heyu.api.baidu.response.medical.BMedicalInvoiceResp;
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.MedicalInvoiceResp;
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=1740580001377#/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_invoice&method=post
 *
 *医疗发票识别
 *
 *
 * 接口描述
 * 支持识别全国各地门诊/住院发票的全字段信息，包括业务流水号、发票号、姓名、性别、社保卡号、金额大/小写、收款单位、省市、医保统筹支付、个人账户支付等关键字段，及收费项目明细、各省直辖市的专有信息，
 * 其中北京/广东/河北/河南/江苏/山东/上海/天津/浙江等地区票据识别效果较佳。
 * 支持识别收费项目明细，并可根据不同省市地区返回对应的识别信息，同时支持收费项目信息的医保三目录信息核验，支持包括北京、上海、广州、深圳等21 个城市的医保三目录信息核验。
 */


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


    @Autowired
    private BMedicalInvoiceHandle bMedicalInvoiceHandle;

    @RequestMapping("/invoice")
    @CacheResult
    public R invoice(BMedicalInvoiceRequest req) {
        MedicalInvoiceResp resp = new MedicalInvoiceResp();

        ApiR<BMedicalInvoiceResp> bR = bMedicalInvoiceHandle.handle(req);
        if (bR.isSuccess()) {
            BMedicalInvoiceResp bMedicalInvoiceResp = bR.getData();
            BMedicalInvoiceResp.WordsResultDTO wordsResultDTO = bMedicalInvoiceResp.getWordsResult();

            resp.setProvince(bMedicalInvoiceResp.getProvince());    // 省市：支持返回以下省市 , n北京/广东/河北/河南/江苏/山东/上海/天津/浙江等
            resp.setInvoiceType(bMedicalInvoiceResp.getInvoiceType());     //票据种类
            resp.setAmountInWords(wordsResultDTO.getAmountInWords().getWord());                   //大写合计金额
            resp.setSex(wordsResultDTO.getAmountInWords().getWord());                             //性别
            resp.setInsuranceType(wordsResultDTO.getInsuranceType().getWord());                   //医保类型
            resp.setName(wordsResultDTO.getName().getWord());                            //姓名
            resp.setSocialSecurityNum(wordsResultDTO.getSocialSecurityNum().getWord());               //社保卡号
            resp.setDischargeDate(wordsResultDTO.getDischargeDate().getWord());               //出院时间
            resp.setHospitalNum(wordsResultDTO.getHospitalNum().getWord());             //	住院号
            resp.setHospitalName(wordsResultDTO.getHospitalName().getWord());                    //医院名称
         //地区字段：根据省市返回改地区特有的字段
            resp.setClinicNum(wordsResultDTO.getClinicNum().getWord());                       //	门诊号
            resp.setAmountInFiguers(wordsResultDTO.getAmountInFiguers().getWord());                 //小写合计金额
            resp.setAdmissionDate(wordsResultDTO.getAdmissionDate().getWord());                   //	入院时间
            resp.setHospitalType(wordsResultDTO.getHospitalType().getWord());                        //医疗机构类型
            resp.setRefundAmount(wordsResultDTO.getRefundAmount().getWord());                     //退费金额
            resp.setDate(wordsResultDTO.getDate().getWord());                        //开票日期
            resp.setChargingUnit(wordsResultDTO.getChargingUnit().getWord());                        //收款单位
            resp.setPaymentAmount(wordsResultDTO.getPaymentAmount().getWord());               //补缴金额
            resp.setPrepayAmount(wordsResultDTO.getPaymentAmount().getWord());                    //	预缴金额
            resp.setPersonalPayment(wordsResultDTO.getPersonalPayment().getWord());                         //个人账户支付
            resp.setHospitalDay(wordsResultDTO.getHospitalDay().getWord());                 //住院天数
            resp.setBusinessNum(wordsResultDTO.getBusinessNum().getWord());                 //业务流水号
            resp.setInsurancePayment(wordsResultDTO.getInsurancePayment().getWord());                    //医保统筹支付
            resp.setPayee(wordsResultDTO.getPayee().getWord());                       //收款人
            resp.setRecordNum(wordsResultDTO.getRecordNum().getWord());                       //病例号
            resp.setInvoiceNum(wordsResultDTO.getInvoiceNum().getWord());                  //发票号码

            List< List<MedicalInvoiceResp.CostDTO>> costCategories = new ArrayList<>();
            if(CollectionUtils.isNotEmpty(wordsResultDTO.getCostCategories())){
                for (List<BMedicalInvoiceResp.WordsResultDTO.CostCategoriesDTO> costCategory : wordsResultDTO.getCostCategories()) {
                    List<MedicalInvoiceResp.CostDTO> cost = new ArrayList<>();
                    for (BMedicalInvoiceResp.WordsResultDTO.CostCategoriesDTO costCategoriesDTO : costCategory) {
                        MedicalInvoiceResp.CostDTO costDTO = new MedicalInvoiceResp.CostDTO();
                        costDTO.setName(costCategoriesDTO.getName());                //字段名，包括：收费项目、金额
                        costDTO.setWord(costCategoriesDTO.getWord());                    //name字段对应的识别结果
                        costDTO.setMediInfo(costCategoriesDTO.getMediInfo());            //医保目录查询结果，**当 medi_query 参数存在时，仅在「收费项目」的数组里返回，**以下参数同此说明
                        costDTO.setMediCheck(costCategoriesDTO.getMediCheck());           //查询是否成功，“1”表示成功，“0”表示不成功
                        costDTO.setMediName(costCategoriesDTO.getMediName());                //药品名
                        costDTO.setMediType(costCategoriesDTO.getMediType());                //医保类别
                        costDTO.setMediRegion(costCategoriesDTO.getMediRegion());                  //医保目录的城市
                        costDTO.setMediCode(costCategoriesDTO.getMediCode());                    //	药品编码
                        costDTO.setMediRegister(costCategoriesDTO.getMediRegister());            //	药品注册号
                        cost.add(costDTO);
                    }
                    costCategories.add(cost);
                }
            }


            resp.setCostCategories(costCategories);             //项目大类：治疗费、检查费等项目大类
             List<MedicalInvoiceResp.CostDTO> regionSupplement = new ArrayList<>();         //地区字段：根据省市返回改地区特有的字段
            if(CollectionUtils.isNotEmpty(wordsResultDTO.getRegionSupplement())){
                for (BMedicalInvoiceResp.WordsResultDTO.RegionSupplementDTO regionSupplementDTO : wordsResultDTO.getRegionSupplement()) {
                    MedicalInvoiceResp.CostDTO costDTO = new MedicalInvoiceResp.CostDTO();
                    costDTO.setName(regionSupplementDTO.getName());
                    costDTO.setWord(regionSupplementDTO.getWord());
                }
            }
            resp.setRegionSupplement(regionSupplement);
             List<List<MedicalInvoiceResp.CostDTO>> costDetail = new ArrayList<>();         //明细类别：药物/检查的明细类别

            if(CollectionUtils.isNotEmpty(wordsResultDTO.getCostDetail())){
                for (List<BMedicalInvoiceResp.WordsResultDTO.CostDetailDTO> costDetailList : wordsResultDTO.getCostDetail()) {
                    List<MedicalInvoiceResp.CostDTO> cost = new ArrayList<>();
                    for (BMedicalInvoiceResp.WordsResultDTO.CostDetailDTO costCategoriesDTO : costDetailList) {
                        MedicalInvoiceResp.CostDTO costDTO = new MedicalInvoiceResp.CostDTO();
                        costDTO.setName(costCategoriesDTO.getName());                //字段名，包括：收费项目、金额
                        costDTO.setWord(costCategoriesDTO.getWord());                    //name字段对应的识别结果
                        costDTO.setMediInfo(costCategoriesDTO.getMediInfo());            //医保目录查询结果，**当 medi_query 参数存在时，仅在「收费项目」的数组里返回，**以下参数同此说明
                        costDTO.setMediCheck(costCategoriesDTO.getMediCheck());           //查询是否成功，“1”表示成功，“0”表示不成功
                        costDTO.setMediName(costCategoriesDTO.getMediName());                //药品名
                        costDTO.setMediType(costCategoriesDTO.getMediType());                //医保类别
                        costDTO.setMediRegion(costCategoriesDTO.getMediRegion());                  //医保目录的城市
                        costDTO.setMediCode(costCategoriesDTO.getMediCode());                    //	药品编码
                        costDTO.setMediRegister(costCategoriesDTO.getMediRegister());            //	药品注册号
                        cost.add(costDTO);
                    }
                    costDetail.add(cost);
                }
            }
            resp.setCostDetail(costDetail);         //明细类别：药物/检查的明细类别

        }
        return R.error();
    }
}
