package com.webmanage.service.impl; import com.webmanage.entity.Cart; import com.webmanage.mapper.CartMapper; import com.webmanage.service.CartPersistenceService; import com.webmanage.vo.CartItemVO; import org.springframework.beans.BeanUtils; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.AsyncConfigurer; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; import javax.annotation.Resource; import java.time.LocalDateTime; @Service public class CartPersistenceServiceImpl implements CartPersistenceService { @Resource private CartMapper cartMapper; @Override @Async("asyncExecutor") public void saveOrUpdate(String userId, String unitId, CartItemVO item) { try { Cart cart = new Cart(); BeanUtils.copyProperties(item, cart); cart.setUserId(userId); if(StringUtils.hasText(unitId)){ cart.setUnitId(unitId); } cart.setUpdateTime(LocalDateTime.now()); Cart existing = cartMapper.selectByUserIdUnitIdAndPricingId(userId, unitId, item.getPricingId()); if (existing != null) { cart.setId(existing.getId()); cartMapper.updateById(cart); } else { cart.setAddTime(LocalDateTime.now()); cartMapper.insert(cart); } } catch (Exception ignored) {} } @Override @Async("asyncExecutor") public void remove(String userId, String unitId, Long pricingId) { try { Cart existing = cartMapper.selectByUserIdUnitIdAndPricingId(userId, unitId, pricingId); if (existing != null) { cartMapper.deleteById(existing.getId()); } } catch (Exception ignored) {} } @Override @Async("asyncExecutor") public void clear(String userId, String unitId) { try { java.util.List cartItems = cartMapper.selectByUserIdAndUnitId(userId, unitId); for (Cart item : cartItems) { cartMapper.deleteById(item.getId()); } } catch (Exception ignored) {} } }