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.TDxRecordInvoiceDetailDao;
import com.dxhy.portrait.business.entity.TDxRecordInvoiceDetail;
import com.dxhy.portrait.business.service.IInvoiceDetail;

/**
 *
 * **************************************************************************** 底账明细表业务实现类
 *
 * @author xuyongyun
 * @date 2019年4月30日
 ******************************************************************************
 */
@Service
public class InvoiceDetailImpl extends MpBaseServiceImpl<TDxRecordInvoiceDetailDao, TDxRecordInvoiceDetail>
    implements IInvoiceDetail {
    /**
     * 批量操作的大小
     */
    private final int batchSize = 500;

    @Override
    public void saveInvoiceDetail(List<TDxRecordInvoiceDetail> details, String dbName) {
        DynamicContextHolder.push(dbName + DbConstant.DETAIL_WRITE);
        this.insertDetail(details);

    }

    @Transactional(rollbackFor = Exception.class)
    public void insertDetail(List<TDxRecordInvoiceDetail> details) {
        this.saveBatch(details, batchSize);
    }

    @Override
    public boolean deleteDetail(String uuid, String dbName) {
        DynamicContextHolder.push(dbName + DbConstant.DETAIL_WRITE);
        return this.removeDetail(uuid);
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean removeDetail(String uuid) {
        try {
            baseMapper.delete(new QueryWrapper<TDxRecordInvoiceDetail>().eq("uuid", uuid));
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}
