package com.ruoyi.api.controller.info;

import com.ruoyi.api.controller.common.AbstractApiController;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.info.patient.domain.bo.PatientBo;
import com.ruoyi.info.patient.domain.vo.PatientVo;
import com.ruoyi.info.patient.service.IPatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 就诊人Controller
 *
 * @author ruoyi
 * @date 2025-09-20
 */
@Validated
@Api(value = "就诊人控制器", tags = {"就诊人管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/patient/patient")
public class ApiPatientController extends AbstractApiController {

    private final IPatientService iPatientService;

    /**
     * 分页查询就诊人列表
     */
    @ApiOperation("查询就诊人列表")
    @GetMapping("/page")
    public TableDataInfo<PatientVo> page(@Validated(QueryGroup.class) PatientBo bo, PageQuery pageQuery) {
        bo.setUserId(getUserId());
        return iPatientService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询就诊人列表
     */
    @ApiOperation("查询就诊人列表")
    @GetMapping("/list")
    public R<List<PatientVo>> list(@Validated(QueryGroup.class) PatientBo bo) {
        bo.setUserId(getUserId());
        return R.ok(iPatientService.queryList(bo));
    }

    /**
     * 获取就诊人详细信息
     */
    @ApiOperation("获取就诊人详细信息")
    @GetMapping("/info/{id}")
    public R<PatientVo> getInfo(@ApiParam("主键")
                                                  @NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return R.ok(iPatientService.queryById(id));
    }

    /**
     * 新增就诊人
     */
    @ApiOperation("新增就诊人")
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PatientBo bo) {
        bo.setUserId(getUserId());
        return toAjax(iPatientService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改就诊人
     */
    @ApiOperation("修改就诊人")
    @RepeatSubmit()
    @PostMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PatientBo bo) {
        bo.setUserId(getUserId());
        return toAjax(iPatientService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除就诊人
     */
    @ApiOperation("删除就诊人")
    @PostMapping("/del/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iPatientService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 设置用户就诊人是否默认
     */
    @ApiOperation("设置用户就诊人是否默认")
    @RepeatSubmit()
    @PostMapping("/setDefaulted/{patientId}")
    public R<Void> setDefaulted(@PathVariable("patientId") Long patientId) {
        iPatientService.setDefaulted(getUserId(),patientId);
        return R.ok();
    }


    /**
     * 获取用户默认就诊人
     */
    @ApiOperation("获取用户默认就诊人")
    @GetMapping("/getDefaulted")
    public R<PatientVo> getDefaulted() {
        return R.ok(iPatientService.getDefaulted(getUserId()));
    }
}
