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.SaasParkPropertyBillOverdueBillDto;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyBillOverdueBill;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyBillOverdueBillVo;
import com.yuchen.saas.park.service.mapper.property.SaasParkPropertyBillOverdueBillMapper;
import com.yuchen.saas.park.service.service.property.ISaasParkPropertyBillOverdueBillService;
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 SaasParkPropertyBillOverdueBillServiceImpl extends BaseServiceImpl<SaasParkPropertyBillOverdueBillMapper, SaasParkPropertyBillOverdueBill> implements ISaasParkPropertyBillOverdueBillService {



    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveOrUpdateByParam(SaasParkPropertyBillOverdueBillDto saasParkPropertyBillOverdueBillDto) {
        SaasParkPropertyBillOverdueBill saasParkPropertyBillOverdueBill = new SaasParkPropertyBillOverdueBill();
        BeanUtil.copyProperties(saasParkPropertyBillOverdueBillDto, saasParkPropertyBillOverdueBill);
        //保存
        boolean result = saveOrUpdate(saasParkPropertyBillOverdueBill);
        return result;
    }

    @Override
    public Boolean call(Long id) {
        return false;
    }


    @Override
    public R<IPage<SaasParkPropertyBillOverdueBillVo>> getList(SaasParkPropertyBillOverdueBillDto saasParkPropertyBillOverdueBillDto, Query query) {
        Page<SaasParkPropertyBillOverdueBill> page = new Page<>(query.getCurrent(), query.getSize());
        //分页查询
        LambdaQueryWrapper<SaasParkPropertyBillOverdueBill> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Func.isNotEmpty(saasParkPropertyBillOverdueBillDto.getParkId()), SaasParkPropertyBillOverdueBill::getParkId, saasParkPropertyBillOverdueBillDto.getParkId())
                //楼栋id
                .eq(Func.isNotEmpty(saasParkPropertyBillOverdueBillDto.getBuildingId()), SaasParkPropertyBillOverdueBill::getBuildingId, saasParkPropertyBillOverdueBillDto.getBuildingId())
                //应收起止日期
                .ge(Func.isNotEmpty(saasParkPropertyBillOverdueBillDto.getStartDate()), SaasParkPropertyBillOverdueBill::getReceivableDate, saasParkPropertyBillOverdueBillDto.getStartDate())
                .le(Func.isNotEmpty(saasParkPropertyBillOverdueBillDto.getStartDate()), SaasParkPropertyBillOverdueBill::getReceivableDate, saasParkPropertyBillOverdueBillDto.getStartDate())
                //收费类型id
                .eq(Func.isNotEmpty(saasParkPropertyBillOverdueBillDto.getFeeTypeId()), SaasParkPropertyBillOverdueBill::getFeeTypeId, saasParkPropertyBillOverdueBillDto.getFeeTypeId())
                .orderByDesc(SaasParkPropertyBillOverdueBill::getCreateTime);
        IPage<SaasParkPropertyBillOverdueBill> pages = this.page(page, lambdaQueryWrapper);

        List<SaasParkPropertyBillOverdueBill> saasParkPropertyBillOverdueBillList = pages.getRecords();

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

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

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

    public R<SaasParkPropertyBillOverdueBillVo> detail(Long id, SaasParkPropertyBillOverdueBill saasParkPropertyBillOverdueBill) {
        SaasParkPropertyBillOverdueBillVo saasParkPropertyBillOverdueBillVo = new SaasParkPropertyBillOverdueBillVo();
        BeanUtils.copyProperties(saasParkPropertyBillOverdueBill, saasParkPropertyBillOverdueBillVo);
        return R.data(saasParkPropertyBillOverdueBillVo);
    }















}

