package thyyxxk.webserver.service.inpatient.casefrontsheet;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.constants.frntsheet.DismissWay;
import thyyxxk.webserver.constants.frntsheet.Hbsag;
import thyyxxk.webserver.constants.frntsheet.Provinces;
import thyyxxk.webserver.dao.his.inpatient.casefrontsheet.FrontSheetExportDao;
import thyyxxk.webserver.entity.casefrontsheet.CaseFrontsheetDisdiag;
import thyyxxk.webserver.entity.casefrontsheet.CaseFrontsheetMain;
import thyyxxk.webserver.entity.casefrontsheet.CaseFrontsheetSurgery;
import thyyxxk.webserver.service.redislike.RedisLikeService;
import thyyxxk.webserver.utils.DateUtil;
import thyyxxk.webserver.utils.DecimalUtil;
import thyyxxk.webserver.utils.StringUtil;

import java.util.List;

@Slf4j
@Service
public class CaseFrontSheetExportService {
    private final FrontSheetExportDao dao;
    private final RedisLikeService redis;

    @Autowired
    public CaseFrontSheetExportService(FrontSheetExportDao dao, RedisLikeService redis) {
        this.dao = dao;
        this.redis = redis;
    }

    public List<CaseFrontsheetMain> fetchSheets(String month) {
        String begin = month + "-01 00:00:00.000";
        String end = DateUtil.getMonthEndtime(month);
        List<CaseFrontsheetMain> list = dao.fetchSheets(begin, end);
        for (CaseFrontsheetMain sheet : list) {
            sheet.setSurgicalFee(DecimalUtil.add(sheet.getAnesthetizeFee(), sheet.getSurgeryFee()));
            sheet.setSurgeryList(selectSurgeries(sheet.getBah(), sheet.getAdmissTimes()));
            sheet.setZyDismissWay(DismissWay.getWjwCodeByHisCode(sheet.getZyDismissWay()));
            sheet.setHbsAg(Hbsag.getWjwCodeByHisCode(sheet.getHbsAg()));
            String ntvplc = sheet.getNativePlace().substring(0, 2);
            sheet.setPayMethod(filterPayMethod(sheet.getPayMethod(), sheet.getSocialNo()));
            sheet.setRh(filterRh(sheet.getRh()));
            sheet.setNativePlace(Provinces.getWjwCodeByHisCode(ntvplc));
            sheet.setCountry(redis.getCountryName(sheet.getCountry()));
            sheet.setAdmissDeptCode(redis.getWjwDepartment(sheet.getAdmissDeptCode()));
            sheet.setZkWardCode(redis.getWjwDepartment(sheet.getZkWardCode()));
            sheet.setDismissDeptCode(redis.getWjwDepartment(sheet.getDismissDeptCode()));
            sheet.setNation(redis.getWjwNation(sheet.getNation()));
            sheet.setDisdiagList(selectDiags(sheet.getBah(), sheet.getAdmissTimes()));
        }
        list.removeIf(item -> item.getPathologicDiagStr().contains(",")
                || item.getClinicDiagStr().contains(",") || item.getDisdiagList().isEmpty());
        return list;
    }

    private List<CaseFrontsheetDisdiag> selectDiags(String bah, int times) {
        List<CaseFrontsheetDisdiag> diags = dao.fetchDiags(bah, times);
        diags.removeIf(item -> item.getName().contains(","));
        return diags;
    }

    private List<CaseFrontsheetSurgery> selectSurgeries(String bah, int times) {
        List<CaseFrontsheetSurgery> surgeries = dao.fetchSurgeries(bah, times);
        surgeries.removeIf(item -> StringUtil.isBlank(item.getName()) || item.getName().contains(","));
        for (CaseFrontsheetSurgery s : surgeries) {
            s.setName(s.getName().replaceAll(",", "，"));
            int cut = 0;
            if (StringUtil.notBlank(s.getCut())) {
                cut = Integer.parseInt(s.getCut());
            }
            int heal = 0;
            if (StringUtil.notBlank(s.getHeal())) {
                heal = Integer.parseInt(s.getHeal());
            }
            if (cut == 0 || heal == 0) {
                s.setWjwCutHeal(0);
            } else {
                int wjwCutHeal = (cut - 1) * cut + heal + negationCalculatingResidue(cut);
                s.setWjwCutHeal(wjwCutHeal);
            }
        }
        return surgeries;
    }

    private String filterPayMethod(String method, String idNumber) {
        if (StringUtil.isBlank(method)) {
            return "9";
        }
        int tempVal = Integer.parseInt(method);
        if (tempVal > 3) {
            return method;
        }
        if (idNumber.startsWith("4301")) {
            return method + ".1";
        }
        return method + ".2";
    }

    private String filterRh(String rh) {
        if (StringUtil.isBlank(rh) || rh.equals("0")) {
            return "4";
        }
        return rh;
    }

    private int negationCalculatingResidue(int source) {
        return source % 2 == 0 ? 1 : 0;
    }
}
