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.SaasParkPropertyFeeStandardsDto;
import com.yuchen.saas.park.api.dto.property.SaasParkPropertyFeeZeroAccountDto;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyFeeStandards;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyFeeZeroAccount;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyFeeStandardsVo;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyFeeZeroAccountVo;
import com.yuchen.saas.park.service.mapper.property.SaasParkPropertyFeeZeroAccountMapper;
import com.yuchen.saas.park.service.service.property.ISaasParkPropertyFeeZeroAccountService;
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-26
 */
@Service
public class SaasParkPropertyFeeZeroAccountServiceImpl extends BaseServiceImpl<SaasParkPropertyFeeZeroAccountMapper, SaasParkPropertyFeeZeroAccount> implements ISaasParkPropertyFeeZeroAccountService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Object saveOrUpdateByParam(SaasParkPropertyFeeZeroAccountDto saasParkPropertyFeeZeroAccountDto) {
        SaasParkPropertyFeeZeroAccount saasParkPropertyFeeZeroAccount = new SaasParkPropertyFeeZeroAccount();
        BeanUtil.copyProperties(saasParkPropertyFeeZeroAccountDto, saasParkPropertyFeeZeroAccount);
        //保存
        boolean result = saveOrUpdate(saasParkPropertyFeeZeroAccount);
        return result;
    }

    @Override
    public R<IPage<SaasParkPropertyFeeZeroAccountVo>> getList(SaasParkPropertyFeeZeroAccountDto saasParkPropertyFeeZeroAccountDto, Query query) {

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

        List<SaasParkPropertyFeeZeroAccount> saasParkPropertyFeeZeroAccountList = pages.getRecords();

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

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

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

    }

    public R<SaasParkPropertyFeeZeroAccountVo> detail(Long id, SaasParkPropertyFeeZeroAccount saasParkPropertyFeeZeroAccount) {
        SaasParkPropertyFeeZeroAccountVo saasParkPropertyFeeZeroAccountVo = new SaasParkPropertyFeeZeroAccountVo();
        BeanUtils.copyProperties(saasParkPropertyFeeZeroAccount, saasParkPropertyFeeZeroAccountVo);
        return R.data(saasParkPropertyFeeZeroAccountVo);
    }
}




