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.SaasParkPropertyFeeItemDto;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyFeeItem;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyFeeItemVo;
import com.yuchen.saas.park.service.mapper.property.SaasParkPropertyFeeItemMapper;
import com.yuchen.saas.park.service.service.property.ISaasParkPropertyFeeItemService;
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-06
 */
@Service
public class SaasParkPropertyFeeItemServiceImpl extends BaseServiceImpl<SaasParkPropertyFeeItemMapper, SaasParkPropertyFeeItem> implements ISaasParkPropertyFeeItemService {


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Object saveOrUpdateByParam(SaasParkPropertyFeeItemDto saasParkPropertyFeeItemDto) {
        SaasParkPropertyFeeItem saasParkPropertyFeeItem = new SaasParkPropertyFeeItem();
        BeanUtil.copyProperties(saasParkPropertyFeeItemDto, saasParkPropertyFeeItem);
        //保存
        boolean result = saveOrUpdate(saasParkPropertyFeeItem);
        return result;
    }

    @Override
    public R<IPage<SaasParkPropertyFeeItemVo>> getList(SaasParkPropertyFeeItemDto saasParkPropertyFeeItemDto, Query query) {

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

        List<SaasParkPropertyFeeItem> saasParkPropertyFeeItemList = pages.getRecords();

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

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

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

    }

    public R<SaasParkPropertyFeeItemVo> detail(Long id, SaasParkPropertyFeeItem saasParkPropertyFeeItem) {
        SaasParkPropertyFeeItemVo saasParkPropertyFeeItemVo = new SaasParkPropertyFeeItemVo();
        BeanUtils.copyProperties(saasParkPropertyFeeItem, saasParkPropertyFeeItemVo);
        return R.data(saasParkPropertyFeeItemVo);
    }

}
