package cn.hnthyy.thmz.service.impl.his.zy;

import cn.hnthyy.thmz.common.Constants;
import cn.hnthyy.thmz.entity.MzException;
import cn.hnthyy.thmz.entity.his.zy.ZyReceipt;
import cn.hnthyy.thmz.entity.thmz.Receipt;
import cn.hnthyy.thmz.entity.thmz.User;
import cn.hnthyy.thmz.mapper.his.zy.ZyReceiptMapper;
import cn.hnthyy.thmz.mapper.thmz.ReceiptMapper;
import cn.hnthyy.thmz.service.his.zy.ZyReceiptService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 住院发票数据库接口
 */
@Service
public class ZyReceiptServiceImpl implements ZyReceiptService {
    @SuppressWarnings("all")
    @Autowired
    private ZyReceiptMapper zyReceiptMapper;
    @SuppressWarnings("all")
    @Autowired
    private ReceiptMapper receiptMapper;


    @Override
    public List<ZyReceipt> queryAllValidReceipt(String inpatientNo, Integer admissTimes) {
        return zyReceiptMapper.selectAllValidReceipt(inpatientNo,admissTimes);
    }

    @Override
    public List<ZyReceipt> queryAllValidAndNotPrintReceipt(String inpatientNo, Integer admissTimes) {
        return zyReceiptMapper.selectAllValidAndNotPrintReceipt(inpatientNo,admissTimes);
    }

    @Override
    public ZyReceipt queryZyReceiptByLedgerSn(String inpatientNo, Integer admissTimes, Long ledgerSn) {
        return zyReceiptMapper.selectZyReceiptByLedgerSn(inpatientNo,admissTimes,ledgerSn);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public ZyReceipt printReceipt(String inpatientNo, Integer admissTimes, Long ledgerSn, User user) throws MzException {
        Receipt receipt = receiptMapper.selectByUserIdCode(user.getUserIdCode());
        if (receipt == null || StringUtils.isBlank(receipt.getZyReceiptCurrent())) {
            throw new MzException("用户id=" + user.getUserName() + "没有可用的住院发票,请先维护发票。");
        }
        ZyReceipt updateZyReceipt = new ZyReceipt(inpatientNo, admissTimes,ledgerSn);
        updateZyReceipt.setReceiptNo(receipt.getZyReceiptCurrent());
        Date now = new Date();
        updateZyReceipt.setPrintDate(now);
        updateZyReceipt.setOpIdCode(user.getUserIdCode());
        int num =zyReceiptMapper.printReceipt(updateZyReceipt);
        if (num == 0) {
            throw new MzException("已经打印过发票，请勿重复打印，如果需要重新打印，请点击重新打印或者作废打印");
        }
        if (receipt.getZyReceiptCurrent() == receipt.getZyReceiptEnd()) {
            //此时发票号已经用完，当前发票号是最后一个
            receipt.setZyReceiptCurrent(null);
        } else {
            Integer temp = Integer.valueOf(receipt.getZyReceiptCurrent()) + 1;
            receipt.setZyReceiptCurrent(temp.toString());
        }
        receipt.setUpdateDate(new Date());
        receiptMapper.updateReceipt(receipt);
        return zyReceiptMapper.selectZyReceiptByLedgerSn(inpatientNo,admissTimes,ledgerSn);
    }
}
