package org.dromara.system.controller.system;

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.system.domain.vo.SysHospitalVo;
import org.dromara.system.domain.bo.SysHospitalBo;
import org.dromara.system.service.ISysHospitalService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 医院管理
 *
 * @author Lion Li
 * @date 2025-06-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/hospital")
public class SysHospitalController extends BaseController {

    private final ISysHospitalService sysHospitalService;

    /**
     * 查询医院管理列表
     */
    @SaCheckPermission("system:hospital:list")
    @GetMapping("/list")
    public TableDataInfo<SysHospitalVo> list(SysHospitalBo bo, PageQuery pageQuery) {
        return sysHospitalService.queryPageList(bo, pageQuery);
    }

    @SaCheckPermission("system:hospital:list")
    @GetMapping("/getHospitalData")
    public R<SysHospitalVo> getHospitalData() {
        return R.ok(sysHospitalService.getOneHospital());
    }

    /**
     * 导出医院管理列表
     */
    @SaCheckPermission("system:hospital:export")
    @Log(title = "医院管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysHospitalBo bo, HttpServletResponse response) {
        List<SysHospitalVo> list = sysHospitalService.queryList(bo);
        ExcelUtil.exportExcel(list, "医院管理", SysHospitalVo.class, response);
    }

    /**
     * 获取医院管理详细信息
     *
     * @param hospitalId 主键
     */
    @SaCheckPermission("system:hospital:query")
    @GetMapping("/{hospitalId}")
    public R<SysHospitalVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long hospitalId) {
        return R.ok(sysHospitalService.queryById(hospitalId));
    }

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

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

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