package com.gxwebsoft.clinic.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.clinic.service.ClinicListService;
import com.gxwebsoft.clinic.entity.ClinicList;
import com.gxwebsoft.clinic.param.ClinicListParam;
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-09 17:29:44
 */
@Api(tags = "诊所列表管理")
@RestController
@RequestMapping("/api/clinic/clinic-list")
public class ClinicListController extends BaseController {
    @Resource
    private ClinicListService clinicListService;

    @GetMapping("/getUserItem")
    public ApiResult<ClinicList> getUserItem() {
        // 使用关联查询
        return success(clinicListService.getByUserId(getLoginUserId()));
    }

    @ApiOperation("分页查询诊所列表")
    @GetMapping("/page")
    public ApiResult<PageResult<ClinicList>> page(ClinicListParam param) {
        // 使用关联查询
        return success(clinicListService.pageRel(param));
    }

    @ApiOperation("查询全部诊所列表")
    @GetMapping()
    public ApiResult<List<ClinicList>> list(ClinicListParam param) {
        // 使用关联查询
        return success(clinicListService.listRel(param));
    }

    @PreAuthorize("hasAuthority('clinic:clinicList:list')")
    @ApiOperation("根据id查询诊所列表")
    @GetMapping("/{id}")
    public ApiResult<ClinicList> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(clinicListService.getByIdRel(id));
    }

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

    @ApiOperation("修改诊所列表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ClinicList clinicList) {
        if (clinicListService.updateById(clinicList)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

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

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

}
