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 javax.annotation.Resource;
|
import java.time.LocalDateTime;
|
|
@Service
|
public class CartPersistenceServiceImpl implements CartPersistenceService {
|
|
@Resource
|
private CartMapper cartMapper;
|
|
@Override
|
@Async("asyncExecutor")
|
public void saveOrUpdate(Long userId, Long unitId, CartItemVO item) {
|
try {
|
Cart cart = new Cart();
|
BeanUtils.copyProperties(item, cart);
|
cart.setUserId(userId);
|
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(Long userId, Long 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(Long userId, Long unitId) {
|
try {
|
java.util.List<Cart> cartItems = cartMapper.selectByUserIdAndUnitId(userId, unitId);
|
for (Cart item : cartItems) {
|
cartMapper.deleteById(item.getId());
|
}
|
} catch (Exception ignored) {}
|
}
|
}
|