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.SignCheckRecordInvoiceDao;
import com.dxhy.api.model.sign.TDxCheckRecordInvoice;
import com.dxhy.jxpt.baseService.sign.service.singletable.SignCheckRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 *
 * 发票查验表业务逻辑实现类
 *
 * @author ld
 */
@Service("signCheckRecordService")
@Slf4j

public class SignCheckRecordServiceImpl extends MpBaseServiceImpl<SignCheckRecordInvoiceDao, TDxCheckRecordInvoice>
    implements SignCheckRecordService {

    @Override
    public TDxCheckRecordInvoice getCheckInvoiceByUuid(String uuid) {
        QueryWrapper<TDxCheckRecordInvoice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uuid", uuid);
        return super.getOne(queryWrapper);
    }

    @Override
    public void saveCheckRecordInvoice(TDxCheckRecordInvoice checkRecordInvoice, String dbName) {
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_WRITE);
        if (checkRecordInvoice != null) {
            TDxCheckRecordInvoice checkRecordTmp = getCheckInvoiceByUuid(checkRecordInvoice.getUuid());
            if (checkRecordTmp != null) {
                if (StringUtils.isNotBlank(checkRecordTmp.getCycs())) {
                    int cycs = Integer.parseInt(checkRecordTmp.getCycs()) + 1;
                    checkRecordInvoice.setCycs(cycs + "");
                } else {
                    checkRecordInvoice.setCycs("1");
                }
            } else {
                checkRecordInvoice.setCycs("1");
            }
            // 删除重复数据，防止重复的操作
            QueryWrapper<TDxCheckRecordInvoice> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uuid", checkRecordInvoice.getUuid());
            remove(queryWrapper);
            // 保存数据
            save(checkRecordInvoice);

        }
    }

}
