package com.unis.module.coding.controller.admin.spqyxx;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.unis.framework.common.pojo.PageParam;
import com.unis.framework.common.pojo.PageResult;
import com.unis.framework.common.pojo.CommonResult;
import com.unis.framework.common.util.object.BeanUtils;
import static com.unis.framework.common.pojo.CommonResult.success;

import com.unis.framework.excel.core.util.ExcelUtils;

import com.unis.framework.apilog.core.annotation.ApiAccessLog;
import static com.unis.framework.apilog.core.enums.OperateTypeEnum.*;

import com.unis.module.coding.controller.admin.spqyxx.vo.*;
import com.unis.module.coding.dal.dataobject.spqyxx.SpqyxxDO;
import com.unis.module.coding.service.spqyxx.SpqyxxService;

@Tag(name = "管理后台 - 视频区域信息")
@RestController
@RequestMapping("/delivery/spqyxx")
@Validated
public class SpqyxxController {

    @Resource
    private SpqyxxService spqyxxService;

    @PostMapping("/create")
    @Operation(summary = "创建视频区域信息")
    @PreAuthorize("@ss.hasPermission('delivery:spqyxx:create')")
    public CommonResult<Long> createSpqyxx(@Valid @RequestBody SpqyxxSaveReqVO createReqVO) {
        return success(spqyxxService.createSpqyxx(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新视频区域信息")
    @PreAuthorize("@ss.hasPermission('delivery:spqyxx:update')")
    public CommonResult<Boolean> updateSpqyxx(@Valid @RequestBody SpqyxxSaveReqVO updateReqVO) {
        spqyxxService.updateSpqyxx(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除视频区域信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('delivery:spqyxx:delete')")
    public CommonResult<Boolean> deleteSpqyxx(@RequestParam("id") Long id) {
        spqyxxService.deleteSpqyxx(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得视频区域信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('delivery:spqyxx:query')")
    public CommonResult<SpqyxxRespVO> getSpqyxx(@RequestParam("id") Long id) {
        SpqyxxDO spqyxx = spqyxxService.getSpqyxx(id);
        return success(BeanUtils.toBean(spqyxx, SpqyxxRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得视频区域信息分页")
    @PreAuthorize("@ss.hasPermission('delivery:spqyxx:query')")
    public CommonResult<PageResult<SpqyxxRespVO>> getSpqyxxPage(@Valid SpqyxxPageReqVO pageReqVO) {
        PageResult<SpqyxxDO> pageResult = spqyxxService.getSpqyxxPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SpqyxxRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出视频区域信息 Excel")
    @PreAuthorize("@ss.hasPermission('delivery:spqyxx:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSpqyxxExcel(@Valid SpqyxxPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SpqyxxDO> list = spqyxxService.getSpqyxxPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "视频区域信息.xls", "数据", SpqyxxRespVO.class,
                        BeanUtils.toBean(list, SpqyxxRespVO.class));
    }

}