package com.dxhy.core.service.export.impl;

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

import javax.annotation.Resource;

import com.dxhy.common.constant.DictConstant;
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.FpqsjgEnum;
import com.dxhy.common.enums.QsfsEnum;
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.vo.DictdetaModel;
import com.dxhy.core.dao.QstjbDao;
import com.dxhy.core.model.QstjbRespVO;
import com.dxhy.core.service.export.IQstjbService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

/**
 * 签收统计业务实现类
 *
 * @author user
 */
@SuppressWarnings("DuplicatedCode")
@Service("qstjbService")
@Slf4j

public class QstjbServiceImpl extends MpBaseServiceImpl<QstjbDao, QstjbRespVO> implements IQstjbService {
    @Resource
    private DictdetaServcice dictdetaServcice;

    @Override
    public JSONObject selectQstjbByCxtj(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<QstjbRespVO> pageList = super.baseMapper.selectQstjbByCxtj(pramsMap);
        PageInfo<QstjbRespVO> pageInfo = new PageInfo<>(pageList);
        json.put("hasNext", pageInfo.isHasNextPage());
        json.put("nextPage", pageInfo.getNextPage());

        // 用于页面展示
        List<QstjbRespVO> resultList = pageInfo.getList();
        if (resultList != null && resultList.size() > 0) {
            for (int i = 0; i < resultList.size(); i++) {
                resultList.get(i).setXh(String.valueOf((curr - 1) * size + i + 1));
                //字典值转换
                resultList.get(i).setQszt(dictdetaServcice.queryDictName(DictConstant.QS_RESULTS,resultList.get(i).getQszt()));
                resultList.get(i).setQsfs(dictdetaServcice.queryDictName(DictConstant.QS_TYPE,resultList.get(i).getQsfs()));
                resultList.get(i).setInvoiceType(dictdetaServcice.queryDictName(DictConstant.INVOICE_TYPE_VAT,resultList.get(i).getInvoiceType()));

                if (resultList.get(i).getJe() != null && !"".equals(resultList.get(i).getJe())) {
                    resultList.get(i).setJe(MathUtil.round(resultList.get(i).getJe()));
                }
                if (resultList.get(i).getSe() != null && !"".equals(resultList.get(i).getSe())) {
                    resultList.get(i).setSe(MathUtil.round(resultList.get(i).getSe()));
                }
            }
        }

        json.put("datalist", resultList);

        return json;
    }

    @Override
    public JSONObject selectYqswrzByCxtj(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<QstjbRespVO> pageList = super.baseMapper.selectYqswrzByCxtj(pramsMap);
        PageInfo<QstjbRespVO> pageInfo = new PageInfo<>(pageList);
        json.put("hasNext", pageInfo.isHasNextPage());
        json.put("nextPage", pageInfo.getNextPage());

        // 用于页面展示
        List<QstjbRespVO> resultList = pageInfo.getList();
        if (resultList != null && resultList.size() > 0) {

            for (int i = 0; i < resultList.size(); i++) {
                resultList.get(i).setXh(String.valueOf((curr - 1) * size + i + 1));

                //字典转换
                resultList.get(i).setQsfs(dictdetaServcice.queryDictName(DictConstant.QS_TYPE,resultList.get(i).getQsfs()));
                resultList.get(i).setInvoiceType(dictdetaServcice.queryDictName(DictConstant.INVOICE_TYPE_VAT,resultList.get(i).getInvoiceType()));

                resultList.get(i).setJe(AmountFormatUtil.fmtMicrometer(resultList.get(i).getJe()));
                resultList.get(i).setSe(AmountFormatUtil.fmtMicrometer(resultList.get(i).getSe()));
            }
        }

        json.put("datalist", resultList);

        return json;
    }

    /**
     * 签收状态码统一放入map中
     */
    public static final Map<String, String> QSZT_MAP = new HashMap<String, String>(16) {

        {
            for (FpqsjgEnum en : FpqsjgEnum.values()) {
                this.put(en.getQsjgDm(), en.getQsjgMcSmb(en.getIndex()));
            }
        }
    };

    /**
     * 签收方式统一放入map中
     */
    public static final Map<String, String> QSFS_MAP = new HashMap<String, String>(16) {
        {
            for (QsfsEnum en : QsfsEnum.values()) {
                this.put(en.getQsfsDm(), en.getQsfsMc(en.getIndex()));
            }
        }
    };

}
