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.SaasParkPropertyFeeStandardsBindSourceDto;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyFeeStandardsBindSource;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyFeeStandardsBindSourceVo;
import com.yuchen.saas.park.service.mapper.property.SaasParkPropertyFeeStandardsBindSourceMapper;
import com.yuchen.saas.park.service.service.property.ISaasParkPropertyFeeStandardsBindSourceService;
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 SaasParkPropertyFeeStandardsBindSourceServiceImpl extends BaseServiceImpl<SaasParkPropertyFeeStandardsBindSourceMapper, SaasParkPropertyFeeStandardsBindSource> implements ISaasParkPropertyFeeStandardsBindSourceService {



    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveOrUpdateByParam(SaasParkPropertyFeeStandardsBindSourceDto saasParkPropertyFeeStandardsBindSourceDto) {
        SaasParkPropertyFeeStandardsBindSource saasParkPropertyFeeStandardsBindSource = new SaasParkPropertyFeeStandardsBindSource();
        BeanUtil.copyProperties(saasParkPropertyFeeStandardsBindSourceDto, saasParkPropertyFeeStandardsBindSource);
        //保存
        boolean result = saveOrUpdate(saasParkPropertyFeeStandardsBindSource);
        return result;
    }

    @Override
    public R<IPage<SaasParkPropertyFeeStandardsBindSourceVo>> getList(SaasParkPropertyFeeStandardsBindSourceDto saasParkPropertyFeeStandardsBindSourceDto, Query query) {

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

        List<SaasParkPropertyFeeStandardsBindSource> saasParkPropertyFeeStandardsBindSourceList = pages.getRecords();

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

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

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

    }

    public R<SaasParkPropertyFeeStandardsBindSourceVo> detail(Long id, SaasParkPropertyFeeStandardsBindSource saasParkPropertyFeeStandardsBindSource) {
        SaasParkPropertyFeeStandardsBindSourceVo saasParkPropertyFeeStandardsBindSourceVo = new SaasParkPropertyFeeStandardsBindSourceVo();
        BeanUtils.copyProperties(saasParkPropertyFeeStandardsBindSource, saasParkPropertyFeeStandardsBindSourceVo);
        return R.data(saasParkPropertyFeeStandardsBindSourceVo);
    }















}
