package com.gxwebsoft.dormitory.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.dormitory.service.DormitoryFloorService;
import com.gxwebsoft.dormitory.entity.DormitoryFloor;
import com.gxwebsoft.dormitory.param.DormitoryFloorParam;
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-floor")
public class DormitoryFloorController extends BaseController {
    @Resource
    private DormitoryFloorService dormitoryFloorService;

    @Operation(summary = "分页查询宿舍楼层")
    @GetMapping("/page")
    public ApiResult<PageResult<DormitoryFloor>> page(DormitoryFloorParam param) {
        // 使用关联查询
        return success(dormitoryFloorService.pageRel(param));
    }

    @Operation(summary = "查询全部宿舍楼层")
    @GetMapping()
    public ApiResult<List<DormitoryFloor>> list(DormitoryFloorParam param) {
        // 使用关联查询
        return success(dormitoryFloorService.listRel(param));
    }

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

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

    @PreAuthorize("hasAuthority('dormitory:dormitoryFloor:update')")
    @OperationLog
    @Operation(summary = "修改宿舍楼层")
    @PutMapping()
    public ApiResult<?> update(@RequestBody DormitoryFloor dormitoryFloor) {
        if (dormitoryFloorService.updateById(dormitoryFloor)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

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

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

}
