package com.yuchen.saas.park.service.controller.building;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuchen.saas.park.api.dto.building.PmgBatDwellingDto;
import com.yuchen.saas.park.api.dto.building.PmgBatDwellingQueryDto;
import com.yuchen.saas.park.api.entity.building.PmgBatDwelling;
import com.yuchen.saas.park.api.entity.building.PmgBatDwellingConstruction;
import com.yuchen.saas.park.api.vo.building.PmgBatDwellingDetailVO;
import com.yuchen.saas.park.service.service.building.IPmgBatDwellingConstructionService;
import com.yuchen.saas.park.service.service.building.IPmgBatDwellingService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nest.springwrap.core.tool.api.R;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 *
 * 楼栋信息表 相关接口
 *
 *
 * @author yuanzhelin
 * @since 2024-10-21
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/pmgBatDwelling")
public class PmgBatDwellingController {

    @Resource
    private IPmgBatDwellingService pmgBatDwellingService;
    @Resource
    private IPmgBatDwellingConstructionService pmgBatDwellingConstructionService;

    /**
     * 保存
     * @tag pauly-lyz
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存")
    public R save(@Valid @RequestBody PmgBatDwellingDto dto) {
        pmgBatDwellingService.save(dto);
        return R.success();
    }
    /**
     * 分页查询
     * @tag pauly-lyz
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询")
    public R<Page<PmgBatDwelling>> page(@RequestBody PmgBatDwellingQueryDto dto) {
        return R.data(pmgBatDwellingService.page(dto));
    }
    /**
     * 楼栋详情
     * @tag pauly-lyz
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "楼栋详情")
    public R<PmgBatDwellingDetailVO> detail(@PathVariable Long id) {
        return R.data(pmgBatDwellingService.detail(id));
    }
    /**
     * 删除
     * @tag pauly-lyz
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除")
    @Transactional
    public R delete(@PathVariable Long id) {
        boolean byId = pmgBatDwellingService.removeById(id);
        if (byId) {
            LambdaQueryWrapper<PmgBatDwellingConstruction> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(PmgBatDwellingConstruction::getDwellingId, id);
            boolean remove = pmgBatDwellingConstructionService.remove(wrapper);
            return R.data(remove);
        }
        return R.fail("未查询到数据");
    }
}
