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.entity.elevator.SaasParkElevatorMaintenanceWarn;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyFeeItemDetail;
import com.yuchen.saas.park.api.response.ElevatorMonitorRunningStateResponse;
import com.yuchen.saas.park.api.response.MaintenanceWarnResponse;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyFeeItemDetailVo;
import com.yuchen.saas.park.service.mapper.property.SaasParkPropertyFeeItemDetailMapper;
import com.yuchen.saas.park.service.service.property.ISaasParkPropertyFeeItemDetailService;
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 SaasParkPropertyFeeItemDetailServiceImpl extends BaseServiceImpl<SaasParkPropertyFeeItemDetailMapper, SaasParkPropertyFeeItemDetail> implements ISaasParkPropertyFeeItemDetailService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveOrUpdateByParam(SaasParkPropertyFeeItemDetailDto saasParkPropertyFeeItemDetailDto) {
        SaasParkPropertyFeeItemDetail saasParkPropertyFeeItemDetail = new SaasParkPropertyFeeItemDetail();
        BeanUtil.copyProperties(saasParkPropertyFeeItemDetailDto, saasParkPropertyFeeItemDetail);
        //保存
        boolean result = saveOrUpdate(saasParkPropertyFeeItemDetail);
        return result;
    }

    @Override
    public R<IPage<SaasParkPropertyFeeItemDetailVo>> getList(SaasParkPropertyFeeItemDetailDto saasParkPropertyFeeItemDetailDto, Query query) {

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

        List<SaasParkPropertyFeeItemDetail> saasParkPropertyFeeItemDetailList = pages.getRecords();

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

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

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

    }

    public R<SaasParkPropertyFeeItemDetailVo> detail(Long id, SaasParkPropertyFeeItemDetail saasParkPropertyFeeItemDetail) {
        SaasParkPropertyFeeItemDetailVo saasParkPropertyFeeItemDetailVo = new SaasParkPropertyFeeItemDetailVo();
        BeanUtils.copyProperties(saasParkPropertyFeeItemDetail, saasParkPropertyFeeItemDetailVo);
        return R.data(saasParkPropertyFeeItemDetailVo);
    }


}
