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.SaasParkPropertyFeeTemporaryChargesDto;
import com.yuchen.saas.park.api.dto.property.SaasParkPropertyFeeTemporaryChargesDto;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyFeeTemporaryCharges;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyFeeTemporaryCharges;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyFeeTemporaryChargesVo;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyFeeTemporaryChargesVo;
import com.yuchen.saas.park.service.mapper.property.SaasParkPropertyFeeTemporaryChargesMapper;
import com.yuchen.saas.park.service.service.property.ISaasParkPropertyFeeTemporaryChargesService;
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 SaasParkPropertyFeeTemporaryChargesServiceImpl extends BaseServiceImpl<SaasParkPropertyFeeTemporaryChargesMapper, SaasParkPropertyFeeTemporaryCharges> implements ISaasParkPropertyFeeTemporaryChargesService {



    @Transactional(rollbackFor = Exception.class)
    @Override
    public Object saveOrUpdateByParam(SaasParkPropertyFeeTemporaryChargesDto saasParkPropertyFeeTemporaryChargesDto) {
        SaasParkPropertyFeeTemporaryCharges saasParkPropertyFeeTemporaryCharges = new SaasParkPropertyFeeTemporaryCharges();
        BeanUtil.copyProperties(saasParkPropertyFeeTemporaryChargesDto, saasParkPropertyFeeTemporaryCharges);
        //保存
        boolean result = saveOrUpdate(saasParkPropertyFeeTemporaryCharges);
        return result;
    }

    @Override
    public R<IPage<SaasParkPropertyFeeTemporaryChargesVo>> getList(SaasParkPropertyFeeTemporaryChargesDto saasParkPropertyFeeTemporaryChargesDto, Query query){

        Page<SaasParkPropertyFeeTemporaryCharges> page = new Page<>(query.getCurrent(), query.getSize());
        //分页查询
        LambdaQueryWrapper<SaasParkPropertyFeeTemporaryCharges> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Func.isNotEmpty(saasParkPropertyFeeTemporaryChargesDto.getPersonName()), SaasParkPropertyFeeTemporaryCharges::getPersonName, saasParkPropertyFeeTemporaryChargesDto.getPersonName());
        lambdaQueryWrapper.eq(Func.isNotEmpty(saasParkPropertyFeeTemporaryChargesDto.getRoomNo()), SaasParkPropertyFeeTemporaryCharges::getRoomNo, saasParkPropertyFeeTemporaryChargesDto.getRoomNo())
                .orderByDesc(SaasParkPropertyFeeTemporaryCharges::getCreateTime);
        IPage<SaasParkPropertyFeeTemporaryCharges> pages = this.page(page, lambdaQueryWrapper);

        List<SaasParkPropertyFeeTemporaryCharges> saasParkPropertyFeeTemporaryChargesList = pages.getRecords();

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

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

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

    }

    public R<SaasParkPropertyFeeTemporaryChargesVo> detail(Long id, SaasParkPropertyFeeTemporaryCharges saasParkPropertyFeeTemporaryCharges) {
        SaasParkPropertyFeeTemporaryChargesVo saasParkPropertyFeeTemporaryChargesVo = new SaasParkPropertyFeeTemporaryChargesVo();
        BeanUtils.copyProperties(saasParkPropertyFeeTemporaryCharges, saasParkPropertyFeeTemporaryChargesVo);
        return R.data(saasParkPropertyFeeTemporaryChargesVo);
    }














}
