package com.dxhy.passengerticket.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.util.AmountFormatUtil;
import com.dxhy.passengerticket.dao.PassengerInvoiceStatisticsDao;
import com.dxhy.passengerticket.service.PassengerInvoiceStatisticsService;
import com.dxhy.passengerticket.vo.InvoiceAmountVo;
import com.dxhy.passengerticket.vo.InvoiceTableVo;

import lombok.extern.slf4j.Slf4j;

/**
 * @author jiaohongyang
 */
@SuppressWarnings("AlibabaCollectionInitShouldAssignCapacity")
@Service("passengerInvoiceStatisticsService")
@Slf4j

public class PassengerInvoiceStatisticsServiceImpl implements PassengerInvoiceStatisticsService {
    @Resource
    private PassengerInvoiceStatisticsDao passengerInvoiceStatisticsDao;

    @Override
    public Map<String, Object> selectLegalizedInvoiceCard(Map<String, Object> selectMonthMap,
        Map<String, Object> selectYearMap, String dbName) {
        Map<String, Object> mothodMap = new HashMap<>();
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_READ);
        InvoiceAmountVo invVo = passengerInvoiceStatisticsDao.selectLegalizedInvoiceCard(selectMonthMap);
        if (invVo != null) {
            mothodMap.put("monthTaxAmount", AmountFormatUtil
                .fmtMicrometer(invVo.getMonthTaxAmount() == null ? "0" : invVo.getMonthTaxAmount().toString()));

            mothodMap.put("monthCount",
                AmountFormatUtil.fmtMicrometer(invVo.getMonthCount() == null ? "0" : invVo.getMonthCount().toString()));
        }

