feat(可回溯): 重构

处理类型映射
This commit is contained in:
BrandWang
2023-05-09 11:03:31 +08:00
parent dbaab0484b
commit 875a14f2b7
11 changed files with 142 additions and 69 deletions
@@ -48,4 +48,6 @@ public interface RecallRecordDao extends BaseMapper<RecallRecordEntity> {
* @return
*/
RecallRecordVO queryRecallRecordById(@Param("id") String id);
List<RecallRecordVO> queryTraceRecords(@Param("traceId") String traceId);
}
@@ -1,6 +1,10 @@
package com.wabestway.recall.trace.handler;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wabestway.recall.trace.entity.RecallRecordEntity;
import com.wabestway.recall.trace.vo.RecallRecordVO;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
@@ -11,32 +15,62 @@ import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@MappedTypes(value = {RecallRecordVO.class})
@MappedJdbcTypes(value = {JdbcType.LONGNVARCHAR})
@MappedTypes(value = {List.class})
@MappedJdbcTypes(value = {JdbcType.BLOB})
public class ListTypeHandler implements TypeHandler<List<String>> {
private ObjectMapper objectMapper = new ObjectMapper();
@Override
public void setParameter(PreparedStatement ps, int i, List<String> strings, JdbcType jdbcType) throws SQLException {
ps.setString(i, String.join(",", strings));
String jsonStr = null;
try {
jsonStr = objectMapper.writeValueAsString(strings);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
ps.setString(i, jsonStr);
}
@Override
public List<String> getResult(ResultSet rs, String s) throws SQLException {
String str = rs.getString(s);
return Arrays.asList(str.split(","));
List<String> newList = new ArrayList<>();
try {
newList = objectMapper.readValue(str, new TypeReference<List<String>>() {
});
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
return newList;
}
@Override
public List<String> getResult(ResultSet rs, int i) throws SQLException {
String str = rs.getString(i);
return Arrays.asList(str.split(","));
List<String> newList = new ArrayList<>();
try {
newList = objectMapper.readValue(str, new TypeReference<List<String>>() {
});
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
return newList;
}
@Override
public List<String> getResult(CallableStatement cs, int i) throws SQLException {
String str = cs.getString(i);
return Arrays.asList(str.split(","));
List<String> newList = new ArrayList<>();
try {
newList = objectMapper.readValue(str, new TypeReference<List<String>>() {
});
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
return newList;
}
}
@@ -44,5 +44,12 @@ public interface RecallRecordService extends IService<RecallRecordEntity> {
* @return
*/
RecallRecordVO queryRecallRecordById(String id);
/**
* 根据traceId 查询列表
* @param traceId
* @return
*/
List<RecallRecordVO> queryTraceRecords(String traceId);
}
@@ -53,7 +53,7 @@ public class RecallOrderServiceImpl extends ServiceImpl<RecallOrderDao, RecallOr
@Override
public RecallOrderEntity queryRecallOrderByTraceId(String traceId) {
LambdaQueryWrapper<RecallOrderEntity> orderQuery = Wrappers.lambdaQuery();
orderQuery.eq(RecallOrderEntity::getOrderId, traceId);
orderQuery.eq(RecallOrderEntity::getTraceId, traceId);
orderQuery.last("limit 1");
return baseMapper.selectOne(orderQuery);
}
@@ -43,4 +43,9 @@ public class RecallRecordServiceImpl extends ServiceImpl<RecallRecordDao, Recall
public RecallRecordVO queryRecallRecordById(String id ) {
return baseMapper.queryRecallRecordById(id);
}
@Override
public List<RecallRecordVO> queryTraceRecords(String traceId) {
return baseMapper.queryTraceRecords(traceId);
}
}
@@ -2,7 +2,10 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wabestway.recall.trace.dao.RecallRecordDao">
<resultMap id="queryOrderRecordMap" type="com.wabestway.recall.trace.vo.RecallRecordVO">
<result column="events" property="events"
typeHandler="com.wabestway.recall.trace.handler.ListTypeHandler"></result>
</resultMap>
<sql id="queryRecallRecordSql">
t.id id,
t.product_code productCode,
@@ -18,11 +21,11 @@
<select id="queryRecallRecordByCondition"
parameterType="com.wabestway.recall.trace.dto.RecallRecordDTO"
resultType="com.wabestway.recall.trace.vo.RecallRecordVO">
select
select
<include refid="queryRecallRecordSql"/>
from
from
recall_record t
where 1 = 1
where 1 = 1
<if test="recallRecordDTO.id != null and recallRecordDTO.id != '' ">
and t.id = #{recallRecordDTO.id}
@@ -50,10 +53,10 @@
<select id="queryRecallRecordListNoPage"
parameterType="com.wabestway.recall.trace.dto.RecallRecordDTO"
resultType="com.wabestway.recall.trace.vo.RecallRecordVO">
select
select
<include refid="queryRecallRecordSql"/>
from recall_record t
where 1 = 1
from recall_record t
where 1 = 1
<if test="recallRecordDTO.id != null and recallRecordDTO.id != '' ">
and t.id = #{recallRecordDTO.id}
@@ -79,10 +82,10 @@
</select>
<select id="queryRecallRecordById" resultType="com.wabestway.recall.trace.vo.RecallRecordVO">
select
select
<include refid="queryRecallRecordSql"/>
from recall_record t
where t.id= #{id}
from recall_record t
where t.id= #{id}
</select>
<update id="updateRecallRecordById" parameterType="com.wabestway.recall.trace.entity.RecallRecordEntity">
@@ -114,4 +117,13 @@
where t.id= #{recallRecord.id}
</update>
<select id="queryTraceRecords" resultMap="queryOrderRecordMap">
select
<include refid="queryRecallRecordSql"/>
from recall_record t
where t.trace_id = #{traceId}
order by t.create_time asc
</select>
</mapper>