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

import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.model.Result;
import com.mortals.xhx.module.alarm.model.AlarmConfigEntity;
import com.mortals.xhx.module.alarm.model.AlarmConfigQuery;
import com.mortals.xhx.module.device.model.DeviceEntity;
import com.mortals.xhx.module.device.model.DeviceModuleQuery;
import com.mortals.xhx.module.device.service.DeviceService;
import com.mortals.xhx.module.platform.model.PlatformEntity;
import com.mortals.xhx.module.platform.service.PlatformService;
import com.mortals.xhx.module.product.model.ProductEntity;
import com.mortals.xhx.module.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.module.device.dao.DeviceModuleDao;
import com.mortals.xhx.module.device.model.DeviceModuleEntity;
import com.mortals.xhx.module.device.service.DeviceModuleService;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * DeviceModuleService
 * 设备模块信息 service实现
 *
 * @author zxfei
 * @date 2022-07-02
 */
@Service("deviceModuleService")
public class DeviceModuleServiceImpl extends AbstractCRUDServiceImpl<DeviceModuleDao, DeviceModuleEntity, Long> implements DeviceModuleService {


    @Override
    protected void validData(DeviceModuleEntity entity, Context context) throws AppException {
        if (entity.newEntity()) {
            DeviceModuleEntity deviceModuleEntity = this.selectOne(new DeviceModuleQuery().moduleMsgCode(entity.getModuleMsgCode()));
            if (!ObjectUtils.isEmpty(deviceModuleEntity)) {
                throw new AppException("设备模块消息编码已存在");
            }
        } else {
            if (!ObjectUtils.isEmpty(entity.getModuleMsgCode())) {
                DeviceModuleEntity deviceModuleEntity = this.selectOne(new DeviceModuleQuery().moduleMsgCode(entity.getModuleMsgCode()));
                if (!ObjectUtils.isEmpty(deviceModuleEntity) && entity.getId() != deviceModuleEntity.getId()) {
                    throw new AppException("设备模块消息编码已存在");
                }
            }
        }
        super.validData(entity, context);
    }
}