package com.fdkankan.platform.goods.controller.app;

import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.controller.BaseController;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.platform.goods.service.IDeviceService;
import com.fdkankan.platform.goods.vo.ResponseCameraApp;
import com.fdkankan.platform.user.request.RequestCamera;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 设备中心接口-app设备模块
 */
@Log4j2
@RestController
@RequestMapping("/service/platform/app/camera")
public class DeviceCameraAppController extends BaseController {

    @Autowired
    private IDeviceService deviceService;

    /**
     * 获取设备详情
     * childPassword        相机密码
     * childName            相机的物理地址
     * @return
     */
    @RequestMapping(value = "/getCameraInfo", method = RequestMethod.POST)
    public ResponseCameraApp getCameraInfo(@RequestBody RequestCamera param) throws Exception {
        return deviceService.getCameraInfo(param);
    }

    /**
     * 绑定设备
     * userName     用户名
     * childName    相机的物理地址
     * cameraType   相机类型，1双目，4八目
     * snCode       sn码
     * @return
     */
    @RequestMapping(value = "/bindCamera", method = RequestMethod.POST)
    public ResponseCameraApp bindCamera(@RequestBody RequestCamera camera) throws Exception {
        if((StringUtils.isEmpty(camera.getChildName()) && StringUtils.isEmpty(camera.getSnCode())) || StringUtils.isEmpty(camera.getUserName())){
            throw new BusinessException(ErrorCode.MISSING_REQUIRED_PARAMETERS);
        }
        return deviceService.bindProCamera(camera);
    }

    /**
     * 获取用户所有设备
     * userName     用户名
     * cameraType   相机类型，1双目，4八目
     */
    @RequestMapping(value = "/getCamerasForUser", method = RequestMethod.POST)
    public List<ResponseCameraApp> getCamerasForUser(@RequestBody RequestCamera camera) throws Exception {
        return  deviceService.getCamerasForUser(camera);
    }

    /**
     * 设备解除绑定
     * userName         用户名
     * cameraType       相机类型，1双目，4八目
     * childName        相机的物理地址
     * @return
     */
    @RequestMapping(value = "/unbind", method = RequestMethod.POST)
    public String unbind(@RequestBody RequestCamera camera) throws Exception {
        return deviceService.unbindPro(camera);
    }
}
