package cn.iocoder.yudao.module.member.controller.admin.devicewarn;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.member.controller.admin.devicewarn.vo.DeviceWarnPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.devicewarn.vo.DeviceWarnRespVO;
import cn.iocoder.yudao.module.member.controller.admin.devicewarn.vo.DeviceWarnSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.devicewarn.DeviceWarnNumDO;
import cn.iocoder.yudao.module.member.service.devicewarn.DeviceWarnNumService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 设备告警次数")
@RestController
@RequestMapping("/member/device-warn")
@Validated
public class DeviceWarnNumController {

    @Resource
    private DeviceWarnNumService deviceWarnService;

    @PostMapping("/create")
    @Operation(summary = "创建设备告警次数")
    @PreAuthorize("@ss.hasPermission('member:device-warn:create')")
    public CommonResult<Long> createDeviceWarn(@Valid @RequestBody DeviceWarnSaveReqVO createReqVO) {
        return success(deviceWarnService.createDeviceWarn(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备告警次数")
    @PreAuthorize("@ss.hasPermission('member:device-warn:update')")
    public CommonResult<Boolean> updateDeviceWarn(@Valid @RequestBody DeviceWarnSaveReqVO updateReqVO) {
        deviceWarnService.updateDeviceWarn(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备告警次数")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:device-warn:delete')")
    public CommonResult<Boolean> deleteDeviceWarn(@RequestParam("id") Long id) {
        deviceWarnService.deleteDeviceWarn(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备告警次数")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:device-warn:query')")
    public CommonResult<DeviceWarnRespVO> getDeviceWarn(@RequestParam("id") Long id) {
        DeviceWarnNumDO deviceWarn = deviceWarnService.getDeviceWarn(id);
        return success(BeanUtils.toBean(deviceWarn, DeviceWarnRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备告警次数分页")
    @PreAuthorize("@ss.hasPermission('member:device-warn:query')")
    public CommonResult<PageResult<DeviceWarnRespVO>> getDeviceWarnPage(@Valid DeviceWarnPageReqVO pageReqVO) {
        PageResult<DeviceWarnNumDO> pageResult = deviceWarnService.getDeviceWarnPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceWarnRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备告警次数 Excel")
    @PreAuthorize("@ss.hasPermission('member:device-warn:export')")
    @OperateLog(type = EXPORT)
    public void exportDeviceWarnExcel(@Valid DeviceWarnPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceWarnNumDO> list = deviceWarnService.getDeviceWarnPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备告警次数.xls", "数据", DeviceWarnRespVO.class,
                        BeanUtils.toBean(list, DeviceWarnRespVO.class));
    }

}