package cn.lihongjie.coal.coalWashingDailyAnalysis.mapper;

import cn.lihongjie.coal.coalBlend.dto.CoalBlendDto;
import cn.lihongjie.coal.coalWashingDailyAnalysis.entity.CoalWashingDailyAnalysisEntity;
import cn.lihongjie.coal.coalWashingDailyAnalysis.entity.CoalWashingDailyAnalysisItemVo;

import org.mapstruct.Mapper;
import org.mapstruct.MappingConstants;
import org.mapstruct.control.DeepClone;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.math.RoundingMode;

@Mapper(
        componentModel = MappingConstants.ComponentModel.SPRING,
        uses = {cn.lihongjie.coal.base.mapper.CommonMapper.class, cn.lihongjie.coal.base.mapper.CommonEntityMapper.class},
        mappingControl = DeepClone.class)
public interface RoundMapper {

    CoalWashingDailyAnalysisEntity round(CoalWashingDailyAnalysisEntity entity);

    CoalWashingDailyAnalysisItemVo round(CoalWashingDailyAnalysisItemVo entity);

    CoalBlendDto round(CoalBlendDto entity);

    default Double formatDouble(Double d) {

        if (d == null) {
            return d;
        }

        try {

        return new BigDecimal(d).setScale(2, RoundingMode.HALF_UP).doubleValue();
        } catch (Exception e) {
            LoggerFactory.getLogger(RoundMapper.class).info("error when round double: {}", d, e);
            return null;
        }
    }
}
