Parcourir la source

feat:重构人员新增接口

liuchuanwei il y a 2 mois
Parent
commit
01eecaee40

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

@@ -8,8 +8,8 @@ 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.util.StringUtils;
+import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.servlet.ModelAndView;
 
@@ -24,13 +24,27 @@ import java.util.Map;
  * @author liuchuanwei
  * @date 2024-02-20
  */
-@Controller
+@RestController
 @RequestMapping("/person")
 public class PersonController {
     @Autowired
     private PersonService personService;
 
     /**
+     * 添加人员
+     *
+     * @param person
+     * @return
+     */
+    @ResponseBody
+    @RequestMapping("/add")
+    public R<Void> add(@Validated @RequestBody Person person) {
+        User currentUser = SessionUtils.currentUser();
+        person.setCreateBy(currentUser.getId());
+        return personService.add(person) ? R.ok() : R.fail("新增失败");
+    }
+
+    /**
      * 跳转添加人员页面
      *
      * @return
@@ -61,20 +75,6 @@ public class PersonController {
         return mv;
     }
 
-    /**
-     * 添加人员
-     *
-     * @param person
-     * @return
-     */
-    @ResponseBody
-    @RequestMapping("/add")
-    public R add(@RequestBody Person person) {
-        User currentUser = SessionUtils.currentUser();
-        person.setCreateBy(currentUser.getId());
-        person.setModifyBy(currentUser.getId());
-        return personService.add(person) ? R.ok() : R.fail("新增失败");
-    }
 
     /**
      * 跳转编辑人员页面

+ 25 - 21
src/main/java/com/anyway/favor/model/Person.java

@@ -5,11 +5,12 @@ import lombok.Getter;
 import lombok.Setter;
 import org.springframework.format.annotation.DateTimeFormat;
 
+import javax.validation.constraints.NotBlank;
 import java.util.Date;
 
 /**
  * 人员表(t_person)
- * 
+ *
  * @author anyway
  * @date 2024-02-20
  */
@@ -20,37 +21,40 @@ public class Person extends BaseModel implements java.io.Serializable {
     private static final long serialVersionUID = -476789774769490644L;
     /** id */
     private Long id;
-    /** 姓名 */
-    private String name;
-    /** 身份证号 */
-    private String cardId;
     /** 称呼 */
+    @NotBlank(message = "称呼不能为空")
     private String callName;
+    /** 姓名 */
+    @NotBlank(message = "姓名不能为空")
+    private String name;
     /** 性别,1男2女 */
     private Integer gender;
-    /** 生日 */
-    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
-    @DateTimeFormat(pattern = "yyyy-MM-dd")
-    private Date birthday;
-    /** 去世日期 */
-    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
-    @DateTimeFormat(pattern = "yyyy-MM-dd")
-    private Date deathDate;
-    /** 婚姻状况,0未婚1已婚 */
-    private Integer maritalStatus;
     /** 配偶ID */
     private Long mateId;
-    /** 母亲ID */
-    private Long motherId;
+    /** 婚姻状况,0未婚1已婚 */
+    private Integer maritalStatus;
     /** 父亲ID */
     private Long fatherId;
+    /** 母亲ID */
+    private Long motherId;
     /** 家中排行 */
     private Integer sort;
     /** 详细地址 */
     private String address;
-    /** 备注 */
-    private String remark;
     /** 标签 json 字符串 */
     private String tags;
-
-}
+    /** 身份证号 */
+    private String cardId;
+    /** 手机号 */
+    private String phone;
+    /** 生日 */
+    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
+    @DateTimeFormat(pattern = "yyyy-MM-dd")
+    private Date birthday;
+    /** 去世日期 */
+    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
+    @DateTimeFormat(pattern = "yyyy-MM-dd")
+    private Date deathDate;
+    /** 备注 */
+    private String remark;
+}

+ 30 - 23
src/main/java/com/anyway/favor/service/impl/PersonServiceImpl.java

@@ -1,5 +1,6 @@
 package com.anyway.favor.service.impl;
 
