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.SaasParkPropertyPayReductionDto;
import com.yuchen.saas.park.api.dto.property.SaasParkPropertyReductionDetailDto;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyPayReduction;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyReductionDetail;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyPayReductionVo;
import com.yuchen.saas.park.service.mapper.property.SaasParkPropertyPayReductionMapper;
import com.yuchen.saas.park.service.service.property.ISaasParkPropertyPayReductionService;
import com.yuchen.saas.park.service.service.property.ISaasParkPropertyReductionDetailService;
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.beans.factory.annotation.Autowired;
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-18
 */
@Service
public class SaasParkPropertyPayReductionServiceImpl extends BaseServiceImpl<SaasParkPropertyPayReductionMapper, SaasParkPropertyPayReduction> implements ISaasParkPropertyPayReductionService {


    @Autowired
    private ISaasParkPropertyReductionDetailService iSaasParkPropertyReductionDetailService;


    @Override
    public R<IPage<SaasParkPropertyPayReductionVo>> getList(SaasParkPropertyPayReductionDto saasParkPropertyPayReductionDto, Query query) {
        Page<SaasParkPropertyPayReduction> page = new Page<>(query.getCurrent(), query.getSize());
        //分页查询
        LambdaQueryWrapper<SaasParkPropertyPayReduction> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Func.isNotEmpty(saasParkPropertyPayReductionDto.getParkId()), SaasParkPropertyPayReduction::getParkId, saasParkPropertyPayReductionDto.getParkId())
                //状态
                .in(SaasParkPropertyPayReduction::getStatus, saasParkPropertyPayReductionDto.getStatusList())
                //资源代码
                .eq(Func.isNotEmpty(saasParkPropertyPayReductionDto.getResourceCode()), SaasParkPropertyPayReduction::getResourceCode, saasParkPropertyPayReductionDto.getResourceCode())
                //收费项目id
                .eq(Func.isNotEmpty(saasParkPropertyPayReductionDto.getPayServiceId()), SaasParkPropertyPayReduction::getPayServiceId, saasParkPropertyPayReductionDto.getPayServiceId())
                //收费标准id
                .eq(Func.isNotEmpty(saasParkPropertyPayReductionDto.getFeeStandardsId()), SaasParkPropertyPayReduction::getFeeStandardsId, saasParkPropertyPayReductionDto.getFeeStandardsId())
                //客户名称
                .eq(Func.isNotEmpty(saasParkPropertyPayReductionDto.getPersonName()), SaasParkPropertyPayReduction::getPersonName, saasParkPropertyPayReductionDto.getPersonName())
                //楼栋
                .eq(Func.isNotEmpty(saasParkPropertyPayReductionDto.getBuildingId()), SaasParkPropertyPayReduction::getBuildingId, saasParkPropertyPayReductionDto.getBuildingId())
                .orderByDesc(SaasParkPropertyPayReduction::getSequence);
        IPage<SaasParkPropertyPayReduction> pages = this.page(page, lambdaQueryWrapper);

        List<SaasParkPropertyPayReduction> saasParkPropertyPayReductionList = pages.getRecords();

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

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

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

    }

    public R<SaasParkPropertyPayReductionVo> detail(Long id, SaasParkPropertyPayReduction saasParkPropertyPayReduction) {
        SaasParkPropertyPayReductionVo saasParkPropertyPayReductionVo = new SaasParkPropertyPayReductionVo();
        BeanUtils.copyProperties(saasParkPropertyPayReduction, saasParkPropertyPayReductionVo);
        return R.data(saasParkPropertyPayReductionVo);
    }



    @Transactional(rollbackFor = Exception.class)
    @Override
    public Object saveOrUpdateByParam(SaasParkPropertyPayReductionDto saasParkPropertyDirectReductionDto) {
        SaasParkPropertyPayReduction saasParkPropertyPayReduction = new SaasParkPropertyPayReduction();
        BeanUtil.copyProperties(saasParkPropertyDirectReductionDto, saasParkPropertyPayReduction);

        List<SaasParkPropertyReductionDetailDto> saasParkPropertyReductionDetailDtoList = saasParkPropertyDirectReductionDto.getSaasParkPropertyReductionDetailDtoList();
        for (int i = 0; i < saasParkPropertyReductionDetailDtoList.size(); i++) {
            SaasParkPropertyReductionDetail saasParkPropertyReductionDetail = new SaasParkPropertyReductionDetail();
            BeanUtil.copyProperties(saasParkPropertyReductionDetailDtoList.get(i), saasParkPropertyReductionDetail);
            iSaasParkPropertyReductionDetailService.saveOrUpdate(saasParkPropertyReductionDetail);
            Long id = saasParkPropertyReductionDetail.getId();
            if (i == saasParkPropertyReductionDetailDtoList.size() - 1) {
                saasParkPropertyPayReduction.setReductionDetail(id + "");
            } else {
                saasParkPropertyPayReduction.setReductionDetail(id + ",");
            }
        }

        //保存
        boolean result = saveOrUpdate(saasParkPropertyPayReduction);
        return result;
    }





}
