package thyyxxk.webserver.service.inpatient.charge;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import thyyxxk.webserver.config.exception.BizException;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.inpatient.charge.MzsfBillnoRecDao;
import thyyxxk.webserver.dao.his.zygl.ZyDepositFileDao;
import thyyxxk.webserver.entity.inpatient.charge.MzsfBillnoRec;
import thyyxxk.webserver.utils.AssertUtil;
import thyyxxk.webserver.utils.TokenUtil;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Description:
 * @Author:lihong
 * @Date: 2024/9/9
 */
@Slf4j
@Service
public class MzsfBillnoRecService {
    @Resource
    private MzsfBillnoRecDao dao;

    @Resource
    private ZyDepositFileDao zyDepositFileDao;


    /**
     * @description: 登记收费发票
     * @author: lihong
     * @date: 2024/9/9 10:14
     * @param: mzsfBillnoRec
     **/
    @Transactional(rollbackFor = Exception.class)
    public void save(MzsfBillnoRec mzsfBillnoRec) {
        AssertUtil.isnotBlank(mzsfBillnoRec.getStartNo(), "发票起始号不能为空");
        AssertUtil.isnotBlank(mzsfBillnoRec.getEndNo(), "发票起结束号不能为空");
        if (mzsfBillnoRec.getStartNo() > mzsfBillnoRec.getEndNo()) {
            throw new BizException(ExceptionEnum.LOGICAL_ERROR, "发票起始号不能大于结束号");
        }
        if (StrUtil.isBlank(mzsfBillnoRec.getCashId())) {
            mzsfBillnoRec.setCashId(TokenUtil.getInstance().getTokenUserId());
        }
        mzsfBillnoRec.setCurrentNo(mzsfBillnoRec.getStartNo());
        mzsfBillnoRec.setInputDate(new Date());
        mzsfBillnoRec.setStatusFlag("1");
        mzsfBillnoRec.setReceiptType("2");
        dao.updateStatusFlag(mzsfBillnoRec);
        dao.insert(mzsfBillnoRec);
    }


    public MzsfBillnoRec queryCurrentNo(MzsfBillnoRec param) {
        if (StrUtil.isBlank(param.getCashId())) {
            param.setCashId(TokenUtil.getInstance().getTokenUserId());
        }
        MzsfBillnoRec data = dao.selectCurMzsfBillnoRec(param.getCashId());
        if (data == null || data.getCurrentNo() == null) {
            throw new BizException(ExceptionEnum.LOGICAL_ERROR, "没有发票号了,请及时登记收费发票");
        }
        data.setCurrentNoStr(StrUtil.fillBefore(Convert.toStr(data.getCurrentNo()), '0', 8));
        return data;
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveYjjSjh(MzsfBillnoRec param) {
        AssertUtil.isnotBlank(param.getCurrentNo(), "当前收据号不能为空");
        if (StrUtil.isBlank(param.getCashId())) {
            param.setCashId(TokenUtil.getInstance().getTokenUserId());
        }
        Integer count = zyDepositFileDao.countReceiptNo(Convert.toStr(param.getCurrentNo()));
        if(count > 0){
            throw new BizException(ExceptionEnum.LOGICAL_ERROR, "当前收据号已经存在,请重新设置");
        }
        param.setInputDate(new Date());
        param.setStatusFlag("2");
        param.setReceiptType("9");
        param.setStartNo(0);
        param.setEndNo(0);
        MzsfBillnoRec mzsfBillnoRec = dao.selectCurYjjSjh(param.getCashId());
        if(mzsfBillnoRec == null){
            dao.insert(param);
        }else {
            dao.updateYjjSj(param);
        }
    }

    public MzsfBillnoRec queryYjjSjh(MzsfBillnoRec param) {
        if (StrUtil.isBlank(param.getCashId())) {
            param.setCashId(TokenUtil.getInstance().getTokenUserId());
        }
        return dao.selectCurYjjSjh(param.getCashId());
    }
}
