package org.dromara.system.controller.system;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
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.system.domain.vo.SysWardVo;
import org.dromara.system.domain.bo.SysWardBo;
import org.dromara.system.service.ISysWardService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 病区管理
 *
 * @author Lion Li
 * @date 2025-06-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/ward")
public class SysWardController extends BaseController {

    private final ISysWardService sysWardService;

    /**
     * 查询病区管理列表
     */
    @SaCheckPermission("system:ward:list")
    @GetMapping("/list")
    public TableDataInfo<SysWardVo> list(SysWardBo bo, PageQuery pageQuery) {
        return sysWardService.queryPageList(bo, pageQuery);
    }

    @SaCheckPermission("system:ward:list")
    @GetMapping("/listUsed")
    public R<JSONObject> listUsed() {
        return sysWardService.listUsed();
    }

    /**
     * 导出病区管理列表
     */
    @SaCheckPermission("system:ward:export")
    @Log(title = "病区管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysWardBo bo, HttpServletResponse response) {
        List<SysWardVo> list = sysWardService.queryList(bo);
        ExcelUtil.exportExcel(list, "病区管理", SysWardVo.class, response);
    }

    /**
     * 获取病区管理详细信息
     *
     * @param wardId 主键
     */
    @SaCheckPermission("system:ward:query")
    @GetMapping("/{wardId}")
    public R<SysWardVo> getInfo(@NotNull(message = "主键不能为空")
                                @PathVariable Long wardId) {
        return R.ok(sysWardService.queryById(wardId));
    }

    /**
     * 新增病区管理
     */
    @SaCheckPermission("system:ward:add")
    @Log(title = "病区管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysWardBo bo) {
        return toAjax(sysWardService.insertByBo(bo));
    }

    /**
     * 修改病区管理
     */
    @SaCheckPermission("system:ward:edit")
    @Log(title = "病区管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysWardBo bo) {
        return toAjax(sysWardService.updateByBo(bo));
    }

    /**
     * 删除病区管理
     *
     * @param wardIds 主键串
     */
    @SaCheckPermission("system:ward:remove")
    @Log(title = "病区管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{wardIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] wardIds) {
        return toAjax(sysWardService.deleteWithValidByIds(List.of(wardIds), true));
    }
}
