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

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.expertinfo.vo.*;
import com.cy.guoyan.admin.module.guoyan.dal.dataobject.expertinfo.ExpertInfoDO;
import com.cy.guoyan.admin.module.guoyan.service.expertinfo.ExpertInfoService;

@Tag(name = "管理后台 - 专家信息")
@RestController
@RequestMapping("/guoyan/expert-info")
@Validated
public class ExpertInfoController {

    @Resource
    private ExpertInfoService expertInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建专家信息")
    @PreAuthorize("@ss.hasPermission('guoyan:expert-info:create')")
    public CommonResult<Long> createExpertInfo(@Valid @RequestBody ExpertInfoSaveReqVO createReqVO) {
        return success(expertInfoService.createExpertInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新专家信息")
    @PreAuthorize("@ss.hasPermission('guoyan:expert-info:update')")
    public CommonResult<Boolean> updateExpertInfo(@Valid @RequestBody ExpertInfoSaveReqVO updateReqVO) {
        expertInfoService.updateExpertInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除专家信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('guoyan:expert-info:delete')")
    public CommonResult<Boolean> deleteExpertInfo(@RequestParam("id") Long id) {
        expertInfoService.deleteExpertInfo(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除专家信息")
                @PreAuthorize("@ss.hasPermission('guoyan:expert-info:delete')")
    public CommonResult<Boolean> deleteExpertInfoList(@RequestParam("ids") List<Long> ids) {
        expertInfoService.deleteExpertInfoListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得专家信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('guoyan:expert-info:query')")
    public CommonResult<ExpertInfoRespVO> getExpertInfo(@RequestParam("id") Long id) {
        ExpertInfoDO expertInfo = expertInfoService.getExpertInfo(id);
        return success(BeanUtils.toBean(expertInfo, ExpertInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得专家信息分页")
    @PreAuthorize("@ss.hasPermission('guoyan:expert-info:query')")
    public CommonResult<PageResult<ExpertInfoRespVO>> getExpertInfoPage(@Valid ExpertInfoPageReqVO pageReqVO) {
        PageResult<ExpertInfoDO> pageResult = expertInfoService.getExpertInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ExpertInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出专家信息 Excel")
    @PreAuthorize("@ss.hasPermission('guoyan:expert-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportExpertInfoExcel(@Valid ExpertInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ExpertInfoDO> list = expertInfoService.getExpertInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "专家信息.xls", "数据", ExpertInfoRespVO.class,
                        BeanUtils.toBean(list, ExpertInfoRespVO.class));
    }

}