From 45377caf4fca054cc1246dcc1cbccb1aed98b456 Mon Sep 17 00:00:00 2001 From: wangxuanran <3055088966@qq.com> Date: Fri, 14 Jul 2023 15:16:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../workflow/controller/GetExchangeRate.java | 44 +++++++ .../wang/log/cus_api_log/CusApiLogUtil.java | 109 ++++++++++++++++++ .../annotations/CusApiLogTable.java | 16 +++ .../log/cus_api_log/dto/CusApiLogBaseDto.java | 81 +++++++++++++ .../wang/log/cus_api_log/dto/DemoDto.java | 20 ++++ .../interfaces/CusLogDtoInterface.java | 10 ++ .../cus_api_log/mapper/CusApiLogMapper.java | 21 ++++ .../log/cus_api_log/pojo/CusApiLogPojo.java | 20 ++++ .../cus_api_log/test/CusApiLogUtilTest.java | 63 ++++++++++ 9 files changed, 384 insertions(+) create mode 100644 src/main/java/com/api/xuanran/wang/eny/workflow/controller/GetExchangeRate.java create mode 100644 src/test/java/xuanran/wang/log/cus_api_log/CusApiLogUtil.java create mode 100644 src/test/java/xuanran/wang/log/cus_api_log/annotations/CusApiLogTable.java create mode 100644 src/test/java/xuanran/wang/log/cus_api_log/dto/CusApiLogBaseDto.java create mode 100644 src/test/java/xuanran/wang/log/cus_api_log/dto/DemoDto.java create mode 100644 src/test/java/xuanran/wang/log/cus_api_log/interfaces/CusLogDtoInterface.java create mode 100644 src/test/java/xuanran/wang/log/cus_api_log/mapper/CusApiLogMapper.java create mode 100644 src/test/java/xuanran/wang/log/cus_api_log/pojo/CusApiLogPojo.java create mode 100644 src/test/java/xuanran/wang/log/cus_api_log/test/CusApiLogUtilTest.java diff --git a/src/main/java/com/api/xuanran/wang/eny/workflow/controller/GetExchangeRate.java b/src/main/java/com/api/xuanran/wang/eny/workflow/controller/GetExchangeRate.java new file mode 100644 index 0000000..c0bcc2a --- /dev/null +++ b/src/main/java/com/api/xuanran/wang/eny/workflow/controller/GetExchangeRate.java @@ -0,0 +1,44 @@ +package com.api.xuanran.wang.eny.workflow.controller; + +import aiyh.utils.ApiResult; +import aiyh.utils.Util; +import io.swagger.v3.oas.annotations.parameters.RequestBody; +import org.apache.commons.lang3.StringUtils; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.*; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import java.util.Map; + +/** + *

+ * + * @author xuanran.wang + * @date 2023/7/14 15:02 + */ +@Path("/wxr/eny/workflow/rate") +public class GetExchangeRate { + + @POST + @Path("/exchange") + @Produces(MediaType.APPLICATION_OCTET_STREAM) + @Consumes(MediaType.APPLICATION_JSON) + public String exchangeRate(@Context HttpServletRequest request, + @Context HttpServletResponse response, + @RequestBody Map params){ + String baseCurrency = request.getParameter("baseCurrency"); + String interfaceCurrency = request.getParameter("interfaceCurrency"); + params.get("area"); + if(StringUtils.isBlank(baseCurrency) || StringUtils.isBlank(interfaceCurrency)){ + return ApiResult.error("本位币或接口响应币种为空!"); + } + + } + + private void checkParams(Map params){ + for (Map.Entry entry : params.entrySet()) { + } + } +} diff --git a/src/test/java/xuanran/wang/log/cus_api_log/CusApiLogUtil.java b/src/test/java/xuanran/wang/log/cus_api_log/CusApiLogUtil.java new file mode 100644 index 0000000..80f6763 --- /dev/null +++ b/src/test/java/xuanran/wang/log/cus_api_log/CusApiLogUtil.java @@ -0,0 +1,109 @@ +package xuanran.wang.log.cus_api_log; + + +import aiyh.utils.Util; +import aiyh.utils.annotation.recordset.SqlDbFieldAnn; +import aiyh.utils.excention.CustomerException; +import aiyh.utils.recordset.MapperBuilderSql; +import org.apache.commons.lang3.StringUtils; +import weaver.soa.workflow.request.RequestInfo; +import xuanran.wang.log.cus_api_log.annotations.CusApiLogTable; +import xuanran.wang.log.cus_api_log.dto.CusApiLogBaseDto; +import xuanran.wang.log.cus_api_log.mapper.CusApiLogMapper; +import xuanran.wang.log.cus_api_log.pojo.CusApiLogPojo; + +import java.lang.reflect.Field; +import java.util.*; + +/** + *

+ * + * @author xuanran.wang + * @date 2023/7/11 11:39 + */ +public class CusApiLogUtil { + + private static final CusApiLogMapper mapper = Util.getMapper(CusApiLogMapper.class); + public static void insertApiLog(CusApiLogBaseDto dto){ + insertApiLog(dto, -1); + } + + public static void insertApiLog(CusApiLogBaseDto dto, int modelId){ + CusApiLogPojo pojo = parseDto(dto); + String tableName = pojo.getTableName(); + if(modelId < 0){ + modelId = Util.getIntValue(Util.getModeIdByTableName(tableName),-1); + if(modelId < 0){ + throw new CustomerException(tableName + " not found model id!"); + } + } + int modeDataId = Util.getModeDataId(tableName, modelId, 1); + Map params = pojo.getParams(); + Map whereMap = new HashMap<>(); + whereMap.put("id", modeDataId); + String where = MapperBuilderSql.builderNoWhereAndEn(whereMap); + String updateSql = MapperBuilderSql.builderUpdateSql(tableName, pojo.getParams()); + params.putAll(whereMap); + if (!mapper.insertApiLog(updateSql + " where " + where, params)) { + mapper.deleteErrorLog(tableName, modeDataId); + throw new CustomerException("insert log sql execute error! please check ecology.log and /cus/sql_log/cus.log"); + } + Util.rebuildModeDataShare(modeDataId, modelId, 1); + } + + public static T initCusApiLogDto(RequestInfo requestInfo, Class clazz){ + T obj; + try { + obj = clazz.newInstance(); + }catch (Exception e){ + throw new CustomerException("class : " + clazz.getName() + " new instance error!"); + } + if(Objects.isNull(requestInfo)){ + return obj; + } + obj.setUserWorkflow(requestInfo.getWorkflowid()); + String requestId = requestInfo.getRequestid(); + obj.setSourceRequestId(requestId); + String requestMark = Util.null2DefaultStr(mapper.selectRequestMark(requestId),""); + obj.setWorkflowNo(requestMark); + obj.setCreateUser(requestInfo.getRequestManager().getUser().getUID()); + return obj; + } + + private static CusApiLogPojo parseDto(CusApiLogBaseDto dto){ + String tableName = null; + Class childClass = dto.getClass(); + Field[] childFields = childClass.getDeclaredFields(); + List fieldList = new ArrayList<>(Arrays.asList(childFields)); + Class superClass = childClass.getSuperclass(); + Map params = new HashMap<>(); + if(superClass.isAssignableFrom(CusApiLogBaseDto.class)){ + fieldList.addAll(Arrays.asList(superClass.getDeclaredFields())); + tableName = parseTableName(superClass); + } + String childTableName = parseTableName(childClass); + if(StringUtils.isNotBlank(childTableName)){ + tableName = childTableName; + } + for (Field field : fieldList) { + SqlDbFieldAnn sqlDbFieldAnn = field.getAnnotation(SqlDbFieldAnn.class); + if(null != sqlDbFieldAnn){ + try { + field.setAccessible(true); + params.put(sqlDbFieldAnn.value(), field.get(dto)); + }catch (IllegalAccessException e){ + throw new IllegalArgumentException("field get value error! current field is [ " + field.getName() + " ]", e); + } + } + } + return CusApiLogPojo.builder().tableName(tableName).params(params).build(); + } + + private static String parseTableName(Class clazz){ + CusApiLogTable apiLogTable = clazz.getAnnotation(CusApiLogTable.class); + if(apiLogTable != null){ + return apiLogTable.value(); + } + return ""; + } +} diff --git a/src/test/java/xuanran/wang/log/cus_api_log/annotations/CusApiLogTable.java b/src/test/java/xuanran/wang/log/cus_api_log/annotations/CusApiLogTable.java new file mode 100644 index 0000000..4c04866 --- /dev/null +++ b/src/test/java/xuanran/wang/log/cus_api_log/annotations/CusApiLogTable.java @@ -0,0 +1,16 @@ +package xuanran.wang.log.cus_api_log.annotations; + +import java.lang.annotation.*; + +/** + *

+ * + * @author xuanran.wang + * @date 2023/7/11 13:05 + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +@Documented +public @interface CusApiLogTable { + String value(); +} diff --git a/src/test/java/xuanran/wang/log/cus_api_log/dto/CusApiLogBaseDto.java b/src/test/java/xuanran/wang/log/cus_api_log/dto/CusApiLogBaseDto.java new file mode 100644 index 0000000..903b7ce --- /dev/null +++ b/src/test/java/xuanran/wang/log/cus_api_log/dto/CusApiLogBaseDto.java @@ -0,0 +1,81 @@ +package xuanran.wang.log.cus_api_log.dto; + +import aiyh.utils.annotation.recordset.SqlDbFieldAnn; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import xuanran.wang.log.cus_api_log.annotations.CusApiLogTable; + +/** + *

接口日志实体类

+ * + * @author xuanran.wang + * @date 2023/7/11 11:40 + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +@CusApiLogTable(value = "uf_api_log") +public class CusApiLogBaseDto{ + /** + *

相关流程

+ **/ + @SqlDbFieldAnn(value = "user_workflow") + private String userWorkflow; + /** + *

流程编号

+ **/ + @SqlDbFieldAnn(value = "workflow_no") + private String workflowNo; + /** + *

接口标识

+ **/ + @SqlDbFieldAnn(value = "unique_code") + private String uniqueCode; + /** + *

请求地址

+ **/ + @SqlDbFieldAnn(value = "request_url") + private String requestUrl; + /** + *

请求说明

+ **/ + @SqlDbFieldAnn(value = "request_desc") + private String requestDesc; + /** + *

请求参数

+ **/ + @SqlDbFieldAnn(value = "request_param") + private String requestParam; + /** + *

请求结果

+ **/ + @SqlDbFieldAnn(value = "response") + private String response; + /** + *

异常信息

+ **/ + @SqlDbFieldAnn(value = "exception") + private String exception; + /** + *

流程创建人

+ **/ + @SqlDbFieldAnn(value = "create_user") + private int createUser; + /** + *

响应信息

+ **/ + @SqlDbFieldAnn(value = "response_msg") + private String responseMsg; + /** + *

请求id

+ **/ + @SqlDbFieldAnn(value = "source_request_id") + private String sourceRequestId; + /** + *

成功/失败

+ **/ + @SqlDbFieldAnn(value = "request_success") + private int requestSuccess; + +} diff --git a/src/test/java/xuanran/wang/log/cus_api_log/dto/DemoDto.java b/src/test/java/xuanran/wang/log/cus_api_log/dto/DemoDto.java new file mode 100644 index 0000000..4633993 --- /dev/null +++ b/src/test/java/xuanran/wang/log/cus_api_log/dto/DemoDto.java @@ -0,0 +1,20 @@ +package xuanran.wang.log.cus_api_log.dto; + +import aiyh.utils.annotation.recordset.SqlDbFieldAnn; +import lombok.*; +import xuanran.wang.log.cus_api_log.annotations.CusApiLogTable; + +/** + *

+ * + * @author xuanran.wang + * @date 2023/7/11 12:45 + */ +@EqualsAndHashCode(callSuper = true) +@Data +@NoArgsConstructor +@AllArgsConstructor +public class DemoDto extends CusApiLogBaseDto{ + @SqlDbFieldAnn(value = "ext") + private String ext; +} diff --git a/src/test/java/xuanran/wang/log/cus_api_log/interfaces/CusLogDtoInterface.java b/src/test/java/xuanran/wang/log/cus_api_log/interfaces/CusLogDtoInterface.java new file mode 100644 index 0000000..a2fd472 --- /dev/null +++ b/src/test/java/xuanran/wang/log/cus_api_log/interfaces/CusLogDtoInterface.java @@ -0,0 +1,10 @@ +package xuanran.wang.log.cus_api_log.interfaces; + +/** + *

+ * + * @author xuanran.wang + * @date 2023/7/11 12:44 + */ +public interface CusLogDtoInterface { +} diff --git a/src/test/java/xuanran/wang/log/cus_api_log/mapper/CusApiLogMapper.java b/src/test/java/xuanran/wang/log/cus_api_log/mapper/CusApiLogMapper.java new file mode 100644 index 0000000..21897c5 --- /dev/null +++ b/src/test/java/xuanran/wang/log/cus_api_log/mapper/CusApiLogMapper.java @@ -0,0 +1,21 @@ +package xuanran.wang.log.cus_api_log.mapper; + +import aiyh.utils.annotation.recordset.*; +import java.util.Map; + +/** + *

+ * + * @author xuanran.wang + * @date 2023/7/11 13:24 + */ +@SqlMapper +public interface CusApiLogMapper { + @Update(custom = true) + boolean insertApiLog(@SqlString String sql, Map params); + @Select("select requestMark from workflow_requestBase where requestId = #{requestId}") + String selectRequestMark(@ParamMapper("requestId") String requestId); + @Delete("delete from $t{tableName} where id = #{id}") + void deleteErrorLog(@ParamMapper("tableName") String tableName, + @ParamMapper("id") Integer id); +} diff --git a/src/test/java/xuanran/wang/log/cus_api_log/pojo/CusApiLogPojo.java b/src/test/java/xuanran/wang/log/cus_api_log/pojo/CusApiLogPojo.java new file mode 100644 index 0000000..7102fcd --- /dev/null +++ b/src/test/java/xuanran/wang/log/cus_api_log/pojo/CusApiLogPojo.java @@ -0,0 +1,20 @@ +package xuanran.wang.log.cus_api_log.pojo; + +import lombok.*; + +import java.util.Map; + +/** + *

+ * + * @author xuanran.wang + * @date 2023/7/11 13:08 + */ +@Builder +@Data +@AllArgsConstructor +@NoArgsConstructor +public class CusApiLogPojo { + private String tableName; + private Map params; +} diff --git a/src/test/java/xuanran/wang/log/cus_api_log/test/CusApiLogUtilTest.java b/src/test/java/xuanran/wang/log/cus_api_log/test/CusApiLogUtilTest.java new file mode 100644 index 0000000..ae4e744 --- /dev/null +++ b/src/test/java/xuanran/wang/log/cus_api_log/test/CusApiLogUtilTest.java @@ -0,0 +1,63 @@ +package xuanran.wang.log.cus_api_log.test; + +import aiyh.utils.httpUtil.ResponeVo; +import basetest.BaseTest; +import org.junit.Test; +import weaver.hrm.User; +import weaver.soa.workflow.request.RequestInfo; +import weaver.workflow.request.RequestManager; +import xuanran.wang.rest_test.pojo.CusRestTemplateResponse; +import xuanran.wang.log.cus_api_log.CusApiLogUtil; +import xuanran.wang.log.cus_api_log.dto.DemoDto; +import xuanran.wang.rest_test.CusRestTemplate; + +import java.util.HashMap; +import java.util.Map; + +/** + *

+ * + * @author xuanran.wang + * @date 2023/7/11 12:47 + */ +public class CusApiLogUtilTest extends BaseTest { + + private final CusRestTemplate cusRestTemplate = new CusRestTemplate(); + + @Test + public void test(){ + RequestInfo info = new RequestInfo(); + info.setWorkflowid("1"); + info.setRequestid("123"); + RequestManager manager = new RequestManager(); + manager.setUser(new User(1)); + info.setRequestManager(manager); + DemoDto demoDto = CusApiLogUtil.initCusApiLogDto(info, DemoDto.class); + Map param = new HashMap<>(); + param.put("grant_type","client_credentials"); + param.put("client_id","fanwei"); + param.put("client_secret","b7c20b12cb124cbebb0b8833caac6b87"); + + CusRestTemplateResponse success = CusRestTemplateResponse + .builder() + .successField("code") + .successValue(0) + .errorMsg("msg") + .logDto(demoDto) + .build(); + + success.setSuccessCallBack(cusLogDtoInterface -> { + DemoDto dto = (DemoDto) cusLogDtoInterface; + dto.setExt("1"); + }); + + success.setErrorCallBack((exception,cusLogDtoInterface)->{ + DemoDto dto = (DemoDto) cusLogDtoInterface; + dto.setExt("失败了"); + } ); + + ResponeVo responeVo = cusRestTemplate.apiGet("https://gl.shstx.cn:48002/auth/oauth/token", param, new HashMap<>(), success); + String token = cusRestTemplate.parseCommon(responeVo.getResponseMap(), "data.access_token", String.class); + System.out.println("token => " + token); + } +}