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.SaasParkPropertyFeeItemDetailDto;
import com.yuchen.saas.park.api.dto.property.SaasParkPropertyFeeStandardsDto;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyFeeItemDetail;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyFeeStandards;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyFeeItemDetailVo;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyFeeStandardsVo;
import com.yuchen.saas.park.service.mapper.property.SaasParkPropertyFeeStandardsMapper;
import com.yuchen.saas.park.service.service.property.ISaasParkPropertyFeeStandardsService;
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-10
 */
@Service
public class SaasParkPropertyFeeStandardsServiceImpl extends BaseServiceImpl<SaasParkPropertyFeeStandardsMapper, SaasParkPropertyFeeStandards> implements ISaasParkPropertyFeeStandardsService {






    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveOrUpdateByParam(SaasParkPropertyFeeStandardsDto saasParkPropertyFeeStandardsDto) {
        SaasParkPropertyFeeStandards saasParkPropertyFeeStandards = new SaasParkPropertyFeeStandards();
        BeanUtil.copyProperties(saasParkPropertyFeeStandardsDto, saasParkPropertyFeeStandards);
        //保存
        boolean result = saveOrUpdate(saasParkPropertyFeeStandards);
        return result;
    }

    @Override
    public R<IPage<SaasParkPropertyFeeStandardsVo>> getList(SaasParkPropertyFeeStandardsDto saasParkPropertyFeeStandardsDto, Query query) {

        Page<SaasParkPropertyFeeStandards> page = new Page<>(query.getCurrent(), query.getSize());
        //分页查询
        LambdaQueryWrapper<SaasParkPropertyFeeStandards> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Func.isNotEmpty(saasParkPropertyFeeStandardsDto.getStandardsName()),SaasParkPropertyFeeStandards::getStandardsName, saasParkPropertyFeeStandardsDto.getStandardsName())
                .orderByDesc(SaasParkPropertyFeeStandards::getCreateTime);
        IPage<SaasParkPropertyFeeStandards> pages  = this.page(page, lambdaQueryWrapper);

        List<SaasParkPropertyFeeStandards> saasParkPropertyFeeStandardsList = pages.getRecords();

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

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

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

    }

    public R<SaasParkPropertyFeeStandardsVo> detail(Long id, SaasParkPropertyFeeStandards saasParkPropertyFeeStandards) {
        SaasParkPropertyFeeStandardsVo saasParkPropertyFeeStandardsVo = new SaasParkPropertyFeeStandardsVo();
        BeanUtils.copyProperties(saasParkPropertyFeeStandards, saasParkPropertyFeeStandardsVo);
        return R.data(saasParkPropertyFeeStandardsVo);
    }









}
