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.dromara.biz.domain.vo.PoliceCapacityTree;
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.BizPoliceCapacityVo;
import org.dromara.biz.domain.bo.BizPoliceCapacityBo;
import org.dromara.biz.service.IBizPoliceCapacityService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 警种能力
 *
 * @author Luo.J
 * @date 2025-05-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/biz/policeCapacity")
public class BizPoliceCapacityController extends BaseController {

    private final IBizPoliceCapacityService bizPoliceCapacityService;

    /**
     * 查询警种能力列表
     */
    @SaCheckPermission("biz:policeCapacity:list")
    @GetMapping("/list")
    public TableDataInfo<BizPoliceCapacityVo> list(BizPoliceCapacityBo bo, PageQuery pageQuery) {
        return bizPoliceCapacityService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询警种能力树列表
     */
//    @SaCheckPermission("biz:policeCapacity:list")
    @GetMapping("/treeList")
    public R<List<PoliceCapacityTree>> policeCapacityTree(@RequestParam(required = false) String name) {
        return R.ok(bizPoliceCapacityService.selectPoliceCapacityTreeList(name));
    }

    /**
     * 导出警种能力列表
     */
    @SaCheckPermission("biz:policeCapacity:export")
    @Log(title = "警种能力", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BizPoliceCapacityBo bo, HttpServletResponse response) {
        List<BizPoliceCapacityVo> list = bizPoliceCapacityService.queryList(bo);
        ExcelUtil.exportExcel(list, "警种能力", BizPoliceCapacityVo.class, response);
    }

    /**
     * 获取警种能力详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("biz:policeCapacity:query")
    @GetMapping("/{id}")
    public R<BizPoliceCapacityVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(bizPoliceCapacityService.queryById(id));
    }

    /**
     * 新增警种能力
     */
    @SaCheckPermission("biz:policeCapacity:add")
    @Log(title = "警种能力", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BizPoliceCapacityBo bo) {
        return toAjax(bizPoliceCapacityService.insertByBo(bo));
    }

    /**
     * 修改警种能力
     */
    @SaCheckPermission("biz:policeCapacity:edit")
    @Log(title = "警种能力", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BizPoliceCapacityBo bo) {
        return toAjax(bizPoliceCapacityService.updateByBo(bo));
    }

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