package com.dxhy.erp.service.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.erp.dao.SignRecordInvoiceDetailDao;
import com.dxhy.erp.entity.TDxRecordInvoiceDetail;
import com.dxhy.erp.service.SignInvoiceDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 *
 * 底账明细表业务实现类
 *
 * @author ld
 *
 *
 */
@Service("signInvoiceDetailService")
@Slf4j

public class SignInvoiceDetailServiceImpl extends MpBaseServiceImpl<SignRecordInvoiceDetailDao, TDxRecordInvoiceDetail>
    implements SignInvoiceDetailService {

    @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) {
        /*
          批量操作的大小
         */
        int batchSize = 500;
        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;
        }
    }

}
