AiLockingKeyServiceImpl.java 6.12 KB
package com.glsx.smart.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.glsx.plat.common.utils.SnowFlake;
import com.glsx.smart.dao.entity.SmartKeyCommandLog;
import com.glsx.smart.dao.entity.SmartVehicleBlueKey;
import com.glsx.smart.dao.entity.SmartVehicleBlueKeyExample;
import com.glsx.smart.pojo.request.*;
import com.glsx.smart.pojo.response.SmartCarKeyOpreateResponse;
import com.glsx.smart.pojo.response.SmartCarKeyResponse;
import com.glsx.smart.service.AiLockingKeyService;
import com.glsx.smart.service.SmartKeyCommandLogService;
import com.glsx.smart.service.SmartVehicleBlueKeyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

/**
 * <p>Description:智能车钥匙service
 * <p>Project: chejia-smart
 *
 * @author fengzhi
 * @date 2021-04-19
 */

@Service
@Slf4j
public class AiLockingKeyServiceImpl implements AiLockingKeyService {
    @Autowired
    private SmartKeyCommandLogService smartKeyCommandLogService;

    @Autowired
    private SmartVehicleBlueKeyService smartVehicleBlueKeyService;

    //sn查询/新增车钥匙
    @Override
    public SmartCarKeyResponse getLockingKey(SmartCarKeyRequest request) {
        SmartCarKeyResponse response = new SmartCarKeyResponse();
        SmartVehicleBlueKeyExample example = new SmartVehicleBlueKeyExample();
        example.createCriteria().andUserIdEqualTo(request.getUserId()).andSnEqualTo(request.getSn()).andIsDeleteEqualTo(0);
        List<SmartVehicleBlueKey> smartVehicleBlueKeys = smartVehicleBlueKeyService.selectByExample(example);

        if(CollectionUtils.isEmpty(smartVehicleBlueKeys)){
            //如果sn,userid没有数据,生成只含有crep钥匙信息返回
            SmartVehicleBlueKey newKey = new SmartVehicleBlueKey();
            //todo 雪花算法生成crep,对称加密
            long id = SnowFlake.nextId();
            long userId = request.getUserId();

            newKey.setCrepCode("");
            newKey.setCreateTime(new Date());
            smartVehicleBlueKeyService.insert(newKey);

            BeanUtils.copyProperties(newKey,response);
            return response;
        }else {
            SmartVehicleBlueKey carkey = smartVehicleBlueKeys.get(0);
            String crepCode = carkey.getCrepCode();
            if(StringUtils.isNotEmpty(crepCode)){
                //如果sn,userid有对应的crep,返回钥匙信息
                BeanUtils.copyProperties(carkey,response);
                return response;
            }else {
                //如果sn,userid没有对应的crep。生成只含有crep钥匙信息返回
                carkey.setCrepCode(request.getSn());
                carkey.setCreateTime(new Date());
                smartVehicleBlueKeyService.updateByPrimaryKey(carkey);
                BeanUtils.copyProperties(carkey,response);
                return response;
            }
        }
    }

    //智能车钥匙解绑
    @Override
    public SmartCarKeyOpreateResponse removeLockingKey(SmartCarKeyRmRequest request) {
        SmartVehicleBlueKey smartVehicleBlueKey = new SmartVehicleBlueKey();
        smartVehicleBlueKey.setIsDelete(1);
        int conut = smartVehicleBlueKeyService.updateByExample(smartVehicleBlueKey, getExample(request.getUserId(), request.getCrep()));
        return getOpreateResponse(conut);
    }


    //车钥匙操作指令上报
    @Override
    public SmartCarKeyOpreateResponse LogLockingKeyCommand(SmartCarKeyCommandRequest carKeyCommandRequest) {
        SmartKeyCommandLog log = new SmartKeyCommandLog();
        log.setUserId(carKeyCommandRequest.getUserId());
        log.setCrep(carKeyCommandRequest.getCrepCode());
        log.setKeyCommand(carKeyCommandRequest.getKeyCommond());
        log.setCreateTime(new Date());
        int conut = smartKeyCommandLogService.insert(log);
        return getOpreateResponse(conut);
    }

    //车钥匙修改昵称
    @Override
    public SmartCarKeyOpreateResponse setLockingKeyName(SmartCarKeyNameRequest request) {
        SmartVehicleBlueKey smartVehicleBlueKey = new SmartVehicleBlueKey();
        smartVehicleBlueKey.setKeyName(request.getName());
        int conut = smartVehicleBlueKeyService.updateByExample(smartVehicleBlueKey, getExample(request.getUserId(), request.getCrep()));
        return getOpreateResponse(conut);
    }

    //车钥匙修改自动感应
    @Override
    public SmartCarKeyOpreateResponse setLockingKeyAutoSense(SmartCarKeyAutoSenseRequest request) {
        SmartVehicleBlueKey smartVehicleBlueKey = new SmartVehicleBlueKey();
        smartVehicleBlueKey.setAutoSenseStatus(request.getAutoSenseStatus());
        int conut = smartVehicleBlueKeyService.updateByExample(smartVehicleBlueKey, getExample(request.getUserId(), request.getCrep()));
        return getOpreateResponse(conut);
    }


    //智能车钥匙注册成功后的数据保存
    @Override
    public SmartCarKeyOpreateResponse saveLockingKeyRegisterInfo(SmartCarKeyRegisterRequest request) {
        SmartVehicleBlueKey smartVehicleBlueKey = new SmartVehicleBlueKey();
        smartVehicleBlueKey.setMobileBlueMac(request.getMobileMac());
        smartVehicleBlueKey.setSmartLockMac(request.getSmartLockMac());
        smartVehicleBlueKey.setSecretKey(request.getKeyLock());

        int conut = smartVehicleBlueKeyService.updateByExample(smartVehicleBlueKey, getExample(request.getUserId(), request.getCrepCode()));
        return getOpreateResponse(conut);
    }

    //查询车钥匙example,根据用户ID和crep查
    private SmartVehicleBlueKeyExample getExample(Long userId, String crep) {
        SmartVehicleBlueKeyExample example = new SmartVehicleBlueKeyExample();
        example.createCriteria().andUserIdEqualTo(userId).andCrepCodeEqualTo(crep).andIsDeleteEqualTo(0);
        return example;
    }

    private SmartCarKeyOpreateResponse getOpreateResponse(int conut) {
        SmartCarKeyOpreateResponse response = new SmartCarKeyOpreateResponse();
        response.setSuccess(conut > 0);
        return response;
    }
}