package com.gxwebsoft.house.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.house.service.HouseReservationService;
import com.gxwebsoft.house.entity.HouseReservation;
import com.gxwebsoft.house.param.HouseReservationParam;
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-reservation")
public class HouseReservationController extends BaseController {
    @Resource
    private HouseReservationService houseReservationService;

    @Operation(summary = "分页查询预约记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<HouseReservation>> page(HouseReservationParam param) {
        // 使用关联查询
        return success(houseReservationService.pageRel(param));
    }

    @Operation(summary = "查询全部预约记录表")
    @GetMapping()
    public ApiResult<List<HouseReservation>> list(HouseReservationParam param) {
        // 使用关联查询
        return success(houseReservationService.listRel(param));
    }

    @PreAuthorize("hasAuthority('house:houseReservation:list')")
    @Operation(summary = "根据id查询预约记录表")
    @GetMapping("/{id}")
    public ApiResult<HouseReservation> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(houseReservationService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('house:houseReservation:save')")
    @Operation(summary = "添加预约记录表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody HouseReservation houseReservation) {
// 记录当前登录用户id
      User loginUser = getLoginUser();
      if (loginUser != null) {
        houseReservation.setUserId(loginUser.getUserId());
      }
      if (houseReservationService.count(new LambdaQueryWrapper<HouseReservation>().eq(HouseReservation::getHouseId,houseReservation.getHouseId()).eq(HouseReservation::getUserId,loginUser.getUserId()).eq(HouseReservation::getStatus,0)) > 0){
        return fail("请勿重复提交");
      }
      if (houseReservationService.save(houseReservation)) {
        return success("提交成功");
      }
      return fail("提交失败");
    }

    @PreAuthorize("hasAuthority('house:houseReservation:update')")
    @Operation(summary = "修改预约记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody HouseReservation houseReservation) {
        if (houseReservationService.updateById(houseReservation)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

    @PreAuthorize("hasAuthority('house:houseReservation:update')")
    @Operation(summary = "批量修改预约记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<HouseReservation> batchParam) {
        if (batchParam.update(houseReservationService, "log_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}
