package com.gxwebsoft.house.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.house.service.HouseViewsLogService;
import com.gxwebsoft.house.entity.HouseViewsLog;
import com.gxwebsoft.house.param.HouseViewsLogParam;
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:15
 */
@Tag(name = "看房记录表管理")
@RestController
@RequestMapping("/api/house/house-views-log")
public class HouseViewsLogController extends BaseController {
    @Resource
    private HouseViewsLogService houseViewsLogService;

    @Operation(summary = "分页查询看房记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<HouseViewsLog>> page(HouseViewsLogParam param) {
        // 使用关联查询
        return success(houseViewsLogService.pageRel(param));
    }

    @Operation(summary = "查询全部看房记录表")
    @GetMapping()
    public ApiResult<List<HouseViewsLog>> list(HouseViewsLogParam param) {
        // 使用关联查询
        return success(houseViewsLogService.listRel(param));
    }

    @Operation(summary = "根据id查询看房记录表")
    @GetMapping("/{id}")
    public ApiResult<HouseViewsLog> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(houseViewsLogService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('house:houseViewsLog:save')")
    @Operation(summary = "添加看房记录表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody HouseViewsLog houseViewsLog) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
         houseViewsLog.setUserId(loginUser.getUserId());
        }
        if (houseViewsLogService.save(houseViewsLog)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('house:houseViewsLog:update')")
    @Operation(summary = "修改看房记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody HouseViewsLog houseViewsLog) {
        if (houseViewsLogService.updateById(houseViewsLog)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

    @PreAuthorize("hasAuthority('house:houseViewsLog:save')")
    @Operation(summary = "批量添加看房记录表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<HouseViewsLog> list) {
        if (houseViewsLogService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('house:houseViewsLog:update')")
    @Operation(summary = "批量修改看房记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<HouseViewsLog> batchParam) {
        if (batchParam.update(houseViewsLogService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}
