package com.trs.digital.module.manager.controller.admin.errorwold;

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.trs.digital.framework.common.pojo.PageParam;
import com.trs.digital.framework.common.pojo.PageResult;
import com.trs.digital.framework.common.pojo.CommonResult;
import com.trs.digital.framework.common.util.object.BeanUtils;
import static com.trs.digital.framework.common.pojo.CommonResult.success;

import com.trs.digital.framework.excel.core.util.ExcelUtils;

import com.trs.digital.framework.operatelog.core.annotations.OperateLog;
import static com.trs.digital.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.trs.digital.module.manager.controller.admin.errorwold.vo.*;
import com.trs.digital.module.manager.dal.dataobject.errorwold.ErrorwoldDO;
import com.trs.digital.module.manager.service.errorwold.ErrorwoldService;

@Tag(name = "管理后台 - 搜索词纠错")
@RestController
@RequestMapping("/manager/errorwold")
@Validated
public class ErrorwoldController {

    @Resource
    private ErrorwoldService errorwoldService;

    @PostMapping("/create")
    @Operation(summary = "创建搜索词纠错")
    @PreAuthorize("@ss.hasPermission('manager:errorwold:create')")
    public CommonResult<Long> createErrorwold(@Valid @RequestBody ErrorwoldSaveReqVO createReqVO) {
        return success(errorwoldService.createErrorwold(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新搜索词纠错")
    @PreAuthorize("@ss.hasPermission('manager:errorwold:update')")
    public CommonResult<Boolean> updateErrorwold(@Valid @RequestBody ErrorwoldSaveReqVO updateReqVO) {
        errorwoldService.updateErrorwold(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除搜索词纠错")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('manager:errorwold:delete')")
    public CommonResult<Boolean> deleteErrorwold(@RequestParam("id") Long id) {
        errorwoldService.deleteErrorwold(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得搜索词纠错")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('manager:errorwold:query')")
    public CommonResult<ErrorwoldDO> getErrorwold(@RequestParam("id") Long id) {
        ErrorwoldDO errorwold = errorwoldService.getErrorwold(id);
        return success(errorwold);
    }

    @GetMapping("/page")
    @Operation(summary = "获得搜索词纠错分页")
    @PreAuthorize("@ss.hasPermission('manager:errorwold:query')")
    public CommonResult<PageResult<ErrorwoldRespVO>> getErrorwoldPage(@Valid ErrorwoldPageReqVO pageReqVO) {
        PageResult<ErrorwoldDO> pageResult = errorwoldService.getErrorwoldPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ErrorwoldRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出搜索词纠错 Excel")
    @PreAuthorize("@ss.hasPermission('manager:errorwold:export')")
    @OperateLog(type = EXPORT)
    public void exportErrorwoldExcel(@Valid ErrorwoldPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErrorwoldDO> list = errorwoldService.getErrorwoldPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "搜索词纠错.xls", "数据", ErrorwoldRespVO.class,
                        BeanUtils.toBean(list, ErrorwoldRespVO.class));
    }

}