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.SaasParkPropertyFeePreAccountDto;
import com.yuchen.saas.park.api.entity.elevator.SaasParkPropertyFeePreAccount;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyFeePreAccountVo;
import com.yuchen.saas.park.service.mapper.property.SaasParkPropertyFeePreAccountMapper;
import com.yuchen.saas.park.service.service.property.ISaasParkPropertyFeePreAccountService;
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-10-31
 */
@Service
public class SaasParkPropertyFeePreAccountServiceImpl extends BaseServiceImpl<SaasParkPropertyFeePreAccountMapper, SaasParkPropertyFeePreAccount> implements ISaasParkPropertyFeePreAccountService {





    @Transactional(rollbackFor = Exception.class)
    @Override
    public Object saveOrUpdateByParam(SaasParkPropertyFeePreAccountDto saasParkPropertyFeePreAccountDto) {

        SaasParkPropertyFeePreAccount saasParkPropertyFeePreAccount = new SaasParkPropertyFeePreAccount();
        BeanUtil.copyProperties(saasParkPropertyFeePreAccountDto, saasParkPropertyFeePreAccount);
        //保存
        boolean result = saveOrUpdate(saasParkPropertyFeePreAccount);
        return result;
    }

    @Override
    public R<IPage<SaasParkPropertyFeePreAccountVo>> getList(SaasParkPropertyFeePreAccountDto saasParkPropertyFeePreAccountDto, Query query) {

        Page<SaasParkPropertyFeePreAccount> page = new Page<>(query.getCurrent(), query.getSize());
        //分页查询
        LambdaQueryWrapper<SaasParkPropertyFeePreAccount> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Func.isNotEmpty(saasParkPropertyFeePreAccountDto.getParkId()), SaasParkPropertyFeePreAccount::getParkId, saasParkPropertyFeePreAccountDto.getParkId());
        lambdaQueryWrapper.eq(Func.isNotEmpty(saasParkPropertyFeePreAccountDto.getPersonId()), SaasParkPropertyFeePreAccount::getPersonId, saasParkPropertyFeePreAccountDto.getPersonId());
        lambdaQueryWrapper.eq(Func.isNotEmpty(saasParkPropertyFeePreAccountDto.getSourceId()), SaasParkPropertyFeePreAccount::getSourceId, saasParkPropertyFeePreAccountDto.getSourceId());
        lambdaQueryWrapper.eq(Func.isNotEmpty(saasParkPropertyFeePreAccountDto.getAccountName()), SaasParkPropertyFeePreAccount::getAccountName, saasParkPropertyFeePreAccountDto.getAccountName());
        lambdaQueryWrapper.orderByDesc(SaasParkPropertyFeePreAccount::getCreateTime);
        IPage<SaasParkPropertyFeePreAccount> pages = this.page(page, lambdaQueryWrapper);

        List<SaasParkPropertyFeePreAccount> saasParkPropertyFeePreAccountList = pages.getRecords();

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

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

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

    }

    public R<SaasParkPropertyFeePreAccountVo> detail(Long id, SaasParkPropertyFeePreAccount saasParkPropertyFeePreAccount) {
        SaasParkPropertyFeePreAccountVo saasParkPropertyFeePreAccountVo = new SaasParkPropertyFeePreAccountVo();
        BeanUtils.copyProperties(saasParkPropertyFeePreAccount, saasParkPropertyFeePreAccountVo);
        return R.data(saasParkPropertyFeePreAccountVo);
    }









}
