package org.dromara.system.controller;

import java.io.IOException;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.extern.slf4j.Slf4j;
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.system.domain.vo.VsNavBottomMainVo;
import org.dromara.system.domain.bo.VsNavBottomMainBo;
import org.dromara.system.service.IVsNavBottomMainService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 底部主导航表管理
 *
 * @author system
 * @date 2025-01-20
 */
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/scenic/navBottomMain")
public class VsNavBottomMainController extends BaseController {

    private final IVsNavBottomMainService vsNavBottomMainService;

    /**
     * 查询底部主导航表列表
     */
    @SaCheckPermission("scenic:navBottomMain:list")
    @GetMapping("/list")
    public TableDataInfo<VsNavBottomMainVo> list(VsNavBottomMainBo bo, PageQuery pageQuery) {
        return vsNavBottomMainService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出底部主导航表列表
     */
    @SaCheckPermission("scenic:navBottomMain:export")
    @Log(title = "底部主导航表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(VsNavBottomMainBo bo, HttpServletResponse response) {
        try {
            List<VsNavBottomMainVo> list = vsNavBottomMainService.queryList(bo);
            ExcelUtil.exportExcel(list, "底部主导航表", VsNavBottomMainVo.class, response);
        } catch (Exception e) {
            // 如果导出过程中出现异常，重置Content-Type并返回错误信息
            response.reset();
            response.setContentType("application/json;charset=UTF-8");
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            try {
                response.getWriter().write("{\"code\":500,\"msg\":\"导出失败：" + e.getMessage() + "\"}");
            } catch (IOException ex) {
                // 忽略写入错误
            }
        }
    }

    /**
     * 获取底部主导航表详细信息
     *
     * @param navId 主键
     */
    @SaCheckPermission("scenic:navBottomMain:query")
    @GetMapping("/{navId}")
    public R<VsNavBottomMainVo> getInfo(@NotNull(message = "主键不能为空")
                                        @PathVariable Long navId) {
        return R.ok(vsNavBottomMainService.queryById(navId));
    }

    /**
     * 新增底部主导航表
     */
    @SaCheckPermission("scenic:navBottomMain:add")
    @Log(title = "底部主导航表", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody VsNavBottomMainBo bo) {
        return toAjax(vsNavBottomMainService.insertByBo(bo));
    }

    /**
     * 修改底部主导航表
     */
    @SaCheckPermission("scenic:navBottomMain:edit")
    @Log(title = "底部主导航表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody VsNavBottomMainBo bo) {
        return toAjax(vsNavBottomMainService.updateByBo(bo));
    }

    /**
     * 删除底部主导航表
     *
     * @param ids 主键串
     */
    @SaCheckPermission("scenic:navBottomMain:remove")
    @Log(title = "底部主导航表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(vsNavBottomMainService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 获取启用的底部主导航列表
     */
    @GetMapping("/enabled")
    public R<List<VsNavBottomMainVo>> getEnabledNavList() {
        return R.ok(vsNavBottomMainService.getEnabledNavList());
    }


    /**
     * 根据类型获取导航列表
     */
    @GetMapping("/byType/{type}")
    @SaCheckPermission("scenic:navBottomMain:queryById")
    public R<List<VsNavBottomMainVo>> getNavListByType(@PathVariable String type) {
        return R.ok(vsNavBottomMainService.getNavListByType(type));
    }
}
