package cn.iocoder.yudao.module.system.controller.admin.information;

import cn.iocoder.yudao.module.system.controller.admin.information.dto.DeviceInformationDTO;
import cn.iocoder.yudao.module.system.controller.admin.information.dto.MapBindDeviceInfoDTO;
import cn.iocoder.yudao.module.system.controller.admin.information.dto.StatisticsInformationDTO;
import cn.iocoder.yudao.module.system.controller.admin.information.vo.DeviceInformationPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.information.vo.DeviceInformationRespVO;
import cn.iocoder.yudao.module.system.controller.admin.information.vo.DeviceInformationSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.information.DeviceInformationDO;
import cn.iocoder.yudao.module.system.service.information.DeviceInformationService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 设备信息")
@RestController
@RequestMapping("/system/device/information")
@Validated
public class DeviceInformationController {

    @Resource
    private DeviceInformationService informationService;

    @PostMapping("/create")
    @Operation(summary = "创建设备信息")
    @PreAuthorize("@ss.hasPermission('device:information:create')")
    public CommonResult<Long> createInformation(@Valid @RequestBody DeviceInformationSaveReqVO createReqVO) {
        return success(informationService.createInformation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备信息")
    @PreAuthorize("@ss.hasPermission('device:information:update')")
    public CommonResult<Boolean> updateInformation(@Valid @RequestBody DeviceInformationSaveReqVO updateReqVO) {
        informationService.updateInformation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('device:information:delete')")
    public CommonResult<Boolean> deleteInformation(@RequestParam("id") Long id) {
        informationService.deleteInformation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('device:information:query')")
    public CommonResult<DeviceInformationRespVO> getInformation(@RequestParam("id") Long id) {
        DeviceInformationRespVO information = informationService.getInformation(id);
        return success(information);
    }

    @GetMapping("/getCameraCode")
    @Operation(summary = "获得设备信息的摄像头code")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('device:information:getCameraCode')")
    public CommonResult<String> getCameraCode(@RequestParam("id") Long id) throws Exception {
        return success(informationService.getCameraCode(id));
    }

    @GetMapping("/list")
    @Operation(summary = "获得设备信息列表,3D地图也用到")
    public CommonResult<List<DeviceInformationRespVO>> getList(@Valid DeviceInformationDTO dto) {
        List<DeviceInformationDO> list = informationService.getList(dto);
        return success(BeanUtils.toBean(list, DeviceInformationRespVO.class));
    }

    @PostMapping("/mapBindDeviceInfo")
    @Operation(summary = "地图绑定设备")
    public CommonResult<Boolean> mapBindDeviceInfo(@Valid @RequestBody MapBindDeviceInfoDTO dto) {
        informationService.mapBindDeviceInfo(dto);
        return success(true);
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备信息分页")
    @PreAuthorize("@ss.hasPermission('device:information:query')")
    public CommonResult<PageResult<DeviceInformationRespVO>> getInformationPage(@Valid DeviceInformationPageReqVO pageReqVO) {
        PageResult<DeviceInformationDO> pageResult = informationService.getInformationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceInformationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备信息 Excel")
    @PreAuthorize("@ss.hasPermission('device:information:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportInformationExcel(@Valid DeviceInformationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceInformationDO> list = informationService.getInformationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备信息.xls", "数据", DeviceInformationRespVO.class,
                        BeanUtils.toBean(list, DeviceInformationRespVO.class));
    }

    @GetMapping("/classification")
    @Operation(summary = "获得设备信息分类列表")
    public CommonResult<Map<Integer,List<DeviceInformationDO>>> classification() {
        Map<Integer,List<DeviceInformationDO>> map = informationService.classification();
        return success(map);
    }

    @GetMapping("/deviceNumber")
    @Operation(summary = "统计设备数量")
    public CommonResult<List<StatisticsInformationDTO>> getDeviceNumber() {
        List<StatisticsInformationDTO> list = informationService.getDeviceNumber();
        return success(list);
    }

    @GetMapping("/getInformationList")
    @Operation(summary = "获得所有设备信息(含最后通讯时间)")
    @PreAuthorize("@ss.hasPermission('device:information:query')")
    public CommonResult<List<DeviceInformationRespVO>> getInformationList(@Valid DeviceInformationPageReqVO pageReqVO) {
        List<DeviceInformationRespVO> list = informationService.getInformationList(pageReqVO);
        return success(BeanUtils.toBean(list, DeviceInformationRespVO.class));
    }

    @GetMapping("/getMapImageUrl")
    @Operation(summary = "获得设备图标URL")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('device:information:getMapImageUrl')")
    public CommonResult<String> getMapImageUrl(@RequestParam("deviceType") Integer deviceType) {
        String str = informationService.getMapImageUrl(deviceType);
        return success(str);
    }
}
