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

import com.alibaba.fastjson.JSONObject;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.service.TabulateRedisService;
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.ExtFprzhrDao;
import com.dxhy.extend.model.ExtFprzhrRespVO;
import com.dxhy.extend.service.tjbb.IExtFprzhrService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author dxhy
 */
@Service("fprzhrService")
@Slf4j
public class ExtFprzhrServiceImpl extends MpBaseServiceImpl<ExtFprzhrDao, ExtFprzhrRespVO>
    implements IExtFprzhrService {
    @Resource
    private TabulateRedisService tabulateRedisService;

    @Override
    public String invoiceStatByRzhr(Map<String, Object> pramsMap) {
        String company = (String)pramsMap.get("company");
        String menuId = (String)pramsMap.get("menuId");
        JSONObject json = tabulateRedisService.getTabulateMenu(company, menuId);
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        // 需要设置返回列表标题时调用
        List<String> gfshList = (List<String>)pramsMap.get("gfsh");
        if (gfshList == null || gfshList.size() == 0) {
            json.put("zjsl", AmountFormatUtil.fmtMicrometer("0"));
            json.put("zjje", AmountFormatUtil.fmtMicrometer("0"));
            json.put("zjse", AmountFormatUtil.fmtMicrometer("0"));
            json.put("zdkse", AmountFormatUtil.fmtMicrometer("0"));

            json.put("datalist", new ArrayList<ExtFprzhrRespVO>());
            return super.responseResult(json);
        }
        String zjsl = "0";
        String zjje = "0";
        String zjse = "0";
        String zdkse = "0";
        List<ExtFprzhrRespVO> resultList = super.baseMapper.invoiceStatByRzhr(pramsMap);
        String hjje = "0.00";
        String hjse = "0.00";
        String dkse;
        if (resultList != null && resultList.size() > 0) {
            for (ExtFprzhrRespVO extFprzhrRespVO : resultList) {
                if (StringUtils.isBlank(extFprzhrRespVO.getHjdk())) {
                    dkse = MathUtil.round(extFprzhrRespVO.getHjse());
                    extFprzhrRespVO.setHjdk(AmountFormatUtil.fmtMicrometer(dkse));
                } 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);

    }
}
