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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fdkankan.common.controller.BaseController;
import com.fdkankan.common.util.JwtUtil;
import com.fdkankan.platform.agent.vo.ResponseAgentCamera;
import com.fdkankan.platform.goods.entity.Camera;
import com.fdkankan.platform.goods.service.ICameraService;
import com.fdkankan.platform.goods.service.ManagerCameraService;
import com.fdkankan.platform.order.service.IVirtualOrderService;
import com.fdkankan.platform.user.request.RequestCamera;
import com.fdkankan.platform.user.vo.ResponseCamera;
import com.fdkankan.platform.user.vo.ResponseVirtualOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 后台管理-设备查询/设备管理接口
 */
@RestController
@RequestMapping("/service/platform/manager/camera")
public class ManagerCameraController extends BaseController {

    @Autowired
    private ManagerCameraService managerCameraService;
    @Autowired
    private ICameraService cameraService;
    @Autowired
    private IVirtualOrderService virtualOrderService;

    /**
     * 设备查询列表
     * startDate      开始时间
     * endDate        结束时间
     * childName     相机的Mac地址
     * @return
     */
    @RequestMapping(value = {"/search"}, method = RequestMethod.POST)
    public Page<ResponseAgentCamera> searchAgentListDetail(@RequestBody RequestCamera param) throws Exception{
        return managerCameraService.searchAgentListDetail(param);
    }
    /**
     * 设备查询列表统计
     * startDate      开始时间
     * endDate        结束时间
     * childName     相机的Mac地址
     * @return
     */
    @RequestMapping(value = {"/searchStatistics"}, method = RequestMethod.POST)
    public JSONObject searchStatistics(@RequestBody RequestCamera param) throws Exception{
        return managerCameraService.searchStatistics(param);
    }
    /**
     * 设备详情充值/消费记录
     * type      0-充值记录，1-消费记录
     * cameraId        设备id
     * startDate     开始时间
     * endDate     结束时间
     * @return
     */
    @RequestMapping(value = {"/detailLog"}, method = RequestMethod.POST)
    public Page<ResponseVirtualOrder> detailLog(@RequestBody RequestCamera param) throws Exception{
        return  virtualOrderService.getPageVoByParam(param);
    }
    /**
     * 设备查询列表统计
     * type      0-充值记录，1-消费记录
     * cameraId        设备id
     * startDate     开始时间
     * endDate     结束时间
     * childName     相机的Mac地址
     * @return
     */
    @RequestMapping(value = {"/searchCameraDetailStatistics"}, method = RequestMethod.POST)
    public JSONObject searchCameraDetailStatistics(@RequestBody RequestCamera param) throws Exception{
        return managerCameraService.searchCameraDetailStatistics(param);
    }
    /**
     * 设备管理列表
     * type      类型，1：入库，2：出库，3：激活
     * cameraId        设备id
     * startDate     开始时间
     * endDate     结束时间
     * searchKey     关键词
     * @return
     */
    @RequestMapping(value = {"/list"}, method = RequestMethod.POST)
    public Page<ResponseCamera> list(@RequestBody RequestCamera param) throws Exception{
        return managerCameraService.queryCameraListByParams(param);
    }
    /**
     * 添加相机
     * childName      物理地址
     * orderSn        订单号
     * wifiName     wifi名称
     * own     出库类型
     * balance     初始点数
     * companyId     企业id
     * type     相机类型，0表示双目，1表示八目  9转台  10激光
     * @return
     */
    @RequestMapping(value = {"/add"}, method = RequestMethod.POST)
    public String addCamera(@RequestBody RequestCamera param) throws Exception{
        return managerCameraService.addCamera(param);
    }
    /**
     * 更新相机
     * childName      物理地址
     * orderSn        订单号
     * wifiName     wifi名称
     * own     出库类型
     * balance     初始点数
     * companyId     企业id
     * type     相机类型，0表示双目，1表示八目  9转台  10激光
     * @return
     */
    @RequestMapping(value = {"/update"}, method = RequestMethod.POST)
    public Camera updateCamera(@RequestBody RequestCamera param) throws Exception{
        return cameraService.updateCameraByManager(param);
    }
    /**
     * 导出库存数据
     * searchKey      关键词
     * startDate        开始时间
     * endDate     结束时间
     * type     类型，1：入库，2：出库，3：激活
     */
    @RequestMapping(value = {"/exportCamera"}, method = RequestMethod.POST)
    public void exportCamera(@RequestBody RequestCamera param, HttpServletRequest request, HttpServletResponse response) throws Exception {
        managerCameraService.exportCamera(param, request, response);
    }

    /**
     * 批量删除相机
     * ids      id集合，多个用逗号分割
     * @return
     */
    @RequestMapping(value = {"/deleteCamera"}, method = RequestMethod.POST)
    public String deleteCamera(@RequestBody RequestCamera param) throws Exception {
        return cameraService.deleteCamera(param);
    }

