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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.ConfirmDao;
import com.dxhy.core.job.dao.TDxRecordInvoiceJobDao;
import com.dxhy.core.job.dao.TDxViolationLogDao;
import com.dxhy.core.job.entity.TDxViolationLog;
import com.dxhy.core.job.service.ViolationTDxRecordInvoice;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("violationTDxRecordInvoice")
public class ViolationTDxRecordInvoiceImpl extends ServiceImpl<TDxViolationLogDao, TDxViolationLog>
        implements ViolationTDxRecordInvoice {

    @Resource
    private ConfirmDao confirmDao;

    @Resource
    TDxRecordInvoiceJobDao tDxRecordInvoiceJobDao;

    @Override
    public void updateTdxRecodeInvoce() {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        List<String> dbList = confirmDao.getDbSource();
        if (CollectionUtils.isNotEmpty(dbList)) {
            String db = dbList.get(0);
            DynamicContextHolder.push(db + DbConstant.BUSINESS_READ);
            QueryWrapper<TDxViolationLog> tDxViolationLogQueryWrapper = new QueryWrapper<>();
            //分组获取供应商风险销方税号信息
            tDxViolationLogQueryWrapper.groupBy("xf_tax_no");
            List<TDxViolationLog> tDxViolationLogs = baseMapper.selectList(tDxViolationLogQueryWrapper);
            if (CollectionUtils.isNotEmpty(tDxViolationLogs)) {
                //循环获得供应商风险的销方税号信息
                tDxViolationLogs.stream().forEach(tDxViolationLog -> {
                    DynamicContextHolder.push(db + DbConstant.BUSINESS_WRITE);
                    String xfTaxNo = tDxViolationLog.getXfTaxNo();
                    //根据销方税号信息更新底账数据库中illegal_supplier供应商违规标志0否，1是
                    tDxRecordInvoiceJobDao.updateInvoiceByxfTaxNo(xfTaxNo);
                });
            }
        }

    }
}
