package com.njcn.relational.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.relational.pojo.po.SyncTableConfig;
import com.njcn.relational.pojo.vo.HttpResult;
import com.njcn.relational.service.SyncTableConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 同步表配置控制器
 *
 * @author cdf
 * @date 2026/6/28
 */
@RestController
@RequestMapping("/sync-table-config")
@RequiredArgsConstructor
public class SyncTableConfigController {


    private final SyncTableConfigService syncTableConfigService;

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public HttpResult<IPage<SyncTableConfig>> page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String tableName,
            @RequestParam(required = false) String syncMode,
            @RequestParam(required = false) Integer enabled) {

        Page<SyncTableConfig> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SyncTableConfig> wrapper = new LambdaQueryWrapper<>();

        if (tableName != null && !tableName.isEmpty()) {
            wrapper.like(SyncTableConfig::getTableName, tableName);
        }
        if (syncMode != null && !syncMode.isEmpty()) {
            wrapper.eq(SyncTableConfig::getSyncMode, syncMode);
        }
        if (enabled != null) {
            wrapper.eq(SyncTableConfig::getEnabled, enabled);
        }

        wrapper.orderByAsc(SyncTableConfig::getSortOrder);

        IPage<SyncTableConfig> result = syncTableConfigService.page(page, wrapper);
        return HttpResult.success(result);
    }

    /**
     * 查询所有启用的配置
     */
    @GetMapping("/list")
    public HttpResult<List<SyncTableConfig>> list() {
        LambdaQueryWrapper<SyncTableConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SyncTableConfig::getEnabled, 1)
                .orderByAsc(SyncTableConfig::getSortOrder);
        return HttpResult.success(syncTableConfigService.list(wrapper));
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/{tableName}")
    public HttpResult<SyncTableConfig> getById(@PathVariable String tableName) {
        return HttpResult.success(syncTableConfigService.getById(tableName));
    }

    /**
     * 新增
     */
    @PostMapping
    public HttpResult<Boolean> add(@RequestBody SyncTableConfig config) {
        return HttpResult.success(syncTableConfigService.save(config));
    }

    /**
     * 更新
     */
    @PutMapping
    public HttpResult<Boolean> update(@RequestBody SyncTableConfig config) {
        return HttpResult.success(syncTableConfigService.updateById(config));
    }

    /**
     * 删除
     */
    @DeleteMapping("/{tableName}")
    public HttpResult<Boolean> delete(@PathVariable String tableName) {
        return HttpResult.success(syncTableConfigService.removeById(tableName));
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/batch")
    public HttpResult<Boolean> deleteBatch(@RequestBody List<String> tableNames) {
        return HttpResult.success(syncTableConfigService.removeByIds(tableNames));
    }

    /**
     * 更新启用状态
     */
    @PutMapping("/status/{tableName}")
    public HttpResult<Boolean> updateStatus(@PathVariable String tableName, @RequestParam Integer enabled) {
        SyncTableConfig config = new SyncTableConfig();
        config.setTableName(tableName);
        config.setEnabled(enabled);
        return HttpResult.success(syncTableConfigService.updateById(config));
    }
}