package thyyxxk.webserver.service.casefrontsheet;

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.casefrontsheet.FrontSheetExportDao;
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.StringUtil;

import java.util.List;

@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);
        list.forEach(item -> {
            String pmtd = item.getPayMethod();
            if (StringUtil.invalidValue(pmtd)) {
                item.setPayMethod("9");
            } else {
                int mtd = Integer.parseInt(pmtd);
                if (mtd < 4) {
                    if (item.getSocialNo().startsWith("4301")) {
                        pmtd = pmtd + ".1";
                    } else {
                        pmtd = pmtd + ".2";
                    }
                    item.setPayMethod(pmtd);
                }
            }
            item.setZyDismissWay(DismissWay.getWjwCodeByHisCode(item.getZyDismissWay()));
            item.setHbsAg(Hbsag.getWjwCodeByHisCode(item.getHbsAg()));
            String ntvplc = item.getNativePlace().substring(0, 2);
            item.setNativePlace(Provinces.getWjwCodeByHisCode(ntvplc));
            item.setCountry(redis.getCountryName(item.getCountry()));
            item.setAdmissDeptCode(redis.getWjwDepartment(item.getAdmissDeptCode()));
            item.setZkWardCode(redis.getWjwDepartment(item.getZkWardCode()));
            item.setDismissDeptCode(redis.getWjwDepartment(item.getDismissDeptCode()));
            item.setNation(redis.getWjwNation(item.getNation()));
            item.setDisdiagList(dao.fetchDiags(item.getBah(), item.getAdmissTimes()));
            List<CaseFrontsheetSurgery> surgeries = dao.fetchSurgeries(item.getBah(), item.getAdmissTimes());
            surgeries.forEach(s -> {
                s.setAnaesthesia(redis.getWjwAnaesthesia(s.getAnaesthesia()));
                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);
                }
            });
            item.setSurgeryList(surgeries);
        });
        return list;
    }

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