package com.mortals.xhx.module.alarm.service.impl;

import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.alarm.model.AlarmConfigQuery;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.module.alarm.dao.AlarmConfigDao;
import com.mortals.xhx.module.alarm.model.AlarmConfigEntity;
import com.mortals.xhx.module.alarm.service.AlarmConfigService;
import org.springframework.util.ObjectUtils;

/**
 * AlarmConfigService
 * 设备告警配置 service实现
 *
 * @author zxfei
 * @date 2022-06-27
 */
@Service("alarmConfigService")
public class AlarmConfigServiceImpl extends AbstractCRUDServiceImpl<AlarmConfigDao, AlarmConfigEntity, Long> implements AlarmConfigService {


    @Override
    protected void saveBefore(AlarmConfigEntity entity, Context context) throws AppException {
        //校验产品是否已经存在配置
        AlarmConfigEntity alarmConfigEntity = this.selectOne(new AlarmConfigQuery().productId(entity.getProductId()));
        if (!ObjectUtils.isEmpty(alarmConfigEntity)) {
            throw new AppException("当前产品已存在告警配置！");
        }
        super.saveBefore(entity, context);
    }

    @Override
    protected void updateBefore(AlarmConfigEntity entity, Context context) throws AppException {
        //校验
        if (!ObjectUtils.isEmpty(entity.getProductId())) {
            AlarmConfigEntity alarmConfigEntity = this.selectOne(new AlarmConfigQuery().productId(entity.getProductId()));
            if (!ObjectUtils.isEmpty(alarmConfigEntity) && entity.getId() != alarmConfigEntity.getId()) {
                entity.setId(alarmConfigEntity.getId());
            }
        }
        super.updateBefore(entity, context);
    }
}