package org.dromara.work.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.work.domain.vo.TpBlockVo;
import org.dromara.work.domain.bo.TpBlockBo;
import org.dromara.work.service.ITpBlockService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 屏蔽拉黑
 *
 * @author Maosw
 * @date 2025-04-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work/block")
public class TpBlockController extends BaseController {

    private final ITpBlockService tpBlockService;

    /**
     * 查询屏蔽拉黑列表
     */
    @SaCheckPermission("work:block:list")
    @GetMapping("/list")
    public TableDataInfo<TpBlockVo> list(TpBlockBo bo, PageQuery pageQuery) {
        return tpBlockService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出屏蔽拉黑列表
     */
    @SaCheckPermission("work:block:export")
    @Log(title = "屏蔽拉黑", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TpBlockBo bo, HttpServletResponse response) {
        List<TpBlockVo> list = tpBlockService.queryList(bo);
        ExcelUtil.exportExcel(list, "屏蔽拉黑", TpBlockVo.class, response);
    }

    /**
     * 获取屏蔽拉黑详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("work:block:query")
    @GetMapping("/{id}")
    public R<TpBlockVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tpBlockService.queryById(id));
    }

    /**
     * 新增屏蔽拉黑
     */
    @SaCheckPermission("work:block:add")
    @Log(title = "屏蔽拉黑", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TpBlockBo bo) {
        return toAjax(tpBlockService.insertByBo(bo));
    }

    /**
     * 修改屏蔽拉黑
     */
    @SaCheckPermission("work:block:edit")
    @Log(title = "屏蔽拉黑", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TpBlockBo bo) {
        return toAjax(tpBlockService.updateByBo(bo));
    }

    /**
     * 删除屏蔽拉黑
     *
     * @param ids 主键串
     */
    @SaCheckPermission("work:block:remove")
    @Log(title = "屏蔽拉黑", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tpBlockService.deleteWithValidByIds(List.of(ids), true));
    }
}
