1
0

2 Commits 01eecaee40 ... a85ab1f133

Autor SHA1 Nachricht Datum
  liuchuanwei a85ab1f133 test:调试人员接口 vor 2 Monaten
  liuchuanwei 530ec9acbf feat:重构人员接口 vor 2 Monaten

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

@@ -71,7 +71,7 @@ public class FavorItemController {
      * @return
      */
     @PostMapping("/delete")
-    public R<Boolean> delete(@RequestParam("ids") @NotBlank String ids) {
+    public R<Boolean> delete(@RequestParam @NotBlank(message = "ids不能为空") String ids) {
         String[] arr = ids.split(",");
         List<Long> idList = Arrays.stream(arr).map(Long::valueOf).collect(Collectors.toList());
         boolean b = favorItemService.deleteByIds(idList);

+ 53 - 111
src/main/java/com/anyway/favor/controller/PersonController.java

@@ -2,21 +2,21 @@ package com.anyway.favor.controller;
 
 import com.anyway.favor.model.Person;
 import com.anyway.favor.model.User;
+import com.anyway.favor.model.enums.Update;
+import com.anyway.favor.model.vo.PersonVo;
 import com.anyway.favor.service.PersonService;
-import com.anyway.util.Couple;
 import com.anyway.util.PageQuery;
+import com.anyway.util.Pair;
 import com.anyway.util.R;
 import com.anyway.util.SessionUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.util.StringUtils;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
-import org.springframework.web.servlet.ModelAndView;
 
-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;
 
 /**
  * 人员控制器
@@ -43,100 +43,42 @@ public class PersonController {
         person.setCreateBy(currentUser.getId());
         return personService.add(person) ? R.ok() : R.fail("新增失败");
     }
-
     /**
-     * 跳转添加人员页面
+     * 更新人员
      *
+     * @param person
      * @return
      */
-    @RequestMapping("/toAdd")
-    public ModelAndView toAdd(ModelAndView mv) {
+    @PostMapping("/update")
+    public R<Void> update(@Validated({Update.class}) @RequestBody Person person) {
         User currentUser = SessionUtils.currentUser();
-        Map<String, Object> mapCondition = new HashMap<>();
-        mapCondition.put("relatedId", currentUser.getId());
-        //母亲列表
-        mapCondition.put("gender", "F");
-        mapCondition.put("maritalStatus", "1");
-        List<Person> motherPersonList = personService.findByCondition(mapCondition);
-        mv.addObject("motherPersonOptionList", Couple.toList(motherPersonList, e -> e.getCallName() + "(" + e.getName() + ")", e -> String.valueOf(e.getId())));
-        //父亲列表
-        mapCondition.put("gender", "M");
-        List<Person> fatherPersonList = personService.findByCondition(mapCondition);
-        mv.addObject("fatherPersonOptionList", Couple.toList(fatherPersonList, e -> e.getCallName() + "(" + e.getName() + ")", e -> String.valueOf(e.getId())));
-        //配偶列表
-        mapCondition.put("gender", null);
-        mapCondition.put("maritalStatus", null);
-        mapCondition.put("emptyMate", true);
-        mapCondition.put("sortName", "marital_status");
-        mapCondition.put("sortOrder", "asc");
-        List<Person> matePersonList = personService.findByCondition(mapCondition);
-        mv.addObject("matePersonOptionList", Couple.toList(matePersonList, e -> e.getCallName() + "(" + e.getName() + ")", e -> String.valueOf(e.getId())));
-        mv.setViewName("/person/edit");
-        return mv;
+        person.setModifyBy(currentUser.getId());
+        return personService.update(person) ? R.ok() : R.fail("保存失败");
     }
 
-
     /**
-     * 跳转编辑人员页面
+     * 人员详情
      *
+     * @param id
      * @return
      */
-    @RequestMapping("/edit/{id}")
-    public ModelAndView edit(@PathVariable Long id) {
-        ModelAndView mv = new ModelAndView();
+    @GetMapping("/detail/{id}")
+    public R<PersonVo> detail(@PathVariable Long id) {
         Person person = personService.findById(id);
-        mv.addObject("person", person);
-        mv.setViewName("/person/edit");
-
-        User currentUser = SessionUtils.currentUser();
-        Map<String, Object> mapCondition = new HashMap<>();
-        mapCondition.put("relatedId", currentUser.getId());
-        //母亲列表(女且已婚)
-        mapCondition.put("gender", "F");
-        mapCondition.put("maritalStatus", "1");
-        List<Person> motherPersonList = personService.findByCondition(mapCondition);
-        mv.addObject("motherPersonOptionList", Couple.toList(motherPersonList, e -> e.getCallName() + "(" + e.getName() + ")", e -> String.valueOf(e.getId())));
-        //父亲列表(男且已婚)
-        mapCondition.put("gender", "M");
-        List<Person> fatherPersonList = personService.findByCondition(mapCondition);
-        mv.addObject("fatherPersonOptionList", Couple.toList(fatherPersonList, e -> e.getCallName() + "(" + e.getName() + ")", e -> String.valueOf(e.getId())));
-        //配偶列表(男/女且配偶为空且未婚)
-        mapCondition.put("gender", "F".equals(person.getGender()) ? "M" : "F");
-        mapCondition.put("emptyMate", true);
-        mapCondition.put("maritalStatus", "0");
-        List<Person> matePersonList = personService.findByCondition(mapCondition);
-        mv.addObject("matePersonOptionList", Couple.toList(matePersonList, e -> e.getCallName() + "(" + e.getName() + ")", e -> String.valueOf(e.getId())));
-        return mv;
-    }
-    /**
-     * 更新人员
-     *
-     * @param person
-     * @return
-     */
-    @ResponseBody
-    @RequestMapping("/update")
-    public R update(@RequestBody Person person) {
-        User currentUser = SessionUtils.currentUser();
-        person.setModifyBy(currentUser.getId());
-        return personService.update(person) ? R.ok() : R.fail("保存失败");
+        return R.data(this.toPersonVo(person));
     }
 
     /**
-     * 跳转人员列表页面
-     *
+     * 转为 PersonVo
+     * @param person
      * @return
      */
-    @RequestMapping("/toList")
-    public ModelAndView toList(@RequestParam Map<String, Object> mapCondition) {
-        ModelAndView mv = new ModelAndView();
-        //查询和当前登录用户相关的人员
-        User currentUser = SessionUtils.currentUser();
-        mapCondition.put("relatedId", currentUser.getId());
-        List<Person> personList = personService.findByCondition(mapCondition);
-        mv.addObject("familyPersonList", personList);
-        mv.setViewName("/person/list");
-        return mv;
+    private PersonVo toPersonVo(Person person) {
+        PersonVo personVo = new PersonVo(person);
+        personVo.setMatePerson(personService.findById(person.getMateId()));
+        personVo.setFatherPerson(personService.findById(person.getFatherId()));
+        personVo.setMotherPerson(personService.findById(person.getMotherId()));
+        return personVo;
     }
 
     /**
@@ -147,13 +89,18 @@ public class PersonController {
      */
     @ResponseBody
     @RequestMapping("/listPage")
-    public R listPage(@RequestBody PageQuery<Map<String, Object>> pageQuery) {
+    public R<List<PersonVo>> 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("relatedId", currentUser.getId());
+        mapCondition.put("createBy", currentUser.getId());
         List<Person> personList = personService.findPage(pageQuery);
-        return R.page(personList, pageQuery.getPage());
+        //设置关联属性
+        List<PersonVo> personVoList = new ArrayList<>();
+        for (Person person : personList) {
+            personVoList.add(this.toPersonVo(person));
+        }
+        return R.page(personVoList, pageQuery.getPage());
     }
 
     /**
@@ -161,37 +108,32 @@ public class PersonController {
      *
      * @return
      */
-    @ResponseBody
-    @RequestMapping("/listOption")
-    public List<Couple> listOption(Person person) {
+    @GetMapping("/listOption")
+    public R<List<Pair<String, Long>>> listOption(@RequestBody PageQuery<Map<String, Object>> pageQuery) {
         User currentUser = SessionUtils.currentUser();
-        Map<String, Object> mapCondition = new HashMap<>();
-        if (!StringUtils.isEmpty(person.getGender())) {
-            mapCondition.put("gender", person.getGender());
-        }
-        if (person.getMaritalStatus() != null) {
-            mapCondition.put("maritalStatus", person.getMaritalStatus());
-        }
-        mapCondition.put("relatedId", currentUser.getId());
-        List<Person> personList = personService.findByCondition(mapCondition);
-        List<Couple> coupleList = new ArrayList<>();
-        for (Person p : personList) {
-            coupleList.add(new Couple(p.getId().toString(), p.getCallName() + "("+p.getName()+")"));
-        }
-        return coupleList;
+        Map<String, Object> mapCondition = pageQuery.getTerms(new HashMap<>());
+        //查询和当前登录用户相关的人员
+        mapCondition.put("createBy", currentUser.getId());
+        List<Person> personList = personService.findPage(pageQuery);
+        List<Pair<String, Long>> personOptionList = Pair.toList(personList, e -> e.getCallName() + "(" + e.getName() + ")", Person::getId);
+        return R.data(personOptionList);
     }
 
 
     /**
-     * 删除人员
+     * 批量删除
      *
-     * @param id
+     * @param ids
      * @return
      */
-    @ResponseBody
-    @RequestMapping("/delete/{id}")
-    public R delete(@PathVariable Long id) {
-        int c = personService.delete(id);
-        return  c > 0? R.ok() : R.fail("删除失败");
+    @PostMapping("/delete")
+    public R<Void> delete(@RequestParam(defaultValue = "") @NotBlank(message = "ids不能为空") String ids) {
+        if(!StringUtils.hasText(ids)) {
+            return R.fail("无效的ID");
+        }
+        String[] arr = ids.split(",");
+        List<Long> idList = Arrays.stream(arr).map(Long::valueOf).collect(Collectors.toList());
+        int r = personService.deleteByIds(idList);
+        return r > 0 ? R.ok() : R.fail("删除失败");
     }
 }

+ 5 - 0
src/main/java/com/anyway/favor/model/Person.java

@@ -1,11 +1,13 @@
 package com.anyway.favor.model;
 
+import com.anyway.favor.model.enums.Update;
 import com.fasterxml.jackson.annotation.JsonFormat;
 import lombok.Getter;
 import lombok.Setter;
 import org.springframework.format.annotation.DateTimeFormat;
 
 import javax.validation.constraints.NotBlank;
+import javax.validation.constraints.NotNull;
 import java.util.Date;
 
 /**
@@ -20,6 +22,7 @@ public class Person extends BaseModel implements java.io.Serializable {
     /** 版本号 */
     private static final long serialVersionUID = -476789774769490644L;
     /** id */
+    @NotNull(message = "id不能为空", groups = {Update.class})
     private Long id;
     /** 称呼 */
     @NotBlank(message = "称呼不能为空")
@@ -57,4 +60,6 @@ public class Person extends BaseModel implements java.io.Serializable {
     private Date deathDate;
     /** 备注 */
     private String remark;
+    /** 权重 */
+    private int weight;
 }

+ 24 - 0
src/main/java/com/anyway/favor/model/vo/PersonVo.java

@@ -0,0 +1,24 @@
+package com.anyway.favor.model.vo;
+
+import com.anyway.favor.model.Person;
+import lombok.Getter;
+import lombok.Setter;
+import org.springframework.beans.BeanUtils;
+
+@Setter
+@Getter
+public class PersonVo extends Person{
+
+
+    public PersonVo(Person person) {
+        BeanUtils.copyProperties(person, this);
+    }
+
+    /** 配偶 */
+    private Person matePerson;
+    /** 父亲 */
+    private Person fatherPerson;
+    /** 母亲 */
+    private Person motherPerson;
+
+}

+ 11 - 1
src/main/java/com/anyway/favor/service/PersonService.java

@@ -3,6 +3,8 @@ package com.anyway.favor.service;
 import com.anyway.favor.model.Person;
 import com.anyway.util.PageQuery;
 
+import javax.annotation.Nullable;
+import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 
@@ -59,7 +61,7 @@ public interface PersonService {
      * @param id
      * @return
      */
-    Person findById(Long id);
+    Person findById(@Nullable Long id);
 
     /**
      * 根据ID删除
@@ -68,4 +70,12 @@ public interface PersonService {
      */
     int delete(Long id);
 
+    /**
+     * 根据ID集合删除
+     *
+     * @param ids
+     * @return
+     */
+    int deleteByIds(Collection<Long> ids);
+
 }

+ 39 - 2
src/main/java/com/anyway/favor/service/impl/PersonServiceImpl.java

@@ -14,8 +14,10 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import javax.annotation.Nullable;
 import javax.annotation.PostConstruct;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.TimeUnit;
@@ -71,22 +73,40 @@ public class PersonServiceImpl implements PersonService {
     @Override
     @Transactional
     public boolean add(Person person) {
+        this.setAssociatedProperty(person);
         int c = personDao.add(person);
         this.updateMate(person);
         return c > 0;
     }
 
+    /**
+     * 设置关联属性
+     * @param p
+     */
+    private void setAssociatedProperty(Person p) {
+        //如果配偶ID不为空,则设置为已婚
+        if (p.getMateId() != null) {
+            p.setMaritalStatus(1);
+        }
+    }
+
+    @Transactional
     @Override
     public boolean update(Person person) {
+        this.setAssociatedProperty(person);
         this.updateMate(person);
         return personDao.update(person) > 0;
     }
 
     @Override
-    public Person findById(Long id) {
-        return personCache.getUnchecked(id);
+    public Person findById(@Nullable Long id) {
+        if (id != null) {
+            return personCache.getUnchecked(id);
+        }
+        return null;
     }
 
+    @Transactional
     @Override
     public int delete(Long id) {
         int c = personDao.deleteByIds(Arrays.asList(id));
@@ -100,6 +120,15 @@ public class PersonServiceImpl implements PersonService {
         return c;
     }
 
+    @Transactional
+    @Override
+    public int deleteByIds(Collection<Long> ids) {
+        for (Long id : ids) {
+            this.delete(id);
+        }
+        return 1;
+    }
+
     /**
      * 更新配偶人员
      *
@@ -107,6 +136,12 @@ public class PersonServiceImpl implements PersonService {
      */
     private void updateMate(Person person) {
         if (person.getMateId() == null) {
+            //解除婚姻状态
+            if (person.getId() != null) {
+                log.info("解除配偶关系:{}", person.getId());
+                personDao.updateNullMate(person.getId());
+                person.setMaritalStatus(0);
+            }
             return ;
         }
         //配偶人员
@@ -117,7 +152,9 @@ public class PersonServiceImpl implements PersonService {
         }
 
         if(matePerson.getMateId() == null) {
+            log.info("关联配偶关系:{}, {}", person.getMateId(), person.getId());
             matePerson.setMateId(person.getId());
+            matePerson.setMaritalStatus(1);
             personDao.update(matePerson);
         } else if (!person.getMateId().equals(matePerson.getMateId())) {
             log.error("该配偶{}的配偶{}不是当前人员", person.getMateId(), matePerson.getMateId());

+ 0 - 37
src/main/java/com/anyway/util/Couple.java

@@ -1,37 +0,0 @@
-package com.anyway.util;
-
-import lombok.Getter;
-import lombok.Setter;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.function.Function;
-
-/**
- * 成对的对象
- *
- * @author liuchuanwei
- * @date 2024-02-21
- */
-@Setter
-@Getter
-public class Couple {
-    private String title;
-    private String value;
-
-    public Couple() {
-    }
-
-    public Couple(String title, String value) {
-        this.title = title;
-        this.value = value;
-    }
-
-    public static <T> List<Couple> toList(List<T> list, Function<T, String> titleFun, Function<T, String> valFun) {
-        List<Couple> coupleList = new ArrayList<>();
-        for (T t : list) {
-            coupleList.add(new Couple(titleFun.apply(t), valFun.apply(t)));
-        }
-        return coupleList;
-    }
-}

+ 38 - 0
src/main/java/com/anyway/util/Pair.java

@@ -0,0 +1,38 @@
+package com.anyway.util;
+
+import lombok.Getter;
+import lombok.Setter;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Function;
+
+/**
+ * 成对的对象
+ *
+ * @author liuchuanwei
+ * @date 2024-02-21
+ */
+@Setter
+@Getter
+public class Pair<L, R> implements Serializable {
+    private L key;
+    private R value;
+
+    public Pair(L first, R second) {
+        this.key = first;
+        this.value = second;
+    }
+
+    public static <L, R> Pair<L, R> of(L first, R second) {
+        return new Pair<>(first, second);
+    }
+    public static <L, R, T> List<Pair<L, R>> toList(List<T> list, Function<T, L> keyFun, Function<T, R> valueFun) {
+        List<Pair<L, R>> pairList = new ArrayList<>();
+        for (T t : list) {
+            pairList.add(new Pair<>(keyFun.apply(t), valueFun.apply(t)));
+        }
+        return pairList;
+    }
+}

+ 13 - 1
src/main/resources/mapper/PersonDao.xml

@@ -76,6 +76,9 @@
                 #{item}
             </foreach>
         </if>
+        <if test="createBy != null">
+            AND p.create_by = #{createBy}
+        </if>
         <if test="sortName != null and sortName !=''">
             ORDER BY p.${sortName} ${sortOrder}
         </if>
@@ -146,12 +149,21 @@
             <if test="mateId != null">
                 mate_id = #{mateId},
             </if>
+            <if test="mateId == null">
+                mate_id = Null,
+            </if>
             <if test="motherId != null">
                 mother_id = #{motherId},
             </if>
+            <if test="motherId == null">
+                mother_id = Null,
+            </if>
             <if test="fatherId != null">
                 father_id = #{fatherId},
             </if>
+            <if test="fatherId == null">
+                father_id = Null,
+            </if>
             <if test="sort != null">
                 sort = #{sort},
             </if>
@@ -178,7 +190,7 @@
 
     <!-- 更新配偶为空 -->
     <update id="updateNullMate">
-        UPDATE t_person p set p.mate_id=Null WHERE p.mate_id=#{id}
+        UPDATE t_person p set p.mate_id=Null, p.marital_status=0 WHERE p.mate_id=#{id}
     </update>
     <!-- 更新母亲为空 -->
     <update id="updateNullMother">

+ 60 - 0
人员.http

@@ -0,0 +1,60 @@
+### 新增人员
+POST {{api_url}}/person/add
+Content-Type: application/json
+
+{
+  "name": "李四",
+  "callName": "四哥",
+  "gender": 1,
+  "mateId": 77,
+  "birthday": "1990-01-01",
+  "phone": "13800000000",
+  "address": "北京市朝阳区",
+  "remark": "李四嫂"
+}
+
+### 查询人员详情
+GET {{api_url}}/person/detail/75
+
+### 修改人员
+POST http://localhost:8080/person/update
+Content-Type: application/json
+
+{
+  "id": 79,
+  "name": "李四",
+  "callName": "四哥",
+  "gender": 1,
+  "mateId": 77,
+  "birthday": "1990-01-01",
+  "phone": "13800000000",
+  "address": "北京市朝阳区",
+  "remark": "李四"
+}
+
+
+### 删除人员
+POST {{api_url}}/person/delete?id=77&ids=
+
+### 分页查询
+GET {{api_url}}/person/listPage
+Content-Type: application/json
+
+{
+  "page": {
+    "pageNo": 1,
+    "pageSize": 10
+  }
+}
+
+
+### 下拉查询
+GET {{api_url}}/person/listOption
+Content-Type: application/json
+
+{
+  "page": {
+    "pageNo": 1,
+    "pageSize": 10
+  }
+}

+ 15 - 1
人情事件.http

@@ -5,7 +5,6 @@ Content-Type: application/json
 {
   "favorItemList": [
     {
-      "receivePersonId": 1,
       "returnGift": {
         "title": "100斤猪肉",
         "type": "T",
@@ -33,6 +32,21 @@ Content-Type: application/json
         "amount": 20000
       },
       "remark": "恭喜"
+    },
+    {
+      "receivePersonId": 1,
+      "returnGift": {
+        "title": "300斤猪肉",
+        "type": "T",
+        "amount": 2300
+      },
+      "givePersonId": 3,
+      "giveGift": {
+        "title": "3万",
+        "type": "M",
+        "amount": 30000
+      },
+      "remark": "恭喜"
     }
   ],
   "holdDate": "2024-08-01",