package com.fdkankan.platform.user.controller.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fdkankan.common.controller.BaseController;
import com.fdkankan.common.response.ResultData;
import com.fdkankan.platform.goods.entity.CameraDetailNumEntity;
import com.fdkankan.platform.goods.service.ICameraService;
import com.fdkankan.platform.user.request.RequestCamera;
import com.fdkankan.platform.user.service.IUserService;
import com.fdkankan.platform.user.vo.ResponseCamera;
import com.fdkankan.platform.user.vo.ResponseCameraSpace;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * 个人中心-设备模块
 */
@RestController
@RequestMapping("/service/platform/user/camera")
public class UserCameraController extends BaseController {

    private static Logger log = LoggerFactory.getLogger("programLog");

    @Autowired
    private IUserService userService;
    @Autowired
    private ICameraService cameraService;
    /**
     * 获取用户设备--新（返回增值权益到期时间）
     * childName        sn编码
     * selecttype       1：协作者，2：sn编码 ，3：全部
     * cameraType       设备类型
     * @return
     */
    @RequestMapping(value = "/listNew", method = RequestMethod.POST)
    public Page<ResponseCamera> listNew(@RequestBody RequestCamera param, @RequestHeader String token) throws Exception {
        return userService.getCameraPage(param,token);
    }

    /**
     * 获取相机类别 个数
     * @return
     */
    @RequestMapping(value = "/cameratypelist", method = RequestMethod.GET)
    public List<CameraDetailNumEntity> listCameraTypeNum(Integer type, @RequestHeader String token) throws Exception {
        return userService.getCameraTypeNum(type ,token);
    }

    /**
     * 获取用户设备
     * childName    设备号
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public ResponseCamera detail(@RequestBody RequestCamera param, @RequestHeader String token) throws Exception {
        return userService.getCameraDetailByChildName(param,token);
    }

    /**
     * 获取设备到期信息
     * childName    设备号
     */
    @RequestMapping(value = "/deadline", method = RequestMethod.POST)
    public HashMap<String, Object> deadline(@RequestBody RequestCamera param, @RequestHeader String token) throws Exception {
        return userService.getCameraDetailDeadline(param,token);
    }
    /**
     * 获取设备到期数量
     */
    @RequestMapping(value = "/deadlineNumber", method = RequestMethod.POST)
    public Integer deadlineNumber(@RequestHeader String token) throws Exception {
        return userService.getCameraDetailNumber(token);
    }

    /**
     * 获取用户所有设备
     * cameraType   相机类型，1双目，4八目
     * @return
     */
    @RequestMapping(value = "/getAll", method = RequestMethod.POST)
    public List<ResponseCamera> getAllCameras(@RequestBody RequestCamera param, @RequestHeader String token) throws Exception {
        return userService.getCameraByUser(param, token);
    }

    /**
     * 用户解绑设备
     * cameraId     cameraId单个操作时相机id
     * ids          设备id的集合
     */
    @RequestMapping(value = "/unbind", method = RequestMethod.POST)
    public String unbindCamera(@RequestBody RequestCamera camera) throws Exception {
        return userService.unbindCamera(camera.getCameraId(), camera.getIds());
    }
    /**
     * 用户添加设备
     * snCode sn码，批量的时候以逗号分割
     * cameraType   设备类型
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addCamera(@RequestBody RequestCamera camera,@RequestHeader String token) throws Exception {
        return cameraService.addCamera(camera,token);
    }
    /**
     * 获取设备未过期的扩容信息
     * childName 设备号
     * @return
     */
    @RequestMapping(value = "/cameraSpaceList", method = RequestMethod.POST)
    public List<ResponseCameraSpace> cameraSpaceList(@RequestBody RequestCamera camera, @RequestHeader String token) throws Exception {
        return cameraService.cameraSpaceList(camera);
    }

    /**
     * 根据sn查询相机信息
     * snCode 设备号
     * @return
     */
    @RequestMapping(value = "/findCameraBySn", method = RequestMethod.POST)
    public ResponseCamera findCameraBySn(@RequestBody RequestCamera camera, @RequestHeader String token) throws Exception {
        return cameraService.findCameraBySn(camera);
    }

    /**
     * 添加相机协作用户
     * cameraId
     * ids
     * resourceIds
     * userName
     */
    @RequestMapping(value = "/saveCooperationUser", method = RequestMethod.POST)
    public String saveCooperationUser(@RequestBody RequestCamera camera,@RequestHeader String token) throws Exception {
        return userService.saveCooperationUser(camera, token);
    }

    /**
     * 删除相机协作用户
     * cameraId     相机id
     */
    @RequestMapping(value = "/deleteCooperationUser", method = RequestMethod.POST)
    public String deleteCooperationUser(@RequestBody RequestCamera camera,@RequestHeader String token) throws Exception{
        return userService.deleteCooperationUser(camera,null);
    }

    /**
     * 根据相机id获取场景资源集合
     * cameraId  相机id
     */
    @RequestMapping(value = "/sceneResourceByCameraId", method = RequestMethod.POST)
    public ResultData sceneResourceByCameraId(@RequestBody RequestCamera camera, @RequestHeader String token) throws Exception{
        return userService.sceneResourceByCameraId(camera);
    }

    /**
     * 获取四维看看PRO - 我的相机列表 并到处execl
     */
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    public HSSFWorkbook export(@RequestHeader String token, HttpServletResponse response,@RequestBody RequestCamera param) throws Exception {
        return cameraService.export(token,response,param);
    }

    /**
     * 获取用户设备--新（根据sn返回全部不分页）
     * @return
     */
    @RequestMapping(value = "/listNewAll", method = RequestMethod.POST)
    public List<ResponseCamera> listNewAll(@RequestBody RequestCamera param) throws Exception {
        Long userId = this.getUserId();
        return userService.findNewAll(userId, param);
    }
}
