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

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.iotdailyreporttemplate.vo.IotDailyReportTemplatePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdailyreporttemplate.vo.IotDailyReportTemplateRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotdailyreporttemplate.vo.IotDailyReportTemplateSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotdailyreporttemplate.IotDailyReportTemplateDO;
import cn.iocoder.yudao.module.pms.service.iotdailyreporttemplate.IotDailyReportTemplateService;
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-template")
@Validated
public class IotDailyReportTemplateController {

    @Resource
    private IotDailyReportTemplateService iotDailyReportTemplateService;

    @PostMapping("/create")
    @Operation(summary = "创建生产日报属性模板")
    @PreAuthorize("@ss.hasPermission('rq:iot-daily-report-template:create')")
    public CommonResult<Long> createIotDailyReportTemplate(@Valid @RequestBody IotDailyReportTemplateSaveReqVO createReqVO) {
        return success(iotDailyReportTemplateService.createIotDailyReportTemplate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新生产日报属性模板")
    @PreAuthorize("@ss.hasPermission('rq:iot-daily-report-template:update')")
    public CommonResult<Boolean> updateIotDailyReportTemplate(@Valid @RequestBody IotDailyReportTemplateSaveReqVO updateReqVO) {
        iotDailyReportTemplateService.updateIotDailyReportTemplate(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得生产日报属性模板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-daily-report-template:query')")
    public CommonResult<IotDailyReportTemplateRespVO> getIotDailyReportTemplate(@RequestParam("id") Long id) {
        IotDailyReportTemplateDO iotDailyReportTemplate = iotDailyReportTemplateService.getIotDailyReportTemplate(id);
        return success(BeanUtils.toBean(iotDailyReportTemplate, IotDailyReportTemplateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得生产日报属性模板分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-daily-report-template:query')")
    public CommonResult<PageResult<IotDailyReportTemplateRespVO>> getIotDailyReportTemplatePage(@Valid IotDailyReportTemplatePageReqVO pageReqVO) {
        PageResult<IotDailyReportTemplateDO> pageResult = iotDailyReportTemplateService.getIotDailyReportTemplatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotDailyReportTemplateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出生产日报属性模板 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-daily-report-template:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotDailyReportTemplateExcel(@Valid IotDailyReportTemplatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotDailyReportTemplateDO> list = iotDailyReportTemplateService.getIotDailyReportTemplatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "生产日报属性模板.xls", "数据", IotDailyReportTemplateRespVO.class,
                        BeanUtils.toBean(list, IotDailyReportTemplateRespVO.class));
    }

}