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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuchen.saas.park.api.dto.renovation.SaasParkPropertyHouseRenovationTypeDto;
import com.yuchen.saas.park.api.entity.renovation.SaasParkPropertyHouseRenovationType;
import com.yuchen.saas.park.api.vo.renovation.SaasParkPropertyHouseRenovationTypeVo;
import com.yuchen.saas.park.service.mapper.renovation.SaasParkPropertyHouseRenovationTypeMapper;
import com.yuchen.saas.park.service.service.renovation.ISaasParkPropertyHouseRenovationTypeService;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.mp.support.Condition;
import org.nest.springwrap.core.mp.support.Query;
import org.nest.springwrap.core.tool.api.R;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 房源-装修类型表 服务实现类
 * </p>
 *
 * @author baichaojie
 * @since 2024-12-02
 */
@Service
public class SaasParkPropertyHouseRenovationTypeServiceImpl extends BaseServiceImpl<SaasParkPropertyHouseRenovationTypeMapper, SaasParkPropertyHouseRenovationType> implements ISaasParkPropertyHouseRenovationTypeService {

    @Override
    public R<IPage<SaasParkPropertyHouseRenovationTypeVo>> getList(SaasParkPropertyHouseRenovationTypeDto saasParkPropertyHouseRenovationTypeDto, Query query) {
        Page<SaasParkPropertyHouseRenovationType> page = new Page<>(query.getCurrent(), query.getSize());
        //分页查询
        LambdaQueryWrapper<SaasParkPropertyHouseRenovationType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Func.isNotEmpty(saasParkPropertyHouseRenovationTypeDto.getParkId()), SaasParkPropertyHouseRenovationType::getParkId, saasParkPropertyHouseRenovationTypeDto.getParkId())
                .orderByDesc(SaasParkPropertyHouseRenovationType::getCreateTime);
        IPage<SaasParkPropertyHouseRenovationType> pages = this.page(page, lambdaQueryWrapper);

        List<SaasParkPropertyHouseRenovationType> saasParkPropertyHouseRenovationTypeList = pages.getRecords();

        if (saasParkPropertyHouseRenovationTypeList.isEmpty()) {
            return R.data(Condition.getPage(query));
        }
        //组装返回response
        IPage<SaasParkPropertyHouseRenovationTypeVo> pageVo = new Page(pages.getCurrent(), pages.getSize(), pages.getTotal());
        List<SaasParkPropertyHouseRenovationTypeVo> records = this.listVO(pages.getRecords());

        pageVo.setRecords(records);
        return R.data(pageVo);
    }

    private List<SaasParkPropertyHouseRenovationTypeVo> listVO(List<SaasParkPropertyHouseRenovationType> listRecords) {
        return (List) listRecords.stream().map(entity -> {
            SaasParkPropertyHouseRenovationTypeVo saasParkPropertyHouseRenovationTypeVo = detail(entity.getId(), entity).getData();
            return saasParkPropertyHouseRenovationTypeVo;
        }).collect(Collectors.toList());

    }

    public R<SaasParkPropertyHouseRenovationTypeVo> detail(Long id, SaasParkPropertyHouseRenovationType saasParkPropertyHouseRenovationType) {
        SaasParkPropertyHouseRenovationTypeVo saasParkPropertyHouseRenovationTypeVo = new SaasParkPropertyHouseRenovationTypeVo();
        BeanUtils.copyProperties(saasParkPropertyHouseRenovationType, saasParkPropertyHouseRenovationTypeVo);
        return R.data(saasParkPropertyHouseRenovationTypeVo);
    }
}
