# HotelKpiExplainConfigServiceImpl.java

    package cloud.wconnect.workperformance.service.hotelkpi.impl.old;

    import cloud.wconnect.commonlib.util.JsonUtils;
    import cloud.wconnect.workperformance.cons.hotelkpi.old.HotelKpiExplainFormulaCons;
    import cloud.wconnect.workperformance.cons.hotelkpi.old.comparison.HotelKpiJudgmentCons;
    import cloud.wconnect.workperformance.entity.hotelkpi.old.HotelKpiExplainConfig;
    import cloud.wconnect.workperformance.mapper.hotelkpi.old.HotelKpiExplainConfigMapper;
    import cloud.wconnect.workperformance.pojo.dto.hotelkpi.old.explain.HotelKpiExplainDTO;
    import cloud.wconnect.workperformance.pojo.dto.hotelkpi.old.explain.HotelKpiExplainDetailDTO;
    import cloud.wconnect.workperformance.pojo.dto.hotelkpi.old.kpi.HotelKpiCalculationFactorDTO;
    import cloud.wconnect.workperformance.pojo.dto.hotelkpi.old.kpi.HotelKpiComparisonRuleDTO;
    import cloud.wconnect.workperformance.pojo.dto.hotelkpi.old.kpi.HotelKpiValueDTO;
    import cloud.wconnect.workperformance.service.hotelkpi.old.IHotelKpiExplainConfigService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.stereotype.Service;

    import jakarta.annotation.Resource;
    import java.math.BigDecimal;
    import java.util.Objects;

    /**
     * <p>
     * Hotel KPI Explain Config 服务实现类
     * </p>
     *
     * @author zc
     * @since 2024-06-26
     */
    @Service("hotelKpiExplainConfigServiceImpl_old")
    @DS("work_performance")
    public class HotelKpiExplainConfigServiceImpl extends ServiceImpl<HotelKpiExplainConfigMapper, HotelKpiExplainConfig> implements IHotelKpiExplainConfigService {

        @Resource
        private ObjectMapper objectMapper;

        /**
         * 根据指标类型获取 Explain 配置
         *
         * @param propertyId  资产 id
         * @param metricsType 指标类型
         * @return HotelKpiExplainConfig
         * @author zc
         * @date 2024/6/28
         */
        @Override
        public HotelKpiExplainConfig getByMetricsType(int propertyId, int metricsType) {
            return getOne(new LambdaQueryWrapper<HotelKpiExplainConfig>()
                    .eq(HotelKpiExplainConfig::getMetricsType, metricsType)
                    .eq(HotelKpiExplainConfig::getPropertyId, propertyId));
        }

        /**
         * 判断 Explain 是否显示
         *
         * @param explainConfig  Explain 配置
         * @param explainMonthly Explain 数据
         * @return Boolean true: 通过, false: 不通过, null: 不可比较
         * @author zc
         * @date 2024/6/28
         */
        @Override
        public Boolean getExplainIsShow(HotelKpiExplainConfig explainConfig, HotelKpiExplainDTO explainMonthly) {
            if (Objects.nonNull(explainConfig) && Objects.nonNull(explainMonthly)) {
                if (HotelKpiExplainFormulaCons.SURVEY_COUNT.equals(explainConfig.getFormula())) {
                    HotelKpiExplainDetailDTO detail = explainMonthly.getDetailDTO();
                    String actual = detail.getActual();
                    String target = detail.getTarget();
                    if (StringUtils.isNotBlank(actual) && StringUtils.isNotBlank(target)) {
                        return new BigDecimal(actual).compareTo(new BigDecimal(target)) < 0;
                    }
                }
            }
            return null;
        }

        /**
         * 获取 KPI 是否达标
         *
         * @param comparisonRule 比较规则
         * @param actual         实际值
         * @param target         目标值
         * @return Boolean true: 达标, false: 未达标, null: 不可比较
         * @author zc
         * @date 2024/6/28
         */
        @Override
        public Boolean getKpiComparison(String comparisonRule, HotelKpiValueDTO actualKpiValueDTO, BigDecimal actual, BigDecimal target) {
            HotelKpiComparisonRuleDTO comparisonRuleDTO = JsonUtils.convertStringToObject(objectMapper,
                    comparisonRule, HotelKpiComparisonRuleDTO.class);
            // 若比较规则关闭，则返回 null
            if (!comparisonRuleDTO.getOn()) {
                return null;
            }
            if (Objects.isNull(target)) {
                return null;
            }
            if (Objects.isNull(actual)) {
                HotelKpiCalculationFactorDTO calculationFactorDTO = actualKpiValueDTO.getCalculationFactor().stream()
                        .filter(item -> item.getIndex() == 2).findFirst().orElse(null);
                if (Objects.nonNull(calculationFactorDTO) && Objects.nonNull(calculationFactorDTO.getCrawl())) {
                    if (calculationFactorDTO.getCrawl().compareTo(BigDecimal.ZERO) == 0) {
                        return true;
                    }
                }
                return null;
            }
            String actualGreaterTarget = comparisonRuleDTO.getActualGreaterTarget();
            if (HotelKpiJudgmentCons.GOOD.equals(actualGreaterTarget)) {
                return actual.compareTo(target) >= 0;
            } else if (HotelKpiJudgmentCons.BAD.equals(actualGreaterTarget)) {
                return actual.compareTo(target) <= 0;
            } else {
                return null;
            }
        }

        /**
         * 获取 Target Explain Value
         *
         * @param explainConfig  Explain 配置
         * @param explainMonthly Explain 数据
         * @return Target Explain Value
         * @author zc
         * @date 2024/7/4
         */
        @Override
        public String getTargetExplainValue(HotelKpiExplainConfig explainConfig, HotelKpiExplainDTO explainMonthly) {
            if (Objects.nonNull(explainConfig) && Objects.nonNull(explainMonthly)) {
                HotelKpiExplainDetailDTO detail = explainMonthly.getDetailDTO();
                String target = detail.getTarget();
                return StringUtils.isNotBlank(target) ? target : explainConfig.getValue();
            }
            return null;
        }
    }