package com.ruoyi.base.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.base.domain.BaseConfig;
import com.ruoyi.base.service.IBaseConfigService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 基础配置-账户配置、小程序配置的内容Controller
 * 
 * @author vinjor-m
 * @date 2025-03-17
 */
@RestController
@RequestMapping("/base/config")
public class BaseConfigController extends BaseController
{
    @Autowired
    private IBaseConfigService baseConfigService;

    /**
     * 查询基础配置-账户配置、小程序配置的内容列表
     */
    @PreAuthorize("@ss.hasPermi('base:config:list')")
    @GetMapping("/list")
    public AjaxResult list(BaseConfig baseConfig,
                                @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize)
    {
        Page<BaseConfig> page = new Page<>(pageNum, pageSize);
        IPage<BaseConfig> list = baseConfigService.queryListPage(baseConfig,page);
        return success(list);
    }

    /**
     * 导出基础配置-账户配置、小程序配置的内容列表
     */
    @PreAuthorize("@ss.hasPermi('base:config:export')")
    @Log(title = "基础配置-账户配置、小程序配置的内容", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseConfig baseConfig)
    {
        List<BaseConfig> list = baseConfigService.list();
        ExcelUtil<BaseConfig> util = new ExcelUtil<BaseConfig>(BaseConfig.class);
        util.exportExcel(response, list, "基础配置-账户配置、小程序配置的内容数据");
    }

    /**
     * 获取基础配置-账户配置、小程序配置的内容详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:config:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(baseConfigService.getById(id));
    }

    /**
     * 新增基础配置-账户配置、小程序配置的内容
     */
    @PreAuthorize("@ss.hasPermi('base:config:add')")
    @Log(title = "基础配置-账户配置、小程序配置的内容", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseConfig baseConfig)
    {
        return toAjax(baseConfigService.save(baseConfig));
    }

    /**
     * 修改基础配置-账户配置、小程序配置的内容
     */
    @PreAuthorize("@ss.hasPermi('base:config:edit')")
    @Log(title = "基础配置-账户配置、小程序配置的内容", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseConfig baseConfig)
    {
        return toAjax(baseConfigService.updateById(baseConfig));
    }

    /**
     * 删除基础配置-账户配置、小程序配置的内容
     */
    @PreAuthorize("@ss.hasPermi('base:config:remove')")
    @Log(title = "基础配置-账户配置、小程序配置的内容", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        List<String> list = new ArrayList<>(Arrays.asList(ids));
        return toAjax(baseConfigService.removeByIds(list));
    }
    /**
     * 获取基础配置-账户配置、小程序配置的内容详细信息-根据code获取
     */
    @GetMapping(value = "/getConfigByCode")
    public AjaxResult getConfigByCode(String code)
    {
        LambdaQueryWrapper<BaseConfig> queryWrapper = new LambdaQueryWrapper<BaseConfig>().eq(BaseConfig::getCode,code);
        List<BaseConfig> list = baseConfigService.list(queryWrapper);
        if(list.isEmpty()){
            error("未查询到相关配置！");
        }
        return AjaxResult.ok(list.get(0).getJsonStr());
    }

}
