package com.yida.data.device.camera.controller.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yida.data.common.core.common.ModuleName;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.camera.EduCamera;
import com.yida.data.common.core.entity.camera.EduCameraStrategyTrans;
import com.yida.data.common.core.entity.system.enums.OperationLogTypeEnum;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.common.core.utils.FileUtil;
import com.yida.data.device.camera.service.EduCameraService;
import com.yida.data.device.camera.service.EduCameraStrategyTransService;
import com.yida.data.device.dto.camera.CameraDTO;
import com.yida.data.device.dto.camera.ListCameraDTO;
import com.yida.data.device.dto.camera.ListCameraSetDTO;
import com.yida.data.device.dto.camera.ListGroupDTO;
import com.yida.data.device.vo.camera.ListCameraVO;
import com.yida.data.log.annotation.OperationLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * @author ccl
 */
@Api(tags = "监控-后台")
@Slf4j
@Validated
@RestController
@RequestMapping("/camera")
@RequiredArgsConstructor
public class WebEduCameraController {

    private final EduCameraService cameraService;
    private final EduCameraStrategyTransService transService;

    @ApiOperation("查询设备列表")
    @GetMapping("/listCamera")
    public ResultBean<Page<ListCameraVO>> listCamera(ListCameraDTO listCameraDTO) {
        return ResultBean.buildSuccess(cameraService.listCamera(listCameraDTO));
    }


    @ApiOperation("新增或修改监控设备")
    @PostMapping("/saveOrUpCamera")
    public ResultBean<String> saveOrUpCamera(@RequestBody CameraDTO dto) {
        cameraService.saveOrUpCamera(dto);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("批量分组")
    @PostMapping("/listGroup")
    public ResultBean listGroup(@RequestBody ListGroupDTO dto) {

        cameraService.listGroup(dto);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("批量设置")
    @PostMapping("/listCameraSet")
    public ResultBean listCameraSet(@RequestBody ListCameraSetDTO dto) {

        cameraService.listCameraSet(dto);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("设置设备转码")
    @PostMapping("/cameraTrans")
    public ResultBean cameraTrans(@RequestBody List<EduCameraStrategyTrans> trans) {
        transService.saveBatch(trans);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("注销监控设备")
    @GetMapping("/removeCamera")
    public ResultBean removeCamera(@RequestParam Long id) {

        cameraService.removeCamera(id);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("开启或关闭监控各种状态")
    @PostMapping("/updateCamera")
    public ResultBean updateCamera(@RequestBody EduCamera camera) {

        cameraService.updateById(camera);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("查询监控详情")
    @GetMapping("/findCamera")
    public ResultBean<ListCameraVO> findCamera(@RequestParam @ApiParam("监控id") Long id) {

        return ResultBean.buildSuccess(cameraService.findCamera(id));
    }

    @ApiOperation("查询监控转码详情")
    @GetMapping("/findCameraTrans")
    public ResultBean<List<EduCameraStrategyTrans>> findCameraTrans(@RequestParam @ApiParam("监控id") Long id) {
        List<EduCameraStrategyTrans> list = transService.list(Wrappers.query(new EduCameraStrategyTrans()).lambda().eq(EduCameraStrategyTrans::getCameraId, id));
        return ResultBean.buildSuccess(list);
    }

    @ApiOperation("下载健康体测相关导入模板")
    @PostMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response) throws Exception {
        cameraService.downloadTemplate(response);
    }

    @ApiOperation("批量上传ic卡")
    @PostMapping("/batchUpCamera")
    @OperationLog(module = ModuleName.TELEPHONE, methods = "导入学生ic卡", type =
            OperationLogTypeEnum.INSERT)
    public ResultBean batchUpIcCard(@RequestBody MultipartFile file) {

        cameraService.batchUpIcCard(FileUtil.uploadMultipartFile(file),
                FebsUtil.getCurrentUser());
        return ResultBean.buildSuccess();
    }

    @ApiOperation("查询开放监控列表")
    @GetMapping("/findOpenCamera")
    public ResultBean<List<EduCamera>> findOpenCamera(
            @ApiParam(value = "设备类型.0:非公共摄像头,1:公共摄像头,不传则查询全部") @RequestParam(required = false) Integer commonType) {
        LambdaQueryWrapper<EduCamera> wrapper = Wrappers.lambdaQuery(new EduCamera())
                .eq(EduCamera::getSchoolId, FebsUtil.getDeptId())
                .eq(EduCamera::getOpenStatus, 1);
        if (Objects.nonNull(commonType)) {
            wrapper.eq(EduCamera::getCommonType, commonType);
        }
        return ResultBean.buildSuccess(cameraService.list(wrapper));
    }
}
