package com.dxhy.core.service.export.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.enums.FplxEnum;
import com.dxhy.common.service.DictdetaServcice;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.AmountFormatUtil;
import com.dxhy.common.util.MathUtil;
import com.dxhy.common.util.StringHelper;
import com.dxhy.core.dao.CoreRzjgqdDao;
import com.dxhy.core.model.RzjgqdRespVO;
import com.dxhy.core.service.export.IRzjgqdService;
import com.dxhy.extend.dao.ExtRzjgqdDao;
import com.dxhy.extend.model.ExtRzjgqdRespVO;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 认证结果
 * 
 * @author aa
 */
@Service("corerzjgqdService")
@Slf4j

public class RzjgqdServiceImpl extends MpBaseServiceImpl<CoreRzjgqdDao, RzjgqdRespVO> implements IRzjgqdService {
    @Resource
    private DictdetaServcice dictdetaServcice;

    @Resource
    private ExtRzjgqdDao extRzjgqdDao;

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

        PageHelper.startPage(curr, size);
        List<ExtRzjgqdRespVO> pageList = extRzjgqdDao.rzjgByGfshAndSkssq(pramsMap);

        PageInfo<ExtRzjgqdRespVO> pageInfo = new PageInfo<>(pageList);
        List<ExtRzjgqdRespVO> resultList = pageInfo.getList();
        json.put("hasNext", pageInfo.isHasNextPage());
        json.put("nextPage", pageInfo.getNextPage());

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

        if (resultList != null && resultList.size() > 0) {
            int start = (int)pageInfo.getStartRow();
            for (int i = 0; i < resultList.size(); i++) {
                if (!StringHelper.isBlank(resultList.get(i).getJe())) {
                    resultList.get(i).setJe(AmountFormatUtil.fmtMicrometer(MathUtil.round(resultList.get(i).getJe())));
                }
                if (!StringHelper.isBlank(resultList.get(i).getSe())) {
                    resultList.get(i).setSe(AmountFormatUtil.fmtMicrometer(MathUtil.round(resultList.get(i).getSe())));
                }
                if (StringUtils.isNotBlank(resultList.get(i).getDkse())) {
                    resultList.get(i)
                            .setDkse(AmountFormatUtil.fmtMicrometer(MathUtil.round(resultList.get(i).getDkse())));
                } else {
                    if(StringUtils.isNotEmpty(resultList.get(i).getSe())){
                        resultList.get(i).setDkse(resultList.get(i).getSe());
                    }

                }
                //字典值转换
                String fplx = resultList.get(i).getFplx();
                resultList.get(i).setInvoiceType(dictdetaServcice.queryDictName(DictConstant.INVOICE_TYPEALL,fplx));

                String dklx = resultList.get(i).getDklx();
                resultList.get(i).setDklx(dictdetaServcice.queryDictName(DictConstant.FPDK_TYPE,dklx));

                String glzt = resultList.get(i).getGlzt();
                resultList.get(i).setGlzt(dictdetaServcice.queryDictName(DictConstant.GLZT, glzt));
                //特定要素
                if(StringUtils.isBlank(resultList.get(i).getLqTdyslxDm())){
                    resultList.get(i)
                            .setLqTdyslxDm("非特定业务");
                }else{
                    resultList.get(i)
                            .setLqTdyslxDm(dictdetaServcice.queryDictName(DictConstant.TDYS_LXDM, resultList.get(i).getLqTdyslxDm()));
                }
                String invoiceStatus = resultList.get(i).getInvoiceStatus();
                resultList.get(i).setInvoiceStatus(dictdetaServcice.queryDictName(DictConstant.INVOICE_STATUS, invoiceStatus));
                String lqHzsdbs = resultList.get(i).getLqHzsdbs();
                resultList.get(i).setLqHzsdbs(dictdetaServcice.queryDictName(DictConstant.HZSDZT, lqHzsdbs));
                String lqCpyycbs = resultList.get(i).getLqCpyycbs();
                resultList.get(i).setLqCpyycbs(dictdetaServcice.queryDictName(DictConstant.CPY_YCBS, lqCpyycbs));
                String lqJdcycbs = resultList.get(i).getLqJdcycbs();
                resultList.get(i).setLqJdcycbs(dictdetaServcice.queryDictName(DictConstant.JDC_YCBS, lqJdcycbs));

                resultList.get(i).setXh(String.valueOf(start + i));
            }
            json.put("datalist", resultList);
        }

        return json;
    }

    /**
     * 发票类型码统一放入map中
     */
    public static final Map<String, String> FPLX_MAP = new HashMap<String, String>(16) {
        {
            for (FplxEnum en : FplxEnum.values()) {
                this.put(en.getFplxDm(), en.getFplxMc(en.getIndex()));
            }
        }
    };

}
