|
@@ -1,23 +1,30 @@
|
|
|
package com.anyway.favor.service.impl;
|
|
|
|
|
|
-import com.anyway.favor.model.dto.FavorDto;
|
|
|
-import com.anyway.favor.model.dto.FavorItemDto;
|
|
|
+import com.anyway.favor.BusinessException;
|
|
|
import com.anyway.favor.dao.FavorDao;
|
|
|
import com.anyway.favor.dao.FavorItemDao;
|
|
|
import com.anyway.favor.dao.GiftDao;
|
|
|
import com.anyway.favor.model.Favor;
|
|
|
import com.anyway.favor.model.FavorItem;
|
|
|
import com.anyway.favor.model.Gift;
|
|
|
+import com.anyway.favor.model.dto.FavorDto;
|
|
|
+import com.anyway.favor.model.dto.FavorItemDto;
|
|
|
+import com.anyway.favor.service.FavorItemService;
|
|
|
import com.anyway.favor.service.FavorService;
|
|
|
import com.anyway.util.PageQuery;
|
|
|
import com.anyway.util.PageUtils;
|
|
|
+import com.google.common.cache.CacheBuilder;
|
|
|
+import com.google.common.cache.CacheLoader;
|
|
|
+import com.google.common.cache.LoadingCache;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
import java.util.*;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
@@ -33,10 +40,31 @@ public class FavorServiceImpl implements FavorService {
|
|
|
@Autowired
|
|
|
private FavorDao favorDao;
|
|
|
@Autowired
|
|
|
+ private FavorItemService favorItemService;
|
|
|
+ @Autowired
|
|
|
private FavorItemDao favorItemDao;
|
|
|
@Autowired
|
|
|
private GiftDao giftDao;
|
|
|
|
|
|
+ /**
|
|
|
+ * 人员缓存
|
|
|
+ */
|
|
|
+ private LoadingCache<Long, Favor> favorCache;
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ public void init() {
|
|
|
+ favorCache = CacheBuilder.newBuilder()
|
|
|
+ .maximumSize(100)
|
|
|
+ .expireAfterWrite(5, TimeUnit.MINUTES)
|
|
|
+ .build(new CacheLoader<Long, Favor>() {
|
|
|
+ @Override
|
|
|
+ public Favor load(Long id) {
|
|
|
+ return favorDao.findById(id);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
@Override
|
|
|
public List<Favor> findByCondition(Map<String, Object> map) {
|
|
|
return favorDao.findByCondition(map);
|
|
@@ -44,9 +72,9 @@ public class FavorServiceImpl implements FavorService {
|
|
|
|
|
|
@Override
|
|
|
public List<Favor> findPage(PageQuery<Map<String, Object>> pageQuery) {
|
|
|
- PageUtils.startPage(pageQuery);
|
|
|
+ PageUtils.startPage(pageQuery.getPage());
|
|
|
List<Favor> favorList = favorDao.findByCondition(pageQuery.getTerms());
|
|
|
- PageUtils.setPageTotal(favorList, pageQuery);
|
|
|
+ PageUtils.setPageTotal(favorList, pageQuery.getPage());
|
|
|
return favorList;
|
|
|
}
|
|
|
|
|
@@ -165,21 +193,25 @@ public class FavorServiceImpl implements FavorService {
|
|
|
return favorDto;
|
|
|
}
|
|
|
|
|
|
+ @Transactional
|
|
|
@Override
|
|
|
public boolean deleteById(Long id) {
|
|
|
int i = favorDao.deleteById(id);
|
|
|
- if (i>0) {
|
|
|
- List<FavorItemDto> favorItemList = favorItemDao.findByFavorId(id);
|
|
|
- Set<Long> giftIdSet = new HashSet<>();
|
|
|
- for (FavorItem favorItem : favorItemList) {
|
|
|
- giftIdSet.add(favorItem.getGiveGiftId());
|
|
|
- if (favorItem.getReturnGiftId() != null) {
|
|
|
- giftIdSet.add(favorItem.getReturnGiftId());
|
|
|
- }
|
|
|
- }
|
|
|
- giftDao.deleteByIds(giftIdSet);
|
|
|
- favorItemDao.deleteByFavorId(id);
|
|
|
+ if (i <= 0) {
|
|
|
+ log.error("删除人情事件失败,人情事件:{}", id);
|
|
|
+ return false;
|
|
|
}
|
|
|
- return i>0;
|
|
|
+ //删除人情明细
|
|
|
+ boolean b = favorItemService.deleteByFavorId(id);
|
|
|
+ if (!b) {
|
|
|
+ log.error("删除人情明细失败,人情事件:{}", id);
|
|
|
+ throw new BusinessException("删除人情明细失败");
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Favor findById(Long id) {
|
|
|
+ return favorCache.getUnchecked(id);
|
|
|
}
|
|
|
}
|