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.SaasParkPropertyHouseRenovationHandleTypeDto;
import com.yuchen.saas.park.api.entity.renovation.SaasParkPropertyHouseRenovationHandleType;
import com.yuchen.saas.park.api.vo.renovation.SaasParkPropertyHouseRenovationHandleTypeVo;
import com.yuchen.saas.park.service.mapper.renovation.SaasParkPropertyHouseRenovationHandleTypeMapper;
import com.yuchen.saas.park.service.service.renovation.ISaasParkPropertyHouseRenovationHandleTypeService;
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 SaasParkPropertyHouseRenovationHandleTypeServiceImpl extends BaseServiceImpl<SaasParkPropertyHouseRenovationHandleTypeMapper, SaasParkPropertyHouseRenovationHandleType> implements ISaasParkPropertyHouseRenovationHandleTypeService {

    @Override
    public R<IPage<SaasParkPropertyHouseRenovationHandleTypeVo>> getList(SaasParkPropertyHouseRenovationHandleTypeDto saasParkPropertyHouseRenovationHandleTypeDto, Query query) {

        Page<SaasParkPropertyHouseRenovationHandleType> page = new Page<>(query.getCurrent(), query.getSize());
        //分页查询
        LambdaQueryWrapper<SaasParkPropertyHouseRenovationHandleType> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Func.isNotEmpty(saasParkPropertyHouseRenovationHandleTypeDto.getParkId()), SaasParkPropertyHouseRenovationHandleType::getParkId, saasParkPropertyHouseRenovationHandleTypeDto.getParkId())
                .orderByDesc(SaasParkPropertyHouseRenovationHandleType::getCreateTime);
        IPage<SaasParkPropertyHouseRenovationHandleType> pages = this.page(page, lambdaQueryWrapper);

        List<SaasParkPropertyHouseRenovationHandleType> saasParkPropertyHouseRenovationHandleTypeList = pages.getRecords();

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

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

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

    }

    public R<SaasParkPropertyHouseRenovationHandleTypeVo> detail(Long id, SaasParkPropertyHouseRenovationHandleType saasParkPropertyHouseRenovationHandleType) {
        SaasParkPropertyHouseRenovationHandleTypeVo saasParkPropertyHouseRenovationHandleTypeVo = new SaasParkPropertyHouseRenovationHandleTypeVo();
        BeanUtils.copyProperties(saasParkPropertyHouseRenovationHandleType, saasParkPropertyHouseRenovationHandleTypeVo);
        return R.data(saasParkPropertyHouseRenovationHandleTypeVo);
    }
}
