package cn.iocoder.yudao.module.pms.controller.admin.iotprojecttaskattrs;

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.pms.controller.admin.iotprojecttaskattrs.vo.IotProjectTaskAttrsPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotprojecttaskattrs.vo.IotProjectTaskAttrsRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotprojecttaskattrs.vo.IotProjectTaskAttrsSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotprojecttaskattrs.IotProjectTaskAttrsDO;
import cn.iocoder.yudao.module.pms.service.iotprojecttaskattrs.IotProjectTaskAttrsService;
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("/rq/iot-project-task-attrs")
@Validated
public class IotProjectTaskAttrsController {

    @Resource
    private IotProjectTaskAttrsService iotProjectTaskAttrsService;

    @PostMapping("/create")
    @Operation(summary = "创建项目任务扩展属性")
    @PreAuthorize("@ss.hasPermission('rq:iot-project-task-attrs:create')")
    public CommonResult<Long> createIotProjectTaskAttrs(@Valid @RequestBody IotProjectTaskAttrsSaveReqVO createReqVO) {
        return success(iotProjectTaskAttrsService.createIotProjectTaskAttrs(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新项目任务扩展属性")
    @PreAuthorize("@ss.hasPermission('rq:iot-project-task-attrs:update')")
    public CommonResult<Boolean> updateIotProjectTaskAttrs(@Valid @RequestBody IotProjectTaskAttrsSaveReqVO updateReqVO) {
        iotProjectTaskAttrsService.updateIotProjectTaskAttrs(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除项目任务扩展属性")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-project-task-attrs:delete')")
    public CommonResult<Boolean> deleteIotProjectTaskAttrs(@RequestParam("id") Long id) {
        iotProjectTaskAttrsService.deleteIotProjectTaskAttrs(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得项目任务扩展属性")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-project-task-attrs:query')")
    public CommonResult<IotProjectTaskAttrsRespVO> getIotProjectTaskAttrs(@RequestParam("id") Long id) {
        IotProjectTaskAttrsDO iotProjectTaskAttrs = iotProjectTaskAttrsService.getIotProjectTaskAttrs(id);
        return success(BeanUtils.toBean(iotProjectTaskAttrs, IotProjectTaskAttrsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得项目任务扩展属性分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-project-task-attrs:query')")
    public CommonResult<PageResult<IotProjectTaskAttrsRespVO>> getIotProjectTaskAttrsPage(@Valid IotProjectTaskAttrsPageReqVO pageReqVO) {
        PageResult<IotProjectTaskAttrsDO> pageResult = iotProjectTaskAttrsService.getIotProjectTaskAttrsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotProjectTaskAttrsRespVO.class));
    }

    @GetMapping("/attrs")
    @Operation(summary = "获得项目任务扩展属性列表")
    @PreAuthorize("@ss.hasPermission('rq:iot-project-task-attrs:query')")
    public CommonResult<List<IotProjectTaskAttrsRespVO>> attrs(@Valid IotProjectTaskAttrsPageReqVO pageReqVO) {
        List<IotProjectTaskAttrsDO> pageResult = iotProjectTaskAttrsService.getIotProjectTaskAttrsList(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotProjectTaskAttrsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出项目任务扩展属性 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-project-task-attrs:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotProjectTaskAttrsExcel(@Valid IotProjectTaskAttrsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotProjectTaskAttrsDO> list = iotProjectTaskAttrsService.getIotProjectTaskAttrsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "项目任务扩展属性.xls", "数据", IotProjectTaskAttrsRespVO.class,
                        BeanUtils.toBean(list, IotProjectTaskAttrsRespVO.class));
    }

}