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.SaasParkPropertyFeeRefundCollectionDto;
import com.yuchen.saas.park.api.dto.property.SaasParkPropertyFeeRefundCollectionDto;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyFeeRefundCollection;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyFeeRefundCollection;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyFeeRefundCollectionVo;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyFeeRefundCollectionVo;
import com.yuchen.saas.park.service.mapper.property.SaasParkPropertyFeeRefundCollectionMapper;
import com.yuchen.saas.park.service.service.property.ISaasParkPropertyFeeRefundCollectionService;
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 SaasParkPropertyFeeRefundCollectionServiceImpl extends BaseServiceImpl<SaasParkPropertyFeeRefundCollectionMapper, SaasParkPropertyFeeRefundCollection> implements ISaasParkPropertyFeeRefundCollectionService {


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Object saveOrUpdateByParam(SaasParkPropertyFeeRefundCollectionDto saasParkPropertyFeeRefundCollectionDto) {

        SaasParkPropertyFeeRefundCollection saasParkPropertyFeeRefundCollection = new SaasParkPropertyFeeRefundCollection();
        BeanUtil.copyProperties(saasParkPropertyFeeRefundCollectionDto, saasParkPropertyFeeRefundCollection);
        //保存
        boolean result = saveOrUpdate(saasParkPropertyFeeRefundCollection);
        return result;
    }

    @Override
    public R<IPage<SaasParkPropertyFeeRefundCollectionVo>> getList(SaasParkPropertyFeeRefundCollectionDto saasParkPropertyFeeRefundCollectionDto, Query query) {

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

        List<SaasParkPropertyFeeRefundCollection> saasParkPropertyFeeRefundCollectionList = pages.getRecords();

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

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

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

    }

    public R<SaasParkPropertyFeeRefundCollectionVo> detail(Long id, SaasParkPropertyFeeRefundCollection saasParkPropertyFeeRefundCollection) {
        SaasParkPropertyFeeRefundCollectionVo saasParkPropertyFeeRefundCollectionVo = new SaasParkPropertyFeeRefundCollectionVo();
        BeanUtils.copyProperties(saasParkPropertyFeeRefundCollection, saasParkPropertyFeeRefundCollectionVo);
        return R.data(saasParkPropertyFeeRefundCollectionVo);
    }















}
