package com.yuchen.saas.park.service.service.property.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.property.SaasParkPropertyFeePrePaySelectionDto;
import com.yuchen.saas.park.api.dto.property.SaasParkPropertyFeePrePaySelectionDto;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyFeePrePaySelection;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyFeePrePaySelection;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyFeePrePaySelectionVo;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyFeePrePaySelectionVo;
import com.yuchen.saas.park.service.mapper.property.SaasParkPropertyFeePrePaySelectionMapper;
import com.yuchen.saas.park.service.service.property.ISaasParkPropertyFeePrePaySelectionService;
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.BeanUtil;
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 java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 物业-收费-前台收费-预收选择表 服务实现类
 * </p>
 *
 * @author baichaojie
 * @since 2024-09-26
 */
@Service
public class SaasParkPropertyFeePrePaySelectionServiceImpl extends BaseServiceImpl<SaasParkPropertyFeePrePaySelectionMapper, SaasParkPropertyFeePrePaySelection> implements ISaasParkPropertyFeePrePaySelectionService {


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Object saveOrUpdateByParam(SaasParkPropertyFeePrePaySelectionDto saasParkPropertyFeePrePaySelectionDto){

        SaasParkPropertyFeePrePaySelection saasParkPropertyFeePrePaySelection = new SaasParkPropertyFeePrePaySelection();
        BeanUtil.copyProperties(saasParkPropertyFeePrePaySelectionDto, saasParkPropertyFeePrePaySelection);
        //保存
        boolean result = saveOrUpdate(saasParkPropertyFeePrePaySelection);
        return result;
    }

    @Override
    public R<IPage<SaasParkPropertyFeePrePaySelectionVo>> getList(SaasParkPropertyFeePrePaySelectionDto saasParkPropertyFeePrePaySelectionDto, Query query) {

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

        List<SaasParkPropertyFeePrePaySelection> saasParkPropertyFeePrePaySelectionList = pages.getRecords();

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

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

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

    }

    public R<SaasParkPropertyFeePrePaySelectionVo> detail(Long id, SaasParkPropertyFeePrePaySelection saasParkPropertyFeePrePaySelection) {
        SaasParkPropertyFeePrePaySelectionVo saasParkPropertyFeePrePaySelectionVo = new SaasParkPropertyFeePrePaySelectionVo();
        BeanUtils.copyProperties(saasParkPropertyFeePrePaySelection, saasParkPropertyFeePrePaySelectionVo);
        return R.data(saasParkPropertyFeePrePaySelectionVo);
    }














}
