package com.dxhy.core.service.log.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.core.dao.InvoiceLogDao;
import com.dxhy.core.entity.InvoiceLogEntity;
import com.dxhy.core.job.dao.TDxRecordInvoiceJobDao;
import com.dxhy.core.service.log.CoreInvoiceLogService;

/**
 * @author user
 */
@Service("coreInvoiceLogService")
public class CoreInvoiceLogServiceImpl extends ServiceImpl<InvoiceLogDao, InvoiceLogEntity>
    implements CoreInvoiceLogService {
    @Resource
    private TDxRecordInvoiceJobDao recordInvoiceJobDao;

    @Override
    public void saveLog(List<InvoiceLogEntity> logs) {
        List<InvoiceLogEntity> invoiceLogs = new ArrayList<>();
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        for (InvoiceLogEntity invoiceLogEntity : logs) {
            if ("1".equals(invoiceLogEntity.getType())) {
                Integer logNum = recordInvoiceJobDao.getInvoiceLog(invoiceLogEntity.getInvoiceCode(),
                    invoiceLogEntity.getInvoiceNo(), "1");
                if (logNum == 0) {
                    invoiceLogs.add(invoiceLogEntity);
                }
            } else {
                invoiceLogs.add(invoiceLogEntity);
            }
        }
        if (invoiceLogs.size() > 0) {
            DynamicContextHolder.push(DbConstant.BASICS_WRITE);
            this.insertLog(invoiceLogs);
        }
    }

    public void insertLog(List<InvoiceLogEntity> logs) {
        this.saveBatch(logs);
    }

}
