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

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.PmgBatBuilding;
import com.yuchen.saas.park.api.entity.building.PmgBatBuildingPreSale;
import com.yuchen.saas.park.api.entity.park.PmgBatParkInfo;
import com.yuchen.saas.park.api.vo.building.PmgBatBuildingDetailVO;
import com.yuchen.saas.park.api.vo.building.PmgBatBuildingPageVO;
import com.yuchen.saas.park.service.mapper.building.PmgBatBuildingMapper;
import com.yuchen.saas.park.service.mapper.building.PmgBatBuildingPreSaleMapper;
import com.yuchen.saas.park.service.service.PmgBatParkInfoService;
import com.yuchen.saas.park.service.service.building.IPmgBatBuildingService;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 楼盘信息表 服务实现类
 * </p>
 *
 * @author yuanzhelin
 * @since 2024-10-16
 */
@Service
public class PmgBatBuildingServiceImpl extends BaseServiceImpl<PmgBatBuildingMapper, PmgBatBuilding> implements IPmgBatBuildingService {

    @Resource
    private PmgBatBuildingPreSaleMapper pmgBatBuildingPreSaleMapper;
    @Resource
    private PmgBatParkInfoService pmgBatParkInfoService;
    @Override
    @Transactional
    public Boolean save(PmgBatBuildingDto dto) {
        PmgBatBuilding pmgBatBuilding = new PmgBatBuilding();
        BeanUtils.copyProperties(dto, pmgBatBuilding);
        if (Func.isNotEmpty(dto.getId())){
            if (updateById(pmgBatBuilding)){
                LambdaQueryWrapper<PmgBatBuildingPreSale> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(PmgBatBuildingPreSale::getBuildingId, pmgBatBuilding.getId());
                pmgBatBuildingPreSaleMapper.delete(queryWrapper);
                if(Func.isNotEmpty(dto.getPreSaleList())){
                    dto.getPreSaleList().forEach(item->{
                        item.setBuildingId(pmgBatBuilding.getId());
                        pmgBatBuildingPreSaleMapper.insert(item);
                    });
                }
            }
            return true;
        }
        if (save(pmgBatBuilding)){
            if(Func.isNotEmpty(dto.getPreSaleList())){
                dto.getPreSaleList().forEach(item->{
                    item.setBuildingId(pmgBatBuilding.getId());
                    pmgBatBuildingPreSaleMapper.insert(item);
                });
            }
        }
        return true;
    }

    @Override
    public Page<PmgBatBuildingPageVO> page(PmgBatBuildingQueryDto dto) {
        Page<PmgBatBuildingPageVO> page = new Page<>(dto.getCurrent(), dto.getSize());
        return baseMapper.page(dto, page);
    }

    @Override
    public PmgBatBuildingDetailVO getByProjectId(Long id) {
        LambdaQueryWrapper<PmgBatBuilding> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmgBatBuilding::getProjectId, id);
        PmgBatBuilding byId = getOne(wrapper);
        if (byId != null) {
            PmgBatBuildingDetailVO vo = new PmgBatBuildingDetailVO();
            BeanUtils.copyProperties(byId, vo);
            PmgBatParkInfo pmgBatParkInfo = pmgBatParkInfoService.getById(byId.getProjectId());
            vo.setParkName(pmgBatParkInfo.getParkName());
            LambdaQueryWrapper<PmgBatBuildingPreSale> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(PmgBatBuildingPreSale::getBuildingId, id);
            List<PmgBatBuildingPreSale> pmgBatBuildingPreSales = pmgBatBuildingPreSaleMapper.selectList(queryWrapper);
            vo.setPmgBatBuildingPreSales(pmgBatBuildingPreSales);
            return vo;
        }
        return null;
    }

    @Override
    public PmgBatBuildingDetailVO detail(Long id) {
        PmgBatBuilding byId = getById(id);
        if (byId != null) {
            PmgBatBuildingDetailVO vo = new PmgBatBuildingDetailVO();
            BeanUtils.copyProperties(byId, vo);
            PmgBatParkInfo pmgBatParkInfo = pmgBatParkInfoService.getById(byId.getProjectId());
            vo.setParkName(pmgBatParkInfo.getParkName());
            LambdaQueryWrapper<PmgBatBuildingPreSale> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(PmgBatBuildingPreSale::getBuildingId, id);
            List<PmgBatBuildingPreSale> pmgBatBuildingPreSales = pmgBatBuildingPreSaleMapper.selectList(queryWrapper);
            vo.setPmgBatBuildingPreSales(pmgBatBuildingPreSales);
            return vo;
        }
        return null;
    }
}
