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

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.platform.agent.request.RequestSearch;
import com.fdkankan.platform.goods.entity.CameraVersion;
import com.fdkankan.platform.goods.service.ICameraVersionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 相机版本管理模块
 */
@RestController
@RequestMapping("/service/platform")
public class CameraVersionController {

    @Autowired
    private ICameraVersionService cameraVersionService;

    /**
     * 仅有有一台相机是活动状态
     * type     相机类型，1八目，2双目,不传默认八目
     * @return
     */
    @GetMapping(value = "/manager/goods/version/list")
    public Page<CameraVersion> getList(@RequestParam(value="pageNum", defaultValue="0", required = false) Integer pageNum,
                                       @RequestParam(value="pageSize", defaultValue="10", required = false) Integer pageSize,
                                       @RequestParam(value="type", required = false) String type) throws Exception{
        return cameraVersionService.getPageByType(pageNum,pageSize,type);
    }
    /**
     * 仅有有一台相机是活动状态 更新状态
     * type     相机类型，1八目，2双目,不传默认八目 9:转台相机 10：激光 5:第三方相机
     * id       id
     * state    状态
     * @return
     */
    @GetMapping(value = "/manager/goods/version/update/{id}/{status}/{type}")
    public String updateStatus(@PathVariable("id") Long id,
                               @PathVariable("status") String status,
                               @PathVariable("type") String type) throws Exception{
        return cameraVersionService.updateStatus(id, StringUtils.upperCase(status),type);
    }

    /**
     * 搜索查询
     * type     相机类型，1八目，2双目,不传默认八目
     * pageNum         页码
     * pageSize         页大小
     * startDate    开始时间
     * endDate      结束时间
     * itemName     项目名称
     * @return
     */
    @PostMapping("/manager/goods/version/search")
    public Page<CameraVersion> searchLike(@RequestBody RequestSearch param) throws Exception{
        return cameraVersionService.searchLike(param);
    }

    /**
     * 上传文件
     * type     相机类型，1八目，2双目,不传默认八目
     * file       文件流
     * version    版本
     * description  描述
     * @return
     */
    @PostMapping(value = "/manager/goods/version/upload", consumes = { "multipart/form-data" })
    public String upload(@RequestParam("file") MultipartFile file,
                         @RequestParam("version") String version,
                         @RequestParam("description") String description,
                         @RequestParam("minVersion") String minVersion,
                         @RequestParam("type") String type) {
        try {
           return cameraVersionService.upload(file,version,description,minVersion,type);
        }catch (Exception e){
            return ErrorCode.ERROR_MSG.message();
        }
    }
    /**
     * 给硬件部接口，不需要拦截 获取激活的相机
     * type     相机类型，相机类型，1八目，2双目, 9转台双目, 10激光转台
     * @return
     */
    @GetMapping("/goods/version/enabled")
    public CameraVersion getEnabledCameraVersion(
                                         @RequestParam("type") String type) throws Exception{
        return cameraVersionService.getByType(type);
    }
    /**
     * 更新imageVersion 不需要拦截
     * @return
     */
    @RequestMapping(value = {"/updateimageVersion"}, method = RequestMethod.GET)
    public int updateImageVersion(
            @RequestParam("num") String num) throws Exception{
        return cameraVersionService.updateImageVersion(num);
    }

}
