package cn.ibizlab.core.extensions.service;

import cn.ibizlab.core.analysis.service.impl.DAMetricServiceImpl;
import cn.ibizlab.core.rule.domain.RuleItem;
import cn.ibizlab.core.rule.service.IRuleItemService;
import lombok.extern.slf4j.Slf4j;
import cn.ibizlab.core.analysis.domain.DAMetric;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.context.annotation.Primary;
import java.util.*;

/**
 * 实体[指标] 自定义服务对象
 */
@Slf4j
@Primary
@Service("DAMetricExService")
public class DAMetricExService extends DAMetricServiceImpl {

    @Autowired
    @Lazy
    IRuleItemService ruleService;

    @Override
    public boolean create(DAMetric et) {
        super.create(et);
        syncRule(et);
        return true;
    }

    @Override
    public boolean update(DAMetric et) {
         super.update(et);
         syncRule(et);
         return true;

    }

    @Override
    protected Class currentModelClass() {
        return com.baomidou.mybatisplus.core.toolkit.ReflectionKit.getSuperClassGenericType(this.getClass().getSuperclass(), 1);
    }

    /**
     * [SyncRule:同步指标到规则] 行为扩展
     * @param et
     * @return
     */
    @Override
    @Transactional
    public DAMetric syncRule(DAMetric et) {
        RuleItem rule =new RuleItem();
        rule.setRuleCode(et.getMetricId());
        rule.setRuleName(et.getMetricName());
        rule.setModelId(et.getModelId());
        rule.setModelName(et.getModelName());
        rule.setCfg(et.getCfg());
        rule.setFieldSet(et.getFieldSet());
        rule.setGroup("REP");
        rule.setBusinessCat(et.getMetricType());
        rule.setMemo(et.getModelName()+"."+et.getMetricName());
        ruleService.save(rule);
        return super.syncRule(et);
    }
}

