浏览代码

refactor:重构人情明细分页接口

liuchuanwei 1 月之前
父节点
当前提交
68caf7b3f7

+ 6 - 0
pom.xml

@@ -114,5 +114,11 @@
             <version>2.6</version>
         </dependency>
 
+        <!--google guava -->
+        <dependency>
+            <groupId>com.google.guava</groupId>
+            <artifactId>guava</artifactId>
+            <version>33.2.1-jre</version>
+        </dependency>
     </dependencies>
 </project>

+ 36 - 25
src/main/java/com/anyway/favor/controller/FavorItemController.java

@@ -2,17 +2,22 @@ package com.anyway.favor.controller;
 
 import com.anyway.favor.model.FavorItem;
 import com.anyway.favor.model.User;
+import com.anyway.favor.model.vo.FavorItemVo;
 import com.anyway.favor.service.FavorItemService;
+import com.anyway.favor.service.FavorService;
+import com.anyway.favor.service.GiftService;
+import com.anyway.favor.service.PersonService;
 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.stereotype.Controller;
+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.ResponseBody;
-import org.springframework.web.servlet.ModelAndView;
+import org.springframework.web.bind.annotation.RestController;
 
+import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -22,38 +27,44 @@ import java.util.Map;
  * @author liuchuanwei
  * @date 2024-02-24
  */
-@Controller
+@RestController
 @RequestMapping("/favorItem")
 public class FavorItemController {
     @Autowired
     private FavorItemService favorItemService;
-
-    /**
-     * 跳转列表页面
-     *
-     * @param mv
-     * @return
-     */
-    @RequestMapping("/toList")
-    public ModelAndView toList(ModelAndView mv) {
-        mv.setViewName("/favorItem/list");
-        return mv;
-    }
-
+    @Autowired
+    private PersonService personService;
+    @Autowired
+    private FavorService favorService;
+    @Autowired
+    private GiftService giftService;
     /**
-     * 人情往来记录列表
+     * 人情明细分页
      *
      * @param pageQuery
      * @return
      */
-    @ResponseBody
-    @RequestMapping("/listPage")
-    public R listPage(@RequestBody PageQuery<Map<String, Object>> pageQuery) {
+    @GetMapping("/listPage")
+    public R<List<FavorItemVo>> listPage(@RequestBody PageQuery<Map<String, Object>> pageQuery) {
         User currentUser = SessionUtils.currentUser();
-        Map<String, Object> mapCondition = pageQuery.getTerms();
+        Map<String, Object> mapCondition = pageQuery.getTerms(new HashMap<>());
         //查询和当前登录用户相关的人员
-//        mapCondition.put("createBy", currentUser.getId());
+        mapCondition.put("createBy", currentUser.getId());
+        mapCondition.put("sortName", "id");
+        mapCondition.put("sortOrder", "asc");
         List<FavorItem> favorItemList = favorItemService.findPage(pageQuery);
-        return R.page(favorItemList, pageQuery.getPage());
+        //补充人情明细的关联信息
+        List<FavorItemVo> favorItemVoList = new ArrayList<>();
+        for (FavorItem favorItem : favorItemList) {
+            FavorItemVo favorItemVo = new FavorItemVo(favorItem);
+            favorItemVo.setFavor(favorService.findById(favorItem.getFavorId()));
+            favorItemVo.setHoldPerson(personService.findById(favorItemVo.getFavor().getHoldPersonId()));
+            favorItemVo.setReceivePerson(personService.findById(favorItem.getReceivePersonId()));
+            favorItemVo.setReturnGift(giftService.findById(favorItem.getReturnGiftId()));
+            favorItemVo.setGivePerson(personService.findById(favorItem.getGivePersonId()));
+            favorItemVo.setGiveGift(giftService.findById(favorItem.getGiveGiftId()));
+            favorItemVoList.add(favorItemVo);
+        }
+        return R.page(favorItemVoList, pageQuery.getPage());
     }
-}
+}

+ 1 - 2
src/main/java/com/anyway/favor/dao/GiftDao.java

