package org.dromara.system.controller.xiake;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.domain.AjaxResult;
import org.dromara.system.domain.xiake.XiakeConfig;
import org.dromara.system.domain.xiake.bo.XiakeConfigBo;
import org.dromara.system.domain.xiake.vo.XiakeConfigVo;
import org.dromara.system.service.xiake.IXiakeConfigService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;

import org.dromara.common.mybatis.core.page.TableDataInfo;

import static org.dromara.common.core.domain.AjaxResult.success;

/**
 * 下课时间配置
 *
 * @author boman
 * @date 2023-08-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xiake/config")
public class XiakeConfigController extends BaseController {

    private final IXiakeConfigService xiakeConfigService;

    /**
     * 查询下课时间配置列表
     */
    @SaCheckPermission("system:config:list")
    @GetMapping("/list")
    public TableDataInfo<XiakeConfigVo> list(XiakeConfigBo bo, PageQuery pageQuery) {
        return xiakeConfigService.queryPageList(bo, pageQuery);
    }

    /**
     * 根据参数键名(班级id)查询参数值config_value(下课时间)
     */
    @PostMapping(value = "/configKey")
    public AjaxResult getConfigKey(@Validated @RequestBody XiakeConfigBo config)
    {
        return success(xiakeConfigService.selectConfigByKey(config));
    }

    /**
     * 根据参数键名修改参数值
     */
    @PostMapping(value = "/updateConfigKey")
    public R<Void> updateConfigKey(@Validated @RequestBody XiakeConfigBo config)
    {
        return toAjax(xiakeConfigService.updateConfigValue(config));
    }
    /**
     * 导出下课时间配置列表
     */
    @SaCheckPermission("system:config:export")
    @Log(title = "下课时间配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(XiakeConfigBo bo, HttpServletResponse response) {
        List<XiakeConfigVo> list = xiakeConfigService.queryList(bo);
        ExcelUtil.exportExcel(list, "下课时间配置", XiakeConfigVo.class, response);
    }

    /**
     * 获取下课时间配置详细信息
     *
     * @param configId 主键
     */
    @SaCheckPermission("system:config:query")
    @GetMapping("/{configId}")
    public R<XiakeConfigVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long configId) {
        return R.ok(xiakeConfigService.queryById(configId));
    }

    /**
     * 新增下课时间配置
     */
    @SaCheckPermission("system:config:add")
    @Log(title = "下课时间配置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody XiakeConfigBo bo) {
        return toAjax(xiakeConfigService.insertByBo(bo));
    }

    /**
     * 修改下课时间配置
     */
    @SaCheckPermission("system:config:edit")
    @Log(title = "下课时间配置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/put")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody XiakeConfigBo bo) {
        return toAjax(xiakeConfigService.updateByBo(bo));
    }

    /**
     * 删除下课时间配置
     *
     * @param configIds 主键串
     */
    @SaCheckPermission("system:config:remove")
    @Log(title = "下课时间配置", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{configIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] configIds) {
        return toAjax(xiakeConfigService.deleteWithValidByIds(List.of(configIds), true));
    }

    /**
     * 刷新参数缓存
     */

    @Log(title = "参数管理", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public AjaxResult refreshCache()
    {
        xiakeConfigService.resetConfigCache();
        return AjaxResult.success();
    }
}
