1
0

3 Revīzijas 04f116ec54 ... a89f684f74

Autors SHA1 Ziņojums Datums
  刘传伟 a89f684f74 feat:修改人员增删改查的问题 1 mēnesi atpakaļ
  刘传伟 7bb2860c5b feat:新增人员保存接口,包含新增和更新 1 mēnesi atpakaļ
  刘传伟 96576c4a33 feat:添加配置忽略json对象属性无法对应@RequestBody类属性 1 mēnesi atpakaļ

+ 13 - 0
src/main/java/com/anyway/exception/GlobalExceptionHandler.java

@@ -45,6 +45,19 @@ public class GlobalExceptionHandler {
     }
 
     /**
+     * 业务异常
+     *
+     * @param e
+     * @return
+     */
+    @ExceptionHandler(BusinessException.class)
+    @ResponseBody
+    public R<Void> handleBusinessException(BusinessException e){
+        log.error("业务异常:{}", e.getMessage(), e);
+        return R.fail(e.getMessage());
+    }
+
+    /**
      * 处理不可预知的异常
      *
      * @param e

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

@@ -53,7 +53,26 @@ public class PersonController {
     public R<Void> update(@Validated({Update.class}) @RequestBody Person person) {
         User currentUser = SessionUtils.currentUser();
         person.setModifyBy(currentUser.getId());
-        return personService.update(person) ? R.ok() : R.fail("保存失败");
+        return personService.update(person) ? R.ok() : R.fail("更新失败");
+    }
+
+    /**
+     * 保存人员
+     *
+     * @param person
+     * @return
+     */
+    @PostMapping("/save")
+    public R<Void> save(@Validated @RequestBody Person person) {
+        User currentUser = SessionUtils.currentUser();
+        if(person.getId() != null) {
+            person.setModifyBy(currentUser.getId());
+            return personService.update(person) ? R.ok() : R.fail("更新失败");
+        } else {
+            person.setCreateBy(currentUser.getId());
+            return personService.add(person) ? R.ok() : R.fail("新增失败");
+        }
+
     }
 
     /**
@@ -62,8 +81,8 @@ public class PersonController {
      * @param id
      * @return
      */
-    @GetMapping("/detail/{id}")
-    public R<PersonVo> detail(@PathVariable Long id) {
+    @GetMapping("/detail")
+    public R<PersonVo> detail(@RequestParam Long id) {
         Person person = personService.findById(id);
         return R.data(this.toPersonVo(person));
     }
@@ -108,14 +127,14 @@ public class PersonController {
      *
      * @return
      */
-    @GetMapping("/listOption")
+    @PostMapping("/listOption")
     public R<List<Pair<String, Long>>> listOption(@RequestBody PageQuery<Map<String, Object>> pageQuery) {
         User currentUser = SessionUtils.currentUser();
         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);
+        List<Pair<String, Long>> personOptionList = Pair.toList(personList, e -> e.getName() + "(" + e.getCallName() + ")", Person::getId);
         return R.data(personOptionList);
     }
 

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

@@ -156,7 +156,7 @@ public class PersonServiceImpl implements PersonService {
             matePerson.setMateId(person.getId());
             matePerson.setMaritalStatus(1);
             personDao.update(matePerson);
-        } else if (!person.getMateId().equals(matePerson.getMateId())) {
+        } else if (!Objects.equals(person.getId(), matePerson.getMateId())) {
             log.error("该配偶{}的配偶{}不是当前人员", person.getMateId(), matePerson.getMateId());
             throw new BusinessException("该配偶的配偶不是当前人员");
         }

+ 24 - 9
src/main/resources/mapper/PersonDao.xml

@@ -8,8 +8,8 @@
 
     <!-- 表查询字段 -->
     <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.address, p.remark, p.create_time, p.create_by, p.modify_time,
+        p.id, p.name, p.card_id, p.phone, 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.tags, p.remark, p.weight, p.create_time, p.create_by, p.modify_time,
         p.modify_by
     </sql>
 
@@ -53,6 +53,9 @@
         <if test="mateId != null">
             AND p.mate_id = #{mateId}
         </if>
+        <if test="mateIsNull != null">
+            AND p.mate_id is null
+        </if>
         <if test="motherId != null">
             AND p.mother_id = #{motherId}
         </if>
@@ -101,11 +104,12 @@
     <!-- 新增人员表信息 -->
     <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, address, remark, create_by
+            name, card_id, phone, call_name, gender, birthday, death_date, marital_status,
+            mate_id, mother_id, father_id, sort, address, tags, remark, weight, create_by
         ) VALUES (
             #{name},
             #{cardId},
+            #{phone},
             #{callName},
             #{gender},
             #{birthday},
@@ -116,7 +120,9 @@
             #{fatherId},
             #{sort},
             #{address},
+            #{tags},
             #{remark},
+            #{weight},
             #{createBy}
         )
     </insert>