@@ -1,6 +1,5 @@
 package com.anyway.favor.dao;
 
-import com.anyway.favor.model.FavorItem;
 import com.anyway.favor.model.Gift;
 
 import java.util.Collection;
@@ -19,7 +18,7 @@ public interface GiftDao {
      * @param id
      * @return
      */
-    FavorItem findById(Long id);
+    Gift findById(Long id);
 
     /**
      * 插入

+ 4 - 2
src/main/java/com/anyway/favor/model/FavorItem.java

@@ -7,7 +7,7 @@ import javax.validation.constraints.NotNull;
 
 /**
  * 人情明细表(t_favor_item)
- * 
+ *
  * @author anyway
  * @date 2024-02-20
  */
@@ -33,4 +33,6 @@ public class FavorItem extends BaseModel implements java.io.Serializable {
     private Long giveGiftId;
     /** 备注 */
     private String remark;
-}
+    /** 排序 */
+    private int sort;
+}

+ 34 - 0
src/main/java/com/anyway/favor/model/vo/FavorItemVo.java

@@ -0,0 +1,34 @@
+package com.anyway.favor.model.vo;
+
+import com.anyway.favor.model.Favor;
+import com.anyway.favor.model.FavorItem;
+import com.anyway.favor.model.Gift;
+import com.anyway.favor.model.Person;
+import lombok.Data;
+import org.springframework.beans.BeanUtils;
+
+/**
+ * 人情明细
+ *
+ * @author liuchuanwei
+ * @date 2024-08-12
+ */
+@Data
+public class FavorItemVo extends FavorItem {
+    /** 人情事件 */
+    private Favor favor;
+    /** 举办人 */
+    private Person holdPerson;
+    /** 收礼人 */
+    private Person receivePerson;
+    /** 回赠礼物 */
+    private Gift returnGift;
+    /** 送礼人 */
+    private Person givePerson;
+    /** 赠送礼物 */
+    private Gift giveGift;
+
+    public FavorItemVo(FavorItem favorItem) {
+        BeanUtils.copyProperties(favorItem, this);
+    }
+}

+ 8 - 0
src/main/java/com/anyway/favor/service/FavorService.java

@@ -62,4 +62,12 @@ public interface FavorService {
      * @return
      */
     boolean deleteById(Long id);
+
+    /**
+     * 根据ID查询
+     *
+     * @param id
+     * @return
+     */
+    Favor findById(Long id);
 }

+ 44 - 0
src/main/java/com/anyway/favor/service/GiftService.java

@@ -0,0 +1,44 @@
+package com.anyway.favor.service;
+
+import com.anyway.favor.model.Gift;
+
+/**
+ * 礼物接口
+ *
+ * @author anyway
+ * @date 2024-08-12
+ */
+public interface GiftService {
+
+    /**
+     * 添加
+     *
+     * @param person
+     * @return
+     */
+    boolean add(Gift person);
+
+    /**
+     * 更新
+     *
+     * @param person
+     * @return
+     */
+    boolean update(Gift person);
+
+    /**
+     * 根据ID查询
+     *
+     * @param id
+     * @return
+     */
+    Gift findById(Long id);
+
+    /**
+     * 根据ID删除
+     * @param id
+     * @return
+     */
+    int delete(Long id);
+
+}

+ 3 - 2
src/main/java/com/anyway/favor/service/impl/FavorItemServiceImpl.java

@@ -5,6 +5,7 @@ import com.anyway.favor.model.FavorItem;
 import com.anyway.favor.service.FavorItemService;
 import com.anyway.util.PageQuery;
 import com.anyway.util.PageUtils;
+import com.google.common.cache.LoadingCache;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -28,9 +29,9 @@ public class FavorItemServiceImpl implements FavorItemService {
     }
     @Override
     public List<FavorItem> findPage(PageQuery<Map<String, Object>> pageQuery) {
-        PageUtils.startPage(pageQuery);
+        PageUtils.startPage(pageQuery.getPage());
         List<FavorItem> favorItemList = favorItemMapper.findByCondition(pageQuery.getTerms());
-        PageUtils.setPageTotal(favorItemList, pageQuery);
+        PageUtils.setPageTotal(favorItemList, pageQuery.getPage());
         return favorItemList;
     }
 

