package com.gxwebsoft.dormitory.controller;

import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.dormitory.entity.DormitoryApply;
import com.gxwebsoft.dormitory.param.DormitoryApplyParam;
import com.gxwebsoft.dormitory.service.DormitoryApplyService;
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 15:54:30
 */
@Tag(name = "审批管理管理")
@RestController
@RequestMapping("/api/dormitory/dormitory-apply")
public class DormitoryApplyController extends BaseController {
    @Resource
    private DormitoryApplyService dormitoryApplyService;

    @PreAuthorize("hasAuthority('dormitory:dormitoryApply:list')")
    @Operation(summary = "分页查询审批管理")
    @GetMapping("/page")
    public ApiResult<PageResult<DormitoryApply>> page(DormitoryApplyParam param) {
        // 使用关联查询
        return success(dormitoryApplyService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('dormitory:dormitoryApply:list')")
    @Operation(summary = "查询全部审批管理")
    @GetMapping()
    public ApiResult<List<DormitoryApply>> list(DormitoryApplyParam param) {
        // 使用关联查询
        return success(dormitoryApplyService.listRel(param));
    }

    @PreAuthorize("hasAuthority('dormitory:dormitoryApply:list')")
    @Operation(summary = "根据id查询审批管理")
    @GetMapping("/{id}")
    public ApiResult<DormitoryApply> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(dormitoryApplyService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('dormitory:dormitoryApply:save')")
    @OperationLog
    @Operation(summary = "添加审批管理")
    @PostMapping()
    public ApiResult<?> save(@RequestBody DormitoryApply dormitoryApply) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
         dormitoryApply.setUserId(loginUser.getUserId());
        }
        if (dormitoryApplyService.save(dormitoryApply)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('dormitory:dormitoryApply:update')")
    @OperationLog
    @Operation(summary = "修改审批管理")
    @PutMapping()
    public ApiResult<?> update(@RequestBody DormitoryApply dormitoryApply) {
        if (dormitoryApplyService.updateById(dormitoryApply)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('dormitory:dormitoryApply:remove')")
    @OperationLog
    @Operation(summary = "删除审批管理")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (dormitoryApplyService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('dormitory:dormitoryApply:save')")
    @OperationLog
    @Operation(summary = "批量添加审批管理")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<DormitoryApply> list) {
        if (dormitoryApplyService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('dormitory:dormitoryApply:update')")
    @OperationLog
    @Operation(summary = "批量修改审批管理")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<DormitoryApply> batchParam) {
        if (batchParam.update(dormitoryApplyService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}