package com.playlet.web.controller.system;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.playlet.common.annotation.Log;
import com.playlet.common.enums.BusinessType;
import com.playlet.system.domain.PlayletConfig;
import com.playlet.system.service.IPlayletConfigService;
import com.playlet.common.core.controller.BaseController;
import com.playlet.common.core.domain.AjaxResult;
import com.playlet.common.utils.poi.ExcelUtil;
import com.playlet.common.core.page.TableDataInfo;

/**
 * 全局配置Controller
 *
 * @author ruoyi
 * @date 2024-06-04
 */
@Controller
@RequestMapping("/system/playlet/config")
public class PlayletConfigController extends BaseController
{
    private String prefix = "system/playlet/config";

    @Autowired
    private IPlayletConfigService playletConfigService;

    @RequiresPermissions("playlet:config:view")
    @GetMapping()
    public String config()
    {
        return prefix + "/config";
    }

    /**
     * 查询全局配置列表
     */
    @RequiresPermissions("playlet:config:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PlayletConfig playletConfig)
    {
        startPage();
        List<PlayletConfig> list = playletConfigService.selectPlayletConfigList(playletConfig);
        return getDataTable(list);
    }

    /**
     * 导出全局配置列表
     */
    @RequiresPermissions("playlet:config:export")
    @Log(title = "全局配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PlayletConfig playletConfig)
    {
        List<PlayletConfig> list = playletConfigService.selectPlayletConfigList(playletConfig);
        ExcelUtil<PlayletConfig> util = new ExcelUtil<PlayletConfig>(PlayletConfig.class);
        return util.exportExcel(list, "全局配置数据");
    }

    /**
     * 新增全局配置
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存全局配置
     */
    @RequiresPermissions("playlet:config:add")
    @Log(title = "全局配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PlayletConfig playletConfig)
    {
        return toAjax(playletConfigService.insertPlayletConfig(playletConfig));
    }

    /**
     * 修改全局配置
     */
    @RequiresPermissions("playlet:config:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        PlayletConfig playletConfig = playletConfigService.selectPlayletConfigById(id);
        mmap.put("playletConfig", playletConfig);
        return prefix + "/edit";
    }

    /**
     * 修改保存全局配置
     */
    @RequiresPermissions("playlet:config:edit")
    @Log(title = "全局配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PlayletConfig playletConfig)
    {
        return toAjax(playletConfigService.updatePlayletConfig(playletConfig));
    }

    /**
     * 删除全局配置
     */
    @RequiresPermissions("playlet:config:remove")
    @Log(title = "全局配置", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(playletConfigService.deletePlayletConfigByIds(ids));
    }
}
