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

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.ExtBbycfpcxDao;
import com.dxhy.extend.model.ExtBbycfpcxRespVO;
import com.dxhy.extend.service.bb.ExtYcfpcxService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

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

public class ExtYcfpcxServiceImpl extends MpBaseServiceImpl<ExtBbycfpcxDao, ExtBbycfpcxRespVO>
    implements ExtYcfpcxService {

    @Resource
    private TabulateRedisService tabulateRedisService;
    @Resource
    private DictdetaServcice dictdetaServcice;

    @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.00";
                String hjse = "0.00";
                List<ExtBbycfpcxRespVO> hjList = super.baseMapper.selectBbfpzhHj(pramsMap);
                ExtBbycfpcxRespVO hjxx = hjList.get(0);
                hjsl = hjxx.getId();
                if (!"0".equals(hjsl)) {
                    hjje = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.getJe()));
                    hjse = AmountFormatUtil.fmtMicrometer(MathUtil.round(hjxx.getSe()));
                }
                json.put("hjsl", hjsl);
                json.put("hjje", hjje);
                json.put("hjse", hjse);
            }

            PageHelper.startPage(curr, size);
            List<ExtBbycfpcxRespVO> pageList = super.baseMapper.selectByPramsMap(pramsMap);
            PageInfo<ExtBbycfpcxRespVO> pageInfo = new PageInfo<>(pageList);
            // 用于页面返回list对象
            if (pageList.size() > 0) {
                // List<DictdetaModel> rzlxModels = dictdetaServcice.selectDictdeta("BDK_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));
                    if(StringUtils.isNotEmpty(pageList.get(i).getJe())){
                        pageList.get(i).setJe(AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getJe())));
                    }
                    if(StringUtils.isNotEmpty(pageList.get(i).getSe())){
                        pageList.get(i).setSe(AmountFormatUtil.fmtMicrometer(MathUtil.round(pageList.get(i).getSe())));
                    }
                    if (pageList.get(i).getRzsj() != null && !"".equals(pageList.get(i).getRzsj())) {

                        pageList.get(i).setRzsj(pageList.get(i).getRzsj().substring(0, 10));
                    }

                    // 字典值转换
                    String yczt = pageList.get(i).getYczt();
                    pageList.get(i).setYczt(dictdetaServcice.queryDictName(DictConstant.INVOICE_STATUS, yczt));
                    String qszt = pageList.get(i).getQszt();
                    pageList.get(i).setQszt(dictdetaServcice.queryDictName(DictConstant.QS_STATUS, qszt));
                    String rzclzt = pageList.get(i).getRzclzt();
                    pageList.get(i).setRzclzt(dictdetaServcice.queryDictName(DictConstant.AUTH_STATUS_ALL, rzclzt));
                    String rzzt = pageList.get(i).getRzzt();
                    pageList.get(i).setRzzt(dictdetaServcice.queryDictName(DictConstant.DEDU_STATUS, rzzt));
                    String rzlx = pageList.get(i).getRzlx();
                    pageList.get(i).setRzlx(dictdetaServcice.queryDictName(DictConstant.INVOICE_RZLX, rzlx));

                    // if (rzlxModels!=null){
                    // for (DictdetaModel model : rzlxModels){
                    // if (model.getDictcode().equals(pageList.get(i).getRzlx())){
                    // pageList.get(i).setRzlx(model.getDictname());
                    // }
                    // }
                    // }
                }
            }

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

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

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

        return super.responseResult(json);
    }

}
