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

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.spxx.vo.*;
import com.unis.module.coding.dal.dataobject.spxx.SpxxDO;
import com.unis.module.coding.service.spxx.SpxxService;

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

    @Resource
    private SpxxService spxxService;

    @PostMapping("/create")
    @Operation(summary = "创建视频信息")
    @PreAuthorize("@ss.hasPermission('delivery:spxx:create')")
    public CommonResult<Long> createSpxx(@Valid @RequestBody SpxxSaveReqVO createReqVO) {
        return success(spxxService.createSpxx(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新视频信息")
    @PreAuthorize("@ss.hasPermission('delivery:spxx:update')")
    public CommonResult<Boolean> updateSpxx(@Valid @RequestBody SpxxSaveReqVO updateReqVO) {
        spxxService.updateSpxx(updateReqVO);
        return success(true);
    }

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

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

    @GetMapping("/page")
    @Operation(summary = "获得视频信息分页")
    @PreAuthorize("@ss.hasPermission('delivery:spxx:query')")
    public CommonResult<PageResult<SpxxRespVO>> getSpxxPage(@Valid SpxxPageReqVO pageReqVO) {
        PageResult<SpxxDO> pageResult = spxxService.getSpxxPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SpxxRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出视频信息 Excel")
    @PreAuthorize("@ss.hasPermission('delivery:spxx:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSpxxExcel(@Valid SpxxPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SpxxDO> list = spxxService.getSpxxPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "视频信息.xls", "数据", SpxxRespVO.class,
                        BeanUtils.toBean(list, SpxxRespVO.class));
    }

}