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

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.dxhy.core.job.dao.TDxRecordInvoiceStatisticsJobDao;
import com.dxhy.core.job.entity.TDxRecordInvoiceStatisticsJobEntity;
import com.dxhy.core.job.pojo.InvoiceDetailInfo;
import com.dxhy.core.job.service.TDxRecordInvoiceStatisticsService;

import lombok.extern.slf4j.Slf4j;

/**
 * @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) {
                set.add(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(mx.getTaxRate()) && !"原价合计".equals(mx.getGoodsName().trim())
                        && !"折扣额合计".equals(mx.getGoodsName().trim()) && !"(详见销货清单)".equals(mx.getGoodsName().trim())&& !"（详见销货清单）".equals(mx.getGoodsName().trim())
                        && (StringUtils.isNotBlank(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(new BigDecimal(rate));
                } catch (Exception e) {
                    ris.setTaxRate(new BigDecimal("0"));
                }
                ris.setDetailAmount(typeAmount);
                ris.setTaxAmount(typeTaxAmount);
                ris.setTotalAmount(typeAmount.add(typeTaxAmount));
                // 存入税率表
                tDxRecordInvoiceStatisticsDao.insert(ris);
            }
        } catch (Exception e) {
            log.error("RecordInvoiceStatisticsService.countTaxRate:err", e);
        }

    }

}
