package com.gxwebsoft.app.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.app.entity.AppConfig;
import com.gxwebsoft.app.param.AppConfigParam;
import com.gxwebsoft.app.service.AppConfigService;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 应用配置表 Controller
 */
@Slf4j
@Tag(name = "应用配置管理")
@RestController
@RequestMapping("/api/app/app-config")
public class AppConfigController extends BaseController {

    @Resource
    private AppConfigService appConfigService;

    /**
     * 分页查询应用配置
     */
    @Operation(summary = "分页查询应用配置")
    @GetMapping("/page")
    public ApiResult<PageResult<AppConfig>> page(AppConfigParam param) {
        return success(new PageResult<>(appConfigService.page(param)));
    }

    /**
     * 获取应用配置列表
     */
    @Operation(summary = "获取应用配置列表")
    @GetMapping()
    public ApiResult<List<AppConfig>> list(AppConfigParam param) {
        return success(appConfigService.list(param));
    }

    /**
     * 根据应用ID获取配置映射
     */
    @Operation(summary = "根据应用ID获取配置映射")
    @GetMapping("/map/{websiteId}")
    public ApiResult<Map<String, Object>> getConfigsByWebsiteId(@PathVariable Integer websiteId) {
        return success(appConfigService.getConfigsByWebsiteId(websiteId));
    }

    /**
     * 获取单个配置值
     */
    @Operation(summary = "获取单个配置值")
    @GetMapping("/value")
    public ApiResult<String> getConfigValue(@RequestParam Integer websiteId, @RequestParam String configKey) {
        return success(appConfigService.getConfigValue(websiteId, configKey),null);
    }

    /**
     * 保存配置
     */
    @Operation(summary = "保存配置")
    @PostMapping()
    public ApiResult<?> save(@RequestBody AppConfig config) {
        appConfigService.saveConfig(config);
        return success("保存成功");
    }

    /**
     * 批量保存配置
     */
    @Operation(summary = "批量保存配置")
    @PostMapping("/batch")
    public ApiResult<?> batchSave(@RequestBody BatchSaveRequest request) {
        appConfigService.batchSaveConfig(request.getWebsiteId(), request.getConfigs());
        return success("保存成功");
    }

    /**
     * 更新配置
     */
    @Operation(summary = "更新配置")
    @PutMapping()
    public ApiResult<?> update(@RequestBody AppConfig config) {
        appConfigService.updateConfig(config);
        return success("更新成功");
    }

    /**
     * 删除配置
     */
    @Operation(summary = "删除配置")
    @DeleteMapping("/{configId}")
    public ApiResult<?> delete(@PathVariable Integer configId) {
        appConfigService.deleteConfig(configId);
        return success("删除成功");
    }

    /**
     * 批量保存请求对象
     */
    public static class BatchSaveRequest {
        private Integer websiteId;
        private List<AppConfig> configs;

        public Integer getWebsiteId() {
            return websiteId;
        }

        public void setWebsiteId(Integer websiteId) {
            this.websiteId = websiteId;
        }

        public List<AppConfig> getConfigs() {
            return configs;
        }

        public void setConfigs(List<AppConfig> configs) {
            this.configs = configs;
        }
    }
}
