package com.gxwebsoft.house.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.house.service.HouseUserService;
import com.gxwebsoft.house.entity.HouseUser;
import com.gxwebsoft.house.param.HouseUserParam;
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.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户表控制器
 *
 * @author 科技小王子
 * @since 2025-03-05 15:13:05
 */
@Tag(name = "用户表管理")
@RestController
@RequestMapping("/api/house/house-user")
public class HouseUserController extends BaseController {
    @Resource
    private HouseUserService houseUserService;

    @Operation(summary = "分页查询用户表")
    @GetMapping("/page")
    public ApiResult<PageResult<HouseUser>> page(HouseUserParam param) {
        // 使用关联查询
        return success(houseUserService.pageRel(param));
    }

    @Operation(summary = "查询全部用户表")
    @GetMapping()
    public ApiResult<List<HouseUser>> list(HouseUserParam param) {
        // 使用关联查询
        return success(houseUserService.listRel(param));
    }

    @PreAuthorize("hasAuthority('house:houseUser:list')")
    @Operation(summary = "根据id查询用户表")
    @GetMapping("/{id}")
    public ApiResult<HouseUser> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(houseUserService.getByIdRel(id));
    }

    @Operation(summary = "添加用户表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody HouseUser houseUser) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
         houseUser.setUserId(loginUser.getUserId());
        }
        if (houseUserService.save(houseUser)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @Operation(summary = "修改用户表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody HouseUser houseUser) {
        if (houseUserService.updateById(houseUser)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @Operation(summary = "删除用户表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (houseUserService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @Operation(summary = "批量添加用户表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<HouseUser> list) {
        if (houseUserService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @Operation(summary = "批量修改用户表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<HouseUser> batchParam) {
        if (batchParam.update(houseUserService, "user_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @Operation(summary = "批量删除用户表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (houseUserService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
