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.MathUtil;
import com.dxhy.common.util.StringHelper;
import com.dxhy.core.dao.FpcjhzLbDao;
import com.dxhy.core.model.FpcjCjhzRespVO;
import com.dxhy.core.service.export.IFpcjService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

/**
 * 发票采集业务实现类
 *
 * @author user
 */

@Service("fpcjService")
@Slf4j
@SuppressWarnings("AlibabaUndefineMagicConstant")
public class FpcjServiceImpl extends MpBaseServiceImpl<FpcjhzLbDao, FpcjCjhzRespVO> implements IFpcjService {

    @Override
    public JSONObject selectByGfshAndCjrq(Map<String, Object> pramsMap, int curr, int size) {

        JSONObject json = new JSONObject();

        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<FpcjCjhzRespVO>());
        } else {
            DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
            // 合计信息,仅第一页时需要统计，减少数据库压力
            if (curr == 1) {
                String hjsl = "0";
                String hjje = "0";
                String hjse = "0";
                List<FpcjCjhzRespVO> hjList = super.baseMapper.selectHjxxByGfshAndCjrq(pramsMap);
                FpcjCjhzRespVO hjxx = hjList.get(0);
                hjsl = hjxx.getSl();
                if (!"0".equals(hjsl)) {
                    hjje = MathUtil.round(hjxx.getJe());
                    hjse = MathUtil.round(hjxx.getSe());
                }
                json.put("hjsl", hjsl);
                json.put("hjje", hjje);
                json.put("hjse", hjse);
            }

            // 查询发票上的购方信息
            List<FpcjCjhzRespVO> gfxxList = super.baseMapper.selectGfxxList(pramsMap);
            log.info("gfxxList====>{}",gfxxList);

            PageHelper.startPage(curr, size);
            List<FpcjCjhzRespVO> pageList = super.baseMapper.selectByGfshAndCjrq(pramsMap);
            PageInfo<FpcjCjhzRespVO> pageInfo = new PageInfo<>(pageList);
            json.put("hasNext", pageInfo.isHasNextPage());
            json.put("nextPage", pageInfo.getNextPage());
            // 用于页面展示
            List<FpcjCjhzRespVO> resultList = pageInfo.getList();
            if (resultList != null && resultList.size() > 0) {
                int i = 1;
                for (FpcjCjhzRespVO hzxx : resultList) {
                    hzxx.setXh(String.valueOf((curr - 1) * size + i));
                    hzxx.setGfmc(this.getGfmcByGfsh(hzxx.getGfsh(), gfxxList));
                    hzxx.setJe(MathUtil.round(hzxx.getJe()));
                    hzxx.setSe(MathUtil.round(hzxx.getSe()));
                    i++;
                }
            }
            json.put("datalist", resultList);
        }

        return json;
    }

    /**
     * 根据购方税号获取购方名称
     *
     * @param gfsh
     * @param gfxxList
     * @return
     *
     */
    private String getGfmcByGfsh(String gfsh, List<FpcjCjhzRespVO> gfxxList) {
        String gfmc = "";
        for (FpcjCjhzRespVO gfxx : gfxxList) {
            if (gfsh.equals(gfxx.getGfsh())) {
                gfmc = gfxx.getGfmc();
                if (StringHelper.isBlank(gfmc)) {
                    gfmc = "";
                }
                break;
            }
        }
        return gfmc;
    }
}
