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.integration.hik.isc.entity.PmgBatCameraDeviceInfo;
import com.yuchen.saas.park.api.dto.property.SaasParkPropertyDirectReductionDto;
import com.yuchen.saas.park.api.dto.property.SaasParkPropertyFeeItemDto;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyDirectReduction;
import com.yuchen.saas.park.api.entity.property.SaasParkPropertyFeeItem;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyDirectReductionVo;
import com.yuchen.saas.park.api.vo.property.SaasParkPropertyFeeItemVo;
import com.yuchen.saas.park.service.mapper.property.SaasParkPropertyDirectReductionMapper;
import com.yuchen.saas.park.service.service.property.ISaasParkPropertyDirectReductionService;
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-18
 */
@Service
public class SaasParkPropertyDirectReductionServiceImpl extends BaseServiceImpl<SaasParkPropertyDirectReductionMapper, SaasParkPropertyDirectReduction> implements ISaasParkPropertyDirectReductionService {

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

        List<SaasParkPropertyDirectReduction> saasParkPropertyDirectReductionList = pages.getRecords();

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

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

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

    }

    public R<SaasParkPropertyDirectReductionVo> detail(Long id, SaasParkPropertyDirectReduction saasParkPropertyDirectReduction) {
        SaasParkPropertyDirectReductionVo saasParkPropertyDirectReductionVo = new SaasParkPropertyDirectReductionVo();
        BeanUtils.copyProperties(saasParkPropertyDirectReduction, saasParkPropertyDirectReductionVo);
        return R.data(saasParkPropertyDirectReductionVo);
    }



    @Transactional(rollbackFor = Exception.class)
    @Override
    public Object saveOrUpdateByParam(SaasParkPropertyDirectReductionDto saasParkPropertyDirectReductionDto) {
        SaasParkPropertyDirectReduction saasParkPropertyDirectReduction = new SaasParkPropertyDirectReduction();
        BeanUtil.copyProperties(saasParkPropertyDirectReductionDto, saasParkPropertyDirectReduction);
        //保存
        boolean result = saveOrUpdate(saasParkPropertyDirectReduction);
        return result;
    }


}
