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

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

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.constant.DictConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.service.DictdetaServcice;
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.ExtBbfpzhDao;
import com.dxhy.extend.model.ExtBbfpzhcxRespVO;
import com.dxhy.extend.service.bb.ExtFpzhcxService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

/**
 *
 *
 * 发票认证综合查询
 *
 * @author kangzq @date 2019年4月24日
 *
 */
@SuppressWarnings({"AlibabaUndefineMagicConstant", "AlibabaMethodTooLong"})
@Service("fpzhcxService")
@Slf4j
public class ExtFpzhcxServiceImpl extends MpBaseServiceImpl<ExtBbfpzhDao, ExtBbfpzhcxRespVO>
    implements ExtFpzhcxService {
    @Resource
    private DictdetaServcice dictdetaServcice;

    @Resource
    private TabulateRedisService tabulateRedisService;

    @Override
    public String selectByPramsMap(Map<String, Object> pramsMap, int curr, int size) {
        long enter = System.currentTimeMillis();
        JSONObject json;
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        log.info("发票综合查询-传入的参数为:{}",pramsMap);
        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<ExtBbfpzhcxRespVO> hjList = super.baseMapper.selectBbfpzhHj(pramsMap);
                ExtBbfpzhcxRespVO 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);
                log.info("发票综合查询-查询首页合计信息耗时:{}",System.currentTimeMillis()-enter);
            }

            PageHelper.startPage(curr, size);
            List<ExtBbfpzhcxRespVO> pageList = super.baseMapper.selectByPramsMap(pramsMap);
            log.info("返回的发票信息为:{}",pageList);
            log.info("发票综合查询-查询列表信息耗时:{}",System.currentTimeMillis()-enter);
            PageInfo<ExtBbfpzhcxRespVO> pageInfo = new PageInfo<>(pageList);
            List<ExtBbfpzhcxRespVO> resultList = pageInfo.getList();
            // 用于页面返回list对象
            if (resultList.size() > 0) {
                int start = (int)pageInfo.getStartRow();
                for (int i = 0; i < resultList.size(); i++) {
                    resultList.get(i).setXh(String.valueOf(i + start));
                    resultList.get(i).setInvoiceAmount(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(resultList.get(i).getInvoiceAmount())));
                    if (!"01".equals(resultList.get(i).getInvoiceType())
                        && !"03".equals(resultList.get(i).getInvoiceType())
                        && !"08".equals(resultList.get(i).getInvoiceType())
                        && !"14".equals(resultList.get(i).getInvoiceType())
                        && !"31".equals(resultList.get(i).getInvoiceType())) {
                        resultList.get(i).setDkTaxAmount(null);
                    } else {
                        if (StringUtils.isNotBlank(resultList.get(i).getDkTaxAmount())) {
                            resultList.get(i).setDkTaxAmount(
                                AmountFormatUtil.fmtMicrometer(MathUtil.round(resultList.get(i).getDkTaxAmount())));
                        } else {
                            resultList.get(i).setDkTaxAmount(
                                AmountFormatUtil.fmtMicrometer(MathUtil.round(resultList.get(i).getTaxAmount())));
                        }
                    }

                    resultList.get(i)
                        .setTaxAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(resultList.get(i).getTaxAmount())));
                    resultList.get(i).setTotalAmount(
                        AmountFormatUtil.fmtMicrometer(MathUtil.round(resultList.get(i).getTotalAmount())));

                    String glzt = resultList.get(i).getGlzt();
                    resultList.get(i).setGlzt(dictdetaServcice.queryDictName(DictConstant.GLZT, glzt));
                    String settlementStatus = resultList.get(i).getSettlementStatus();
                    resultList.get(i).setSettlementStatus(
                        dictdetaServcice.queryDictName(DictConstant.SETTLEMENT_STATUS, settlementStatus));
                    String invoiceStatus = resultList.get(i).getInvoiceStatus();
                    resultList.get(i)
                        .setInvoiceStatus(dictdetaServcice.queryDictName(DictConstant.INVOICE_STATUS, invoiceStatus));
                    String rzzt = resultList.get(i).getRzzt();
                    resultList.get(i).setRzzt(dictdetaServcice.queryDictName(DictConstant.DEDU_STATUS, rzzt));
                    String rzclzt = resultList.get(i).getRzclzt();
                    resultList.get(i).setRzclzt(dictdetaServcice.queryDictName(DictConstant.AUTH_STATUS_ALL, rzclzt));
                    String qszt = resultList.get(i).getQszt();
                    resultList.get(i).setQszt(dictdetaServcice.queryDictName(DictConstant.QS_STATUS, qszt));
                    String qsfs = resultList.get(i).getQsfs();
                    resultList.get(i).setQsfs(dictdetaServcice.queryDictName(DictConstant.QS_TYPE, qsfs));
                    String bdkStatus = resultList.get(i).getBdkStatus();
                    resultList.get(i).setBdkStatus(dictdetaServcice.queryDictName(DictConstant.BDK_STATUS, bdkStatus));
                    String inAccountStatus = resultList.get(i).getInAccountStatus();
                    if ("1".equals(inAccountStatus)){
                        resultList.get(i).setInAccountStatus("已入账");
                    }else {
                        resultList.get(i).setInAccountStatus("未入账");
                    }

                }
            }

            // 设置记录总数
            json.put("total", pageInfo.getTotal());
            json.put("datalist", resultList);
            log.info("发票综合查询-合计耗时:{}",System.currentTimeMillis()-enter);

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

        return super.responseResult(json);
    }

}
