package com.kxs.system.biz.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kxs.common.core.constant.CacheConstants;
import com.kxs.common.core.util.R;
import com.kxs.common.log.annotation.SysLog;
import com.kxs.system.api.model.KxsInterfaceConfig;
import com.kxs.system.biz.service.KxsInterfaceConfigService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 通道配置控制器
 *
 * @author 没秃顶的码农
 * @date 2024-07-12
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("interfaceConfig")
public class SysInterfaceConfigController {


    private final KxsInterfaceConfigService interfaceConfigService;


    /**
     * 分页查询
     * @param page 分页对象
     * @param param 公共参数
     */
    @Operation(description = "分页查询", summary = "分页查询")
    @GetMapping("/page")
    public R getSysPublicParamPage(@ParameterObject Page<KxsInterfaceConfig> page, @ParameterObject KxsInterfaceConfig param) {

        LambdaUpdateWrapper<KxsInterfaceConfig> wrapper = Wrappers.<KxsInterfaceConfig>lambdaUpdate()
                .eq(StrUtil.isNotBlank(param.getName()), KxsInterfaceConfig::getName, param.getName())
                .eq(param.getParamType() != null, KxsInterfaceConfig::getParamType, param.getParamType())
                .eq(param.getStatus() != null, KxsInterfaceConfig::getStatus, param.getStatus());

        return R.ok(interfaceConfigService.page(page, wrapper));
    }

    /**
     * 通过id查询
     * @param id id
     * @return R
     */
    @Operation(description = "通过id查询公共参数", summary = "通过id查询")
    @GetMapping("/getById")
    @Cacheable(value = CacheConstants.INTERFACE_DETAILS, key = "#id", unless = "#result == null ")
    public R getById(@RequestParam("id") Long id) {

        return R.ok(interfaceConfigService.getById(id));
    }


    /**
     * 新增配置
     * @param param  参数
     * @return R
     */
    @Operation(description = "新增配置", summary = "新增配置")
    @SysLog("新增配置")
    @PostMapping("add")
    @PreAuthorize("@pms.hasPermission('sys_interface_add')")
    public R save(@RequestBody KxsInterfaceConfig param) {

        return R.ok(interfaceConfigService.save(param));
    }

    /**
     * 修改配置
     * @param param 配置
     * @return R
     */
    @Operation(description = "修改配置", summary = "修改配置")
    @SysLog("修改配置")
    @PutMapping("update")
    @PreAuthorize("@pms.hasPermission('sys_interface_edit')")
    @CacheEvict(value = CacheConstants.INTERFACE_DETAILS, key = "#param.id")
    public R updateById(@RequestBody KxsInterfaceConfig param) {

        return R.ok(interfaceConfigService.updateById(param));
    }


    /**
     * 通过id删除
     * @param id id
     * @return R
     */
    @Operation(description = "通过id删除", summary = "通过id删除")
    @SysLog("通过id删除")
    @DeleteMapping("removeById/{id}")
    @PreAuthorize("@pms.hasPermission('sys_interface_del')")
    @CacheEvict(value = CacheConstants.INTERFACE_DETAILS, allEntries = true)
    public R removeById(@PathVariable Long id) {

        return R.ok(interfaceConfigService.removeById(id));
    }

}