+import com.anyway.exception.BusinessException;
 import com.anyway.favor.dao.PersonDao;
 import com.anyway.favor.model.Person;
 import com.anyway.favor.service.PersonService;
@@ -11,6 +12,7 @@ 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 javax.annotation.PostConstruct;
 import java.util.Arrays;
@@ -29,7 +31,7 @@ import java.util.concurrent.TimeUnit;
 public class PersonServiceImpl implements PersonService {
 
     @Autowired
-    private PersonDao personMapper;
+    private PersonDao personDao;
     /**
      * 人员缓存
      */
@@ -43,32 +45,33 @@ public class PersonServiceImpl implements PersonService {
                 .build(new CacheLoader<Long, Person>() {
                     @Override
                     public Person load(Long id) {
-                        return personMapper.findById(id);
+                        return personDao.findById(id);
                     }
                 });
     }
 
     @Override
     public List<Person> findAll() {
-        return personMapper.findAll();
+        return personDao.findAll();
     }
 
     @Override
     public List<Person> findByCondition(Map<String, Object> map) {
-        return personMapper.findByCondition(map);
+        return personDao.findByCondition(map);
     }
 
     @Override
     public List<Person> findPage(PageQuery<Map<String, Object>> pageQuery) {
         PageUtils.startPage(pageQuery.getPage());
-        List<Person> personList = personMapper.findByCondition(pageQuery.getTerms());
+        List<Person> personList = personDao.findByCondition(pageQuery.getTerms());
         PageUtils.setPageTotal(personList, pageQuery.getPage());
         return personList;
     }
 
     @Override
+    @Transactional
     public boolean add(Person person) {
-        int c = personMapper.add(person);
+        int c = personDao.add(person);
         this.updateMate(person);
         return c > 0;
     }
@@ -76,7 +79,7 @@ public class PersonServiceImpl implements PersonService {
     @Override
     public boolean update(Person person) {
         this.updateMate(person);
-        return personMapper.update(person) > 0;
+        return personDao.update(person) > 0;
     }
 
     @Override
@@ -86,14 +89,14 @@ public class PersonServiceImpl implements PersonService {
 
     @Override
     public int delete(Long id) {
-        int c = personMapper.deleteByIds(Arrays.asList(id));
+        int c = personDao.deleteByIds(Arrays.asList(id));
         if (c < 1) {
             return c;
         }
         //更新相关人员的配偶、父母
-        personMapper.updateNullMate(id);
-        personMapper.updateNullMother(id);
-        personMapper.updateNullFather(id);
+        personDao.updateNullMate(id);
+        personDao.updateNullMother(id);
+        personDao.updateNullFather(id);
         return c;
     }
 
@@ -103,18 +106,22 @@ public class PersonServiceImpl implements PersonService {
      * @param person
      */
     private void updateMate(Person person) {
-        if(person.getMateId() != null) {
-            Person matePerson = this.findById(person.getMateId());
-            if (matePerson != null) {
-                if(matePerson.getMateId() == null) {
-                    matePerson.setMateId(person.getId());
-                    personMapper.update(matePerson);
-                } else if (!person.getMateId().equals(matePerson.getMateId())) {
-                    log.error("该配偶存在其他配偶");
-                } else {
-                    //
-                }
-            }
+        if (person.getMateId() == null) {
+            return ;
+        }
+        //配偶人员
+        Person matePerson = this.findById(person.getMateId());
+        if (matePerson == null) {
+            log.error("配偶人员{}不存在", person.getMateId());
+            throw new BusinessException("无效的配偶");
+        }
+
+        if(matePerson.getMateId() == null) {
+            matePerson.setMateId(person.getId());
+            personDao.update(matePerson);
+        } else if (!person.getMateId().equals(matePerson.getMateId())) {
+            log.error("该配偶{}的配偶{}不是当前人员", person.getMateId(), matePerson.getMateId());
+            throw new BusinessException("该配偶的配偶不是当前人员");
         }
     }
 }