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

import cn.hnthyy.thmz.entity.his.MzDepositFile;
import cn.hnthyy.thmz.mapper.his.MzDepositFileMapper;
import cn.hnthyy.thmz.service.his.MzDepositFileService;
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.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class MzDepositFileServiceImpl implements MzDepositFileService {
    @SuppressWarnings("all")
    @Autowired
    private MzDepositFileMapper mzDepositFileMapper;
    @Override
    public int saveMzDepositFile(MzDepositFile mzDepositFile) {
        return mzDepositFileMapper.insertMzDepositFile(mzDepositFile);
    }


    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    @Override
    public int batchSaveMzDepositFile(List<MzDepositFile> mzDepositFileList) {
        if (mzDepositFileList.size() == 0) {
            return 0;
        }
        if (mzDepositFileList.size() <= 20) {
            mzDepositFileMapper.batchInsertMzDepositFile(mzDepositFileList);
        } else {
            List<MzDepositFile> tempList = new ArrayList<>();
            for (MzDepositFile md : mzDepositFileList) {
                tempList.add(md);
                if (tempList.size() == 20) {
                    mzDepositFileMapper.batchInsertMzDepositFile(tempList);
                    tempList.clear();
                }
            }
            if (tempList.size() > 0) {
                mzDepositFileMapper.batchInsertMzDepositFile(tempList);
            }
        }
        return mzDepositFileList.size();
    }

    @Override
    public List<MzDepositFile> queryMzDepositFile(MzDepositFile mzDepositFile) {
        return mzDepositFileMapper.selectMzDepositFileByTimes(mzDepositFile.getPatientId(),mzDepositFile.getTimes(),mzDepositFile.getReceiptNo());
    }

    @Override
    public MzDepositFile queryMzDepositFileByAgtordnum(String psordnum, String agtordnum) {
        return mzDepositFileMapper.selectMzDepositFileByAgtordnum(psordnum,agtordnum);
    }

    @Override
    public BigDecimal queryAmount(Date beginDate, Date endDate) {
        return mzDepositFileMapper.selectAmount(beginDate,endDate);
    }

    @Override
    public List<Map<String, Object>> countAndAmount(Date beginDate, Date endDate, String payMark) {
        return mzDepositFileMapper.countAndAmount(beginDate,endDate,payMark);
    }
}
