package com.gxwebsoft.dormitory.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.dormitory.service.DormitoryBuildingService;
import com.gxwebsoft.dormitory.entity.DormitoryBuilding;
import com.gxwebsoft.dormitory.param.DormitoryBuildingParam;
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-building")
public class DormitoryBuildingController extends BaseController {
    @Resource
    private DormitoryBuildingService dormitoryBuildingService;

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

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

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

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

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

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

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

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

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

}
