Selaa lähdekoodia

feat:新增人情明细批量删除接口

liuchuanwei 1 kuukausi sitten
vanhempi
commit
1a74b1eb21

+ 19 - 8
src/main/java/com/anyway/favor/controller/FavorItemController.java

@@ -11,15 +11,11 @@ import com.anyway.util.PageQuery;
 import com.anyway.util.R;
 import com.anyway.util.SessionUtils;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import javax.validation.constraints.NotBlank;
+import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * 人情明细控制器
@@ -67,4 +63,19 @@ public class FavorItemController {
         }
         return R.page(favorItemVoList, pageQuery.getPage());
     }
+
+    /**
+     * 批量删除
+     *
+     * @param ids
+     * @return
+     */
+    @PostMapping("/delete")
+    public R<Boolean> delete(@RequestParam("ids") @NotBlank String ids) {
+        String[] arr = ids.split(",");
+        List<Long> idList = Arrays.stream(arr).map(Long::valueOf).collect(Collectors.toList());
+        boolean b = favorItemService.deleteByIds(idList);
+        return b ? R.ok() : R.fail("删除失败");
+    }
+
 }

+ 8 - 0
src/main/java/com/anyway/favor/dao/FavorItemDao.java

@@ -47,6 +47,14 @@ public interface FavorItemDao {
     FavorItem findById(Long id);
 
     /**
+     * 根据主键ID集合查询
+     *
+     * @param id
+     * @return
+     */
+    List<FavorItem> findByIds(Collection<Long> id);
+
+    /**
      * 插入
      *
      * @param favorItemList

+ 24 - 0
src/main/java/com/anyway/favor/service/FavorItemService.java

@@ -62,5 +62,29 @@ public interface FavorItemService {
      */
     FavorItem findById(Long id);
 
+    /**
+     * 根据IDS删除
+     *
+     * @param ids
+     * @return
+     */
+    boolean deleteByIds(List<Long> ids);
+
+    /**
+     * 根据人情事件ID删除
+     *
+     * @param favorId
+     * @return
+     */
+    boolean deleteByFavorId(Long favorId);
+
+    /**
+     * 批量删除
+     *
+     * @param favorItemList
+     * @return
+     */
+    boolean batchDelete(List<? extends FavorItem> favorItemList);
+
 
 }

+ 54 - 8
src/main/java/com/anyway/favor/service/impl/FavorItemServiceImpl.java

@@ -1,36 +1,42 @@
 package com.anyway.favor.service.impl;
 
 import com.anyway.favor.dao.FavorItemDao;
+import com.anyway.favor.dao.GiftDao;
 import com.anyway.favor.model.FavorItem;
+import com.anyway.favor.model.dto.FavorItemDto;
 import com.anyway.favor.service.FavorItemService;
 import com.anyway.util.PageQuery;
 import com.anyway.util.PageUtils;
-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 java.util.*;
 
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
 /**
  * 人情明细业务实现类
  *
  * @author liuchuanwei
  * @date 2024-02-24
  */
+@Slf4j
 @Service
 public class FavorItemServiceImpl implements FavorItemService {
     @Autowired
-    private FavorItemDao favorItemMapper;
+    private FavorItemDao favorItemDao;
+    @Autowired
+    private GiftDao giftDao;
 
     @Override
     public List<FavorItem> findByCondition(Map<String, Object> map) {
-        return favorItemMapper.findByCondition(map);
+        return favorItemDao.findByCondition(map);
     }
     @Override
     public List<FavorItem> findPage(PageQuery<Map<String, Object>> pageQuery) {
         PageUtils.startPage(pageQuery.getPage());
-        List<FavorItem> favorItemList = favorItemMapper.findByCondition(pageQuery.getTerms());
+        List<FavorItem> favorItemList = favorItemDao.findByCondition(pageQuery.getTerms());
         PageUtils.setPageTotal(favorItemList, pageQuery.getPage());
         return favorItemList;
     }
@@ -43,7 +49,7 @@ public class FavorItemServiceImpl implements FavorItemService {
             map.put("sortName", "create_time");
             map.put("sortOrder", "asc");
         }
-        return favorItemMapper.findByCondition(map);
+        return favorItemDao.findByCondition(map);
     }
 
     @Override
