package com.fdkankan.ucenter.service.impl;

import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.redis.util.RedisUtil;
import com.fdkankan.ucenter.common.RedisKeyUtil;
import com.fdkankan.ucenter.common.constants.ResultCodeMsg;
import com.fdkankan.ucenter.entity.CameraType;
import com.fdkankan.ucenter.entity.Scene3dNum;
import com.fdkankan.ucenter.mapper.IScene3dNumMapper;
import com.fdkankan.ucenter.service.ICameraTypeService;
import com.fdkankan.ucenter.service.IScene3dNumService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 场景编码表 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-07-12
 */
@Service
@Slf4j
public class Scene3dNumServiceImpl extends ServiceImpl<IScene3dNumMapper, Scene3dNum> implements IScene3dNumService {

    @Autowired
    RedisUtil redisUtil;
    @Autowired
    ICameraTypeService cameraTypeService;

    @Override
    public String generateSceneNum(Integer cameraType) {
        String sceneNum = redisUtil.lLeftPop(RedisKeyUtil.SCENE_UNUSED_NUMS);
        if(StringUtils.isEmpty(sceneNum)){
            throw new BusinessException(ResultCodeMsg.FAILURE_CODE_400001, ResultCodeMsg.FAILURE_MSG_400001);
        }
        if(cameraType != null){
            CameraType cameraTypeEntity = cameraTypeService.getByCameraType(cameraType);
            if(cameraTypeEntity == null ){
                log.info("generateSceneNum-error:cameraType{}",cameraType);
                throw new BusinessException(-1,"相机类型不存在");
            }
            return cameraTypeEntity.getScenePrefix() + sceneNum;
        }
        return sceneNum;

    }

}
