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

import cn.hnthyy.thmz.entity.his.mz.MzDepositFile;
import cn.hnthyy.thmz.entity.his.mz.MzReceiptSerial;
import cn.hnthyy.thmz.entity.thmz.Mzmxsr;
import cn.hnthyy.thmz.enums.YesNoEnum;
import cn.hnthyy.thmz.mapper.his.mz.ChargeFeeVoMapper;
import cn.hnthyy.thmz.mapper.his.mz.MzDepositFileMapper;
import cn.hnthyy.thmz.mapper.his.mz.MzReceiptSerialMapper;
import cn.hnthyy.thmz.service.his.mz.ChargeFeeVoService;
import cn.hnthyy.thmz.vo.*;
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.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ChargeFeeVoServiceImpl implements ChargeFeeVoService {
    @SuppressWarnings("all")
    @Autowired
    private ChargeFeeVoMapper chargeFeeVoMapper;
    @SuppressWarnings("all")
    @Autowired
    private MzReceiptSerialMapper mzReceiptSerialMapper;
    @SuppressWarnings("all")
    @Autowired
    private MzDepositFileMapper mzDepositFileMapper;

    @Override
    public List<ChargeFeeVo> queryChargeFeeVoUnAccount(String userIdCode) {
        return chargeFeeVoMapper.selectChargeFeeVoUnAccount(userIdCode);
    }

    @Override
    public List<ChargeFeeVo> queryChargeFeeVoCollect(YesNoEnum hisDate, String caseType, Date beginDate, Date endDate) {
        if (YesNoEnum.YES.equals(hisDate)) {
            return chargeFeeVoMapper.selectHisChargeFeeVoCollect(caseType, beginDate, endDate);
        }
        return chargeFeeVoMapper.selectChargeFeeVoCollect(caseType, beginDate, endDate);
    }

    @Override
    public List<ChargeFeeVo> queryChargeFeeVoReprint(String userIdCode, String caseType, Date dcountDate) {
        return chargeFeeVoMapper.selectChargeFeeVoReprint(userIdCode, caseType, dcountDate);
    }

    @Override
    public List<DcountDateListVo> queryDcountDateList(String userIdCode, Date beginDate, Date endDate) {
        return chargeFeeVoMapper.selectDcountDateList(userIdCode, beginDate, endDate);
    }

    @Override
    public List<ReqrecVo> queryReqrecVoUnAccount(String userIdCode) {
        return chargeFeeVoMapper.selectReqrecVoUnAccount(userIdCode);
    }

    @Override
    public List<ReqrecVo> queryReqrecVoReprint(String userIdCode, String caseType, Date dcountDate) {
        return chargeFeeVoMapper.selectReqrecVoReprint(userIdCode, caseType, dcountDate);
    }

    @Override
    public List<ReqrecVo> queryReqrecVoCollect(YesNoEnum hisDate, String caseType, Date beginDate, Date endDate) {
        if (YesNoEnum.YES.equals(hisDate)) {
            return chargeFeeVoMapper.selectHisReqrecVoCollect(caseType, beginDate, endDate);
        }
        return chargeFeeVoMapper.selectReqrecVoCollect(caseType, beginDate, endDate);
    }

    @Override
    public List<ReceiptBillVo> querySfReceiptBillVoUnAccount(String userIdCode) {
        return chargeFeeVoMapper.selectSfReceiptBillVoUnAccount(userIdCode);
    }

    @Override
    public List<ReceiptBillVo> queryGhReceiptBillVoUnAccount(String userIdCode) {
        return chargeFeeVoMapper.selectGhReceiptBillVoUnAccount(userIdCode);
    }

    @Override
    public List<ReceiptBillVo> querySfReceiptBillVoReprint(String userIdCode, Date dcountDate) {
        return chargeFeeVoMapper.selectSfReceiptBillVoReprint(userIdCode, dcountDate);
    }

    @Override
    public List<ReceiptBillVo> queryGhReceiptBillVoReprint(String userIdCode, Date dcountDate) {
        return chargeFeeVoMapper.selectGhReceiptBillVoReprint(userIdCode, dcountDate);
    }

    @Override
    public List<ChargeCountVo> queryChargeCountVo(String userIdCode, Date dcountDate, Date beginDate, Date endDate) {
        return chargeFeeVoMapper.selectChargeCountVo(userIdCode, dcountDate, beginDate, endDate);
    }

    @Override
    public List<MzReceiptSerial> queryMzReceiptSerial(String userIdCode, Date dcountDate, Date beginDate, Date endDate) {
        return chargeFeeVoMapper.selectMzReceiptSerial(userIdCode, dcountDate, beginDate, endDate);
    }

    @Override
    public Integer countReceipt(String userIdCode, String payMark, Date dcountDate) {
        return chargeFeeVoMapper.countReceipt(userIdCode, payMark, dcountDate);
    }

    @Override
    public List<ReceiptVo> queryReceiptVo(String userIdCode, Date dcountDate) {
        return chargeFeeVoMapper.selectReceiptVo(userIdCode, dcountDate);
    }

    @Override
    public Integer countReqrecReceipt(String userIdCode, String cancelMark, Date dcountDate, Date beginDate, Date endDate) {
        return chargeFeeVoMapper.countReqrecReceipt(userIdCode, cancelMark, dcountDate, beginDate, endDate);
    }

    @Override
    public List<ReqrecVo> queryMzReceiptSerialCount(String userIdCode, Date dcountDate, Date beginDate, Date endDate) {
        return chargeFeeVoMapper.selectMzReceiptSerialCount(userIdCode, dcountDate, beginDate, endDate);
    }

    @Override
    public List<ReceiptVo> queryThMzReceiptSerial(String userIdCode, Date dcountDate) {
        return chargeFeeVoMapper.selectThMzReceiptSerial(userIdCode, dcountDate);
    }

    @Override
    public Integer queryTFWDFPCount(String userIdCode) {
        return chargeFeeVoMapper.selectTFWDFPCount(userIdCode);
    }

    @Override
    public BigDecimal queryTotalCharge(String userIdCode) {
        return chargeFeeVoMapper.selectTotalCharge(userIdCode);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public Date dcountCharge(String userIdCode) {
        Date dcountDate = new Date();
        chargeFeeVoMapper.dcountReceipt(userIdCode, dcountDate);
        chargeFeeVoMapper.dcountReceiptChargeCount(userIdCode, dcountDate);
        chargeFeeVoMapper.dcountDeposit(userIdCode, dcountDate);
        chargeFeeVoMapper.dcountReqrec(userIdCode, dcountDate);
        chargeFeeVoMapper.dcountReqrecReceipt(userIdCode, dcountDate);
        return dcountDate;
    }

    @Override
    public List<String> queryAllOperatorFromReceipt(Date beginDate, Date endDate) {
        return chargeFeeVoMapper.selectAllOperatorFromReceipt(beginDate, endDate);
    }

    @Override
    public List<String> queryAllOperatorFromReqrec(Date beginDate, Date endDate) {
        return chargeFeeVoMapper.selectAllOperatorFromReqrec(beginDate, endDate);
    }

    @Override
    public List<WorkloadVo> queryGhWorkloadVo(YesNoEnum hisDate, Date beginDate, Date endDate,String cashId,YesNoEnum orderByDcountDate) {
        String tableName = "mzy_reqrec";
        if (YesNoEnum.YES.equals(hisDate)) {
            tableName = "mzy_reqrec_b";
        }
        return chargeFeeVoMapper.selectGhWorkloadVo(tableName, beginDate, endDate,cashId,orderByDcountDate);
    }

    @Override
    public List<WorkloadVo> querySfWorkloadVo(YesNoEnum hisDate, Date beginDate, Date endDate,String cashId,YesNoEnum orderByDcountDate) {
        String tableName = "mz_deposit_file";
        if (YesNoEnum.YES.equals(hisDate)) {
            tableName = "mz_deposit_file_b";
        }
        return chargeFeeVoMapper.selectSfWorkloadVo(tableName, beginDate, endDate,cashId,orderByDcountDate);
    }

    @Override
    public List<ChargeDetailVo> queryChargeDetailVos(YesNoEnum hisDate, Date beginDate, Date endDate) {
        String tableName = "mz_charge_detail";
        String tableName1 = "mz_receipt_serial";
        if (YesNoEnum.YES.equals(hisDate)) {
            tableName = "mz_charge_detail_b";
            tableName1 = "mz_receipt_serial_b";
        }
        return chargeFeeVoMapper.selectChargeDetailVos(tableName, tableName1, beginDate, endDate);
    }

    @Override
    public Integer queryCountCanDaily(String userIdCode) {
        Integer chargeCount = chargeFeeVoMapper.selectChargeCountCanDaily(userIdCode);
        Integer requecCount = chargeFeeVoMapper.selectRequecCountCanDaily(userIdCode);
        return chargeCount + requecCount;
    }


    @Override
    public int refeshPayManData(String userIdCode, Date dcountDate) {
        chargeFeeVoMapper.refeshJZReqrecData();
        chargeFeeVoMapper.refeshDepositFile();
        chargeFeeVoMapper.refeshreceiptSerial();
        chargeFeeVoMapper.refeshNomalPayManData(userIdCode, dcountDate);
        return chargeFeeVoMapper.refeshRefundNewData(userIdCode, dcountDate);
    }

    @Override
    public List<ReceiptVo> queryZiZhuReceiptVo(String userIdCode, Date dcountDate) {
        return chargeFeeVoMapper.selectZiZhuReceiptVo(userIdCode,dcountDate);
    }

    @Override
    public int refeshInconformityData(Date beginDate, Date endDate) {
        List<Integer> serialNos =  chargeFeeVoMapper.selectInconformityData(beginDate,endDate);
        if(serialNos==null || serialNos.size()==0){
            return 0;
        }
        for (Integer serialNo:serialNos){
            List<MzDepositFile> mzDepositFiles=mzDepositFileMapper.selectMzDepositFileBySerialNo(serialNo,null);
            if(mzDepositFiles!=null && mzDepositFiles.size()>0){
                MzDepositFile mzDepositFile= mzDepositFiles.get(0);
                MzReceiptSerial mzReceiptSerial = new MzReceiptSerial();
                mzReceiptSerial.setSerialNo(mzDepositFile.getSerialNo());
                mzReceiptSerial.setChargeDcountDate(mzDepositFile.getDcountDate());
                mzReceiptSerial.setChargeDcountNo(mzDepositFile.getDcountNo());
                mzReceiptSerialMapper.updateMzReceiptSerialBySerialNo(mzReceiptSerial);
            }
        }
        return serialNos.size();
    }

    @Override
    public int setPayId() {
        return chargeFeeVoMapper.setPayId();
    }

    @Override
    public List<Mzmxsr> queryThmzmxsr(ThmzmxsrParamsVo thmzmxsrParamsVo) {
        return chargeFeeVoMapper.selectThmzmxsr(thmzmxsrParamsVo);
    }

    @Override
    public int modifyMzService() {
        return mzReceiptSerialMapper.updateMzService();
    }

    @Override
    public List<Map<String, Object>> querySumAmount(ThmzmxsrParamsVo thmzmxsrParamsVo) {
        return chargeFeeVoMapper.selectSumAmount(thmzmxsrParamsVo);
    }
}
