package com.gxwebsoft.house.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.house.service.HouseLikeLogService;
import com.gxwebsoft.house.entity.HouseLikeLog;
import com.gxwebsoft.house.param.HouseLikeLogParam;
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 13:47:14
 */
@Tag(name = "房源点赞表管理")
@RestController
@RequestMapping("/api/house/house-like-log")
public class HouseLikeLogController extends BaseController {
    @Resource
    private HouseLikeLogService houseLikeLogService;

    @Operation(summary = "分页查询房源点赞表")
    @GetMapping("/page")
    public ApiResult<PageResult<HouseLikeLog>> page(HouseLikeLogParam param) {
        // 使用关联查询
        return success(houseLikeLogService.pageRel(param));
    }

    @Operation(summary = "查询全部房源点赞表")
    @GetMapping()
    public ApiResult<List<HouseLikeLog>> list(HouseLikeLogParam param) {
        // 使用关联查询
        return success(houseLikeLogService.listRel(param));
    }

    @PreAuthorize("hasAuthority('house:houseLikeLog:list')")
    @Operation(summary = "根据id查询房源点赞表")
    @GetMapping("/{id}")
    public ApiResult<HouseLikeLog> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(houseLikeLogService.getById(id));
    }

    @Operation(summary = "添加房源点赞表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody HouseLikeLog houseLikeLog) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
         houseLikeLog.setUserId(loginUser.getUserId());
        }
        if (houseLikeLogService.save(houseLikeLog)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }
    @PreAuthorize("hasAuthority('house:houseLikeLog:update')")
    @Operation(summary = "修改房源点赞表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody HouseLikeLog houseLikeLog) {
        if (houseLikeLogService.updateById(houseLikeLog)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('house:houseLikeLog:remove')")
    @Operation(summary = "删除房源点赞表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (houseLikeLogService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('house:houseLikeLog:save')")
    @Operation(summary = "批量添加房源点赞表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<HouseLikeLog> list) {
        if (houseLikeLogService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('house:houseLikeLog:update')")
    @Operation(summary = "批量修改房源点赞表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<HouseLikeLog> batchParam) {
        if (batchParam.update(houseLikeLogService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('house:houseLikeLog:remove')")
    @Operation(summary = "批量删除房源点赞表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (houseLikeLogService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
