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

import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.ICacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDCacheServiceImpl;
import com.mortals.xhx.module.device.dao.DeviceDao;
import com.mortals.xhx.module.device.model.DeviceEntity;
import com.mortals.xhx.module.device.service.DeviceService;
import org.springframework.util.ObjectUtils;

import static com.mortals.xhx.common.key.RedisKey.KEY_DEVICE_TYPE_CACHE;

/**
 * DeviceService
 * 设备 service实现
 *
 * @author zxfei
 * @date 2022-03-09
 */
@Service("deviceService")
public class DeviceServiceImpl extends AbstractCRUDCacheServiceImpl<DeviceDao, DeviceEntity, Long> implements DeviceService {

    @Autowired
    private ICacheService cacheService;

    @Override
    protected String getExtKey(DeviceEntity data) {
        return data.getDeviceCode();
    }


    @Override
    protected void saveBefore(DeviceEntity entity, Context context) throws AppException {
        if (ObjectUtils.isEmpty(entity.getDeviceCode())) {
            entity.setDeviceCode(entity.getDeviceMac());
        }
        super.saveBefore(entity, context);
    }

    @Override
    public void action(Long id, Integer type, Context context) {
        DeviceEntity deviceEntity = this.get(id, context);
        if (!ObjectUtils.isEmpty(deviceEntity)) {
            cacheService.set(KEY_DEVICE_TYPE_CACHE + deviceEntity.getDeviceCode(), type);
        }
    }
}