+ 31 - 2
src/main/java/com/anyway/favor/service/impl/FavorServiceImpl.java

@@ -1,23 +1,28 @@
 package com.anyway.favor.service.impl;
 
-import com.anyway.favor.model.dto.FavorDto;
-import com.anyway.favor.model.dto.FavorItemDto;
 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.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;
 
@@ -37,6 +42,25 @@ public class FavorServiceImpl implements FavorService {
     @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);
@@ -183,4 +207,9 @@ public class FavorServiceImpl implements FavorService {
         }
         return i>0;
     }
+
+    @Override
+    public Favor findById(Long id) {
+        return favorCache.getUnchecked(id);
+    }
 }

+ 59 - 0
src/main/java/com/anyway/favor/service/impl/GiftServiceImpl.java

@@ -0,0 +1,59 @@
+package com.anyway.favor.service.impl;
+
+import com.anyway.favor.dao.GiftDao;
+import com.anyway.favor.model.Gift;
+import com.anyway.favor.service.GiftService;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.PostConstruct;
+import java.util.concurrent.TimeUnit;
+
+@Service
+public class GiftServiceImpl implements GiftService {
+
+    @Autowired
+    private GiftDao giftDao;
+
+    /**
+     * 人员缓存
+     */
+    private LoadingCache<Long, Gift> giftCache;
+
+    @PostConstruct
+    public void init() {
+        giftCache = CacheBuilder.newBuilder()
+                .maximumSize(100)
+                .expireAfterWrite(5, TimeUnit.MINUTES)
+                .build(new CacheLoader<Long, Gift>() {
+                    @Override
+                    public Gift load(Long id) {
+                        return giftDao.findById(id);
+                    }
+                });
+    }
+
+
+    @Override
+    public boolean add(Gift person) {
+        return false;
+    }
+
+    @Override
+    public boolean update(Gift person) {
+        return false;
+    }
+
+    @Override
+    public Gift findById(Long id) {
+        return null;
+    }
+
+    @Override
+    public int delete(Long id) {
+        return 0;
+    }
+}

+ 25 - 3
src/main/java/com/anyway/favor/service/impl/PersonServiceImpl.java