    /**
     * 修改相机点数
     * cameraId     相机id
     * body         0表示系统赠送，-2表示退充值款
     * points       要修改的点数
     * @return
     */
    @RequestMapping(value = {"/modifyPoints"}, method = RequestMethod.POST)
    public String modifyPoints(@RequestBody RequestCamera param) throws Exception {
        return managerCameraService.modifyPoints(param);
    }

    /**
     * 解绑相机
     * cameraId     相机id
     * @return
     */
    @RequestMapping(value = {"/unbind"}, method = RequestMethod.POST)
    public String unbind(@RequestBody RequestCamera param) throws Exception {
        return managerCameraService.unbindCamera(param.getCameraId());
    }
    /**
     * 绑定相机
     * cameraId     相机id
     * userName     用户名（手机号）
     * @return
     */
    @RequestMapping(value = {"/bind"}, method = RequestMethod.POST)
    public String bind(@RequestBody RequestCamera param) throws Exception {
        return managerCameraService.bindCamera(param.getCameraId(), param.getUserName());
    }
    /**
     * 管理容量（增加）
     * cameraId     相机id
     * month        多少个月
     * space        扩容大小（单位G，1T为1024）
     * @return
     */
    @RequestMapping(value = {"/manageSpace"}, method = RequestMethod.POST)
    public String manageSpace(@RequestBody RequestCamera param) throws Exception{
        return managerCameraService.manageSpace(param);
    }

    /**
     * 恢复基本容量
     * cameraId 相机id
     * @return
     */
    @RequestMapping(value = {"/recoverSpace"}, method = RequestMethod.POST)
    public String recoverSpace(@RequestBody RequestCamera param) throws Exception{
        return managerCameraService.recoverSpace(param);
    }

    /**
     * 追溯管理后台的设备列表
     * searchKey    关键词
     * @return
     */
    @RequestMapping(value = {"/companyList"}, method = RequestMethod.POST)
    public Page<ResponseCamera> companyList(@RequestBody RequestCamera param) throws Exception {
        return managerCameraService.companyList(param,getToken());
    }

    /**
     * 追溯管理后台的设备编辑
     * cameraId     相机id
     * recStatus    A激活，I不激活
     * address      设备地址
     * @return
     */
    @RequestMapping(value = {"/companyUpdate"}, method = RequestMethod.POST)
    public String companyUpdate(@RequestBody RequestCamera param) throws Exception {
        return managerCameraService.companyUpdate(param,getToken());
    }

    //以下接口为新管理后台接口
    /**
     * 相机入库(新)
     * snCode       sn编码
     * type         1：八目，2双目
     * @return
     */
    @RequestMapping(value = {"/addCamera"}, method = RequestMethod.POST)
    public Camera addCameraNew(@RequestBody RequestCamera param) throws Exception {
        return managerCameraService.addCameraNew(param);
    }

    /**
     * 相机出库(新)
     * cameraId             相机id
     * agentFrameworkId     经销商id
     * own                  0表示客户，1表示员工，2表示赠送，3表示其他，4经销商销售
     * @return
     */
    @RequestMapping(value = {"/cameraOwn"}, method = RequestMethod.POST)
    public String cameraOwn(@RequestBody RequestCamera param) throws Exception {
        return managerCameraService.cameraOwn(param);
    }


    /**
     * 管理后台相机列表(新)
     * snCode               sn码
     * own                  0表示客户，1表示员工，2表示赠送，3表示其他，4经销商销售
     * agentFrameworkId     经销商id
     * companyId            客户企业id
     * @return
     */
    @RequestMapping(value = {"/cameraList"}, method = RequestMethod.POST)
    public Page<ResponseCamera> cameraList(@RequestBody RequestCamera param) throws Exception {
        String userName = JwtUtil.getUsername(getToken());
        return managerCameraService.cameraList(param, userName);
    }

    /**
     * 删除相机(新)
     * ids      id集合，多个用逗号分割
     * @return
     */
    @RequestMapping(value = {"/deleteCameraNew"}, method = RequestMethod.POST)
    public String deleteCameraNew(@RequestBody RequestCamera param) throws Exception {
        return cameraService.deleteCameraNew(param);
    }

    /**
     * 导入相机
     * file         excel文件
     * @return
     */
    @RequestMapping(value = {"/importCamera"}, method = RequestMethod.POST)
    public Integer importCamera(@RequestParam("file") MultipartFile file) throws Exception {
        return managerCameraService.importCamera(file);
    }

    /**
     * 导入出库相机
     * file         excel文件
     * @return
     */
    @RequestMapping(value = {"/importOutCamera"}, method = RequestMethod.POST)
    public Integer importOutCamera(@RequestParam("file") MultipartFile file) throws Exception {
        return managerCameraService.importOutCamera(file);
    }

    /**
     * 导入客户关联相机
     * file         excel文件
     * @return
     */
    @RequestMapping(value = {"/importCompanyCamera"}, method = RequestMethod.POST)
    public Integer importCompanyCamera(@RequestParam("file") MultipartFile file) throws Exception {
        return managerCameraService.importCompanyCamera(file);
    }
}
