package com.gxwebsoft.dormitory.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.dormitory.service.DormitoryRecordService;
import com.gxwebsoft.dormitory.entity.DormitoryRecord;
import com.gxwebsoft.dormitory.param.DormitoryRecordParam;
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-record")
public class DormitoryRecordController extends BaseController {
    @Resource
    private DormitoryRecordService dormitoryRecordService;

    @Operation(summary = "分页查询宿舍记录")
    @GetMapping("/page")
    public ApiResult<PageResult<DormitoryRecord>> page(DormitoryRecordParam param) {
        // 使用关联查询
        return success(dormitoryRecordService.pageRel(param));
    }

    @Operation(summary = "查询全部宿舍记录")
    @GetMapping()
    public ApiResult<List<DormitoryRecord>> list(DormitoryRecordParam param) {
        // 使用关联查询
        return success(dormitoryRecordService.listRel(param));
    }

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

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

    @PreAuthorize("hasAuthority('dormitory:dormitoryRecord:update')")
    @OperationLog
    @Operation(summary = "修改宿舍记录")
    @PutMapping()
    public ApiResult<?> update(@RequestBody DormitoryRecord dormitoryRecord) {
        if (dormitoryRecordService.updateById(dormitoryRecord)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

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

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

}
