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.SaasParkPropertyFeeOfflinePayDto;
import com.yuchen.saas.park.api.dto.property.SaasParkPropertyFeeOfflinePayDto;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyFeeOfflinePay;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyFeeOfflinePay;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyFeeOfflinePayVo;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyFeeOfflinePayVo;
import com.yuchen.saas.park.service.mapper.property.SaasParkPropertyFeeOfflinePayMapper;
import com.yuchen.saas.park.service.service.property.ISaasParkPropertyFeeOfflinePayService;
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 SaasParkPropertyFeeOfflinePayServiceImpl extends BaseServiceImpl<SaasParkPropertyFeeOfflinePayMapper, SaasParkPropertyFeeOfflinePay> implements ISaasParkPropertyFeeOfflinePayService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Object saveOrUpdateByParam(SaasParkPropertyFeeOfflinePayDto saasParkPropertyFeeOfflinePayDto){

        SaasParkPropertyFeeOfflinePay saasParkPropertyFeeOfflinePay = new SaasParkPropertyFeeOfflinePay();
        BeanUtil.copyProperties(saasParkPropertyFeeOfflinePayDto, saasParkPropertyFeeOfflinePay);
        //保存
        boolean result = saveOrUpdate(saasParkPropertyFeeOfflinePay);
        return result;
    }

    @Override
    public R<IPage<SaasParkPropertyFeeOfflinePayVo>> getList(SaasParkPropertyFeeOfflinePayDto saasParkPropertyFeeOfflinePayDto, Query query) {

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

        List<SaasParkPropertyFeeOfflinePay> saasParkPropertyFeeOfflinePayList = pages.getRecords();

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

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

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

    }

    public R<SaasParkPropertyFeeOfflinePayVo> detail(Long id, SaasParkPropertyFeeOfflinePay saasParkPropertyFeeOfflinePay) {
        SaasParkPropertyFeeOfflinePayVo saasParkPropertyFeeOfflinePayVo = new SaasParkPropertyFeeOfflinePayVo();
        BeanUtils.copyProperties(saasParkPropertyFeeOfflinePay, saasParkPropertyFeeOfflinePayVo);
        return R.data(saasParkPropertyFeeOfflinePayVo);
    }












}