@@ -60,4 +66,44 @@ public class FavorItemServiceImpl implements FavorItemService {
     public FavorItem findById(Long id) {
         return null;
     }
+
+    @Transactional
+    @Override
+    public boolean deleteByIds(List<Long> ids) {
+        List<FavorItem> favorItemList = favorItemDao.findByIds(ids);
+        return this.batchDelete(favorItemList);
+    }
+
+    @Transactional
+    @Override
+    public boolean deleteByFavorId(Long favorId) {
+        List<FavorItemDto> favorItemDtoList = favorItemDao.findByFavorId(favorId);
+        return this.batchDelete(favorItemDtoList);
+    }
+
+    @Transactional
+    @Override
+    public boolean batchDelete(List<? extends FavorItem> favorItemList) {
+        if(CollectionUtils.isEmpty(favorItemList)) {
+            log.warn("删除失败,人情明细列表为空");
+            return false;
+        }
+        Set<Long> favorItemIdSet = new HashSet<>();
+        //删除礼物
+        Set<Long> giftIdSet = new HashSet<>();
+        for (FavorItem favorItem : favorItemList) {
+            favorItemIdSet.add(favorItem.getId());
+            giftIdSet.add(favorItem.getGiveGiftId());
+            if (favorItem.getReturnGiftId() != null) {
+                giftIdSet.add(favorItem.getReturnGiftId());
+            }
+        }
+        giftDao.deleteByIds(giftIdSet);
+        int r = favorItemDao.deleteByIds(favorItemIdSet);
+        if (r <= 0) {
+            log.error("人情明细删除失败:{}", favorItemIdSet);
+            return false;
+        }
+        return true;
+    }
 }

+ 14 - 12
src/main/java/com/anyway/favor/service/impl/FavorServiceImpl.java

@@ -1,5 +1,6 @@
 package com.anyway.favor.service.impl;
 
+import com.anyway.favor.BusinessException;
 import com.anyway.favor.dao.FavorDao;
 import com.anyway.favor.dao.FavorItemDao;
 import com.anyway.favor.dao.GiftDao;
@@ -8,6 +9,7 @@ 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;
@@ -38,6 +40,8 @@ public class FavorServiceImpl implements FavorService {
     @Autowired
     private FavorDao favorDao;
     @Autowired
+    private FavorItemService favorItemService;
+    @Autowired
     private FavorItemDao favorItemDao;
     @Autowired
     private GiftDao giftDao;
@@ -193,19 +197,17 @@ public class FavorServiceImpl implements FavorService {
     @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;
+        }
+        //删除人情明细
+        boolean b = favorItemService.deleteByFavorId(id);
+        if (!b) {
+            log.error("删除人情明细失败,人情事件:{}", id);
+            throw new BusinessException("删除人情明细失败");
         }
-        return i>0;
+        return true;
     }
 
     @Override

+ 10 - 0
src/main/resources/mapper/FavorItemMapper.xml

@@ -89,6 +89,16 @@
         FROM t_favor_item fi WHERE fi.id = #{id}
     </select>
 
+    <!-- 根据主键查询人情明细表信息 -->
+    <select id="findByIds" resultMap="favorItemMap">
+        SELECT
+        <include refid="allColumns" />
+        FROM t_favor_item fi WHERE fi.id in
+        <foreach collection="collection" item="id" separator="," open="(" close=")">
+            #{id}
+        </foreach>
+    </select>
+
     <!-- 批量新增人情明细表信息 -->
     <insert id="batchAdd" parameterType="list">
         INSERT INTO t_favor_item (