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.SaasParkPropertyFeePreStoredDto;
import com.yuchen.saas.park.api.dto.property.SaasParkPropertyFeePreStoredDto;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyFeePreStored;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyFeePreStored;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyFeePreStoredVo;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyFeePreStoredVo;
import com.yuchen.saas.park.service.mapper.property.SaasParkPropertyFeePreStoredMapper;
import com.yuchen.saas.park.service.service.property.ISaasParkPropertyFeePreStoredService;
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 SaasParkPropertyFeePreStoredServiceImpl extends BaseServiceImpl<SaasParkPropertyFeePreStoredMapper, SaasParkPropertyFeePreStored> implements ISaasParkPropertyFeePreStoredService {






    @Transactional(rollbackFor = Exception.class)
    @Override
    public Object saveOrUpdateByParam(SaasParkPropertyFeePreStoredDto saasParkPropertyFeePreStoredDto) {

        SaasParkPropertyFeePreStored saasParkPropertyFeePreStored = new SaasParkPropertyFeePreStored();
        BeanUtil.copyProperties(saasParkPropertyFeePreStoredDto, saasParkPropertyFeePreStored);
        //保存
        boolean result = saveOrUpdate(saasParkPropertyFeePreStored);
        return result;
    }

    @Override
    public R<IPage<SaasParkPropertyFeePreStoredVo>> getList(SaasParkPropertyFeePreStoredDto saasParkPropertyFeePreStoredDto, Query query) {

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

        List<SaasParkPropertyFeePreStored> saasParkPropertyFeePreStoredList = pages.getRecords();

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

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

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

    }

    public R<SaasParkPropertyFeePreStoredVo> detail(Long id, SaasParkPropertyFeePreStored saasParkPropertyFeePreStored) {
        SaasParkPropertyFeePreStoredVo saasParkPropertyFeePreStoredVo = new SaasParkPropertyFeePreStoredVo();
        BeanUtils.copyProperties(saasParkPropertyFeePreStored, saasParkPropertyFeePreStoredVo);
        return R.data(saasParkPropertyFeePreStoredVo);
    }















}
