# HotelKpiWconnectEmployeeRequestServiceImpl.java

    package cloud.wconnect.workperformance.service.hotelkpi.impl;

    import cloud.wconnect.workperformance.entity.hotelkpi.HotelKpiWconnectEmployeeRequest;
    import cloud.wconnect.workperformance.mapper.hotelkpi.HotelKpiWconnectEmployeeRequestMapper;
    import cloud.wconnect.workperformance.pojo.bo.modelevent.employeerequest.NoerRequestedBO;
    import cloud.wconnect.workperformance.service.hotelkpi.IHotelKpiWconnectEmployeeRequestService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.google.common.collect.Lists;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.math.BigDecimal;
    import java.util.Comparator;
    import java.util.List;
    import java.util.Map;
    import java.util.Objects;
    import java.util.stream.Collectors;

    /**
     * <p>
     * Hotel KPI wconnect Number of employee request 数据 服务实现类
     * </p>
     *
     * @author zc
     * @since 2024-07-23
     */
    @Service
    @DS("work_performance")
    public class HotelKpiWconnectEmployeeRequestServiceImpl extends ServiceImpl<HotelKpiWconnectEmployeeRequestMapper, HotelKpiWconnectEmployeeRequest> implements IHotelKpiWconnectEmployeeRequestService {

        /**
         * 保存数据
         *
         * @param boList 数据集合
         * @author zc
         * @date 2024/7/23
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void save(List<NoerRequestedBO> boList) {
            Map<Integer, List<NoerRequestedBO>> propertyId2RequestList = boList.stream()
                    .collect(Collectors.groupingBy(NoerRequestedBO::getPropertyId));

            List<HotelKpiWconnectEmployeeRequest> employeeRequestList = Lists.newArrayList();
            for (Map.Entry<Integer, List<NoerRequestedBO>> entry : propertyId2RequestList.entrySet()) {
                HotelKpiWconnectEmployeeRequest employeeRequest = new HotelKpiWconnectEmployeeRequest();
                employeeRequest.setPropertyId(entry.getKey());
                employeeRequest.setRequestFte(entry.getValue().stream().map(NoerRequestedBO::getRequestFte)
                        .filter(Objects::nonNull).reduce(BigDecimal::add).orElse(null));
                employeeRequest.setCurrentFullTime(entry.getValue().stream().map(NoerRequestedBO::getCurrentFullTime)
                        .filter(Objects::nonNull).reduce(Integer::sum).orElse(null));
                employeeRequest.setCurrentPartTime(entry.getValue().stream().map(NoerRequestedBO::getCurrentPartTime)
                        .filter(Objects::nonNull).reduce(Integer::sum).orElse(null));

                employeeRequestList.add(employeeRequest);
            }

            if (CollectionUtils.isEmpty(employeeRequestList)) {
                return;
            }

            // 按唯一索引字段排序，避免死锁
            employeeRequestList.sort(Comparator.comparing(HotelKpiWconnectEmployeeRequest::getPropertyId));

            baseMapper.insertOrUpdateBatchByUnique(employeeRequestList);
        }

        /**
         * 获取指定资产下的数据
         *
         * @param propertyId 资产 id
         * @return HotelKpiWconnectEmployeeRequest
         * @author zc
         * @date 2024/8/8
         */
        @Override
        public HotelKpiWconnectEmployeeRequest getByPropertyId(int propertyId) {
            HotelKpiWconnectEmployeeRequest employeeRequest = getOne(new LambdaQueryWrapper<HotelKpiWconnectEmployeeRequest>()
                    .eq(HotelKpiWconnectEmployeeRequest::getPropertyId, propertyId));
            if (Objects.isNull(employeeRequest)) {
                return new HotelKpiWconnectEmployeeRequest();
            }
            return employeeRequest;
        }

    }