package com.cy.guoyan.admin.module.guoyan.controller.admin.researchtopicinfo;

import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
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.cy.guoyan.admin.framework.common.pojo.PageParam;
import com.cy.guoyan.admin.framework.common.pojo.PageResult;
import com.cy.guoyan.admin.framework.common.pojo.CommonResult;
import com.cy.guoyan.admin.framework.common.util.object.BeanUtils;
import static com.cy.guoyan.admin.framework.common.pojo.CommonResult.success;

import com.cy.guoyan.admin.framework.excel.core.util.ExcelUtils;

import com.cy.guoyan.admin.framework.apilog.core.annotation.ApiAccessLog;
import static com.cy.guoyan.admin.framework.apilog.core.enums.OperateTypeEnum.*;

import com.cy.guoyan.admin.module.guoyan.controller.admin.researchtopicinfo.vo.*;
import com.cy.guoyan.admin.module.guoyan.dal.dataobject.researchtopicinfo.ResearchTopicInfoDO;
import com.cy.guoyan.admin.module.guoyan.service.researchtopicinfo.ResearchTopicInfoService;

@Tag(name = "管理后台 - 国资研究专题信息")
@RestController
@RequestMapping("/guoyan/research-topic-info")
@Validated
public class ResearchTopicInfoController {

    @Resource
    private ResearchTopicInfoService researchTopicInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建国资研究专题信息")
    @PreAuthorize("@ss.hasPermission('guoyan:research-topic-info:create')")
    public CommonResult<Long> createResearchTopicInfo(@Valid @RequestBody ResearchTopicInfoSaveReqVO createReqVO) {
        return success(researchTopicInfoService.createResearchTopicInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新国资研究专题信息")
    @PreAuthorize("@ss.hasPermission('guoyan:research-topic-info:update')")
    public CommonResult<Boolean> updateResearchTopicInfo(@Valid @RequestBody ResearchTopicInfoSaveReqVO updateReqVO) {
        researchTopicInfoService.updateResearchTopicInfo(updateReqVO);
        return success(true);
    }

    @PutMapping("/audit")
    @Operation(summary = "审核国资研究专题信息")
    @PreAuthorize("@ss.hasPermission('guoyan:research-topic-info:update')")
    public CommonResult<Boolean> auditResearchTopicInfo(@RequestBody ResearchTopicInfoSaveReqVO updateReqVO) {
        researchTopicInfoService.auditResearchTopicInfo(updateReqVO.getId(), updateReqVO.getAuditStatus());
        return success(true);
    }

    @PutMapping("/void")
    @Operation(summary = "作废国资研究专题信息")
    @PreAuthorize("@ss.hasPermission('guoyan:research-topic-info:update')")
    public CommonResult<Boolean> voidResearchTopicInfo(@RequestBody ResearchTopicInfoSaveReqVO updateReqVO) {
        researchTopicInfoService.voidResearchTopicInfo(updateReqVO.getId(), updateReqVO.getStatus());
        return success(true);
    }


    @DeleteMapping("/delete")
    @Operation(summary = "删除国资研究专题信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('guoyan:research-topic-info:delete')")
    public CommonResult<Boolean> deleteResearchTopicInfo(@RequestParam("id") Long id) {
        researchTopicInfoService.deleteResearchTopicInfo(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除国资研究专题信息")
                @PreAuthorize("@ss.hasPermission('guoyan:research-topic-info:delete')")
    public CommonResult<Boolean> deleteResearchTopicInfoList(@RequestParam("ids") List<Long> ids) {
        researchTopicInfoService.deleteResearchTopicInfoListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得国资研究专题信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('guoyan:research-topic-info:query')")
    public CommonResult<ResearchTopicInfoRespVO> getResearchTopicInfo(@RequestParam("id") Long id) {
        ResearchTopicInfoRespVO researchTopicInfo = researchTopicInfoService.getResearchTopicInfo(id);
        return success(BeanUtils.toBean(researchTopicInfo, ResearchTopicInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得国资研究专题信息分页")
    @PreAuthorize("@ss.hasPermission('guoyan:research-topic-info:query')")
    public CommonResult<PageResult<ResearchTopicInfoRespVO>> getResearchTopicInfoPage(@Valid ResearchTopicInfoPageReqVO pageReqVO) {
        PageResult<ResearchTopicInfoDO> pageResult = researchTopicInfoService.getResearchTopicInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ResearchTopicInfoRespVO.class));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出国资研究专题信息 Excel")
    @PreAuthorize("@ss.hasPermission('guoyan:research-topic-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportResearchTopicInfoExcel(@Valid ResearchTopicInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ResearchTopicInfoDO> list = researchTopicInfoService.getResearchTopicInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "国资研究专题信息.xls", "数据", ResearchTopicInfoRespVO.class,
                        BeanUtils.toBean(list, ResearchTopicInfoRespVO.class));
    }

}