package org.dromara.web.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaMode;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.web.domain.bo.ConsultPatientEditBo;
import org.dromara.web.domain.bo.ConsultPatientInitiateBo;
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.web.domain.vo.ConsultPatientVo;
import org.dromara.web.domain.bo.ConsultPatientBo;
import org.dromara.web.service.IConsultPatientService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 会诊患者
 *
 * @author Huanyi
 * @date 2025-08-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/web/patient")
public class ConsultPatientController extends BaseController {

    private final IConsultPatientService consultPatientService;

    /**
     * 查询会诊患者列表
     */
    @SaCheckPermission("workbench:consultationPatient:list")
    @GetMapping("/list")
    public TableDataInfo<ConsultPatientVo> list(ConsultPatientBo bo, PageQuery pageQuery) {
        return consultPatientService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出会诊患者列表
     */
    @SaCheckPermission("web:patient:export")
    @Log(title = "会诊患者", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ConsultPatientBo bo, HttpServletResponse response) {
        List<ConsultPatientVo> list = consultPatientService.queryList(bo);
        ExcelUtil.exportExcel(list, "会诊患者", ConsultPatientVo.class, response);
    }

    /**
     * 获取会诊患者详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("web:patient:query")
    @GetMapping("/{id}")
    public R<ConsultPatientVo> getInfo(@NotNull(message = "主键不能为空")
                                       @PathVariable Long id) {
        return R.ok(consultPatientService.queryById(id));
    }

    /**
     * 新增会诊患者
     */
    @SaCheckPermission("web:patient:add")
    @Log(title = "会诊患者", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ConsultPatientBo bo) {
        return toAjax(consultPatientService.insertByBo(bo));
    }

    /**
     * 修改会诊患者
     */
    @SaCheckPermission("web:patient:edit")
    @Log(title = "会诊患者", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ConsultPatientBo bo) {
        return toAjax(consultPatientService.updateByBo(bo));
    }

    /**
     * 删除会诊患者
     *
     * @param ids 主键串
     */
    @SaCheckPermission("web:patient:remove")
    @Log(title = "会诊患者", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(consultPatientService.deleteWithValidByIds(List.of(ids), true));
    }

    @PostMapping("/initiateConsultation")
    public R initiateConsultation(@RequestBody ConsultPatientInitiateBo bo) {
        return consultPatientService.initiateConsultation(bo);
    }

    @PutMapping("/edit")
    @SaCheckPermission(
        value = {
            "workbench:consultationPatient:edit"
        },
        mode = SaMode.OR
    )
    public R edit(@RequestBody ConsultPatientEditBo bo) {
        return consultPatientService.edit(bo);
    }
}
