package com.ruoyi.busi.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.busi.domain.SysOrg;
import com.ruoyi.busi.domain.vo.SysOrgOperateVO;
import com.ruoyi.busi.domain.vo.SysOrgQuery;
import com.ruoyi.busi.domain.vo.SysOrgStatusVO;
import com.ruoyi.busi.domain.vo.SysOrgTreeSelect;
import com.ruoyi.busi.service.ISysOrgService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;

/**
 * 司法行政机构管理 Controller
 * <p>
 * 网关访问前缀：/busi/org/**
 * </p>
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/org")
public class SysOrgController extends BaseController
{
    @Autowired
    private ISysOrgService sysOrgService;

    /**
     * 分页查询机构列表（支持名称、编码模糊搜索、状态筛选、数据权限）
     */
    @RequiresPermissions("busi:org:list")
    @GetMapping("/list")
    public TableDataInfo list(SysOrgQuery query)
    {
        startPage();
        List<SysOrg> list = sysOrgService.selectSysOrgList(query);
        return getDataTable(list);
    }

    /**
     * 查询机构树形结构（用于左侧树 / 上级机构选择）
     */
    @RequiresPermissions("busi:org:list")
    @GetMapping("/tree")
    public AjaxResult tree(SysOrgQuery query)
    {
        List<SysOrgTreeSelect> tree = sysOrgService.selectSysOrgTree(query);
        return success(tree);
    }

    /**
     * 获取机构详情
     */
    @RequiresPermissions("busi:org:query")
    @GetMapping("/{jgbm}")
    public AjaxResult getInfo(@PathVariable("jgbm") String jgbm)
    {
        return success(sysOrgService.selectSysOrgByJgbm(jgbm));
    }

    /**
     * 新增机构
     */
    @RequiresPermissions("busi:org:add")
    @Log(title = "司法行政机构", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated(SysOrgOperateVO.Add.class) @RequestBody SysOrgOperateVO operateVO)
    {
        return toAjax(sysOrgService.insertSysOrg(operateVO));
    }

    /**
     * 编辑机构
     */
    @RequiresPermissions("busi:org:edit")
    @Log(title = "司法行政机构", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@Validated(SysOrgOperateVO.Edit.class) @RequestBody SysOrgOperateVO operateVO)
    {
        return toAjax(sysOrgService.updateSysOrg(operateVO));
    }

    /**
     * 单条逻辑删除
     */
    @RequiresPermissions("busi:org:remove")
    @Log(title = "司法行政机构", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{jgbm}")
    public AjaxResult remove(@PathVariable("jgbm") String jgbm)
    {
        return toAjax(sysOrgService.deleteSysOrgByJgbm(jgbm));
    }

    /**
     * 修改机构状态（0停用 1正常）
     */
    @RequiresPermissions("busi:org:edit")
    @Log(title = "司法行政机构", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@Validated @RequestBody SysOrgStatusVO statusVO)
    {
        return toAjax(sysOrgService.changeStatus(statusVO.getJgbm(), statusVO.getStatus()));
    }
}