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.service.impl.MpBaseServiceImpl;
import com.dxhy.common.util.MathUtil;
import com.dxhy.core.dao.BbyqfpDao;
import com.dxhy.core.model.BbyqfpRespVO;
import com.dxhy.core.service.export.YqfpcxService;
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 user
 */
@SuppressWarnings("DuplicatedCode")
@Service("yqfpcxService")
@Slf4j

public class YqfpcxServiceImpl extends MpBaseServiceImpl<BbyqfpDao, BbyqfpRespVO> implements YqfpcxService {
    @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 {
            PageHelper.startPage(curr, size);

            List<BbyqfpRespVO> pageList = super.baseMapper.selectByPramsMap(pramsMap);
            PageInfo<BbyqfpRespVO> pageInfo = new PageInfo<>(pageList);
            json.put("hasNext", pageInfo.isHasNextPage());
            json.put("nextPage", pageInfo.getNextPage());
            List<String[]> returnList = new ArrayList<>();
            // 用于页面返回list对象
            if (pageList.size() > 0) {
                for (int i = 0; i < pageList.size(); i++) {
                    String[] dataArray = new String[12];
                    dataArray[0] = String.valueOf((curr - 1) * size + i + 1);
                    dataArray[1] = pageList.get(i).getInvoiceCode();
                    dataArray[2] = pageList.get(i).getInvoiceNo();

                    // 发票类型
                    dataArray[3] = new InvoiceTypeUtils().invoiceTypeName(pageList.get(i).getInvoiceType());
                    dataArray[4] = pageList.get(i).getInvoiceDate();
                    dataArray[5] = pageList.get(i).getGfsh();
                    dataArray[6] = pageList.get(i).getGfmc();
                    dataArray[7] = pageList.get(i).getXfsh();
                    dataArray[8] = pageList.get(i).getXfmc();
                    dataArray[9] = MathUtil.round(pageList.get(i).getInvoiceAmount());
                    dataArray[10] = MathUtil.round(pageList.get(i).getTaxAmount());
                    if (pageList.get(i).getDkTaxAmount() != null) {
                        dataArray[11] = MathUtil.round(pageList.get(i).getDkTaxAmount());
                    } else {
                        dataArray[11] = MathUtil.round(pageList.get(i).getTaxAmount());
                    }

                    returnList.add(dataArray);
                }
            }

            json.put("datalist", returnList);

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

        return json;
    }

}
