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

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

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

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

import lombok.extern.slf4j.Slf4j;

/**
 * 报表异常发票查询实现类
 *
 * @author user
 */
@SuppressWarnings("AlibabaMethodTooLong")
@Service("ycfpcxService")
@Slf4j

public class YcfpcxServiceImpl extends MpBaseServiceImpl<BbycfpcxDao, BbycfpcxRespVO> implements YcfpcxService {

    @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<BbycfpcxRespVO>());
            } else {
                PageHelper.startPage(curr, size);
                List<BbycfpcxRespVO> pageList = super.baseMapper.selectByPramsMap(pramsMap);
                PageInfo<BbycfpcxRespVO> pageInfo = new PageInfo<>(pageList);
                json.put("hasNext", pageInfo.isHasNextPage());
                json.put("nextPage", pageInfo.getNextPage());
                List<String[]> returnList = new ArrayList<>();
                // 用于页面返回list对象
                if (pageList.size() > 0) {
                    int i = 0;
                    for (BbycfpcxRespVO vo : pageList) {
                        i++;
                        vo.setXh(String.valueOf((curr - 1) * size + i));

                        vo.setJe(MathUtil.round(vo.getJe()));
                        vo.setSe(MathUtil.round(vo.getSe()));
                        // 字典转换
                        String inAccountStatus = vo.getInAccountStatus();
                        if ("1".equals(inAccountStatus)){
                            vo.setInAccountStatus("已入账");
                        }else {
                            vo.setInAccountStatus("未入账");
                        }
                        vo.setYczt(dictdetaServcice.queryDictName(DictConstant.INVOICE_STATUS, vo.getYczt()));
                        vo.setInvoiceType(
                            dictdetaServcice.queryDictName(DictConstant.INVOICE_TYPE_VAT, vo.getInvoiceType()));
                        vo.setQszt(dictdetaServcice.queryDictName(DictConstant.QS_STATUS, vo.getQszt()));
                        vo.setRzzt(dictdetaServcice.queryDictName(DictConstant.DEDU_STATUS, vo.getRzzt()));
                        vo.setRzclzt(dictdetaServcice.queryDictName(DictConstant.AUTH_STATUS_ALL, vo.getRzclzt()));
                        vo.setRzlx(dictdetaServcice.queryDictName(DictConstant.INVOICE_RZLX, vo.getRzlx()));
                    }
                }

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

        return json;
    }

}
