SmartUserDeviceServiceImpl.java 4.57 KB
package com.glsx.smart.service.impl;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.glsx.plat.common.utils.HttpUtils;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.smart.dao.entity.SmartUserChannel;
import com.glsx.smart.dao.entity.SmartUserDevice;
import com.glsx.smart.dao.mapper.SmartUserChannelMapper;
import com.glsx.smart.dao.mapper.SmartUserDeviceMapper;
import com.glsx.smart.exception.BusinessException;
import com.glsx.smart.exception.ResultEnum;
import com.glsx.smart.service.BaseService;
import com.glsx.smart.service.SmartUserDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * <p>Description: 用户设备业务
 * <p>Project: chejia-smart
 *
 * @author wbh
 * @date 2021-04-23
 */
@Slf4j
@Service
public class SmartUserDeviceServiceImpl extends BaseService implements SmartUserDeviceService {
    @Value("${device_remote_url}")
    private String remoteUrl;

    @Value("${user_bind_key}")
    private String userBindKeyApi;

    @Value("${user_send_command}")
    private String userSendCommandApi;

    @Resource
    private SmartUserDeviceMapper userDeviceMapper;

    @Resource
    private SmartUserChannelMapper userChannelMapper;

    @Override
    public List<SmartUserDevice> getList(SmartUserDevice req) {
        return userDeviceMapper.getList(req);
    }

    @Override
    public SmartUserDevice getByDeviceId(SmartUserDevice req) {
        List<SmartUserDevice> userDeviceList = userDeviceMapper.select(req);
        if (userDeviceList.isEmpty()) {
            throw BusinessException.create(ResultEnum.DEVICE_NOT_FOUND);
        }
        return userDeviceList.get(0);
    }

    @Override
    @Transactional
    public int save(SmartUserDevice req) {
        int rows;
        Date now = new Date();
        SmartUserDevice record = new SmartUserDevice();
        record.setDeviceId(req.getDeviceId());
        record.setChannel(req.getChannel());
        SmartUserDevice savedUserDevice = userDeviceMapper.selectOne(record);
        if (savedUserDevice == null) {
            if (StringUtils.isBlank(req.getUid())) {
                throw BusinessException.create(ResultEnum.PARAM_EMPTY, "uid");
            }
            SmartUserChannel ucRecord = new SmartUserChannel();
            ucRecord.setThirdId(req.getUid());
            SmartUserChannel savedUserChannel = userChannelMapper.selectOne(ucRecord);
            if (savedUserChannel == null) {
                throw BusinessException.create(ResultEnum.USER_NOT_FOUND);
            }
            req.setUserId(savedUserChannel.getUserId());
            req.setCreateTime(now);
            req.setUpdateTime(now);
            rows = userDeviceMapper.insertSelective(req);
        } else {
            if (StringUtils.isBlank(req.getDeviceName())) {
                throw BusinessException.create(ResultEnum.PARAM_EMPTY, "deviceName");
            }
            savedUserDevice.setDeviceName(req.getDeviceName());
            savedUserDevice.setUpdateTime(now);
            rows = userDeviceMapper.updateByPrimaryKeySelective(savedUserDevice);
        }
        return rows;
    }

    @Override
    public int deleteDevice(SmartUserDevice req) {
        int rows;
        rows = userDeviceMapper.deleteDevice(req);
        return rows;
    }

    @Override
    public String getUserBindKey(String token) {
        JSONObject reqObj = new JSONObject();
        reqObj.put("token", token);
        String url = remoteUrl + userBindKeyApi;
        log.info("$$$$ getUserBindKey url = {}, req = {}", url, reqObj.toJSONString());
        String respJson = HttpUtils.get(url, reqObj);
        log.info("$$$$ getUserBindKey resp = {}", respJson);
        JSONObject respObj = JSON.parseObject(respJson);
        String bindKey = "200".equals(respObj.getString("code"))
                ? respObj.getJSONObject("message").getString("bindKey") : null;
        return bindKey;
    }

    @Override
    public String sendCommand(JSONObject req) {
        String url = remoteUrl + userSendCommandApi;
        log.info("$$$$ sendCommand url = {}, param: {}", url, req.toJSONString());
        String respJson = HttpUtils.post(url, req.toJSONString());
        log.info("$$$$ sendCommand resp = {}", respJson);
        JSONObject respObj = JSON.parseObject(respJson);
        String result = "200".equals(respObj.getString("code"))
                ? respObj.getString("result") : null;
        return result;
    }
}