package com.tashow.cloud.infra.controller.admin.config;

import static com.tashow.cloud.common.exception.util.ServiceExceptionUtil.exception;
import static com.tashow.cloud.common.pojo.CommonResult.success;
import static com.tashow.cloud.web.apilog.core.enums.OperateTypeEnum.EXPORT;

import com.tashow.cloud.common.pojo.CommonResult;
import com.tashow.cloud.common.pojo.PageParam;
import com.tashow.cloud.common.pojo.PageResult;
import com.tashow.cloud.excel.excel.core.util.ExcelUtils;
import com.tashow.cloud.infra.controller.admin.config.vo.ConfigPageReqVO;
import com.tashow.cloud.infra.controller.admin.config.vo.ConfigRespVO;
import com.tashow.cloud.infra.controller.admin.config.vo.ConfigSaveReqVO;
import com.tashow.cloud.infra.convert.config.ConfigConvert;
import com.tashow.cloud.infra.dal.dataobject.config.ConfigDO;
import com.tashow.cloud.infra.service.config.ConfigService;
import com.tashow.cloud.infraapi.enums.ErrorCodeConstants;
import com.tashow.cloud.web.apilog.core.annotation.ApiAccessLog;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import java.io.IOException;
import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/** 管理后台 - 参数配置 */
@RestController
@RequestMapping("/infra/config")
@Validated
public class ConfigController {

  @Resource private ConfigService configService;

  /** 创建参数配置 */
  @PostMapping("/create")
  @PreAuthorize("@ss.hasPermission('infra:config:create')")
  public CommonResult<Long> createConfig(@Valid @RequestBody ConfigSaveReqVO createReqVO) {
    return success(configService.createConfig(createReqVO));
  }

  /** 修改参数配置 */
  @PutMapping("/update")
  @PreAuthorize("@ss.hasPermission('infra:config:update')")
  public CommonResult<Boolean> updateConfig(@Valid @RequestBody ConfigSaveReqVO updateReqVO) {
    configService.updateConfig(updateReqVO);
    return success(true);
  }

  /** 删除参数配置 */
  @DeleteMapping("/delete")
  @PreAuthorize("@ss.hasPermission('infra:config:delete')")
  public CommonResult<Boolean> deleteConfig(@RequestParam("id") Long id) {
    configService.deleteConfig(id);
    return success(true);
  }

  /** 获得参数配置 */
  @GetMapping(value = "/get")
  @PreAuthorize("@ss.hasPermission('infra:config:query')")
  public CommonResult<ConfigRespVO> getConfig(@RequestParam("id") Long id) {
    return success(ConfigConvert.INSTANCE.convert(configService.getConfig(id)));
  }

  /** 根据参数键名查询参数值", description = "不可见的配置，不允许返回给前端 */
  @GetMapping(value = "/get-value-by-key")
  public CommonResult<String> getConfigKey(@RequestParam("key") String key) {
    ConfigDO config = configService.getConfigByKey(key);
    if (config == null) {
      return success(null);
    }
    if (!config.getVisible()) {
      throw exception(ErrorCodeConstants.CONFIG_GET_VALUE_ERROR_IF_VISIBLE);
    }
    return success(config.getValue());
  }

  /** 获取参数配置分页 */
  @GetMapping("/page")
  @PreAuthorize("@ss.hasPermission('infra:config:query')")
  public CommonResult<PageResult<ConfigRespVO>> getConfigPage(@Valid ConfigPageReqVO pageReqVO) {
    PageResult<ConfigDO> page = configService.getConfigPage(pageReqVO);
    return success(ConfigConvert.INSTANCE.convertPage(page));
  }

  /** 导出参数配置 */
  @GetMapping("/export")
  @PreAuthorize("@ss.hasPermission('infra:config:export')")
  @ApiAccessLog(operateType = EXPORT)
  public void exportConfig(ConfigPageReqVO exportReqVO, HttpServletResponse response)
      throws IOException {
    exportReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
    List<ConfigDO> list = configService.getConfigPage(exportReqVO).getList();
    // 输出
    ExcelUtils.write(
        response, "参数配置.xls", "数据", ConfigRespVO.class, ConfigConvert.INSTANCE.convertList(list));
  }
}