@@ -5,13 +5,18 @@ import com.anyway.favor.model.Person;
 import com.anyway.favor.service.PersonService;
 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 javax.annotation.PostConstruct;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.TimeUnit;
 
 /**
  * 人员业务实现类
@@ -25,6 +30,23 @@ public class PersonServiceImpl implements PersonService {
 
     @Autowired
     private PersonDao personMapper;
+    /**
+     * 人员缓存
+     */
+    private LoadingCache<Long, Person> personCache;
+
+    @PostConstruct
+    public void init() {
+        personCache = CacheBuilder.newBuilder()
+                .maximumSize(100)
+                .expireAfterWrite(5, TimeUnit.MINUTES)
+                .build(new CacheLoader<Long, Person>() {
+                    @Override
+                    public Person load(Long id) {
+                        return personMapper.findById(id);
+                    }
+                });
+    }
 
     @Override
     public List<Person> findAll() {
@@ -38,9 +60,9 @@ public class PersonServiceImpl implements PersonService {
 
     @Override
     public List<Person> findPage(PageQuery<Map<String, Object>> pageQuery) {
-        PageUtils.startPage(pageQuery);
+        PageUtils.startPage(pageQuery.getPage());
         List<Person> personList = personMapper.findByCondition(pageQuery.getTerms());
-        PageUtils.setPageTotal(personList, pageQuery);
+        PageUtils.setPageTotal(personList, pageQuery.getPage());
         return personList;
     }
 
@@ -59,7 +81,7 @@ public class PersonServiceImpl implements PersonService {
 
     @Override
     public Person findById(Long id) {
-        return personMapper.findById(id);
+        return personCache.getUnchecked(id);
     }
 
     @Override

+ 4 - 4
src/main/resources/mapper/FavorItemMapper.xml

@@ -76,10 +76,10 @@
             <if test="holdDate != null">
                 AND f.holdDate = #{holdDate}
             </if>
-            <if test="sortName != null">
-                ORDER BY fi.${sortName} ${sortOrder}
-            </if>
         </where>
+        <if test="sortName != null">
+            ORDER BY fi.${sortName} ${sortOrder}
+        </if>
     </select>
 
     <!-- 根据主键查询人情明细表信息 -->
@@ -153,4 +153,4 @@
         DELETE FROM t_favor_item WHERE favor_id = #{favorId}
     </delete>
 
-</mapper>
+</mapper>

+ 2 - 2
src/main/resources/mapper/GiftMapper.xml

@@ -13,7 +13,7 @@
     <select id="findById" resultMap="giftMap">
         SELECT
             <include refid="allColumns" />
-        FROM t_gift fi WHERE fi.id = #{id}
+        FROM t_gift g WHERE g.id = #{id}
     </select>
 
     <!-- 新增礼物表信息 -->
@@ -49,4 +49,4 @@
             #{id}
         </foreach>
     </delete>
-</mapper>
+</mapper>

+ 5 - 12
src/main/resources/mapper/PersonDao.xml

@@ -4,12 +4,12 @@
 <!-- 人员表(t_person) -->
 <mapper namespace="com.anyway.favor.dao.PersonDao">
     <!-- 字段映射 -->
-    <resultMap id="personMap" type="com.anyway.favor.model.Person"></resultMap>
+    <resultMap id="personMap" type="com.anyway.favor.model.Person" />
 
     <!-- 表查询字段 -->
     <sql id="allColumns">
-        p.id, p.name, p.card_id, p.call_name, p.gender, p.birthday, p.death_date, p.marital_status, 
-        p.mate_id, p.mother_id, p.father_id, p.sort, p.related_id, p.address, p.remark, p.create_time, p.create_by, p.modify_time,
+        p.id, p.name, p.card_id, p.call_name, p.gender, p.birthday, p.death_date, p.marital_status,
+        p.mate_id, p.mother_id, p.father_id, p.sort, p.address, p.remark, p.create_time, p.create_by, p.modify_time,
         p.modify_by
     </sql>
 
@@ -62,9 +62,6 @@
         <if test="address != null and address != ''">
             AND p.address = #{address}
         </if>
-        <if test="relatedId != null">
-            AND p.related_id = #{relatedId}
-        </if>
         <if test="familyPersonId != null and familyPersonId != ''">
             AND (
                 p.id=#{familyPersonId} OR p.mate_id=#{familyPersonId} OR p.mother_id=#{familyPersonId} OR p.father_id=#{familyPersonId}
@@ -102,7 +99,7 @@
     <insert id="add" keyColumn="id" keyProperty="id" useGeneratedKeys="true">
         INSERT INTO t_person (
             name, card_id, call_name, gender, birthday, death_date, marital_status,
-            mate_id, mother_id, father_id, sort, related_id, address, remark, create_by
+            mate_id, mother_id, father_id, sort, address, remark, create_by
         ) VALUES (
             #{name},
             #{cardId},
@@ -115,7 +112,6 @@
             #{motherId},
             #{fatherId},
             #{sort},
-            #{relatedId},
             #{address},
             #{remark},
             #{createBy}
@@ -159,9 +155,6 @@
             <if test="sort != null">
                 sort = #{sort},
             </if>
-            <if test="relatedId != null">
-                related_id = #{relatedId},
-            </if>
             <if test="address != null">
                 address = #{address},
             </if>
@@ -195,4 +188,4 @@
     <update id="updateNullFather">
         UPDATE t_person p set p.father_id=Null WHERE p.father_id=#{id}
     </update>
-</mapper>
+</mapper>