package org.dromara.biz.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
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.biz.domain.vo.BizGuideVo;
import org.dromara.biz.domain.bo.BizGuideBo;
import org.dromara.biz.service.IBizGuideService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 办事指南
 *
 * @author Blue
 * @date 2025-05-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/biz/guide")
public class BizGuideController extends BaseController {

    private final IBizGuideService bizGuideService;

    /**
     * 查询办事指南列表
     */
    @SaCheckPermission("biz:guide:list")
    @GetMapping("/list")
    public TableDataInfo<BizGuideVo> list(BizGuideBo bo, PageQuery pageQuery) {
        return bizGuideService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出办事指南列表
     */
    @SaCheckPermission("biz:guide:export")
    @Log(title = "办事指南", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BizGuideBo bo, HttpServletResponse response) {
        List<BizGuideVo> list = bizGuideService.queryList(bo);
        ExcelUtil.exportExcel(list, "办事指南", BizGuideVo.class, response);
    }

    /**
     * 获取办事指南详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("biz:guide:query")
    @GetMapping("/{id}")
    public R<BizGuideVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(bizGuideService.queryById(id));
    }

    /**
     * 新增办事指南
     */
    @SaCheckPermission("biz:guide:add")
    @Log(title = "办事指南", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BizGuideBo bo) {
        return toAjax(bizGuideService.insertByBo(bo));
    }

    /**
     * 修改办事指南
     */
    @SaCheckPermission("biz:guide:edit")
    @Log(title = "办事指南", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BizGuideBo bo) {
        return toAjax(bizGuideService.updateByBo(bo));
    }

    /**
     * 删除办事指南
     *
     * @param ids 主键串
     */
    @SaCheckPermission("biz:guide:remove")
    @Log(title = "办事指南", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(bizGuideService.deleteWithValidByIds(List.of(ids), true));
    }
}
