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.SaasParkPropertyFeeCostAdjustmentDto;
import com.yuchen.saas.park.api.dto.property.SaasParkPropertyFeeCostAdjustmentDto;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyFeeCostAdjustment;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyFeeCostAdjustment;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyFeeCostAdjustmentVo;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyFeeCostAdjustmentVo;
import com.yuchen.saas.park.service.mapper.property.SaasParkPropertyFeeCostAdjustmentMapper;
import com.yuchen.saas.park.service.service.property.ISaasParkPropertyFeeCostAdjustmentService;
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.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 SaasParkPropertyFeeCostAdjustmentServiceImpl extends BaseServiceImpl<SaasParkPropertyFeeCostAdjustmentMapper, SaasParkPropertyFeeCostAdjustment> implements ISaasParkPropertyFeeCostAdjustmentService {



    @Transactional(rollbackFor = Exception.class)
    @Override
    public Object saveOrUpdateByParam(SaasParkPropertyFeeCostAdjustmentDto saasParkPropertyFeeCostAdjustmentDto){

        SaasParkPropertyFeeCostAdjustment saasParkPropertyFeeCostAdjustment = new SaasParkPropertyFeeCostAdjustment();
        BeanUtil.copyProperties(saasParkPropertyFeeCostAdjustmentDto, saasParkPropertyFeeCostAdjustment);
        //保存
        boolean result = saveOrUpdate(saasParkPropertyFeeCostAdjustment);
        return result;
    }

    @Override
    public R<IPage<SaasParkPropertyFeeCostAdjustmentVo>> getList(SaasParkPropertyFeeCostAdjustmentDto saasParkPropertyFeeCostAdjustmentDto, Query query) {

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

        List<SaasParkPropertyFeeCostAdjustment> saasParkPropertyFeeCostAdjustmentList = pages.getRecords();

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

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

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

    }

    public R<SaasParkPropertyFeeCostAdjustmentVo> detail(Long id, SaasParkPropertyFeeCostAdjustment saasParkPropertyFeeCostAdjustment) {
        SaasParkPropertyFeeCostAdjustmentVo saasParkPropertyFeeCostAdjustmentVo = new SaasParkPropertyFeeCostAdjustmentVo();
        BeanUtils.copyProperties(saasParkPropertyFeeCostAdjustment, saasParkPropertyFeeCostAdjustmentVo);
        return R.data(saasParkPropertyFeeCostAdjustmentVo);
    }















}
