package cn.iocoder.yudao.module.iot.controller.admin.kanban;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
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.module.iot.controller.admin.kanban.vo.KanbanPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.kanban.vo.KanbanRespVO;
import cn.iocoder.yudao.module.iot.controller.admin.kanban.vo.KanbanSaveReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.kanban.KanbanDO;
import cn.iocoder.yudao.module.iot.service.kanban.KanbanService;
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.*;

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.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 物联看板")
@RestController
@RequestMapping("/iot/kanban")
@Validated
public class KanbanController {

    @Resource
    private KanbanService kanbanService;

    @PostMapping("/create")
    @Operation(summary = "创建物联看板")
    @PreAuthorize("@ss.hasPermission('iot:kanban:create')")
    public CommonResult<Long> createKanban(@Valid @RequestBody KanbanSaveReqVO createReqVO) {
        return success(kanbanService.createKanban(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物联看板")
    @PreAuthorize("@ss.hasPermission('iot:kanban:update')")
    public CommonResult<Boolean> updateKanban(@Valid @RequestBody KanbanSaveReqVO updateReqVO) {
        kanbanService.updateKanban(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物联看板")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:kanban:delete')")
    public CommonResult<Boolean> deleteKanban(@RequestParam("id") Long id) {
        kanbanService.deleteKanban(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物联看板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:kanban:query')")
    public CommonResult<KanbanRespVO> getKanban(@RequestParam("id") Long id) {
        KanbanDO kanban = kanbanService.getKanban(id);
        return success(BeanUtils.toBean(kanban, KanbanRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物联看板分页")
    @PreAuthorize("@ss.hasPermission('iot:kanban:query')")
    public CommonResult<PageResult<KanbanRespVO>> getKanbanPage(@Valid KanbanPageReqVO pageReqVO) {
        PageResult<KanbanDO> pageResult = kanbanService.getKanbanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KanbanRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物联看板 Excel")
    @PreAuthorize("@ss.hasPermission('iot:kanban:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportKanbanExcel(@Valid KanbanPageReqVO pageReqVO,
                                  HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KanbanDO> list = kanbanService.getKanbanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物联看板.xls", "数据", KanbanRespVO.class,
                BeanUtils.toBean(list, KanbanRespVO.class));
    }

}