package com.dxhy.jxpt.baseService.sign.service.singletable.impl;

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.jxpt.baseService.sign.dao.SignRecordInvoiceStatisticsDao;
import com.dxhy.api.model.sign.TDxRecordInvoiceStatistics;
import com.dxhy.jxpt.baseService.sign.service.singletable.SignInvoiceStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 *
 * 税率统计表业务处理实现类
 *
 * @author ld
 */
@Service("signInvoiceStatisticsService")
@Slf4j

public class SignInvoiceStatisticsServiceImpl
    extends MpBaseServiceImpl<SignRecordInvoiceStatisticsDao, TDxRecordInvoiceStatistics>
    implements SignInvoiceStatisticsService {

    @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));
            return delete > 0;
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            return false;
        }
    }

}
