package thyyxxk.webserver.service.inpatient.dismiss;

import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.BizException;
import thyyxxk.webserver.dao.his.inpatient.dismiss.ReceiveDrugDao;
import thyyxxk.webserver.entity.inpatient.dismiss.SnAndResponce;
import thyyxxk.webserver.entity.inpatient.dismiss.ZyInit;
import thyyxxk.webserver.entity.inpatient.dismiss.ZyTmpReceiveData;
import thyyxxk.webserver.utils.StringUtil;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

@Service
public class ReceiveDrugService {
    private final ReceiveDrugDao dao;

    public ReceiveDrugService(ReceiveDrugDao dao) {
        this.dao = dao;
    }

    public void execute(String inNo, int times) {
        String vNo = inNo.startsWith("$") ? inNo.replace("$", "") : inNo;
        int inHospitalState = dao.getInHospitalState(vNo, times);
        String actTable = inHospitalState == 1 ? "zy_actpatient" : "zy_inactpatient";
        SnAndResponce snAndResponce = dao.getSnAndResponce(vNo, times, actTable);
        if (null == snAndResponce || null == snAndResponce.getSn()) {
            throw new BizException("获取患者账页失败，请联系管理员。");
        }
        int sn = snAndResponce.getSn();
        Date accountDate = dao.getAccountDate(vNo, times, sn);
        String chgStatus = null == accountDate ? "2" : "1";
        ZyInit zyInit = dao.getZyInit(inNo, times);
        zyInit.setChgStatus(chgStatus);
        List<ZyTmpReceiveData> zyTmpReceiveDataList = dao.getTmpReceiveData(zyInit);
        dao.updateYpZyPatient(inNo, times);
        if (zyTmpReceiveDataList.isEmpty()) {
            checkInpatientOweLimit(inNo, times, accountDate);
            return;
        }
        int maxDetailSn = dao.getMaxDetailSn(vNo, times, sn) + 1;
        for (int i = 0; i < zyTmpReceiveDataList.size(); i++) {
            ZyTmpReceiveData item = zyTmpReceiveDataList.get(i);
            item.setLedgerSn(sn);
            item.setDetailSn(maxDetailSn + i);
            if (StringUtil.isBlank(item.getInfantFlag())) {
                item.setInfantFlag("0");
            }
            if (StringUtil.isBlank(item.getSelfFlag())) {
                item.setSelfFlag("0");
            }
            if (StringUtil.isBlank(item.getSeparateFlag())) {
                item.setSeparateFlag("0");
            }
            if (StringUtil.isBlank(item.getSuppressFlag())) {
                item.setSuppressFlag("0");
            }
            BigDecimal price = item.getChargeFee().setScale(2, RoundingMode.HALF_UP);
            if (price.compareTo(new BigDecimal("0.01")) < 0) {
                price = new BigDecimal("0.01");
            }
            item.setChargeFee(price.multiply(item.getOrderCount()).setScale(2, RoundingMode.HALF_UP));
        }
        dao.insertZyChargeFee(zyTmpReceiveDataList);
        checkInpatientOweLimit(inNo, times, accountDate);
    }

    private void checkInpatientOweLimit(String inNo, int times, Date accountDate) {
        if (null != accountDate) {
            return;
        }
        int oweLimit = dao.getOweLimit(inNo, times);
        if (oweLimit <= 0) {
            dao.updateInterCode9(inNo, times);
        } else {
            dao.updateInterCode0(inNo, times);
        }
    }
}
