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.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.entity.park.PmgBatParkInfo;
import com.yuchen.saas.park.api.vo.building.PmgBatDwellingDetailVO;
import com.yuchen.saas.park.service.mapper.building.PmgBatDwellingConstructionMapper;
import com.yuchen.saas.park.service.mapper.building.PmgBatDwellingMapper;
import com.yuchen.saas.park.service.service.PmgBatParkInfoService;
import com.yuchen.saas.park.service.service.building.IPmgBatDwellingService;
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 javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 楼栋信息表 服务实现类
 * </p>
 *
 * @author yuanzhelin
 * @since 2024-10-21
 */
@Service
public class PmgBatDwellingServiceImpl extends BaseServiceImpl<PmgBatDwellingMapper, PmgBatDwelling> implements IPmgBatDwellingService {

    @Resource
    private PmgBatDwellingConstructionMapper pmgBatDwellingConstructionMapper;

    @Resource
    private PmgBatParkInfoService pmgBatParkInfoService;
    @Override
    public Boolean save(PmgBatDwellingDto dto) {
        PmgBatDwelling pmgBatDwelling = new PmgBatDwelling();
        BeanUtils.copyProperties(dto, pmgBatDwelling);
        if (Func.isNotEmpty(dto.getId())){
            if (updateById(pmgBatDwelling)){
                LambdaQueryWrapper<PmgBatDwellingConstruction> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(PmgBatDwellingConstruction::getDwellingId, pmgBatDwelling.getId());
                pmgBatDwellingConstructionMapper.delete(queryWrapper);
                if (Func.isNotEmpty(dto.getConstructions())){
                    dto.getConstructions().forEach(item->{
                        item.setDwellingId(pmgBatDwelling.getId());
                        pmgBatDwellingConstructionMapper.insert(item);
                    });
                }
            }
            return true;
        }
        if (save(pmgBatDwelling)){
            if (Func.isNotEmpty(dto.getConstructions())){
                dto.getConstructions().forEach(item->{
                    item.setDwellingId(pmgBatDwelling.getId());
                    pmgBatDwellingConstructionMapper.insert(item);
                });
            }
        }
        return true;
    }

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

    @Override
    public PmgBatDwellingDetailVO detail(Long id) {
        PmgBatDwelling byId = getById(id);
        if (byId != null) {
            PmgBatDwellingDetailVO vo = new PmgBatDwellingDetailVO();
            BeanUtils.copyProperties(byId, vo);
            PmgBatParkInfo pmgBatParkInfo = pmgBatParkInfoService.getById(byId.getProjectId());
            vo.setParkName(pmgBatParkInfo.getParkName());
            LambdaQueryWrapper<PmgBatDwellingConstruction> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(PmgBatDwellingConstruction::getDwellingId, id);
            List<PmgBatDwellingConstruction> constructions = pmgBatDwellingConstructionMapper.selectList(queryWrapper);
            vo.setConstructions(constructions);
            return vo;
        }
        return null;
    }
}
