package com.dxhy.extend.service.bb.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.extend.dao.ExtBbfpmxDao;
import com.dxhy.extend.model.ExtBbfpmxcxRespVO;
import com.dxhy.extend.service.bb.ExtFpmxcxService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service("extendfpmxcxService")
@Slf4j
public class ExtFpmxcxServiceImpl extends MpBaseServiceImpl<ExtBbfpmxDao, ExtBbfpmxcxRespVO> implements ExtFpmxcxService {


    @Resource
    private TabulateRedisService tabulateRedisService;

    @Override
    public String selectByPramsMap(Map<String, Object> pramsMap, int curr, int size) {
        JSONObject json;
        DynamicContextHolder.push(pramsMap.get("dbName") + DbConstant.BUSINESS_READ);
        try {
            String company = (String) pramsMap.get("company");
            String menuId = (String) pramsMap.get("menuId");
            json = tabulateRedisService.getTabulateMenu(company, menuId);

            PageHelper.startPage(curr, size);
            //添加数据库信息
            List<ExtBbfpmxcxRespVO> pageList = super.baseMapper.selectByPramsMap(pramsMap);
            PageInfo<ExtBbfpmxcxRespVO> pageInfo = new PageInfo<>(pageList);
            List<ExtBbfpmxcxRespVO> resultList = pageInfo.getList();
            if (resultList.size() > 0) {
                int start = (int)pageInfo.getStartRow();
                for (int i = 0; i < resultList.size(); i++) {
                    resultList.get(i).setXh(String.valueOf(i + start));
                    BigDecimal detailAmount= null;
                    try {
                        detailAmount = new BigDecimal(resultList.get(i).getDetailAmount());
                    } catch (Exception e) {
                        detailAmount = new BigDecimal("0.00");
                    }
                    try {
                        //若税额不为特殊字符，则含税金额=金额+税额
                        BigDecimal taxAmount = new BigDecimal(resultList.get(i).getTaxAmount());
                        BigDecimal totalAmount=detailAmount.add(taxAmount);
                        resultList.get(i).setTaxAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(taxAmount.toPlainString())));
                        resultList.get(i).setTotalAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(totalAmount.toPlainString())));
                    } catch (Exception e) {
                        //若税额为特殊字符，则含税金额=金额
                        resultList.get(i).setTaxAmount(resultList.get(i).getTaxAmount());
                        resultList.get(i).setTotalAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(detailAmount.toPlainString())));
                    }
                    resultList.get(i).setDetailAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(detailAmount.toPlainString())));
                    if(StringUtils.isNotBlank(resultList.get(i).getNum())){
                        resultList.get(i).setNum((MathUtil.round(resultList.get(i).getNum())));
                    }
                }

//                resultList.stream().forEach(extBbfpmxcxRespVO -> {
//                    BigDecimal detailAmount = null;
//                    try {
//                        detailAmount = new BigDecimal(extBbfpmxcxRespVO.getDetailAmount());
//                    } catch (Exception e) {
//                        detailAmount = new BigDecimal("0.00");
//                    }
//                    try {
//                        //若税额不为特殊字符，则含税金额=金额+税额
//                        BigDecimal taxAmount = new BigDecimal(extBbfpmxcxRespVO.getTaxAmount());
//                        BigDecimal totalAmount = detailAmount.add(taxAmount);
//                        extBbfpmxcxRespVO.setTaxAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(taxAmount.toPlainString())));
//                        extBbfpmxcxRespVO.setTotalAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(totalAmount.toPlainString())));
//                    } catch (Exception e) {
//                        //若税额为特殊字符，则含税金额=金额
//                        extBbfpmxcxRespVO.setTaxAmount(extBbfpmxcxRespVO.getTaxAmount());
//                        extBbfpmxcxRespVO.setTotalAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(detailAmount.toPlainString())));
//                    }
//                    extBbfpmxcxRespVO.setDetailAmount(AmountFormatUtil.fmtMicrometer(MathUtil.round(detailAmount.toPlainString())));
//                });
            }

            // 设置记录总数
            json.put("total", pageInfo.getTotal());
            json.put("datalist", resultList);
        } catch (Exception e) {
            // 统一设置操作异常标识
            json = super.errorResult(e);
        }

        return super.responseResult(json);

    }
}