        InvoiceAmountVo invVoYe = passengerInvoiceStatisticsDao.selectLegalizedInvoiceCardYear(selectYearMap);
        if (invVoYe != null) {
            mothodMap.put("yearTaxAmount", AmountFormatUtil
                .fmtMicrometer(invVoYe.getYearTaxAmount() == null ? "0" : invVoYe.getYearTaxAmount().toString()));
            mothodMap.put("yearCount", AmountFormatUtil
                .fmtMicrometer(invVoYe.getYearCount() == null ? "0" : invVoYe.getYearCount().toString()));
        }
        return mothodMap;
    }

    @Override
    public Map<String, Object> selectpassengerTicketCar(Map<String, Object> selectMonthMap,
        Map<String, Object> selectYearMap, String dbName) {
        log.info("进入客票发票卡片查询");
        Map<String, Object> mothodMap = new HashMap<>();
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_READ);
        InvoiceAmountVo invVo = passengerInvoiceStatisticsDao.selectpassengerTicketMonthCar(selectMonthMap);
        if (invVo != null) {
            mothodMap.put("monthTaxAmount", AmountFormatUtil
                .fmtMicrometer(invVo.getMonthTaxAmount() == null ? "0" : invVo.getMonthTaxAmount().toString()));

            mothodMap.put("monthCount",
                AmountFormatUtil.fmtMicrometer(invVo.getMonthCount() == null ? "0" : invVo.getMonthCount().toString()));
        }
        InvoiceAmountVo invVoYe = passengerInvoiceStatisticsDao.selectpassengerTicketYearCar(selectYearMap);
        if (invVoYe != null) {
            mothodMap.put("yearTaxAmount", AmountFormatUtil
                .fmtMicrometer(invVoYe.getYearTaxAmount() == null ? "0" : invVoYe.getYearTaxAmount().toString()));
            mothodMap.put("yearCount", AmountFormatUtil
                .fmtMicrometer(invVoYe.getYearCount() == null ? "0" : invVoYe.getYearCount().toString()));
        }
        return mothodMap;
    }

    @Override
    public Map<String, Object> selectOtherInvoiceCar(Map<String, Object> selectMonthMap,
        Map<String, Object> selectYearMap, String dbName) {
        log.info("进入其他发票卡片查询查询");
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_READ);
        // 查询报销数据中的其他发票
        Integer rMonth = passengerInvoiceStatisticsDao.selectReimOtherInvoiceMonthCar(selectMonthMap);
        // 查询底账数据中的其他发票
        Integer pMonth = passengerInvoiceStatisticsDao.selectPassOtherInvoiceMonthCar(selectMonthMap);
        // 查询报销数据中的其他发票
        Integer rYear = passengerInvoiceStatisticsDao.selectReimOtherInvoiceYearCar(selectYearMap);
        // 查询底账数据中的其他发票
        Integer pYear = passengerInvoiceStatisticsDao.selectPassOtherInvoiceYearCar(selectYearMap);
        Map<String, Object> map = new HashMap<>(2);
        map.put("monthCount", rMonth + pMonth);
        map.put("yearCount", rYear + pYear);
        return map;
    }

    @Override
    public List<InvoiceTableVo> selectTable(Map<String, Object> selectMonthMap, String dbName) {
        log.info("进入统计页面表格查询");
        DynamicContextHolder.push(dbName + DbConstant.BUSINESS_READ);
        // 查询实际抵扣税额
        List<InvoiceTableVo> dkseList = passengerInvoiceStatisticsDao.selectSjdkse(selectMonthMap);
        // 查询认证发票数据
        List<InvoiceTableVo> legalizedInvoiceList =
            passengerInvoiceStatisticsDao.selectLegalizedInvoiceTable(selectMonthMap);

        for (InvoiceTableVo tableVo : dkseList) {
            for (InvoiceTableVo invoiceTableVo : legalizedInvoiceList) {
                if (tableVo.getType().equals(invoiceTableVo.getType())) {
                    invoiceTableVo.setDkTaxAmount(tableVo.getDkTaxAmount());
                }
            }
        }

        List<InvoiceTableVo> legalizedInvoiceListSum = returnList(legalizedInvoiceList);
        List<InvoiceTableVo> passengerTicketList = new ArrayList<>();
        // 查询铁路信息
        InvoiceTableVo railwayVo = passengerInvoiceStatisticsDao.slelectPassengerTicketRailwayVo(selectMonthMap);
        passengerTicketList.add(railwayVo);
        // 查询飞机票信息
        InvoiceTableVo aircraftVo = passengerInvoiceStatisticsDao.slelectPassengerTicketaAircraftVo(selectMonthMap);
        passengerTicketList.add(aircraftVo);
        // 查询公路水路信息
        InvoiceTableVo highwayVo = passengerInvoiceStatisticsDao.slelectPassengerTicketaHighwayVo(selectMonthMap);
        passengerTicketList.add(highwayVo);
        // 查询电子普通发票信息
        InvoiceTableVo generalVoteVo =
            passengerInvoiceStatisticsDao.slelectPassengerTicketaGeneralVoteVo(selectMonthMap);
        passengerTicketList.add(generalVoteVo);
        List<InvoiceTableVo> passengerTicketListSum = returnList(passengerTicketList);
        legalizedInvoiceListSum.addAll(passengerTicketListSum);
        // 查询报销数据中的其他发票(电票，普票，通行费)
        List<InvoiceTableVo> otherReimInvoiceList =
            passengerInvoiceStatisticsDao.selectotherReimInvoiceListTable(selectMonthMap);
        // 查询底账数据中的其他发票(电票，普票，通行费)
        List<InvoiceTableVo> otherPassInvoiceList =
            passengerInvoiceStatisticsDao.selectotherPassInvoiceListTable(selectMonthMap);
        InvoiceTableVo otherPassInvoice;
        InvoiceTableVo otherReimInvoice = new InvoiceTableVo();
        List<InvoiceTableVo> otherList = new ArrayList<>();
        // 合并从底账跟报销查询到的电票，普票，通行费的数量
        for (int i = 0; i < otherReimInvoiceList.size(); i++) {
            otherPassInvoice = otherReimInvoiceList.get(i);
            if (otherPassInvoiceList.size() > 0) {
                otherReimInvoice = otherPassInvoiceList.get(i);
            }
            if (otherPassInvoice != null) {
                if (otherPassInvoice.getCount() != null && otherReimInvoice.getCount() != null) {
                    otherReimInvoice.setCount(otherReimInvoice.getCount() + otherPassInvoice.getCount());
                }
            }
            if (otherPassInvoice != null) {
                if (otherPassInvoice.getCount() != null) {
                    otherReimInvoice.setCount(otherPassInvoice.getCount());
                }
            }
            otherList.add(otherReimInvoice);
        }
        // 查询报销数据中的其他发票(其他)
        InvoiceTableVo otherReimInvoiceVo = passengerInvoiceStatisticsDao.selectotherInvoiceReimVo(selectMonthMap);
        // 查询底账数据中的其他发票(其他)
        InvoiceTableVo otherPassInvoiceVo = passengerInvoiceStatisticsDao.selectotherInvoicePassVo(selectMonthMap);
        if (otherPassInvoiceVo != null) {
            otherReimInvoiceVo.setCount(otherReimInvoiceVo.getCount() + otherPassInvoiceVo.getCount());
        }
        otherList.add(otherReimInvoiceVo);
        int count = 0;
        for (InvoiceTableVo vo : otherList) {
            if (vo.getCount() != null) {
                count += vo.getCount();
            }
        }
        InvoiceTableVo legalizedInvoice = new InvoiceTableVo();
        legalizedInvoice.setCount(count);
        otherList.add(legalizedInvoice);
        legalizedInvoiceListSum.addAll(otherList);
        return retrunStringList(legalizedInvoiceListSum);
    }

    /**
     * 合计
     *
     * @param list
     *            入参
     * @return 返回
     */
    private static List<InvoiceTableVo> returnList(List<InvoiceTableVo> list) {
        int count = 0;
        BigDecimal taxAmountsum = new BigDecimal(0);
        BigDecimal authenticationTaxAmountsum = new BigDecimal(0);
        BigDecimal dkTaxAmount = new BigDecimal(0);
        for (InvoiceTableVo vo : list) {
            if (vo.getCount() != null) {
                count += vo.getCount();
            }
            if (vo.getTaxAmountsum() != null) {
                taxAmountsum = taxAmountsum.add(vo.getTaxAmountsum());
            }
            if (vo.getAuthenticationTaxAmountsum() != null) {
                authenticationTaxAmountsum = authenticationTaxAmountsum.add(vo.getAuthenticationTaxAmountsum());
            }
            if (vo.getDkTaxAmount() != null) {
                dkTaxAmount = dkTaxAmount.add(vo.getDkTaxAmount());
            }
        }
        InvoiceTableVo legalizedInvoice = new InvoiceTableVo();
        legalizedInvoice.setCount(count);
        legalizedInvoice.setTaxAmountsum(taxAmountsum);
        legalizedInvoice.setAuthenticationTaxAmountsum(authenticationTaxAmountsum);
        legalizedInvoice.setDkTaxAmount(dkTaxAmount);
        legalizedInvoice.setType("10000");
        list.add(legalizedInvoice);
        return list;
    }

    /**
     * 金额税额价税合计千分位
     *
     * @param list
     *            入参
     * @return 返回
     */
    private List<InvoiceTableVo> retrunStringList(List<InvoiceTableVo> list) {
        for (InvoiceTableVo invoiceTableVo : list) {
            if (invoiceTableVo != null) {
                if (invoiceTableVo.getTaxAmountsum() != null) {
                    invoiceTableVo.setTaxAmountString(
                        AmountFormatUtil.fmtMicrometer(invoiceTableVo.getTaxAmountsum().toString()));
                }
                if (invoiceTableVo.getAuthenticationTaxAmountsum() != null) {
                    invoiceTableVo.setAtTaxAmountString(
                        AmountFormatUtil.fmtMicrometer(invoiceTableVo.getAuthenticationTaxAmountsum().toString()));
                }
                if (invoiceTableVo.getDkTaxAmount() != null) {
                    invoiceTableVo.setDkTaxAmountString(
                        AmountFormatUtil.fmtMicrometer(invoiceTableVo.getDkTaxAmount().toString()));
                }
            }
        }
        return list;
    }

}
