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

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.iotdailyreportattrs.vo.IotDailyReportAttrsPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdailyreportattrs.vo.IotDailyReportAttrsRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdailyreportattrs.vo.IotDailyReportAttrsSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotdailyreportattrs.IotDailyReportAttrsDO;
import cn.iocoder.yudao.module.pms.service.iotdailyreportattrs.IotDailyReportAttrsService;
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-daily-report-attrs")
@Validated
public class IotDailyReportAttrsController {

    @Resource
    private IotDailyReportAttrsService iotDailyReportAttrsService;

    @PostMapping("/create")
    @Operation(summary = "创建日报扩展模板属性")
    @PreAuthorize("@ss.hasPermission('rq:iot-daily-report-attrs:create')")
    public CommonResult<Long> createIotDailyReportAttrs(@Valid @RequestBody IotDailyReportAttrsSaveReqVO createReqVO) {
        return success(iotDailyReportAttrsService.createIotDailyReportAttrs(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新日报扩展模板属性")
    @PreAuthorize("@ss.hasPermission('rq:iot-daily-report-attrs:update')")
    public CommonResult<Boolean> updateIotDailyReportAttrs(@Valid @RequestBody IotDailyReportAttrsSaveReqVO updateReqVO) {
        iotDailyReportAttrsService.updateIotDailyReportAttrs(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除日报扩展模板属性")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-daily-report-attrs:delete')")
    public CommonResult<Boolean> deleteIotDailyReportAttrs(@RequestParam("id") Long id) {
        iotDailyReportAttrsService.deleteIotDailyReportAttrs(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得日报扩展模板属性")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-daily-report-attrs:query')")
    public CommonResult<IotDailyReportAttrsRespVO> getIotDailyReportAttrs(@RequestParam("id") Long id) {
        IotDailyReportAttrsDO iotDailyReportAttrs = iotDailyReportAttrsService.getIotDailyReportAttrs(id);
        return success(BeanUtils.toBean(iotDailyReportAttrs, IotDailyReportAttrsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得日报扩展模板属性分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-daily-report-attrs:query')")
    public CommonResult<PageResult<IotDailyReportAttrsRespVO>> getIotDailyReportAttrsPage(@Valid IotDailyReportAttrsPageReqVO pageReqVO) {
        PageResult<IotDailyReportAttrsDO> pageResult = iotDailyReportAttrsService.getIotDailyReportAttrsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotDailyReportAttrsRespVO.class));
    }

    @GetMapping("/dailyReportAttrs")
    @Operation(summary = "获得日报扩展模板属性列表")
    @PreAuthorize("@ss.hasPermission('rq:iot-daily-report-attrs:query')")
    public CommonResult<List<IotDailyReportAttrsRespVO>> dailyReportAttrs(@Valid IotDailyReportAttrsPageReqVO pageReqVO) {
        List<IotDailyReportAttrsDO> dailyReportAttrs = iotDailyReportAttrsService.getIotDailyReportAttrs(pageReqVO);
        return success(BeanUtils.toBean(dailyReportAttrs, IotDailyReportAttrsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出日报扩展模板属性 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-daily-report-attrs:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotDailyReportAttrsExcel(@Valid IotDailyReportAttrsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotDailyReportAttrsDO> list = iotDailyReportAttrsService.getIotDailyReportAttrsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "日报扩展模板属性.xls", "数据", IotDailyReportAttrsRespVO.class,
                        BeanUtils.toBean(list, IotDailyReportAttrsRespVO.class));
    }

}