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

import com.dxhy.api.model.invoice.InvoiceDetailInfo;
import com.dxhy.common.utils.CommonUtil;
import com.dxhy.core.job.dao.TDxRecordInvoiceStatisticsJobDao;
import com.dxhy.core.job.entity.TDxRecordInvoiceStatisticsJobEntity;
import com.dxhy.core.job.service.TDxRecordInvoiceStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author user
 */

@Service("tDxRecordInvoiceStatisticsService")
@Slf4j
@SuppressWarnings("AlibabaClassNamingShouldBeCamel")

public class TDxRecordInvoiceStatisticsServiceImpl implements TDxRecordInvoiceStatisticsService {

    @Resource
    private TDxRecordInvoiceStatisticsJobDao tDxRecordInvoiceStatisticsDao;

    @Override
    public void countTaxRate(List<InvoiceDetailInfo> recordInvoiceDetail, String invoiceCode, String invoiceNo) {
        try {
            // 1,查看有几种税率
            Set<String> set = new HashSet<String>();
            for (InvoiceDetailInfo rs : recordInvoiceDetail) {
                if(StringUtils.isNotBlank(CommonUtil.processTaxRate(rs.getTaxRate()))){
                    set.add(CommonUtil.processTaxRate(rs.getTaxRate()));
                }
            }
            // 2,计算每种税率金额合计
            for (String rate : set) {
                TDxRecordInvoiceStatisticsJobEntity ris = new TDxRecordInvoiceStatisticsJobEntity();
                BigDecimal typeAmount = new BigDecimal("0");
                BigDecimal typeTaxAmount = new BigDecimal("0");
                for (InvoiceDetailInfo mx : recordInvoiceDetail) {
                    if (rate.equals(CommonUtil.processTaxRate(mx.getTaxRate())) && !"原价合计".equals(mx.getGoodsName().trim())
                        && !"折扣额合计".equals(mx.getGoodsName().trim()) && !"(详见销货清单)".equals(mx.getGoodsName().trim())&& !"（详见销货清单）".equals(mx.getGoodsName().trim())
                        && (StringUtils.isNotBlank(CommonUtil.processTaxRate(mx.getTaxRate())))) {
                        typeAmount = typeAmount.add(new BigDecimal(mx.getDetailAmount()));
                        typeTaxAmount = typeTaxAmount.add(new BigDecimal(mx.getTaxAmount()));
                    }
                }
                ris.setInvoiceCode(invoiceCode);
                ris.setInvoiceNo(invoiceNo);
                try {
                    ris.setTaxRate(CommonUtil.processTaxRate(rate));
                } catch (Exception e) {
                    ris.setTaxRate("0");
                }
                ris.setDetailAmount(typeAmount);
                ris.setTaxAmount(typeTaxAmount);
                ris.setTotalAmount(typeAmount.add(typeTaxAmount));
                // 存入税率表
                tDxRecordInvoiceStatisticsDao.insert(ris);
            }
        } catch (Exception e) {
            log.error("RecordInvoiceStatisticsService.countTaxRate:err", e);
        }

    }

}
