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

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.TDxVehicleSaleInvoiceDao;
import com.dxhy.portrait.business.entity.TDxVehicleSaleInvoice;
import com.dxhy.portrait.business.service.IJdcService;

/**
 *
 * **************************************************************************** 机动车明细表业务操作实现
 *
 * @author xuyongyun
 * @date 2019年4月29日
 ******************************************************************************
 */
@Service
public class JdcServiceImpl extends MpBaseServiceImpl<TDxVehicleSaleInvoiceDao, TDxVehicleSaleInvoice>
    implements IJdcService {

    @Override
    public boolean deleteVehicleSaleInvoice(String uuid, String dbName) {
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_WRITE);
        return this.removeVechicleSale(uuid);
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean removeVechicleSale(String uuid) {
        // 删除已存在的数据，防止数据重复
        try {
            baseMapper.delete(new QueryWrapper<TDxVehicleSaleInvoice>().eq("uuid", uuid));
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public void saveVehicleSaleInvoice(TDxVehicleSaleInvoice tDxVehicleSaleInvoice, String dbName) {
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_WRITE);
        this.insertInvoice(tDxVehicleSaleInvoice);

    }

    @Transactional(rollbackFor = Exception.class)
    public void insertInvoice(TDxVehicleSaleInvoice tDxVehicleSaleInvoice) {
        baseMapper.insert(tDxVehicleSaleInvoice);
    }
}
