package com.dxhy.portrait.business.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.portrait.business.dao.TDxRecordInvoiceStatisticsDao;
import com.dxhy.portrait.business.entity.TDxRecordInvoiceStatistics;
import com.dxhy.portrait.business.service.IInvoiceStatisticsService;

/**
 *
 * **************************************************************************** 税率统计表业务处理实现类
 *
 * @author xuyongyun
 * @date 2019年5月31日
 ******************************************************************************
 */
@Service
public class InvoiceStatisticsServiceImpl extends
    MpBaseServiceImpl<TDxRecordInvoiceStatisticsDao, TDxRecordInvoiceStatistics> implements IInvoiceStatisticsService {

    @Override
    public void saveInvoiceStatistics(List<TDxRecordInvoiceStatistics> statisticsList, String dbName) {
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_WRITE);
        this.insertStatistics(statisticsList);
    }

    @Transactional(rollbackFor = Exception.class)
    public void insertStatistics(List<TDxRecordInvoiceStatistics> statisticsList) {
        this.saveBatch(statisticsList);
    }

    @Override
    public boolean deleteStatistics(String invoiceCode, String invoiceNo, String dbName) {
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_WRITE);

        return this.removeStatistics(invoiceCode, invoiceNo);
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean removeStatistics(String invoiceCode, String invoiceNo) {
        try {

            int delete = baseMapper.delete(new QueryWrapper<TDxRecordInvoiceStatistics>()
                .eq("invoice_code", invoiceCode).eq("invoice_no", invoiceNo));
            if (delete > 0) {

                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}
