package com.dxhy.extend.service.tjbb.impl;

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

import org.apache.commons.lang3.StringUtils;
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.common.util.StringHelper;
import com.dxhy.extend.dao.ExtRzInfoByXfDao;
import com.dxhy.extend.model.ExtFprzhrRespVO;
import com.dxhy.extend.service.tjbb.IExtRzInfoByXfService;

/**
 * @author dxhy
 */
@Service("extIRzInfoByXfService")
public class ExtRzInfoByXfServiceImpl extends MpBaseServiceImpl<ExtRzInfoByXfDao, ExtFprzhrRespVO>
    implements IExtRzInfoByXfService {
    @Override
    public String rzInfoByXf(Map<String, Object> pramsMap) {
        String[] titlelist = {"销方名称", "销方税号", "发票数量", "合计金额", "合计税额", "合计抵扣税额"};
        String[] titleprop = {"xfName", "xfTaxNo", "fpsl", "hjje", "hjse", "hjdk"};
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        // 需要设置返回列表标题时调用
        JSONObject json = super.setTitleList(titlelist, titleprop);

        String zjsl = "0";
        String zjje = "0";
        String zjse = "0";
        String zdkse = "0";
        List<ExtFprzhrRespVO> resultList = super.baseMapper.rzInfoByXf(pramsMap);
        String hjje = "0";
        String hjse = "0";
        String dkse;
        if (resultList != null && resultList.size() > 0) {
            for (ExtFprzhrRespVO extFprzhrRespVO : resultList) {
                if (StringUtils.isBlank(extFprzhrRespVO.getHjdk())) {
                    dkse = MathUtil.round(extFprzhrRespVO.getHjse());
                } else {
                    dkse = MathUtil.round(extFprzhrRespVO.getHjdk());
                }
                extFprzhrRespVO.setHjdk(AmountFormatUtil.fmtMicrometer(dkse));
                if (!StringHelper.isBlank(extFprzhrRespVO.getHjje())) {
                    hjje = MathUtil.round(extFprzhrRespVO.getHjje());
                    extFprzhrRespVO.setHjje(AmountFormatUtil.fmtMicrometer(hjje));
                }
                if (!StringHelper.isBlank(extFprzhrRespVO.getHjse())) {
                    hjse = MathUtil.round(extFprzhrRespVO.getHjse());
                    extFprzhrRespVO.setHjse(AmountFormatUtil.fmtMicrometer(hjse));
                }
                zjsl = String.valueOf(Integer.parseInt(extFprzhrRespVO.getFpsl()) + Integer.parseInt(zjsl));
                zjje = MathUtil.add(zjje, hjje);
                zjse = MathUtil.add(zjse, hjse);
                zdkse = MathUtil.add(zdkse, dkse);
            }

        }
        // 设置记录总数
        json.put("zjsl", AmountFormatUtil.fmtMicrometer(zjsl));
        json.put("zjje", AmountFormatUtil.fmtMicrometer(zjje));
        json.put("zjse", AmountFormatUtil.fmtMicrometer(zjse));
        json.put("zdkse", AmountFormatUtil.fmtMicrometer(zdkse));

        json.put("datalist", resultList);
        return super.responseResult(json);

    }
}
