package com.gxwebsoft.clinic.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.clinic.service.ClinicCaseService;
import com.gxwebsoft.clinic.entity.ClinicCase;
import com.gxwebsoft.clinic.param.ClinicCaseParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 病例列表控制器
 *
 * @author LX
 * @since 2025-12-25 17:24:55
 */
@Api(tags = "病例列表管理")
@RestController
@RequestMapping("/api/clinic/clinic-case")
public class ClinicCaseController extends BaseController {
    @Resource
    private ClinicCaseService clinicCaseService;

    @ApiOperation("分页查询病例列表")
    @GetMapping("/page")
    public ApiResult<PageResult<ClinicCase>> page(ClinicCaseParam param) {
        // 使用关联查询
        return success(clinicCaseService.pageRel(param));
    }

    @ApiOperation("查询全部病例列表")
    @GetMapping()
    public ApiResult<List<ClinicCase>> list(ClinicCaseParam param) {
        // 使用关联查询
        return success(clinicCaseService.listRel(param));
    }

    @ApiOperation("根据id查询病例列表")
    @GetMapping("/{id}")
    public ApiResult<ClinicCase> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(clinicCaseService.getByIdRel(id));
    }

    @ApiOperation("添加病例列表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ClinicCase clinicCase) {
        // 记录当前登录用户id
        if (clinicCase.getUserId() != null) {
            User loginUser = getLoginUser();
            if (loginUser != null) {
                clinicCase.setUserId(loginUser.getUserId());
            }
        }
        if (clinicCaseService.save(clinicCase)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("修改病例列表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ClinicCase clinicCase) {
        if (clinicCaseService.updateById(clinicCase)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("删除病例列表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (clinicCaseService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @ApiOperation("批量添加病例列表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ClinicCase> list) {
        if (clinicCaseService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("批量修改病例列表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ClinicCase> batchParam) {
        if (batchParam.update(clinicCaseService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("批量删除病例列表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (clinicCaseService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
