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

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

import javax.annotation.Resource;

import com.dxhy.common.constant.DictConstant;
import com.dxhy.common.service.DictdetaServcice;
import com.dxhy.common.vo.DictdetaModel;
import org.springframework.beans.factory.annotation.Value;
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.enums.FpztEnum;
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.ExtBbycfpbbcxDao;
import com.dxhy.extend.model.ExtBbycfpbbRespVO;
import com.dxhy.extend.service.bb.ExtYcfpbbcxService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

/**
 *
 *
 * 报表异常发票查询实现类
 *
 * @author kangzq @date 2019年4月24日
 *
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
@Service("ycfpbbcxService")
@Slf4j

public class ExtYcfpbbcxServiceImpl extends MpBaseServiceImpl<ExtBbycfpbbcxDao, ExtBbycfpbbRespVO>
    implements ExtYcfpbbcxService {
    @Resource
    private TabulateRedisService tabulateRedisService;
    @Resource
    private DictdetaServcice dictdetaServcice;
    @Value("${dxhy.datasource.dxhy_admin:dxhy_admin}")
    private String dxhyAdmin;
    @Value("${dxhy.datasource.dxhy_admin:dxhy_tertiary}")
    private String dxhyTertiary;

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

        try {
            pramsMap.put("dxhyAdmin", dxhyAdmin);
            pramsMap.put("dxhyTertiary", dxhyTertiary);
            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<ExtBbycfpbbRespVO> hjList = super.baseMapper.selectHjxx(pramsMap);
                ExtBbycfpbbRespVO hjxx = hjList.get(0);
                hjsl = hjxx.getId();
                if (!"0".equals(hjsl)) {
                    hjje = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.getHjje()));
                    hjse = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.getHjse()));
                }
                json.put("hjsl", hjsl);
                json.put("hjje", hjje);
                json.put("hjse", hjse);
            }

            PageHelper.startPage(curr, size);
            List<ExtBbycfpbbRespVO> pageList = super.baseMapper.selectBbycfpbbHj(pramsMap);
            PageInfo<ExtBbycfpbbRespVO> pageInfo = new PageInfo<>(pageList);
            // 用于页面返回list对象
            if (pageList.size() > 0) {
//                List<DictdetaModel> fpztModels = dictdetaServcice.selectDictdeta("INVOICE_STATUS");
                int start = (int)pageInfo.getStartRow();
                for (int i = 0; i < pageList.size(); i++) {
                    pageList.get(i).setId(String.valueOf(i + start));
                    pageList.get(i).setXh(String.valueOf(i + start));
                    pageList.get(i).setHjje(AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getHjje())));
                    pageList.get(i).setHjse(AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getHjse())));
                    String yczt = pageList.get(i).getYczt();
                    pageList.get(i).setYczt(dictdetaServcice.queryDictName(DictConstant.INVOICE_STATUS,yczt));

                }
            }

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

            List<ExtBbycfpbbRespVO> resultList = pageInfo.getList();
            json.put("datalist", resultList);

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

        return super.responseResult(json);
    }

}
