package com.dxhy.extend.service.bb.impl;

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.extend.dao.ExtJxsebbDao;
import com.dxhy.extend.model.ExtCustomsStatisticsVo;
import com.dxhy.extend.model.ExtDailyReportVo;
import com.dxhy.extend.model.ExtJxsebbRespVO;
import com.dxhy.extend.model.ExtReportStatisticsVo;
import com.dxhy.extend.service.bb.ExtJxsebbcxService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/**
 *
 *
 * 发票认证综合查询
 *
 * @author kangzq @date 2019年4月24日
 *
 */
@SuppressWarnings("AlibabaMethodTooLong")
@Service("extendjxsebbcxService")
@Slf4j

public class ExtJxsebbcxServiceImpl extends MpBaseServiceImpl<ExtJxsebbDao, ExtJxsebbRespVO>
    implements ExtJxsebbcxService {

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

        try {

            String[] titlelist = {"购方名称", "税款所属期", "份数", "金额", "税额", "本期进项税额转出额", "14", "15", "16", "17", "18", "19",
                "20", "21", "22", "23"};
            String[] titleprop = {"gfmc", "skssq", "TotalCount", "totalAmount", "totalTax", "totalOutTax", "outTax1",
                "outTax2", "outTax3", "outTax4", "outTax5", "outTax6", "outTax7", "outTax8", "outTax9", "outTax0"};

            // 需要设置返回列表标题时调用
            json = super.setTitleList(titlelist, titleprop);

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

            ExtReportStatisticsVo statisticsVo = super.baseMapper.getResult(pramsMap);
            ExtCustomsStatisticsVo customsStatisticsVo = super.baseMapper.getTotal(pramsMap);
            List<ExtDailyReportVo> dailyVoList = super.baseMapper.getOutTaxDetail(pramsMap);
            Double totalOutTax = baseMapper.getTotalOutTax(pramsMap);
            ExtJxsebbRespVO out = new ExtJxsebbRespVO();
            // 转化为展示对象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 (ExtDailyReportVo 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)));
            out.setGfmc((String)pramsMap.get("gfmc"));

            json.put("datalist", out);

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

        return super.responseResult(json);
    }

}
