UserDeviceController.java 4.61 KB
package cn.com.glsx.smart.modules.controller;

import java.util.List;
import cn.com.glsx.smart.common.exception.ChejiaBusinessException;
import cn.com.glsx.smart.common.exception.ResultEnum;
import cn.com.glsx.smart.modules.entity.SmartUserDevice;
import cn.com.glsx.smart.modules.model.ValidateGroups;
import cn.com.glsx.smart.modules.model.request.UserDeviceReq;
import cn.com.glsx.smart.modules.service.SmartUserDeviceService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.core.web.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>Description: 用户设备业务
 * <p>Project: chejia-smart
 *
 * @author wbh
 * @date 2021-04-23
 */
@Slf4j
@RestController
@RequestMapping("/userDevice")
public class UserDeviceController {
    @Autowired
    private SmartUserDeviceService userDeviceService;

    /**
     * 获取用户设备
     *
     * @param req 请求
     * @return 结果
     */
    @GetMapping("/getList")
    public R getList(UserDeviceReq req) {
        log.info("$$$$ /userDevice/getList param: {}", JSON.toJSONString(req));
        if (req.getUserId() == null) {
            throw ChejiaBusinessException.create(ResultEnum.PARAM_EMPTY, "userId");
        }
        List<SmartUserDevice> userDeviceList = userDeviceService.getList(req);
        return R.ok().data(userDeviceList);
    }

    /**
     * 根据 deviceId 查询设备详情
     *
     * @param req 请求
     * @return 结果
     */
    @GetMapping("/getByDeviceId")
    public R getByDeviceId(UserDeviceReq req) {
        log.info("$$$$ /userDevice/getByDeviceId param: {}", JSON.toJSONString(req));
        if (StringUtils.isBlank(req.getDeviceId())) {
            throw ChejiaBusinessException.create(ResultEnum.PARAM_EMPTY, "deviceId");
        }
        SmartUserDevice userDevice = userDeviceService.getByDeviceId(req);
        return R.ok().data(userDevice);
    }

    /**
     * 保存用户设备
     *
     * @param req 请求
     * @return 结果
     */
    @PostMapping("/save")
    public R save(@RequestBody @Validated(ValidateGroups.Insert.class) UserDeviceReq req) {
        log.info("$$$$ /userDevice/save param: {}", JSON.toJSONString(req));
        //if (StringUtils.isBlank(req.getDeviceId()) || req.getChannel() == null) {
        //    throw ChejiaBusinessException.create(ResultEnum.PARAM_EMPTY, "deviceId, channel");
        //}
        int rows = userDeviceService.save(req);
        return R.ok().data(rows > 0);
    }

    /**
     * 删除用户设备
     *
     * @param req 请求
     * @return 结果
     */
    @PostMapping("/deleteDevice")
    public R deleteDevice(@RequestBody @Validated(ValidateGroups.Delete.class)
            UserDeviceReq req) {
        log.info("$$$$ /userDevice/deleteDevice param: {}", JSON.toJSONString(req));
        //if (StringUtils.isBlank(req.getDeviceId()) || req.getChannel() == null) {
        //    throw ChejiaBusinessException.create(ResultEnum.PARAM_EMPTY, "deviceId, channel");
        //}
        userDeviceService.deleteDevice(req);
        return R.ok().data(true);
    }

    /**
     * 获取用户 bindKey
     *
     * @param token 用户 token
     * @return 结果
     */
    @GetMapping("/getUserBindKey")
    public R getUserBindKey(@RequestParam String token) {
        log.info("$$$$ /userDevice/getUserBindKey param: {}", token);
        String userBindKey = userDeviceService.getUserBindKey(token);
        return R.ok().data(userBindKey);
    }

    /**
     * 下发指令
     *
     * @param req 请求
     * @return 结果
     */
    @PostMapping("/sendCommand")
    public R sendCommand(@RequestBody JSONObject req) {
        log.info("$$$$ /userDevice/sendCommand param: {}", req.toJSONString());
        String result = userDeviceService.sendCommand(req);
        return R.ok().data(result);
    }

    /**
     * 设备解绑
     *
     * @param req 请求
     * @return 结果
     */
    @PostMapping("/unbind")
    public R unbind(@RequestBody JSONObject req) {
        log.info("$$$$ /userDevice/unbind param: {}", req.toJSONString());
        String result = userDeviceService.unbind(req);
        return R.ok().data(result);
    }
}