package com.gxwebsoft.house.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.house.entity.HouseFaq;
import com.gxwebsoft.house.param.HouseFaqParam;
import com.gxwebsoft.house.service.HouseFaqService;
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;

/**
 * AI找房常见问题控制器
 */
@Tag(name = "AI找房常见问题管理")
@RestController
@RequestMapping("/api/house/house-faq")
public class HouseFaqController extends BaseController {

    @Resource
    private HouseFaqService houseFaqService;

    @Operation(summary = "分页查询AI找房常见问题")
    @GetMapping("/page")
    public ApiResult<PageResult<HouseFaq>> page(HouseFaqParam param) {
        return success(houseFaqService.pageRel(param));
    }

    @Operation(summary = "查询全部AI找房常见问题")
    @GetMapping()
    public ApiResult<List<HouseFaq>> list(HouseFaqParam param) {
        return success(houseFaqService.listRel(param));
    }

    @Operation(summary = "根据id查询AI找房常见问题")
    @GetMapping("/{id}")
    public ApiResult<HouseFaq> get(@PathVariable("id") Integer id) {
        return success(houseFaqService.getByIdRel(id));
    }

    @OperationLog
    @Operation(summary = "添加AI找房常见问题")
    @PostMapping()
    public ApiResult<?> save(@RequestBody HouseFaq houseFaq) {
        User loginUser = getLoginUser();
        if (loginUser != null) {
            houseFaq.setUserId(loginUser.getUserId());
        }
        if (houseFaqService.save(houseFaq)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @OperationLog
    @Operation(summary = "修改AI找房常见问题")
    @PutMapping()
    public ApiResult<?> update(@RequestBody HouseFaq houseFaq) {
        if (houseFaqService.updateById(houseFaq)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @OperationLog
    @Operation(summary = "删除AI找房常见问题")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (houseFaqService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @OperationLog
    @Operation(summary = "批量添加AI找房常见问题")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<HouseFaq> list) {
        if (houseFaqService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @OperationLog
    @Operation(summary = "批量修改AI找房常见问题")
    @PutMapping("/batch")
    public ApiResult<?> updateBatch(@RequestBody BatchParam<HouseFaq> batchParam) {
        if (batchParam.update(houseFaqService, "faq_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @OperationLog
    @Operation(summary = "批量删除AI找房常见问题")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (houseFaqService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }
}
