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

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

import com.dxhy.common.constant.DictConstant;
import com.dxhy.common.service.DictdetaServcice;
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.impl.MpBaseServiceImpl;
import com.dxhy.common.util.MathUtil;
import com.dxhy.core.dao.GxfpqrDao;
import com.dxhy.core.model.FpRzRespVO;
import com.dxhy.core.service.export.GxfpqrService;
import com.dxhy.core.util.InvoiceTypeUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;

/**
 * 勾选发票确认查询
 *
 * @author 焦红阳
 */
@SuppressWarnings({"AlibabaMethodTooLong", "DuplicatedCode"})
@Service("gxfpqrService")
@Slf4j

public class GxfpqrServiceImpl extends MpBaseServiceImpl<GxfpqrDao, FpRzRespVO> implements GxfpqrService {

    @Resource
    private DictdetaServcice dictdetaServcice;

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

            if (gfshList.size() == 0) {
                // 未从getUserInfo()获取到购方税号信息
                json.put("hjsl", "0");
                json.put("hjje", "0");
                json.put("hjse", "0");
                json.put("total", "0");
                json.put("datalist", new ArrayList<FpRzRespVO>());
            } else {
                PageHelper.startPage(curr, size);
                List<FpRzRespVO> pageList = super.baseMapper.selectByPramsMap(pramsMap);
                PageInfo<FpRzRespVO> pageInfo = new PageInfo<>(pageList);
                json.put("hasNext", pageInfo.isHasNextPage());
                json.put("nextPage", pageInfo.getNextPage());

                List<FpRzRespVO> resultList = pageInfo.getList();
                if (resultList != null && resultList.size() > 0) {
                    int i = 0;
                    for (FpRzRespVO vo : pageList) {
                        i++;
                        vo.setXh(String.valueOf((curr - 1) * size + i));
                        vo.setInvoiceType(new InvoiceTypeUtils().invoiceTypeName(vo.getInvoiceType()));
                        vo.setJe(MathUtil.round(vo.getJe()));
                        vo.setSe(MathUtil.round(vo.getSe()));
                        vo.setYxdkse(MathUtil.round(vo.getYxdkse()));
                        vo.setQszt(dictdetaServcice.queryDictName(DictConstant.QS_STATUS,vo.getQszt()));
                        vo.setQsfs(dictdetaServcice.queryDictName(DictConstant.QS_TYPE,vo.getQsfs()));
                        vo.setPaymentStatus(dictdetaServcice.queryDictName(DictConstant.FK_STATUS,vo.getPaymentStatus()));
                    }
                }
                json.put("datalist", pageList);
            }
        } catch (Exception e) {
            // 统一设置操作异常标识
            json = super.errorResult(e);
        }

        return json;
    }

}
