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

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.fmxx.vo.*;
import com.unis.module.coding.dal.dataobject.fmxx.FmxxDO;
import com.unis.module.coding.service.fmxx.FmxxService;

@Tag(name = "管理后台 - 赋码结果信息")
@RestController
@RequestMapping("/coding/fmxx")
@Validated
public class FmxxController {

    @Resource
    private FmxxService fmxxService;

    @PostMapping("/create")
    @Operation(summary = "创建赋码结果信息")
    @PreAuthorize("@ss.hasPermission('coding:fmxx:create')")
    public CommonResult<Long> createFmxx(@Valid @RequestBody FmxxSaveReqVO createReqVO) {
        return success(fmxxService.createFmxx(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新赋码结果信息")
    @PreAuthorize("@ss.hasPermission('coding:fmxx:update')")
    public CommonResult<Boolean> updateFmxx(@Valid @RequestBody FmxxSaveReqVO updateReqVO) {
        fmxxService.updateFmxx(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除赋码结果信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('coding:fmxx:delete')")
    public CommonResult<Boolean> deleteFmxx(@RequestParam("id") Long id) {
        fmxxService.deleteFmxx(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得赋码结果信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('coding:fmxx:query')")
    public CommonResult<FmxxRespVO> getFmxx(@RequestParam("id") Long id) {
        FmxxDO fmxx = fmxxService.getFmxx(id);
        return success(BeanUtils.toBean(fmxx, FmxxRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得赋码结果信息分页")
    @PreAuthorize("@ss.hasPermission('coding:fmxx:query')")
    public CommonResult<PageResult<FmxxRespVO>> getFmxxPage(@Valid FmxxPageReqVO pageReqVO) {
        PageResult<FmxxDO> pageResult = fmxxService.getFmxxPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FmxxRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出赋码结果信息 Excel")
    @PreAuthorize("@ss.hasPermission('coding:fmxx:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFmxxExcel(@Valid FmxxPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FmxxDO> list = fmxxService.getFmxxPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "赋码结果信息.xls", "数据", FmxxRespVO.class,
                        BeanUtils.toBean(list, FmxxRespVO.class));
    }

}