# ExpenseCategoryItemServiceImpl.java

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

    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.workperformance.entity.monthlyreport.AreaTypeConfig;
    import cloud.wconnect.workperformance.entity.monthlyreport.ExpenseCategoryItem;
    import cloud.wconnect.workperformance.entity.monthlyreport.MonthlyReport;
    import cloud.wconnect.workperformance.mapper.monthlyreport.ExpenseCategoryItemMapper;
    import cloud.wconnect.workperformance.service.monthlyreport.IHandlePropertySettingChange;
    import cloud.wconnect.workperformance.service.monthlyreport.IExpenseCategoryItemService;
    import cloud.wconnect.workperformance.service.monthlyreport.IHandleAreaTypeConfigChange;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.stereotype.Service;

    import java.util.List;
    import java.util.stream.Collectors;

    /**
     * <p>
     * 月报EXPENSES（花费）区域，花费分类明细表 服务实现类
     * </p>
     *
     * @author lichen
     * @since 2023-09-01
     */
    @Service
    @DS("monthly_report")
    public class ExpenseCategoryItemServiceImpl extends ServiceImpl<ExpenseCategoryItemMapper, ExpenseCategoryItem> implements IExpenseCategoryItemService, IHandlePropertySettingChange, IHandleAreaTypeConfigChange {

        /**
         * 查询花费分类明细集合
         *
         * @param propertyId 资产id
         * @param reportId   月报id
         * @return java.util.List<cloud.wconnect.workperformance.entity.monthlyreport.ExpenseCategoryItem>
         * @author lichen
         * @date 2023/9/4
         */
        @Override
        public List<ExpenseCategoryItem> listByReportId(Integer propertyId, Long reportId) {
            return list(new LambdaQueryWrapper<ExpenseCategoryItem>()
                    .eq(ExpenseCategoryItem::getReportId, reportId)
                    .eq(ExpenseCategoryItem::getPropertyId, propertyId)
                    .orderByAsc(ExpenseCategoryItem::getId));
        }

        /**
         * 删除区域数据
         *
         * @param propertyDTO    资产DTO
         * @param monthlyReports 月报集合
         * @return void
         * @author lichen
         * @date 2023/9/5
         */
        @Override
        public void delete(PropertyDTO propertyDTO, List<MonthlyReport> monthlyReports) {
            List<Long> reportIds = monthlyReports.stream().map(MonthlyReport::getId).toList();
            if (CollectionUtils.isEmpty(reportIds)) return;
            remove(new LambdaQueryWrapper<ExpenseCategoryItem>()
                    .in(ExpenseCategoryItem::getReportId, reportIds)
                    .eq(ExpenseCategoryItem::getPropertyId, propertyDTO.getId()));
        }

        /**
         * 更新分类
         *
         * @param propertyDTO       资产DTO
         * @param monthlyReports    月报集合
         * @param areaTypeConfig    分类
         * @param oriAreaTypeConfig 修改前的分类
         * @return void
         * @author lichen
         * @date 2023/9/6
         */
        @Override
        public void updateCategory(PropertyDTO propertyDTO, List<MonthlyReport> monthlyReports, AreaTypeConfig areaTypeConfig, AreaTypeConfig oriAreaTypeConfig) {
            List<Long> monthlyReportIds = monthlyReports.stream().map(MonthlyReport::getId).collect(Collectors.toList());

            //查询需要更新分类名称的数据
            List<ExpenseCategoryItem> expenseCategoryItems = list(new LambdaQueryWrapper<ExpenseCategoryItem>()
                    .in(ExpenseCategoryItem::getReportId, monthlyReportIds)
                    .eq(ExpenseCategoryItem::getPropertyId, propertyDTO.getId())
                    .eq(ExpenseCategoryItem::getCategoryId, areaTypeConfig.getId()));
            if (CollectionUtils.isEmpty(expenseCategoryItems)) return;

            //设置新的分类名称
            expenseCategoryItems.forEach(item -> item.setCategoryName(areaTypeConfig.getName()));
            //更新名称
            updateBatchById(expenseCategoryItems);
        }

        /**
         * 删除区域分类id对应的数据
         *
         * @param propertyDTO    资产DTO
         * @param monthlyReports 月报集合
         * @param categoryId     分类id
         * @return void
         * @author lichen
         * @date 2023/9/6
         */
        @Override
        public void deleteAreaDataByCategoryId(PropertyDTO propertyDTO, List<MonthlyReport> monthlyReports, Integer categoryId) {
            List<Long> monthlyReportIds = monthlyReports.stream().map(MonthlyReport::getId).collect(Collectors.toList());

            //删除区域分类id对应的数据
            remove(new LambdaQueryWrapper<ExpenseCategoryItem>()
                    .in(ExpenseCategoryItem::getReportId, monthlyReportIds)
                    .eq(ExpenseCategoryItem::getPropertyId, propertyDTO.getId())
                    .eq(ExpenseCategoryItem::getCategoryId, categoryId));
        }
    }