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

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

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.AmountFormatUtil;
import com.dxhy.common.util.MathUtil;
import com.dxhy.core.dao.BbrzrbcxDao;
import com.dxhy.core.model.BbrzrbcxRespVO;
import com.dxhy.core.service.export.RzrbcxService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

/**
 * 认证日报实现类
 *
 * @author kangzq
 * @version 1.0
 * @date 2019年5月16日
 */
@Service("rzrbcxService")
@Slf4j

public class RzrbcxServiceImpl extends MpBaseServiceImpl<BbrzrbcxDao, BbrzrbcxRespVO> implements RzrbcxService {

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

                String zjje = "0";
                String zjse = "0";
                String dkse = "0";
                String zjsl = "0";
                // 用于页面返回list对象
                if (pageList.size() > 0) {

                    for (int i = 0; i < pageList.size(); i++) {
                        String[] dataArray = new String[19];
                        dataArray[0] = String.valueOf((curr - 1) * size + i + 1);
                        dataArray[1] = pageList.get(i).getRzrq();
                        dataArray[2] = pageList.get(i).getHjsl();
                        dataArray[3] = MathUtil.round(pageList.get(i).getHjje());
                        dataArray[4] = MathUtil.round(pageList.get(i).getHjse());
                        dataArray[5] = MathUtil.round(pageList.get(i).getDkse());
                        zjsl = String.valueOf(Integer.parseInt(zjsl) + Integer.parseInt(pageList.get(i).getHjsl()));
                        zjje = MathUtil.add(zjje, pageList.get(i).getHjje());
                        zjse = MathUtil.add(zjse, pageList.get(i).getHjse());
                        dkse = MathUtil.add(dkse, pageList.get(i).getDkse());
                        returnList.add(dataArray);
                    }
                }
                String[] hjxx = new String[13];
                hjxx[1] = "合计";
                hjxx[2] = zjsl;
                hjxx[3] = AmountFormatUtil.fmtMicrometer(MathUtil.round(zjje));
                hjxx[4] = AmountFormatUtil.fmtMicrometer(MathUtil.round(zjse));
                hjxx[5] = AmountFormatUtil.fmtMicrometer(MathUtil.round(dkse));
                returnList.add(hjxx);
                json.put("datalist", returnList);
            }
        } catch (Exception e) {
            // 统一设置操作异常标识
            json = super.errorResult(e);
        }

        return json;
    }

}