@@ -125,13 +131,16 @@
     <update id="update">
         UPDATE t_person
         <set>
-            <if test="name != null">
+            <if test="@Ognl@isNotEmpty(name)">
                 name = #{name},
             </if>
-            <if test="cardId != null">
+            <if test="@Ognl@isNotEmpty(cardId)">
                 card_id = #{cardId},
             </if>
-            <if test="callName != null">
+            <if test="@Ognl@isNotEmpty(phone)">
+                phone = #{phone},
+            </if>
+            <if test="@Ognl@isNotEmpty(callName)">
                 call_name = #{callName},
             </if>
             <if test="gender != null">
@@ -167,12 +176,18 @@
             <if test="sort != null">
                 sort = #{sort},
             </if>
-            <if test="address != null">
+            <if test="@Ognl@isNotEmpty(address)">
                 address = #{address},
             </if>
-            <if test="remark != null">
+            <if test="@Ognl@isNotEmpty(tags)">
+                tags = #{tags},
+            </if>
+            <if test="@Ognl@isNotEmpty(remark)">
                 remark = #{remark},
             </if>
+            <if test="weight != null">
+                weight = #{weight},
+            </if>
             <if test="modifyBy != null">
                 modify_by = #{modifyBy}
             </if>

+ 20 - 8
src/main/webapp/WEB-INF/spring-servlet.xml

@@ -11,17 +11,29 @@
 	<mvc:annotation-driven>
 		<mvc:message-converters>
 			<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
-				<property name="objectMapper">
-					<bean class="com.fasterxml.jackson.databind.ObjectMapper">
-						<!-- 为null字段时不显示 -->
-						<property name="serializationInclusion">
-							<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
-						</property>
-					</bean>
-				</property>
+				<property name="objectMapper" ref="objectMapper" />
 			</bean>
 		</mvc:message-converters>
 	</mvc:annotation-driven>
+	<bean id="objectMapper" class="com.fasterxml.jackson.databind.ObjectMapper">
+		<!-- 为null字段时不显示 -->
+		<property name="serializationInclusion">
+			<value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value>
+		</property>
+	</bean>
+	<!-- 使用 MethodInvokingFactoryBean 来调用 configure 方法 -->
+    <!-- 解决JSON parse error: Unrecognized field "xxx"异常问题 -->
+	<bean id="configureObjectMapper" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
+		<property name="targetObject" ref="objectMapper"/>
+		<property name="targetMethod" value="configure"/>
+		<property name="arguments">
+			<list>
+				<value type="com.fasterxml.jackson.databind.DeserializationFeature">FAIL_ON_UNKNOWN_PROPERTIES</value>
+				<value>false</value>
+			</list>
+		</property>
+	</bean>
+
 	<!-- 设置使用注解的类所在的jar包 -->
 	<context:component-scan base-package="com.anyway.favor.**.controller"/>
 	<!-- 拦截器 -->