package com.yuchen.saas.park.service.service.property;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuchen.saas.park.api.dto.property.PropertyBillRefundPageQryDto;
import com.yuchen.saas.park.api.entity.property.PropertyBillRefund;
import com.yuchen.saas.park.api.vo.property.PropertyBillRefundListVo;
import com.yuchen.saas.park.api.vo.property.PropertyHouseParentInfoVo;
import com.yuchen.saas.park.service.event.property.SetCreateUserNameEvent;
import com.yuchen.saas.park.service.event.property.SetCustomerInfoEvent;
import com.yuchen.saas.park.service.mapper.property.PropertyBillRefundMapper;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PropertyBillRefundService extends ServiceImpl<PropertyBillRefundMapper, PropertyBillRefund> {
    @Resource
    private PropertySearchParkInfoService propertySearchParkInfoService;
    @Resource
    private PropertyHousesService propertyHousesService;
    @Resource
    private ApplicationContext applicationContext;


    /**
     * 退款记录查询
     * @param dto
     * @return
     */
   public Page<PropertyBillRefundListVo> selectPage(PropertyBillRefundPageQryDto dto) {
       Page<PropertyBillRefundListVo> page = new Page<>(dto.getCurrent(), dto.getSize());
       baseMapper.selectRefundPage(page, this.getQueryCondition(dto));
       this.assembleRefundListVo(page.getRecords());
       return page;
   }


    private LambdaQueryWrapper<PropertyBillRefund> getQueryCondition(PropertyBillRefundPageQryDto dto) {
        LambdaQueryWrapper<PropertyBillRefund> qw = new LambdaQueryWrapper<>();
        if (Func.isNotEmpty(dto.getParkId())) {
            qw.eq(PropertyBillRefund::getParkId, dto.getParkId());
        } else {
            qw.in(PropertyBillRefund::getParkId, propertySearchParkInfoService.getParkIdsByCurrUser());
        }
        qw.ge(Func.isNotEmpty(dto.getStartRefundTime()),PropertyBillRefund::getRefundTime,dto.getStartRefundTime())
                .le(Func.isNotEmpty(dto.getEndRefundTime()),PropertyBillRefund::getRefundTime,dto.getEndRefundTime())
                .eq(Func.isNotEmpty(dto.getHouseId()),PropertyBillRefund::getHouseId,dto.getHouseId())
        ;
        qw.orderByDesc(PropertyBillRefund::getCreateTime);
        return qw;
    }

    private void assembleRefundListVo(List<PropertyBillRefundListVo> records) {
        if (records == null || records.size() == 0) {
            return;
        }
        CopyOptions options = CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true);
        List<Long> houseIdList = records.stream().map(PropertyBillRefundListVo::getHouseId).collect(Collectors.toList());
        List<PropertyHouseParentInfoVo> houseInfoList = propertyHousesService.listHouseParentInfo(houseIdList);
        records.forEach(item -> {
            houseInfoList.forEach(info -> {
                if (info.getHouseId().equals(item.getHouseId())) {
                    BeanUtil.copyProperties(info, item, options);
                }
            });
        });

        List<Long> customerIdList = records.stream().map(PropertyBillRefundListVo::getCustomerId).collect(Collectors.toList());
        List<Long> userIdList = records.stream().map(PropertyBillRefundListVo::getCreateUser).collect(Collectors.toList());
        applicationContext.publishEvent(new SetCustomerInfoEvent(records, customerIdList));
        applicationContext.publishEvent(new SetCreateUserNameEvent(records, userIdList));
    }


}
