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

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.screenfactoryextra.vo.ScreenFactoryExtraPageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.screenfactoryextra.vo.ScreenFactoryExtraRespVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.screenfactoryextra.vo.ScreenFactoryExtraSaveReqVO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.screenfactoryextra.ScreenFactoryExtraDO;
import cn.iocoder.yudao.module.smartfactory.service.screenfactoryextra.ScreenFactoryExtraService;
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/screen-factory-extra")
@Validated
public class ScreenFactoryExtraController {

    @Resource
    private ScreenFactoryExtraService screenFactoryExtraService;

    @PostMapping("/create")
    @Operation(summary = "创建工厂大屏额外字段")
    @PreAuthorize("@ss.hasPermission('factory:screen-factory-extra:create')")
    public CommonResult<Long> createScreenFactoryExtra(@Valid @RequestBody ScreenFactoryExtraSaveReqVO createReqVO) {
        return success(screenFactoryExtraService.createScreenFactoryExtra(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工厂大屏额外字段")
    @PreAuthorize("@ss.hasPermission('factory:screen-factory-extra:update')")
    public CommonResult<Boolean> updateScreenFactoryExtra(@Valid @RequestBody ScreenFactoryExtraSaveReqVO updateReqVO) {
        screenFactoryExtraService.updateScreenFactoryExtra(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工厂大屏额外字段")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('factory:screen-factory-extra:delete')")
    public CommonResult<Boolean> deleteScreenFactoryExtra(@RequestParam("id") Long id) {
        screenFactoryExtraService.deleteScreenFactoryExtra(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工厂大屏额外字段")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('factory:screen-factory-extra:query')")
    public CommonResult<ScreenFactoryExtraRespVO> getScreenFactoryExtra(@RequestParam("id") Long id) {
        ScreenFactoryExtraDO screenFactoryExtra = screenFactoryExtraService.getScreenFactoryExtra(id);
        return success(BeanUtils.toBean(screenFactoryExtra, ScreenFactoryExtraRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工厂大屏额外字段分页")
    @PreAuthorize("@ss.hasPermission('factory:screen-factory-extra:query')")
    public CommonResult<PageResult<ScreenFactoryExtraRespVO>> getScreenFactoryExtraPage(@Valid ScreenFactoryExtraPageReqVO pageReqVO) {
        PageResult<ScreenFactoryExtraDO> pageResult = screenFactoryExtraService.getScreenFactoryExtraPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ScreenFactoryExtraRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工厂大屏额外字段 Excel")
    @PreAuthorize("@ss.hasPermission('factory:screen-factory-extra:export')")
    @OperateLog(type = EXPORT)
    public void exportScreenFactoryExtraExcel(@Valid ScreenFactoryExtraPageReqVO pageReqVO,
                                              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ScreenFactoryExtraDO> list = screenFactoryExtraService.getScreenFactoryExtraPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "工厂大屏额外字段.xls", "数据", ScreenFactoryExtraRespVO.class,
                BeanUtils.toBean(list, ScreenFactoryExtraRespVO.class));
    }

}