# HkInventoryCategoryServiceImpl.java

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

    import cloud.wconnect.workperformance.entity.hkinventory.HkInventoryCategory;
    import cloud.wconnect.workperformance.entity.hkinventory.HkInventoryItem;
    import cloud.wconnect.workperformance.mapper.hkinventory.HkInventoryCategoryMapper;
    import cloud.wconnect.workperformance.service.hkinventory.IHkInventoryCategoryService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
    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.*;
    import java.util.stream.Collectors;

    /**
     * <p>
     * 客房库存管理分类表 服务实现类
     * </p>
     *
     * @author lichen
     * @since 2025-04-19
     */
    @Service
    @DS("work_performance")
    public class HkInventoryCategoryServiceImpl extends ServiceImpl<HkInventoryCategoryMapper, HkInventoryCategory> implements IHkInventoryCategoryService {

        /**
         * 查询库存分类
         *
         * @param propertyId    资产id
         * @param hkInventoryId 库存id
         * @return java.util.List<cloud.wconnect.workperformance.entity.hkinventory.HkInventoryCategory>
         * @author lichen
         * @date 2025/4/21
         */
        @Override
        public List<HkInventoryCategory> listByHkInventoryId(Integer propertyId, Integer hkInventoryId) {
            return list(new LambdaQueryWrapper<HkInventoryCategory>()
                    .eq(HkInventoryCategory::getPropertyId, propertyId)
                    .eq(HkInventoryCategory::getHkInventoryId, hkInventoryId)
                    .orderByDesc(HkInventoryCategory::getId));
        }

        /**
         * 汇总指定分类下的数据
         *
         * @param propertyId    资产id
         * @param hkInventoryId 客房库存管理id
         * @param itemList      item数据集合
         * @return void
         * @author lichen
         * @date 2025/4/21
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void summary(Integer propertyId, Integer hkInventoryId, List<HkInventoryItem> itemList) {
            if (CollectionUtils.isEmpty(itemList))
                return;

            //按照客房库存管理id、分类id进行分组
            Map<Integer, List<HkInventoryItem>> categoryId2ItemList = itemList.stream().collect(Collectors.groupingBy(HkInventoryItem::getCategoryId));
            //查询客房库存管理下的分类
            List<HkInventoryCategory> hkInventoryCategoryList = listByHkInventoryId(propertyId, hkInventoryId);

            ArrayList<HkInventoryCategory> batchList = Lists.newArrayList();
            for (HkInventoryCategory hkInventoryCategory : hkInventoryCategoryList) {
                //指定分类下的item集合
                List<HkInventoryItem> itemListByCategory = categoryId2ItemList.getOrDefault(hkInventoryCategory.getCategoryId(), Collections.emptyList());

                //库存总金额
                BigDecimal totalBalance = itemListByCategory.stream().map(HkInventoryItem::getTotalBalance).filter(Objects::nonNull)
                        .reduce(BigDecimal::add).orElse(null);
                //需求采购金额
                BigDecimal requiredPurchaseAmount = itemListByCategory.stream().map(HkInventoryItem::getRequiredPurchaseAmount).filter(Objects::nonNull)
                        .reduce(BigDecimal::add).orElse(null);

                HkInventoryCategory entity = new HkInventoryCategory();
                entity.setId(hkInventoryCategory.getId());
                entity.setTotalBalance(totalBalance);
                entity.setRequiredPurchaseAmount(requiredPurchaseAmount);
                batchList.add(entity);
            }
            //更新库存分类表
            updateBatchById(batchList);
        }

        /**
         * 根据客房库存管理id集合和分类id查询库存分类
         *
         * @param propertyId     资产id
         * @param hkInventoryIds 客房库存管理id集合
         * @return java.util.List<cloud.wconnect.workperformance.entity.hkinventory.HkInventoryCategory>
         * @author lichen
         * @date 2025/4/22
         */
        @Override
        public List<HkInventoryCategory> listByHkInventoryIdsAndCategoryId(Integer propertyId, List<Integer> hkInventoryIds, Integer categoryId) {
            if (CollectionUtils.isEmpty(hkInventoryIds)) return Collections.emptyList();
            return list(new LambdaQueryWrapper<HkInventoryCategory>()
                    .eq(HkInventoryCategory::getPropertyId, propertyId)
                    .in(HkInventoryCategory::getHkInventoryId, hkInventoryIds)
                    .eq(HkInventoryCategory::getCategoryId, categoryId)
                    .orderByDesc(HkInventoryCategory::getId));
        }

        /**
         * 根据资产id、分类id更新分类名称
         *
         * @param propertyId   资产id
         * @param categoryId   分类id
         * @param categoryName 分类名称
         * @return void
         * @author lichen
         * @date 2025/4/23
         */
        @Override
        public void updateCategoryNameByCategoryId(Integer propertyId, Integer categoryId, String categoryName) {
            update(new LambdaUpdateWrapper<HkInventoryCategory>()
                    .eq(HkInventoryCategory::getPropertyId, propertyId)
                    .eq(HkInventoryCategory::getCategoryId, categoryId)
                    .set(HkInventoryCategory::getCategoryName, categoryName));
        }

        /**
         * 删除指定Housekeeping Inventory指定分类的数据
         *
         * @param propertyId     资产id
         * @param hkInventoryIds Housekeeping Inventory ID集合
         * @param categoryId     分类id
         * @return void
         * @author lichen
         * @date 2025/4/28
         */
        @Override
        public void deleteByHkInventoryIdsAndCategoryId(Integer propertyId, List<Integer> hkInventoryIds, Integer categoryId) {
            if (CollectionUtils.isEmpty(hkInventoryIds)) return;

            remove(new LambdaQueryWrapper<HkInventoryCategory>()
                    .eq(HkInventoryCategory::getPropertyId, propertyId)
                    .in(HkInventoryCategory::getHkInventoryId, hkInventoryIds)
                    .eq(HkInventoryCategory::getCategoryId, categoryId));
        }
    }