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

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.MathUtil;
import com.dxhy.core.dao.JxsebbDao;
import com.dxhy.core.model.CustomsStatisticsVo;
import com.dxhy.core.model.DailyReportVo;
import com.dxhy.core.model.JxsebbRespVO;
import com.dxhy.core.model.ReportStatisticsVo;
import com.dxhy.core.service.export.JxsebbcxService;

import lombok.extern.slf4j.Slf4j;

/**
 * 发票认证综合查询
 *
 * @author user
 */
@Service("jxsebbcxService")
@Slf4j

public class JxsebbcxServiceImpl extends MpBaseServiceImpl<JxsebbDao, JxsebbRespVO> implements JxsebbcxService {

    @Override
    public JSONObject selectByPramsMap(Map<String, Object> pramsMap) {
        JSONObject json = new JSONObject();
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);

        try {

            String skssq = pramsMap.get("skssq").toString().substring(0, 4) + "年"
                + pramsMap.get("skssq").toString().substring(4, 6) + "月";
            json.put("skssq", skssq);

            ReportStatisticsVo statisticsVo = super.baseMapper.getResult(pramsMap);
            CustomsStatisticsVo customsStatisticsVo = super.baseMapper.getTotal(pramsMap);
            List<DailyReportVo> dailyVoList = super.baseMapper.getOutTaxDetail(pramsMap);
            Double totalOutTax = baseMapper.getTotalOutTax(pramsMap);
            JxsebbRespVO out = new JxsebbRespVO();
            // 转化为展示对象a
            out.setTotalOutTax(MathUtil.round(String.valueOf(totalOutTax)));
            out.setTotalAmount(MathUtil.round(String.valueOf(statisticsVo.getTotalAmount())));
            out.setTotalCount(String.valueOf(statisticsVo.getTotalCount()));
            out.setTotalTax(MathUtil.round(String.valueOf(statisticsVo.getTotalTax())));
            if (customsStatisticsVo != null) {

                out.setCustomsCount(String.valueOf(customsStatisticsVo.getCustomsCount()));
                out.setCustomsTax(MathUtil.round(String.valueOf(customsStatisticsVo.getCustomsTax())));
            }
            Double tax = 0.0;
            if (dailyVoList != null && dailyVoList.size() > 0) {
                for (DailyReportVo dailyReportVo : dailyVoList) {
                    String rzhDate = dailyReportVo.getRzhDate();
                    switch (rzhDate) {
                        case "1":
                            out.setOutTax1(MathUtil.round(String.valueOf(dailyReportVo.getTax())));
                            break;
                        case "2":
                            out.setOutTax2(MathUtil.round(String.valueOf(dailyReportVo.getTax())));
                            break;
                        case "3":
                            out.setOutTax3(MathUtil.round(String.valueOf(dailyReportVo.getTax())));
                            break;
                        case "4":
                            out.setOutTax4(MathUtil.round(String.valueOf(dailyReportVo.getTax())));
                            break;
                        case "5":
                            out.setOutTax5(MathUtil.round(String.valueOf(dailyReportVo.getTax())));
                            break;
                        case "6":
                            out.setOutTax6(MathUtil.round(String.valueOf(dailyReportVo.getTax())));
                            break;
                        case "7":
                            out.setOutTax7(MathUtil.round(String.valueOf(dailyReportVo.getTax())));
                            break;
                        case "8":
                            out.setOutTax8(MathUtil.round(String.valueOf(dailyReportVo.getTax())));
                            break;
                        case "9":
                            out.setOutTax9(MathUtil.round(String.valueOf(dailyReportVo.getTax())));
                            break;
                        default:
                            tax += dailyReportVo.getTax();
                            break;
                    }
                }
            }
            out.setOutTax0(MathUtil.round(String.valueOf(tax)));
            String gfmc = (String)pramsMap.get("gfmc");
            out.setGfmc(gfmc);

            json.put("datalist", out);

        } catch (Exception e) {
            // 统一设置操作异常标识
            json = super.errorResult(e);
        }

        return json;
    }

}
