package com.gxwebsoft.clinic.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.clinic.service.ClinicDoctorApplyService;
import com.gxwebsoft.clinic.entity.ClinicDoctorApply;
import com.gxwebsoft.clinic.param.ClinicDoctorApplyParam;
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 io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 医生入驻申请控制器
 *
 * @author 科技小王子
 * @since 2025-10-19 09:27:04
 */
@Tag(name = "医生入驻申请管理")
@RestController
@RequestMapping("/api/clinic/clinic-doctor-apply")
public class ClinicDoctorApplyController extends BaseController {
    @Resource
    private ClinicDoctorApplyService clinicDoctorApplyService;

    @PreAuthorize("hasAuthority('clinic:clinicDoctorApply:list')")
    @Operation(summary = "分页查询医生入驻申请")
    @GetMapping("/page")
    public ApiResult<PageResult<ClinicDoctorApply>> page(ClinicDoctorApplyParam param) {
        // 使用关联查询
        return success(clinicDoctorApplyService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('clinic:clinicDoctorApply:list')")
    @Operation(summary = "查询全部医生入驻申请")
    @GetMapping()
    public ApiResult<List<ClinicDoctorApply>> list(ClinicDoctorApplyParam param) {
        // 使用关联查询
        return success(clinicDoctorApplyService.listRel(param));
    }

    @PreAuthorize("hasAuthority('clinic:clinicDoctorApply:list')")
    @Operation(summary = "根据id查询医生入驻申请")
    @GetMapping("/{id}")
    public ApiResult<ClinicDoctorApply> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(clinicDoctorApplyService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('clinic:clinicDoctorApply:save')")
    @OperationLog
    @Operation(summary = "添加医生入驻申请")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ClinicDoctorApply clinicDoctorApply) {
        // 记录当前登录用户id
        // User loginUser = getLoginUser();
        // if (loginUser != null) {
        // clinicDoctorApply.setUserId(loginUser.getUserId());
        // }
        if (clinicDoctorApplyService.save(clinicDoctorApply)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('clinic:clinicDoctorApply:update')")
    @OperationLog
    @Operation(summary = "修改医生入驻申请")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ClinicDoctorApply clinicDoctorApply) {
        if (clinicDoctorApplyService.updateById(clinicDoctorApply)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('clinic:clinicDoctorApply:remove')")
    @OperationLog
    @Operation(summary = "删除医生入驻申请")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (clinicDoctorApplyService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('clinic:clinicDoctorApply:save')")
    @OperationLog
    @Operation(summary = "批量添加医生入驻申请")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ClinicDoctorApply> list) {
        if (clinicDoctorApplyService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('clinic:clinicDoctorApply:update')")
    @OperationLog
    @Operation(summary = "批量修改医生入驻申请")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ClinicDoctorApply> batchParam) {
        if (batchParam.update(clinicDoctorApplyService, "apply_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('clinic:clinicDoctorApply:remove')")
    @OperationLog
    @Operation(summary = "批量删除医生入驻申请")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (clinicDoctorApplyService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
