package thyyxxk.webserver.controller.medicalinsurance;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import thyyxxk.webserver.config.auth.PassToken;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.constants.YesOrNo;
import thyyxxk.webserver.constants.sidicts.MedChrgitmType;
import thyyxxk.webserver.dao.his.medicalinsurance.SiSetlinfoDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.medicalinsurance.query.SiSetlFeeDetl;
import thyyxxk.webserver.entity.medicalinsurance.setlinfo.SiSetlinfo;
import thyyxxk.webserver.service.medicalinsurance.SiQueryService;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/analyzeSiPatientCharges")
public class AnalyzeSiPatientChargesController {
    private final SiQueryService service;
    private final SiSetlinfoDao dao;

    @Autowired
    public AnalyzeSiPatientChargesController(SiQueryService service, SiSetlinfoDao dao) {
        this.service = service;
        this.dao = dao;
    }

    @GetMapping("/analyzeSetlData")
    @PassToken
    private void startAnalyze() {
        List<SiSetlinfo> unAnalyzedData = dao.selectUnAnalyzedData();
        for (SiSetlinfo setlinfo : unAnalyzedData) {
            ResultVo<List<SiSetlFeeDetl>> chargeList = service.getChrgitems(setlinfo);
            if (Objects.equals(chargeList.getCode(), ExceptionEnum.SUCCESS.getCode())) {
                analyzeChargeItems(setlinfo, chargeList.getData());
            }
            setlinfo.setChargesAnalyzed(YesOrNo.YES.getCode());
            dao.fillAnalyzedData(setlinfo);
        }
    }

    private void analyzeChargeItems(SiSetlinfo setlinfo, List<SiSetlFeeDetl> chargeItems) {
        Map<String, BigDecimal> map = new HashMap<>();
        chargeItems.forEach(itm -> {
            BigDecimal itmfeeSum = new BigDecimal(itm.getDetItemFeeSumamt());
            String typeKey = itm.getMedChrgitmType();
            if (map.containsKey(typeKey)) {
                map.replace(typeKey, map.get(typeKey).add(itmfeeSum));
            } else {
                map.put(typeKey, itmfeeSum);
            }
        });
        setlinfo.setChargeWesternMedicine(map.get(MedChrgitmType.WEST_MED.getCode()));
        setlinfo.setChargePatentMedicine(map.get(MedChrgitmType.CHN_PTNT.getCode()));
        setlinfo.setChargeHerbal(map.get(MedChrgitmType.HERBAL.getCode()));
        setlinfo.setChargeExamination(map.get(MedChrgitmType.CLINIC.getCode()));
        setlinfo.setChargeTreatment(map.get(MedChrgitmType.TREAT.getCode()));
        setlinfo.setChargeOperation(map.get(MedChrgitmType.SURGERY.getCode()));
        setlinfo.setChargeNursing(map.get(MedChrgitmType.NURSING.getCode()));
        setlinfo.setChargeSanitaryMaterial(map.get(MedChrgitmType.MATERIAL.getCode()));
        setlinfo.setChargeAssay(map.get(MedChrgitmType.EXAM.getCode()));
        setlinfo.setChargeGeneralDiagnosis(map.get(MedChrgitmType.HEAL.getCode()));
        setlinfo.setChargeInspection(map.get(MedChrgitmType.INSPECTION.getCode()));
        setlinfo.setChargeRegistration(map.get(MedChrgitmType.REG.getCode()));
        setlinfo.setChargeBed(map.get(MedChrgitmType.BED.getCode()));
        setlinfo.setChargeOthers(map.get(MedChrgitmType.OTH.getCode()));
    }
}
