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

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

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
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.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.ExtBbyqfpDao;
import com.dxhy.extend.model.ExtBbyqfpRespVO;
import com.dxhy.extend.service.bb.ExtYqfpcxService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

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

public class ExtYqfpcxServiceImpl extends MpBaseServiceImpl<ExtBbyqfpDao, ExtBbyqfpRespVO> implements ExtYqfpcxService {
    @Resource
    private TabulateRedisService tabulateRedisService;

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

        try {
            String company = (String)pramsMap.get("company");
            String menuId = (String)pramsMap.get("menuId");
            json = tabulateRedisService.getTabulateMenu(company, menuId);

            // 合计信息,仅第一页时需要统计，减少数据库压力
            if (curr == 1) {
                String hjsl = "0";
                String hjje = "0";
                String hjse = "0";
                List<ExtBbyqfpRespVO> hjList = super.baseMapper.selectBbyqfpHj(pramsMap);
                ExtBbyqfpRespVO hjxx = hjList.get(0);
                hjsl = hjxx.getId();
                if (!"0".equals(hjsl)) {
                    hjje = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.getInvoiceAmount()));
                    hjse = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.getTaxAmount()));
                }
                json.put("hjsl", hjsl);
                json.put("hjje", hjje);
                json.put("hjse", hjse);
            }

            PageHelper.startPage(curr, size);
            List<ExtBbyqfpRespVO> pageList = super.baseMapper.selectByPramsMap(pramsMap);
            PageInfo<ExtBbyqfpRespVO> pageInfo = new PageInfo<>(pageList);
            // 用于页面返回list对象
            if (pageList != null && pageList.size() > 0) {
                int start = (int)pageInfo.getStartRow();
                for (int i = 0; i < pageList.size(); i++) {
                    pageList.get(i).setXh(String.valueOf(i + start));
                    pageList.get(i).setInvoiceAmount(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getInvoiceAmount())));
                    pageList.get(i)
                        .setTaxAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getTaxAmount())));
                    if (StringUtils.isBlank(pageList.get(i).getDkAmount())) {
                        pageList.get(i).setDkAmount(pageList.get(i).getTaxAmount());
                    } else {
                        pageList.get(i)
                            .setDkAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getDkAmount())));
                    }
                }
            }

            // 设置记录总数
            json.put("total", pageInfo.getTotal());

            json.put("datalist", pageInfo.getList());

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

        return super.responseResult(json);
    }

}
