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.TabulateRedisService;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.AmountFormatUtil;
import com.dxhy.common.util.MathUtil;
import com.dxhy.extend.dao.InvoiceCertificationReportDao;
import com.dxhy.extend.model.ExtReportStatisticsModel;
import com.dxhy.extend.model.ExtTtRzhzRespVo;
import com.dxhy.extend.service.bb.InvoiceCertificationReportService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author ld
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
@Service("extendinvoiceCertificationReportService")
public class InvoiceCertificationReportServiceImpl extends
    MpBaseServiceImpl<InvoiceCertificationReportDao, ExtTtRzhzRespVo> implements InvoiceCertificationReportService {

    @Resource
    private TabulateRedisService tabulateRedisService;

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

        String company = (String)params.get("company");
        String menuId = (String)params.get("menuId");
        json = tabulateRedisService.getTabulateMenu(company, menuId);

        // 合计信息,仅第一页时需要统计，减少数据库压力
        if ((int)params.get("curr") == 1) {
            String hjsl = "0";
            String hjje = "0.00";
            String hjse = "0.00";
            ExtReportStatisticsModel extReportStatisticsModel = this.baseMapper.queryInvoiceListCount(params);
            if (extReportStatisticsModel.getTotalCount() != 0) {
                hjje = AmountFormatUtil
                    .fmtMicrometer(MathUtil.round(extReportStatisticsModel.getTotalAmount().toPlainString()));
                hjse = AmountFormatUtil
                    .fmtMicrometer(MathUtil.round(extReportStatisticsModel.getTotalTax().toPlainString()));
            }
            json.put("hjsl", hjsl);
            json.put("hjje", hjje);
            json.put("hjse", hjse);
        }
        PageHelper.startPage((int)params.get("curr"), (int)params.get("size"));
        List<ExtTtRzhzRespVo> pageList = this.baseMapper.queryInvoiceList(params);
        PageInfo<ExtTtRzhzRespVo> pageInfo = new PageInfo<>(pageList);
        for (int i = 0; i < pageInfo.getList().size(); i++) {
            pageInfo.getList().get(i).setXh(String.valueOf(i + 1));
            String amount = AmountFormatUtil.fmtMicrometer(MathUtil.round(pageInfo.getList().get(i).getAmount()));
            String taxAmount = AmountFormatUtil.fmtMicrometer(MathUtil.round(pageInfo.getList().get(i).getTax()));
            String totalAmount =
                AmountFormatUtil.fmtMicrometer(MathUtil.round(pageInfo.getList().get(i).getTotalAmount()));
            pageInfo.getList().get(i).setAmount(amount);
            pageInfo.getList().get(i).setTax(taxAmount);
            pageInfo.getList().get(i).setTotalAmount(totalAmount);

        }
        json.put("datalist", pageInfo.getList());
        return super.responseResult(json);

    }

}
