package com.gxwebsoft.dormitory.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.dormitory.service.DormitoryBedService;
import com.gxwebsoft.dormitory.entity.DormitoryBed;
import com.gxwebsoft.dormitory.param.DormitoryBedParam;
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.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-03 10:49:27
 */
@Tag(name = "宿舍床位管理")
@RestController
@RequestMapping("/api/dormitory/dormitory-bed")
public class DormitoryBedController extends BaseController {
    @Resource
    private DormitoryBedService dormitoryBedService;

    @Operation(summary = "分页查询宿舍床位")
    @GetMapping("/page")
    public ApiResult<PageResult<DormitoryBed>> page(DormitoryBedParam param) {
        // 使用关联查询
        return success(dormitoryBedService.pageRel(param));
    }

    @Operation(summary = "查询全部宿舍床位")
    @GetMapping()
    public ApiResult<List<DormitoryBed>> list(DormitoryBedParam param) {
        // 使用关联查询
        return success(dormitoryBedService.listRel(param));
    }

    @Operation(summary = "根据id查询宿舍床位")
    @GetMapping("/{id}")
    public ApiResult<DormitoryBed> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(dormitoryBedService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('dormitory:dormitoryBed:save')")
    @OperationLog
    @Operation(summary = "添加宿舍床位")
    @PostMapping()
    public ApiResult<?> save(@RequestBody DormitoryBed dormitoryBed) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
         // dormitoryBed实体类没有userId字段，所以不设置
        }
        if (dormitoryBedService.save(dormitoryBed)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('dormitory:dormitoryBed:update')")
    @OperationLog
    @Operation(summary = "修改宿舍床位")
    @PutMapping()
    public ApiResult<?> update(@RequestBody DormitoryBed dormitoryBed) {
        if (dormitoryBedService.updateById(dormitoryBed)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('dormitory:dormitoryBed:remove')")
    @OperationLog
    @Operation(summary = "删除宿舍床位")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (dormitoryBedService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('dormitory:dormitoryBed:save')")
    @OperationLog
    @Operation(summary = "批量添加宿舍床位")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<DormitoryBed> list) {
        if (dormitoryBedService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('dormitory:dormitoryBed:update')")
    @OperationLog
    @Operation(summary = "批量修改宿舍床位")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<DormitoryBed> batchParam) {
        if (batchParam.update(dormitoryBedService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('dormitory:dormitoryBed:remove')")
    @OperationLog
    @Operation(summary = "批量删除宿舍床位")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (dormitoryBedService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
