|
@@ -0,0 +1,356 @@
|
|
|
+package com.rongwei.bscommon.sys.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.rongwei.bscommon.sys.service.CommonInterfaceService;
|
|
|
+import com.rongwei.bscommon.sys.service.ZhcxInterfaceInformationGetParamService;
|
|
|
+import com.rongwei.bscommon.sys.service.ZhcxInterfaceInformationGetService;
|
|
|
+import com.rongwei.bsentity.domain.ZhcxInterfaceInformationGetDo;
|
|
|
+import com.rongwei.bsentity.domain.ZhcxInterfaceInformationGetParamDo;
|
|
|
+import com.rongwei.bsentity.dto.CommonInterfaceRequest;
|
|
|
+import com.rongwei.bsentity.dto.InterfaceCommonSqlPropertyDto;
|
|
|
+import com.rongwei.bsentity.dto.InterfaceWriteParamDto;
|
|
|
+import com.rongwei.commonservice.service.dao.CommonSqlDao;
|
|
|
+import com.rongwei.rwadmincommon.system.dao.SysGeneralCRUDDao;
|
|
|
+import com.rongwei.rwcommon.base.exception.CustomException;
|
|
|
+import com.rongwei.rwcommon.utils.SecurityUtil;
|
|
|
+import com.rongwei.rwcommon.utils.StringUtils;
|
|
|
+import com.rongwei.rwcommon.vo.ColumnQueryType;
|
|
|
+import com.rongwei.rwcommon.vo.CriteriaBuilder;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.PlatformTransactionManager;
|
|
|
+import org.springframework.transaction.TransactionDefinition;
|
|
|
+import org.springframework.transaction.TransactionStatus;
|
|
|
+import org.springframework.transaction.support.DefaultTransactionDefinition;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class CommonInterfaceServiceImpl implements CommonInterfaceService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ZhcxInterfaceInformationGetService zhcxInterfaceInformationGetService;
|
|
|
+ @Autowired
|
|
|
+ private ZhcxInterfaceInformationGetParamService zhcxInterfaceInformationGetParamService;
|
|
|
+ @Autowired
|
|
|
+ private SysGeneralCRUDDao generalCRUDDao;
|
|
|
+ @Autowired
|
|
|
+ private PlatformTransactionManager platformTransactionManager;
|
|
|
+ @Autowired
|
|
|
+ private CommonSqlDao commonSqlDao;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 基础查询
|
|
|
+ *
|
|
|
+ * @param query
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<Map<String, Object>> commonBaseGet(CommonInterfaceRequest<List<ColumnQueryType>> query) {
|
|
|
+ //验签
|
|
|
+ verifySign(query);
|
|
|
+
|
|
|
+ return executeBaseGet(query);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 基础写入
|
|
|
+ *
|
|
|
+ * @param json
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void commonBaseWrite(CommonInterfaceRequest<List<JSONObject>> json) {
|
|
|
+ //验证
|
|
|
+ verifySign(json);
|
|
|
+
|
|
|
+ //写入
|
|
|
+ executeBaseWrite(json);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验签
|
|
|
+ *
|
|
|
+ * @param param
|
|
|
+ */
|
|
|
+ private void verifySign(CommonInterfaceRequest<?> param) {
|
|
|
+ if(ObjectUtil.isEmpty(param.getInterfaceCode())) {
|
|
|
+ throw new CustomException("接口不存在!");
|
|
|
+ }
|
|
|
+
|
|
|
+ if(ObjectUtil.isEmpty(param.getSign())) {
|
|
|
+ throw new CustomException("请求缺少签名!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 执行
|
|
|
+ *
|
|
|
+ * @param query
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<Map<String, Object>> executeBaseGet(CommonInterfaceRequest<List<ColumnQueryType>> query) {
|
|
|
+ //获取接口
|
|
|
+ ZhcxInterfaceInformationGetDo informationGetDo = zhcxInterfaceInformationGetService.getByCodeThrow(query.getInterfaceCode());
|
|
|
+ //格式化
|
|
|
+ String querySql = initQuerySql(informationGetDo, query.getData());
|
|
|
+ return generalCRUDDao.queryDataFromSql(querySql);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 写入
|
|
|
+ *
|
|
|
+ * @param json
|
|
|
+ */
|
|
|
+ private void executeBaseWrite(CommonInterfaceRequest<List<JSONObject>> json) {
|
|
|
+ if(ObjectUtil.isEmpty(json.getData())) {
|
|
|
+ throw new CustomException("请传入请求参数");
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取接口
|
|
|
+ ZhcxInterfaceInformationGetDo informationGetDo = zhcxInterfaceInformationGetService.getByCodeThrow(json.getInterfaceCode());
|
|
|
+
|
|
|
+ //字段信息
|
|
|
+ InterfaceWriteParamDto writeParamDto = getinterfaceWriteCols(informationGetDo.getId());
|
|
|
+
|
|
|
+ InterfaceCommonSqlPropertyDto propertyDto = InterfaceCommonSqlPropertyDto.builder().build();
|
|
|
+ //生成sql
|
|
|
+ List<String> sqlList = genSql(informationGetDo.getTablename(), writeParamDto, json.getData(), propertyDto);
|
|
|
+
|
|
|
+ DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
|
|
|
+ //隔离级别
|
|
|
+ definition.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
|
|
|
+ definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
|
|
|
+ TransactionStatus status = platformTransactionManager.getTransaction(definition);
|
|
|
+ try {
|
|
|
+ for(String sql : sqlList) {
|
|
|
+ commonSqlDao.sqlUpdate(sql);
|
|
|
+ }
|
|
|
+ platformTransactionManager.commit(status);
|
|
|
+ } catch (Exception e) {
|
|
|
+ platformTransactionManager.rollback(status);
|
|
|
+ log.error("保存失败", e);
|
|
|
+ throw new CustomException("保存失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成执行sql
|
|
|
+ *
|
|
|
+ * @param tableName
|
|
|
+ * @param writeParamDto
|
|
|
+ * @param jsonList
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<String> genSql(String tableName, InterfaceWriteParamDto writeParamDto, List<JSONObject> jsonList, InterfaceCommonSqlPropertyDto sqlPropertyDto) {
|
|
|
+ List<String> sqlList = new ArrayList<>();
|
|
|
+ //请求数据
|
|
|
+ for(JSONObject json : jsonList) {
|
|
|
+ //唯一标识sql片段
|
|
|
+ Set<String> uniqueWheres = getUniqueWheres(writeParamDto.getUniqueColList(), json);
|
|
|
+ String sqlType = getExecuteSqlType(tableName, uniqueWheres);
|
|
|
+ sqlPropertyDto.setSqlType(sqlType);
|
|
|
+
|
|
|
+ List<String> valueSql = colValueSql(writeParamDto, json, sqlPropertyDto);
|
|
|
+ StringBuilder sqlSb = new StringBuilder();
|
|
|
+ if("insert".equals(sqlType)) {
|
|
|
+ sqlSb.append("INSERT INTO ").append(tableName).append("(");
|
|
|
+ sqlSb.append(StringUtils.join(writeParamDto.getColList(), ","));
|
|
|
+ //如果没有主键,添加主键
|
|
|
+ if(!writeParamDto.getColList().contains("ID")) {
|
|
|
+ sqlSb.append(", ID");
|
|
|
+ }
|
|
|
+ if(!writeParamDto.getColList().contains("DELETED")) {
|
|
|
+ sqlSb.append(", DELETED");
|
|
|
+ }
|
|
|
+ //value
|
|
|
+ sqlSb.append(") VALUES (");
|
|
|
+ sqlSb.append(StringUtils.join(valueSql, ","));
|
|
|
+ //如果没有主键,添加主键
|
|
|
+ if(!writeParamDto.getColList().contains("ID")) {
|
|
|
+ sqlSb.append(", '").append(SecurityUtil.getUUID()).append("'");
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!writeParamDto.getColList().contains("DELETED")) {
|
|
|
+ sqlSb.append(", '0'");
|
|
|
+ }
|
|
|
+
|
|
|
+ sqlSb.append(")");
|
|
|
+ } else {
|
|
|
+ sqlSb.append("UPDATE ").append(tableName).append(" SET ");
|
|
|
+ sqlSb.append(StringUtils.join(valueSql, ","));
|
|
|
+ sqlSb.append(" WHERE ");
|
|
|
+ sqlSb.append(StringUtils.join(uniqueWheres, " AND "));
|
|
|
+ }
|
|
|
+
|
|
|
+ sqlList.add(sqlSb.toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ return sqlList;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 值sql片段
|
|
|
+ *
|
|
|
+ * @param writeParamDto
|
|
|
+ * @param json
|
|
|
+ * @param sqlPropertyDto
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private List<String> colValueSql(InterfaceWriteParamDto writeParamDto, JSONObject json, InterfaceCommonSqlPropertyDto sqlPropertyDto) {
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
+ for(String col : writeParamDto.getColList()) {
|
|
|
+ //值为空,不做生成sql
|
|
|
+ if(ObjectUtil.isEmpty(json.getStr(col))) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ //修改
|
|
|
+ if("update".equals(sqlPropertyDto.getSqlType())) {
|
|
|
+ sb.append(col).append("='").append(json.getStr(col)).append("'");
|
|
|
+ }
|
|
|
+ //插入
|
|
|
+ else {
|
|
|
+ sb.append("'").append(json.getStr(col)).append("'");
|
|
|
+ }
|
|
|
+
|
|
|
+ list.add(sb.toString());
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断数据为新增或者修改
|
|
|
+ *
|
|
|
+ * @param tableName
|
|
|
+ * @param uniqueWheres
|
|
|
+ * @return insert update
|
|
|
+ */
|
|
|
+ private String getExecuteSqlType(String tableName, Set<String> uniqueWheres) {
|
|
|
+ StringBuilder existSql = new StringBuilder();
|
|
|
+ existSql.append("SELECT COUNT(1) AS total FROM ").append(tableName).append(" WHERE ");
|
|
|
+ existSql.append(StringUtils.join(uniqueWheres, " AND ")).append(" LIMIT 1");
|
|
|
+ List<Map<String, Object>> maps = generalCRUDDao.queryDataFromSql(existSql.toString());
|
|
|
+
|
|
|
+ if(ObjectUtil.isEmpty(maps)) {
|
|
|
+ return "insert";
|
|
|
+ }
|
|
|
+
|
|
|
+ if(ObjectUtil.isNull(maps.get(0))) {
|
|
|
+ return "insert";
|
|
|
+ }
|
|
|
+
|
|
|
+ if(ObjectUtil.isNull(maps.get(0).get("total"))) {
|
|
|
+ return "insert";
|
|
|
+ }
|
|
|
+
|
|
|
+ int total = Integer.parseInt(String.valueOf(maps.get(0).get("total")));
|
|
|
+
|
|
|
+ return total > 0 ? "update" : "insert";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取唯一标识sql片段
|
|
|
+ *
|
|
|
+ * @param uniqueColList
|
|
|
+ * @param json
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private Set<String> getUniqueWheres(List<String> uniqueColList, JSONObject json) {
|
|
|
+ //唯一标识
|
|
|
+ Set<String> uniqueWheres = new HashSet<>(uniqueColList.size());
|
|
|
+ //唯一标识
|
|
|
+ for(String unique: uniqueColList) {
|
|
|
+ StringBuilder uniqueSb = new StringBuilder();
|
|
|
+ uniqueSb.append(unique).append("=");
|
|
|
+ if(ObjectUtil.isNotNull(json.getStr(unique))) {
|
|
|
+ uniqueSb.append("'").append(json.getStr(unique, "")).append("'");
|
|
|
+ } else {
|
|
|
+ uniqueSb.append("''");
|
|
|
+ }
|
|
|
+ uniqueWheres.add(uniqueSb.toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!uniqueColList.contains("DELETED")) {
|
|
|
+ uniqueWheres.add("DELETED='0'");
|
|
|
+ }
|
|
|
+
|
|
|
+ return uniqueWheres;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取字段信息
|
|
|
+ *
|
|
|
+ * @param interfaceId
|
|
|
+ * @return 如果未查询到,会返回null
|
|
|
+ */
|
|
|
+ private InterfaceWriteParamDto getinterfaceWriteCols(String interfaceId) {
|
|
|
+ //请求参数
|
|
|
+ LambdaQueryWrapper<ZhcxInterfaceInformationGetParamDo> queryWrapper = Wrappers.lambdaQuery();
|
|
|
+ queryWrapper.eq(ZhcxInterfaceInformationGetParamDo::getDeleted, "0")
|
|
|
+ .eq(ZhcxInterfaceInformationGetParamDo::getParamtype, "in")
|
|
|
+ .eq(ZhcxInterfaceInformationGetParamDo::getInterfaceid, interfaceId);
|
|
|
+ List<ZhcxInterfaceInformationGetParamDo> paramDoList = zhcxInterfaceInformationGetParamService.list(queryWrapper);
|
|
|
+
|
|
|
+ if(ObjectUtil.isEmpty(paramDoList)) {
|
|
|
+ throw new CustomException("请通知管理员,该接口无请求参数");
|
|
|
+ }
|
|
|
+ List<String> colList = new ArrayList<>(paramDoList.size());
|
|
|
+ List<String> uniqueColList = new ArrayList<>();
|
|
|
+ Map<String, ZhcxInterfaceInformationGetParamDo> map = new HashMap<>(paramDoList.size());
|
|
|
+ for(ZhcxInterfaceInformationGetParamDo param : paramDoList) {
|
|
|
+ colList.add(param.getColname());
|
|
|
+
|
|
|
+ //主键标识
|
|
|
+ if("1".equals(param.getUniqueflag())) {
|
|
|
+ uniqueColList.add(param.getColname());
|
|
|
+ }
|
|
|
+
|
|
|
+ map.put(param.getColname(), param);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(ObjectUtil.isEmpty(uniqueColList)) {
|
|
|
+ throw new CustomException("请联系管理员,该接口未设置唯一标识");
|
|
|
+ }
|
|
|
+
|
|
|
+ return InterfaceWriteParamDto.builder()
|
|
|
+ .colList(colList)
|
|
|
+ .uniqueColList(uniqueColList)
|
|
|
+ .colMap(map)
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ *
|
|
|
+ * @param informationGetDo
|
|
|
+ * @param param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String initQuerySql(ZhcxInterfaceInformationGetDo informationGetDo, List<ColumnQueryType> param) {
|
|
|
+ StringBuilder mainSb = new StringBuilder();
|
|
|
+ mainSb.append("SELECT * FROM");
|
|
|
+ if("table".equals(informationGetDo.getDatasourcetype())) {
|
|
|
+ mainSb.append(informationGetDo.getDatasource());
|
|
|
+ mainSb.append(" tb ");
|
|
|
+ } else if("sql".equals(informationGetDo.getDatasourcetype())) {
|
|
|
+ mainSb.append("(");
|
|
|
+ mainSb.append(informationGetDo.getDatasource());
|
|
|
+ mainSb.append(") tb ");
|
|
|
+ }
|
|
|
+
|
|
|
+ //where条件
|
|
|
+ StringBuffer whereSqlSb = new StringBuffer();
|
|
|
+ CriteriaBuilder.whereSql(whereSqlSb, param, informationGetDo.getDatasourcetype());
|
|
|
+
|
|
|
+ mainSb.append(whereSqlSb);
|
|
|
+
|
|
|
+ return mainSb.toString();
|
|
|
+ }
|
|
|
+}
|