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.PmgBatBuildingDto;
import com.yuchen.saas.park.api.dto.building.PmgBatBuildingQueryDto;
import com.yuchen.saas.park.api.entity.building.PmgBatBuildingPreSale;
import com.yuchen.saas.park.api.vo.building.PmgBatBuildingDetailVO;
import com.yuchen.saas.park.api.vo.building.PmgBatBuildingPageVO;
import com.yuchen.saas.park.service.service.PmgBatParkInfoService;
import com.yuchen.saas.park.service.service.building.IPmgBatBuildingPreSaleService;
import com.yuchen.saas.park.service.service.building.IPmgBatBuildingService;
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-16
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/pmgBatBuilding")
public class PmgBatBuildingController {

    @Resource
    private IPmgBatBuildingService pmgBatBuildingService;
    @Resource
    private PmgBatParkInfoService pmgBatParkInfoService;
    @Resource
    private IPmgBatBuildingPreSaleService pmgBatBuildingPreSaleService;

    /**
     * 新增/修改
     * @tag pauly-lyz
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增/修改")
    public R save(@Valid @RequestBody PmgBatBuildingDto dto) {
        return R.data(pmgBatBuildingService.saveOrUpdate(dto));
    }
    /**
     * 分页查询
     * @tag pauly-lyz
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询")
    public R<Page<PmgBatBuildingPageVO>> page(@RequestBody PmgBatBuildingQueryDto dto) {
        return R.data(pmgBatBuildingService.page(dto));
    }
    /**
     * 楼盘信息
     * @tag pauly-lyz
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "楼盘信息")
    public R<PmgBatBuildingDetailVO> detail(@PathVariable Long id) {
        return R.data(pmgBatBuildingService.detail(id));
    }
    /**
     * 删除
     * @tag pauly-lyz
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除")
    @Transactional
    public R delete(@PathVariable Long id) {
        boolean byId = pmgBatBuildingService.removeById(id);
        if (byId) {
            LambdaQueryWrapper<PmgBatBuildingPreSale> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(PmgBatBuildingPreSale::getBuildingId, id);
            boolean remove = pmgBatBuildingPreSaleService.remove(wrapper);
            return R.data(remove);
        }
        return R.fail("未查询到数据");
    }

    //---------------------------------小程序用--------------------------------------
    @GetMapping("/app/detailByProject/{projectId}")
    @ApiOperation(value = "根据项目id查楼盘详情")
    public R<PmgBatBuildingDetailVO> getByProjectId(@PathVariable Long projectId) {
        return R.data(pmgBatBuildingService.getByProjectId(projectId));
    }
}
