package com.mzl.flower.service.customer; import com.mzl.flower.domain.CategoryConfig; import com.mzl.flower.domain.CategoryConfigDO; import com.mzl.flower.domain.CategoryConfigSync; import com.mzl.flower.mapper.CategoryConfigMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; @Service public class CategoryConfigServiceImpl implements CategoryConfigService { @Autowired private CategoryConfigMapper categoryConfigMapper; @Override @Transactional public void saveOrUpdateCategoryConfig(CategoryConfigSync categoryConfigSync) { if (categoryConfigSync == null || categoryConfigSync.getUserId() == null) { throw new IllegalArgumentException("用户ID不能为空"); } // 先删除该用户之前的所有分类配置 categoryConfigMapper.deleteByUserId(categoryConfigSync.getUserId()); // 然后插入新的分类配置 if (categoryConfigSync.getCategories() != null && !categoryConfigSync.getCategories().isEmpty()) { for (CategoryConfig clientCategory : categoryConfigSync.getCategories()) { CategoryConfigDO dbCategory = new CategoryConfigDO(); dbCategory.setUserId(categoryConfigSync.getUserId()); dbCategory.setCategoryId(clientCategory.getId()); dbCategory.setName(clientCategory.getName()); dbCategory.setOrder(clientCategory.getOrder()); dbCategory.setIsEnabled(clientCategory.getIsEnabled()); categoryConfigMapper.insert(dbCategory); } } } @Override public List getCategoryConfigsByUserId(String userId) { if (userId == null) { throw new IllegalArgumentException("用户ID不能为空"); } List dbCategories = categoryConfigMapper.findByUserId(userId); if (dbCategories == null || dbCategories.isEmpty()) { return new ArrayList<>(); } List clientCategories = new ArrayList<>(); for (CategoryConfigDO dbCategory : dbCategories) { CategoryConfig clientCategory = new CategoryConfig(); clientCategory.setId(dbCategory.getCategoryId()); clientCategory.setName(dbCategory.getName()); clientCategory.setOrder(dbCategory.getOrder()); clientCategory.setIsEnabled(dbCategory.getIsEnabled()); clientCategories.add(clientCategory); } return clientCategories; } }