package cn.iocoder.yudao.module.smartfactory.controller.admin.factorydataoverview;

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.smartfactory.controller.admin.factorydataoverview.vo.FactoryDataOverviewPageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.factorydataoverview.vo.FactoryDataOverviewRespVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.factorydataoverview.vo.FactoryDataOverviewSaveReqVO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.factorydataoverview.FactoryDataOverviewDO;
import cn.iocoder.yudao.module.smartfactory.service.factorydataoverview.FactoryDataOverviewService;
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.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 工厂数据总览")
@RestController
@RequestMapping("/factory/data-overview")
@Validated
public class FactoryDataOverviewController {

    @Resource
    private FactoryDataOverviewService dataOverviewService;

    @PostMapping("/create")
    @Operation(summary = "创建工厂数据总览")
    @PreAuthorize("@ss.hasPermission('factory:data-overview:create')")
    public CommonResult<Long> createDataOverview(@Valid @RequestBody FactoryDataOverviewSaveReqVO createReqVO) {
        return success(dataOverviewService.createDataOverview(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工厂数据总览")
    @PreAuthorize("@ss.hasPermission('factory:data-overview:update')")
    public CommonResult<Boolean> updateDataOverview(@Valid @RequestBody FactoryDataOverviewSaveReqVO updateReqVO) {
        dataOverviewService.updateDataOverview(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工厂数据总览")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('factory:data-overview:delete')")
    public CommonResult<Boolean> deleteDataOverview(@RequestParam("id") Long id) {
        dataOverviewService.deleteDataOverview(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工厂数据总览")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('factory:data-overview:query')")
    public CommonResult<FactoryDataOverviewRespVO> getDataOverview(@RequestParam("id") Long id) {
        FactoryDataOverviewDO dataOverview = dataOverviewService.getDataOverview(id);
        return success(BeanUtils.toBean(dataOverview, FactoryDataOverviewRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工厂数据总览分页")
    @PreAuthorize("@ss.hasPermission('factory:data-overview:query')")
    public CommonResult<PageResult<FactoryDataOverviewRespVO>> getDataOverviewPage(@Valid FactoryDataOverviewPageReqVO pageReqVO) {
        PageResult<FactoryDataOverviewDO> pageResult = dataOverviewService.getDataOverviewPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FactoryDataOverviewRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工厂数据总览 Excel")
    @PreAuthorize("@ss.hasPermission('factory:data-overview:export')")
    @OperateLog(type = EXPORT)
    public void exportDataOverviewExcel(@Valid FactoryDataOverviewPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FactoryDataOverviewDO> list = dataOverviewService.getDataOverviewPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "工厂数据总览.xls", "数据", FactoryDataOverviewRespVO.class,
                        BeanUtils.toBean(list, FactoryDataOverviewRespVO.class));
    }

}