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 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.impl.MpBaseServiceImpl;
import com.dxhy.common.util.MathUtil;
import com.dxhy.core.dao.BbycfpbbcxDao;
import com.dxhy.core.model.BbycfpbbRespVO;
import com.dxhy.core.service.export.YcfpbbcxService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;

/**
 * 报表异常发票查询实现类
 *
 * @author user
 */
@Service
@Slf4j

public class YcfpbbcxServiceImpl extends MpBaseServiceImpl<BbycfpbbcxDao, BbycfpbbRespVO> implements YcfpbbcxService {
    @Resource
    private DictdetaServcice dictdetaServcice;
    @Value("${dxhy.datasource.dxhy_admin:dxhy_admin}")
    private String dxhyAdmin;
    @Value("${dxhy.datasource.dxhy_tertiary:dxhy_tertiary}")
    private String dxhyTertiary;

    @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");
            pramsMap.put("dxhyAdmin", dxhyAdmin);
            pramsMap.put("dxhyTertiary", dxhyTertiary);
            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<BbycfpbbRespVO>());
            } else {
                PageHelper.startPage(curr, size);
                List<BbycfpbbRespVO> pageList = super.baseMapper.selectBbycfpbbHj(pramsMap);
                PageInfo<BbycfpbbRespVO> pageInfo = new PageInfo<>(pageList);
                json.put("hasNext", pageInfo.isHasNextPage());
                json.put("nextPage", pageInfo.getNextPage());

                // 用于页面返回list对象
                if (pageList.size() > 0) {
                    int i = 0;
                    for (BbycfpbbRespVO vo : pageList) {
                        i++;
                        vo.setXh(String.valueOf((curr - 1) * size + i));
                        vo.setYczt(dictdetaServcice.queryDictName(DictConstant.INVOICE_STATUS,vo.getYczt()));
                        vo.setHjsl(vo.getHjsl());
                        vo.setHjje(MathUtil.round(vo.getHjje()));
                        vo.setHjse(MathUtil.round(vo.getHjse()));
                    }
                }

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

        return json;
    }

}
