diff --git a/jcl-hrmorganization/secondev-jcl-hrmorganization/src/main/java/com/weaver/seconddev/jcl/organization/controller/EmployeeRelationController.java b/jcl-hrmorganization/secondev-jcl-hrmorganization/src/main/java/com/weaver/seconddev/jcl/organization/controller/EmployeeRelationController.java new file mode 100644 index 0000000..f47e535 --- /dev/null +++ b/jcl-hrmorganization/secondev-jcl-hrmorganization/src/main/java/com/weaver/seconddev/jcl/organization/controller/EmployeeRelationController.java @@ -0,0 +1,61 @@ +package com.weaver.seconddev.jcl.organization.controller; + +import com.weaver.common.authority.annotation.WeaPermission; +import com.weaver.seconddev.jcl.organization.service.EmployeeRelationService; +import org.apache.commons.collections.CollectionUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@RestController +@RequestMapping("/api/secondev/jcl/employeeRelation") +public class EmployeeRelationController { + + private static final Logger log = LoggerFactory.getLogger(EmployeeRelationController.class); + + @Autowired + private EmployeeRelationService employeeRelationService; + + /** + * 查询结果回写数据 + * @return + */ + @WeaPermission(publicPermission = true) + @Produces(MediaType.APPLICATION_JSON) + @GetMapping("/writeBackRecord") + public Map writeBackRecord( + @RequestParam("yearMonth") String yearMonth){ + log.error("writeBackRecord.yearMonth:"+yearMonth); + Map actionMap = new HashMap<>(); + try { + //查询干部绩效评定结果 + List> leaderPdInfo = employeeRelationService.queryLeaderPdByYear(yearMonth); + log.error("resultCalculAtion.leaderPdInfo:{}", leaderPdInfo); + //循环计算干部总分及等级 + if(CollectionUtils.isNotEmpty(leaderPdInfo)){ + for (Map map : leaderPdInfo) { + //将得分、等级回写到干部考核备案表 + employeeRelationService.updateLeaderDaInfo(map); + } + } + actionMap.put("code","200"); + actionMap.put("msg","接口调用成功!"); + }catch(Exception e){ + log.error("接口调用失败-e:{}",e); + actionMap.put("code","201"); + actionMap.put("msg","接口调用失败!"); + } + return actionMap; + } + +} \ No newline at end of file diff --git a/jcl-hrmorganization/secondev-jcl-hrmorganization/src/main/java/com/weaver/seconddev/jcl/organization/esb/UpdateYgddInfoAction.java b/jcl-hrmorganization/secondev-jcl-hrmorganization/src/main/java/com/weaver/seconddev/jcl/organization/esb/UpdateYgddInfoAction.java new file mode 100644 index 0000000..0db7aaf --- /dev/null +++ b/jcl-hrmorganization/secondev-jcl-hrmorganization/src/main/java/com/weaver/seconddev/jcl/organization/esb/UpdateYgddInfoAction.java @@ -0,0 +1,106 @@ +package com.weaver.seconddev.jcl.organization.esb; + +import com.weaver.common.base.entity.result.WeaResult; +import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface; +import com.weaver.seconddev.jcl.organization.util.DatabaseUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.*; + +/** + * @use:定时更新人员调动数据 + * @date 2025年3月12日 + * @author xuxy + */ +@Service("updateEmployeeTransferInfo_jcl") +public class UpdateYgddInfoAction implements EsbServerlessRpcRemoteInterface { + + @Autowired + private DatabaseUtils databaseUtils; + + private static final Logger log = LoggerFactory.getLogger(UpdateYgddInfoAction.class); + + @Override + public WeaResult> execute(Map params) { + Map rs =new HashMap<>(); + //查询员工调动信息 + List> list = queryYgddInfo(); + //遍历判断是否有当天零点的调动生效日期的人员,更新 + Map result = updateYgddInfo(list); + log.error("UpdateYgddInfoAction.flowV3:"+result); + rs.put("flowV3",result); + return WeaResult.success(rs); + } + + public List> queryYgddInfo() { + String sql=" SELECT * from uf_jcl_rydd where delete_type=0 and TENANT_KEY='t7n9jpeaoa' " ; + log.error("queryYgddInfo.sql:{}",sql); + Map rs = databaseUtils.execute("LOGIC", "weaver-ebuilder-form-service", sql); + List> recordList = databaseUtils.getDataSourceList(rs); + log.error("queryYgddInfo.recordList:{}",recordList); + return recordList; + } + + public Map updateYgddInfo(List> list) { + Map rs = new HashMap<>(); + for (Map map : list) { + String ddr = String.valueOf(map.get("ddr")); + String ddsxrq = String.valueOf(map.get("ddsxrq")); + String ddhgs = String.valueOf(map.get("ddhgs")); + String ddhbm = String.valueOf(map.get("ddhbm")); + String ddhgw = String.valueOf(map.get("ddhgw")); + String ddhzjsj = String.valueOf(map.get("ddhzjsj")); + //判断调动生效日期是否为当天 + if (isToday(ddsxrq)) { + //更新数据到员工基础信息表 + String sql = "update eteams.employee set department = '" + ddhbm + "',POSITION = '" + ddhgw + "',SUPERIOR = '" + ddhzjsj + "'" + + " where id = '" + ddr + "' and TENANT_KEY = 't7n9jpeaoa' and delete_type=0 "; + log.error("UpdateYgddInfoAction.sql:{}", sql); + rs = databaseUtils.execute("LOGIC", "weaver-ebuilder-form-service", sql); + log.error("UpdateYgddInfoAction.rs:{}", rs); + } + } + return rs; + } + + public static boolean isToday(String dateStr) { + // 定义日期格式 + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + + // 解析日期字符串 + try { + Date inputDate = dateFormat.parse(dateStr); + + // 获取当前日期 + Calendar calendar = Calendar.getInstance(); + Date today = calendar.getTime(); + + // 重置时间部分,只比较日期部分 + calendar.setTime(inputDate); + calendar.set(Calendar.HOUR_OF_DAY, 0); + calendar.set(Calendar.MINUTE, 0); + calendar.set(Calendar.SECOND, 0); + calendar.set(Calendar.MILLISECOND, 0); + Date truncatedInputDate = calendar.getTime(); + + calendar.setTime(today); + calendar.set(Calendar.HOUR_OF_DAY, 0); + calendar.set(Calendar.MINUTE, 0); + calendar.set(Calendar.SECOND, 0); + calendar.set(Calendar.MILLISECOND, 0); + Date truncatedTodayDate = calendar.getTime(); + + // 比较日期 + return truncatedInputDate.equals(truncatedTodayDate); + } catch (ParseException e) { + // 如果解析失败,返回 false + e.printStackTrace(); + return false; + } + } + +} diff --git a/jcl-hrmorganization/secondev-jcl-hrmorganization/src/main/java/com/weaver/seconddev/jcl/organization/service/EmployeeRelationService.java b/jcl-hrmorganization/secondev-jcl-hrmorganization/src/main/java/com/weaver/seconddev/jcl/organization/service/EmployeeRelationService.java new file mode 100644 index 0000000..5cee0d3 --- /dev/null +++ b/jcl-hrmorganization/secondev-jcl-hrmorganization/src/main/java/com/weaver/seconddev/jcl/organization/service/EmployeeRelationService.java @@ -0,0 +1,11 @@ +package com.weaver.seconddev.jcl.organization.service; + +import java.util.List; +import java.util.Map; + +public interface EmployeeRelationService { + + List> queryLeaderPdByYear(String yearMonth); + + void updateLeaderDaInfo(Map map); +} diff --git a/jcl-hrmorganization/secondev-jcl-hrmorganization/src/main/java/com/weaver/seconddev/jcl/organization/service/impl/EmployeeRelationServiceImpl.java b/jcl-hrmorganization/secondev-jcl-hrmorganization/src/main/java/com/weaver/seconddev/jcl/organization/service/impl/EmployeeRelationServiceImpl.java new file mode 100644 index 0000000..5027d94 --- /dev/null +++ b/jcl-hrmorganization/secondev-jcl-hrmorganization/src/main/java/com/weaver/seconddev/jcl/organization/service/impl/EmployeeRelationServiceImpl.java @@ -0,0 +1,43 @@ +package com.weaver.seconddev.jcl.organization.service.impl; + +import com.weaver.seconddev.jcl.organization.controller.EmployeeRelationController; +import com.weaver.seconddev.jcl.organization.service.EmployeeRelationService; +import com.weaver.seconddev.jcl.organization.util.DatabaseUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.List; +import java.util.Map; + +public class EmployeeRelationServiceImpl implements EmployeeRelationService { + + private static final Logger log = LoggerFactory.getLogger(EmployeeRelationController.class); + + @Autowired + private DatabaseUtils databaseUtils; + + + @Override + public List> queryLeaderPdByYear(String yearMonth) { + String sql="select a.khny,b.* from uf_gbkhpd a left join uf_gbkhpd_mxb1 b on a.id = b.form_data_id where a.khny = '" + yearMonth + "' and a.IS_DELETE = 0 and a.delete_type = 0 and a.TENANT_KEY = 'tscemvfpqg' " ; + log.error("queryLeaderPdByYear.sql:{}",sql); + Map rs = databaseUtils.execute("LOGIC", "weaver-ebuilder-form-service", sql); + List> recordList = databaseUtils.getDataSourceList(rs); + log.error("queryLeaderPdByYear.recordList:{}",recordList); + return recordList; + } + + @Override + public void updateLeaderDaInfo(Map map) { + String khny = String.valueOf(map.get("khny")); + String xm = String.valueOf(map.get("xm")); + String df = String.valueOf(map.get("df")); + String dj = String.valueOf(map.get("dj")); + log.error("updateLeaderDaInfo.map:{}",map); + String sql="update uf_jcl_gbkhba set ndzhjxkhdf = '" + df + "',ndzhjxkhdj ='" + dj + "' where nd = '" + khny.substring(0, 4) + "' and bkhr = '" + xm + "' "; + log.error("updateLeaderDaInfo.sql:{}",sql); + Map rs = databaseUtils.execute("LOGIC", "weaver-ebuilder-form-service", sql); + log.error("updateEmployeeDjInfo.rs:{}",rs); + } +} diff --git a/jcl-hrmorganization/secondev-jcl-hrmorganization/src/main/java/com/weaver/seconddev/jcl/organization/util/CommonUtils.java b/jcl-hrmorganization/secondev-jcl-hrmorganization/src/main/java/com/weaver/seconddev/jcl/organization/util/CommonUtils.java new file mode 100644 index 0000000..100a04f --- /dev/null +++ b/jcl-hrmorganization/secondev-jcl-hrmorganization/src/main/java/com/weaver/seconddev/jcl/organization/util/CommonUtils.java @@ -0,0 +1,422 @@ +package com.weaver.seconddev.jcl.organization.util; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.math.NumberUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import javax.servlet.http.HttpServletRequest; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.*; + +@Component +public class CommonUtils { + + private final static Logger log = LoggerFactory.getLogger(CommonUtils.class); + + public static JSONObject toJSON(String data){ + if(!StringUtils.isEmpty(data)){ + try { + return JSONObject.parseObject(data); + }catch (Throwable t){ + log.error(t.getMessage(),t); + } + } + return null; + } + + public static Long getJSONLong(JSONObject json,String key){ + if(json != null && json.containsKey(key)){ + try { + return json.getLong(key); + }catch (Throwable t){ + log.error(t.getMessage(),t); + } + } + return null; + } + + public static Integer getJSONInteger(JSONObject json,String key){ + if(json != null && json.containsKey(key)){ + try { + return json.getInteger(key); + }catch (Throwable t){ + log.error(t.getMessage(),t); + } + } + return -1; + } + + public static String getJSONString(JSONObject json,String key){ + if(json != null && json.containsKey(key)){ + try { + return null2String(json.getString(key)); + }catch (Throwable t){ + log.error(t.getMessage(),t); + } + } + return ""; + } + + public static JSONObject getJSONObject(JSONObject json,String key){ + if(json != null && json.containsKey(key)){ + try { + return json.getJSONObject(key); + }catch (Throwable t){ + log.error(t.getMessage(),t); + } + } + return null; + } + + public static JSONArray getJSONArray(JSONObject json, String key){ + if(json != null && json.containsKey(key)){ + try { + return json.getJSONArray(key); + }catch (Throwable t){ + log.error(t.getMessage(),t); + } + } + return null; + } + + public static JSONObject getJSONObject(JSONArray json, int idx){ + if(json != null && json.size() > idx){ + try { + return json.getJSONObject(idx); + }catch (Throwable t){ + log.error(t.getMessage(),t); + } + } + return null; + } + + public static String mapToStrData(Map para){ + if(para != null) { + return JSONObject.toJSON(para).toString(); + } + return ""; + } + + public static void strToLongList(List list,String strs){ + strToLongList(list,strs,true); + } + + public static void strToLongList(List list,String strs,boolean isDist){ + if(list == null){ + return; + } + if(StringUtils.isEmpty(strs)){ + return; + } + + String[] strList = StringUtils.split(strs, ","); + for(String str:strList){ + if(StringUtils.isEmpty(str)){ + continue; + } + + long id = getLongValue(str); + if(id > 0l && (isDist == false || !list.contains(id))){ + list.add(id); + } + } + } + + public static long getLongValue(Object v){ + return getLongValue(null2String(v)); + } + + public static long getLongValue(String v) { + return getLongValue(v, -1); + } + + public static long getLongValue(String v, long def) { + try { + return Long.parseLong(v); + } catch (Exception ex) { + return def; + } + } + + public static int getIntValue(Object o){ + return getIntValue(null2String(o)); + } + + public static int getIntValue(String s){ + return getIntValue(s,-1); + } + + public static int getIntValue(String s, int def){ + try { + return NumberUtils.toInt(s); + } catch (Exception ex) { + return def; + } + + } + + public static String null2String(Object s) { + return s == null ? "" : s.toString(); + } + + public static String null2String(Object s, String def) { + return s == null ? (def == null ? "" : def) : s.toString(); + } + + + public static String stringReplace(String sou, String s1, String s2) { + //int idx = sou.indexOf(s1); + //if (idx < 0) { + // return sou; + //} + //return sou.substring(0, idx) + s2 + StringReplace(sou.substring(idx + s1.length()), s1, s2); + sou = null2String(sou); + s1 = null2String(s1); + s2 = null2String(s2); + try{ + sou = sou.replace(s1, s2); + }catch(Exception e){ + //System.out.println(e);//将未知异常打印出来,便于检查错误。 + } + return sou; + } + + /** + * 替换特殊字符 + * + * @param s 要替换特殊的字符串 + * @return 替换完成的字符串 + */ + public static String toScreen(String s) { + char c[] = s.toCharArray(); + char ch; + int i = 0; + StringBuffer buf = new StringBuffer(); + + while (i < c.length) { + ch = c[i++]; + + if (ch == '\r') + buf.append(""); + else if (ch == '\n') + buf.append(""); + else + buf.append(ch); + } + return buf.toString(); + } + + /** + * 替换特殊字符 + * + * @param s 要替换特殊的字符串 + * @return 替换完成的字符串 + */ + public static String toExcel(String s) { + if (s == null) return ""; + //因本方法会将字符串 &符号转换,故先将欧元符号转义符转换为其Unicode码 + s = s.replaceAll("€", "\u20AC"); + String str = toScreen(s); + str = stringReplace(str, "∠", "∠"); + str = stringReplace(str, "φ", "φ"); + str = stringReplace(str, """, "\""); + str = stringReplace(str, " ", "%nbsp"); + //str=Util.StringReplace(str,"'","‘"); + str = stringReplace(str, "<", "<"); + str = stringReplace(str, ">", ">"); + str = stringReplace(str, "&dt;&at;", "
"); + str = stringReplace(str, "&", "&"); + str = stringReplace(str, "
", "&dt;&at;"); + if ("&dt;&at;".equals(str)) { + str = ""; + } + //在方法最后,又将欧元符号置换为转义符 + str = str.replaceAll("\u20AC", "€"); + return str; + } + + public static String delHtml(final String inputString) { + String htmlStr = toExcel(inputString); // 含html标签的字符串 + + String textStr = ""; + java.util.regex.Pattern p_script; + java.util.regex.Matcher m_script; + java.util.regex.Pattern p_html; + java.util.regex.Matcher m_html; + + try { + String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式 + + String regEx_script = "<[/s]*?script[^>]*?>[/s/S]*?<[/s]*?//[/s]*?script[/s]*?>"; // 定义script的正则表达式{或]*?>[/s/S]*? + + p_script = java.util.regex.Pattern.compile(regEx_script, java.util.regex.Pattern.CASE_INSENSITIVE); + m_script = p_script.matcher(htmlStr); + htmlStr = m_script.replaceAll(""); // 过滤script标签 + + p_html = java.util.regex.Pattern.compile(regEx_html, java.util.regex.Pattern.CASE_INSENSITIVE); + m_html = p_html.matcher(htmlStr); + htmlStr = m_html.replaceAll(""); // 过滤html标签 + + textStr = htmlStr; + + } catch (Exception e) { + System.err.println("Html2Text: " + e.getMessage()); + } + + return htmlToTxt(textStr).trim();// 返回文本字符串 + } + + /** + * 删除字符串中的html格式 + * + * @param input + * @return + */ + public static String htmlToTxt(String input) { + if (input == null || input.trim().equals("")) { + return ""; + } + // 去掉所有html元素, + String str = input.replaceAll("<[a-zA-Z]+[1-9]?[^><]*>", ""); + return str; + } + + + //==new= + + public static int getIntValue(Object s, int def){ + return getIntValue(null2String(s)); + } + + public static List strToLongList(String strs){ + List list = new ArrayList(); + strToLongList(list,strs,true); + return list; + } + + + public static int dateInterval(String fromdate, String todate) { + Calendar fromcalendar = getCalendar(fromdate); + Calendar tocalendar = getCalendar(todate); + + if (fromcalendar == null || tocalendar == null) + return 0; + + return (int) ((tocalendar.getTimeInMillis() - fromcalendar.getTimeInMillis()) / 3600 / 24 / 1000); + } + + /** + * @param datetime - 给定的日期时间,格式为 '2004-05-12 12:00:23' 或者 '2004-05-12' + * @return 返回给定日历, 如果格式不正确,返回null + */ + public static Calendar getCalendar(String datetime) { + int datetimelength = datetime.length() ; + + switch(datetimelength) { + case 19 : + return getCalendar(datetime , "yyyy'-'MM'-'dd' 'HH:mm:ss") ; + case 10 : + return getCalendar(datetime , "yyyy'-'MM'-'dd") ; + default : + return null ; + } + + } + + + /** + * @param datetime - 给定的日期时间 + * @param formart - 给定的日期时间的格式 + * @return 返回给定日历, 如果格式不正确,返回null + */ + public static Calendar getCalendar(String datetime, String formart) { + SimpleDateFormat SDF = new SimpleDateFormat(formart) ; + + Calendar calendar = Calendar.getInstance() ; + try { + calendar.setTime(SDF.parse(datetime)) ; + } catch (ParseException e) { + return null ; + } + + return calendar ; + } + + /** + * @return 返回当前时间字符,格式为 yyyy'-'MM'-'dd + * + * 返回当前时间字符,默认格式为yyyy'-'MM'-'dd + * + * 如 2004-09-07 + */ + public static String getCurrentDateString() { + String timestrformart = "yyyy'-'MM'-'dd" ; + SimpleDateFormat SDF = new SimpleDateFormat(timestrformart) ; + Calendar calendar = Calendar.getInstance() ; + + return SDF.format(calendar.getTime()) ; + } + + public static String getMessage(String msgMode,String key,String val){ + key = null2String(key).trim(); + val = null2String(val).trim(); + msgMode = null2String(msgMode).trim(); + if(!"".equals(msgMode) && !"".equals(key)) { + msgMode = msgMode.replaceAll(key, val); + } + return msgMode; + } + + public static List tranStrToLongList(Object idListObj){ + List docIds = new ArrayList(); + if(idListObj != null) { + try { + JSONArray idObjs = JSONArray.parseArray(null2String(idListObj)); + for (Object idObj : idObjs) { + long id = getLongValue(idObj); + if (id > 0l) { + docIds.add(id); + } + } + } catch (Exception e) { + log.error("trans error :{}", idListObj); + log.error(e.getMessage(), e); + } + } + return docIds; + } + + public static Map requestToMap(HttpServletRequest request) { + Map properties = request.getParameterMap();//把请求参数封装到Map中 + Map returnMap = new HashMap(); + Iterator> iter = properties.entrySet().iterator(); + String name = ""; + String value = ""; + while (iter.hasNext()) { + Map.Entry entry = iter.next(); + name = entry.getKey(); + Object valueObj = entry.getValue(); + if (null == valueObj) { + value = ""; + } else if (valueObj instanceof String[]) { + String[] values = (String[]) valueObj; + for (int i = 0; i < values.length; i++) { + value = values[i] + ","; + } + value = value.substring(0, value.length() - 1); + } else { + value = valueObj.toString(); + } + returnMap.put(name, value); + } + return returnMap; + } + + + +} diff --git a/jcl-hrmorganization/secondev-jcl-hrmorganization/src/main/java/com/weaver/seconddev/jcl/organization/util/DatabaseUtils.java b/jcl-hrmorganization/secondev-jcl-hrmorganization/src/main/java/com/weaver/seconddev/jcl/organization/util/DatabaseUtils.java new file mode 100644 index 0000000..071c032 --- /dev/null +++ b/jcl-hrmorganization/secondev-jcl-hrmorganization/src/main/java/com/weaver/seconddev/jcl/organization/util/DatabaseUtils.java @@ -0,0 +1,328 @@ +package com.weaver.seconddev.jcl.organization.util; + +import cn.hutool.core.codec.Base64; +import cn.hutool.core.collection.CollectionUtil; +import com.alibaba.fastjson.JSONObject; +import com.weaver.ebuilder.datasource.api.entity.ExecuteSqlEntity; +import com.weaver.ebuilder.datasource.api.entity.SqlParamEntity; +import com.weaver.ebuilder.datasource.api.enums.SourceType; +import com.weaver.ebuilder.datasource.api.enums.SqlParamType; +import com.weaver.ebuilder.datasource.api.query.dto.dw.DynamicParamDto; +import com.weaver.ebuilder.datasource.api.query.dto.dw.FieldQuery; +import com.weaver.ebuilder.datasource.api.query.dto.dw.GroupQuery; +import com.weaver.ebuilder.datasource.api.query.dto.dw.TableQuery; +import com.weaver.ebuilder.datasource.api.service.DataSetService; +import com.weaver.ebuilder.datasource.api.service.impl.EbFormDataService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.*; + +/** + * DatabaseUtil 是一个数据库工具类,提供了一些执行 SQL 查询和操作的方法。 + */ + + +@Component +public class DatabaseUtils { + + private final static Logger log = LoggerFactory.getLogger(DatabaseUtils.class); + + @Autowired + private DataSetService dataSetService; + + @Autowired + private EbFormDataService dataService; + + + /** + * 执行 SQL 并返回结果。 + * + * @param entity 包含执行 SQL 的相关信息的对象 + * @return 包含查询结果的 Map 对象 + * @throws RuntimeException 当 SQL 执行失败时抛出异常 + */ + public Map executeSql(ExecuteSqlEntity entity) { + Map map = dataSetService.executeSql(entity); + if ("FAIL".equals(com.weaver.seconddev.jcl.organization.util.CommonUtils.null2String(map.get("status")).toUpperCase(Locale.ROOT))) { + log.error("sql执行失败=>{}", JSONObject.toJSONString(map)); + throw new RuntimeException("sql执行异常"); + } else { + return map; + } + } + + /** + * 执行分页 SQL 查询并返回结果。 + * + * @param entity 包含执行 SQL 和分页信息的对象 + * @param pageNo 当前页码 + * @param pageSize 每页的数据条数 + * @return 包含查询结果的 Map 对象 + * @throws RuntimeException 当 SQL 执行失败时抛出异常 + */ + public Map executeSql(ExecuteSqlEntity entity, int pageNo, int pageSize) { + entity.setPageNo(pageNo); + entity.setPageSize(pageSize); + Map map = dataSetService.executeForQuery(entity); + if ("FAIL".equals(com.weaver.seconddev.jcl.organization.util.CommonUtils.null2String(map.get("status")).toUpperCase(Locale.ROOT))) { + log.error("sql执行失败=>{}", JSONObject.toJSONString(map)); + throw new RuntimeException("sql执行异常"); + } else { + return map; + } + } + + /** + * 创建一个包含执行 SQL 的对象。 + * + * @param sql 需要执行的 SQL + * @param groupId 数据源分组的 ID + * @paramDesc 数据源分组的 ID 获取方式 【select APPLICATION_MARK,APPLICATION_name from eteams.ds_mark_service_relation】 + * @return 包含执行 SQL 的对象 + */ + public ExecuteSqlEntity getExecuteSqlEntity(String sql, String groupId) { + log.error("sql=>{}", sql); + ExecuteSqlEntity executeSqlEntity = new ExecuteSqlEntity(); + executeSqlEntity.setSql(base64(sql)); + executeSqlEntity.setGroupId(groupId); + executeSqlEntity.setSourceType(SourceType.LOGIC); + executeSqlEntity.setGroupKey("0"); + return executeSqlEntity; + } + + /** + * 对 SQL 进行 Base64 编码。 + * + * @param sql 需要进行编码的 SQL + * @return 编码后的字符串 + */ + public String base64(String sql) { + return Base64.encode(sql); + } + + /** + * 获取数据源列表。 + * + * @param map 包含数据源列表信息的 Map 对象 + * @return 数据源列表的 Map 对象 + */ + public List> getDataSourceList(Map map) { + List> entity = new ArrayList(); + if ("OK".equals(com.weaver.seconddev.jcl.organization.util.CommonUtils.null2String(map.get("status")).toUpperCase(Locale.ROOT)) && map.get("count") != null && CommonUtils.getIntValue(map.get("count")) > 0) { + entity = (List) map.get("records"); + } + + return keyToLowerCase((List) entity); + } + + public Map getOneDataSource(Map map) { + List> entity = getDataSourceList(map); + return (Map)(CollectionUtil.isNotEmpty(entity) ? (Map)entity.get(0) : new HashMap()); + } + + /** + * 将 Map 对象中的键转换为小写。 + * + * @param orgMapList 需要转换键的 Map 对象列表 + * @return 转换后的 Map 对象列表 + */ + public List> keyToLowerCase(List> orgMapList) { + List> resultList = new ArrayList(); + Iterator var2 = orgMapList.iterator(); + + while (var2.hasNext()) { + Map stringObjectMap = (Map) var2.next(); + resultList.add(keyToLowerCase(stringObjectMap)); + } + + return resultList; + } + + /** + * 将 Map 对象中的键转换为小写。 + * + * @param orgMap 需要转换键的 Map 对象 + * @return 转换后的 Map 对象 + */ + public Map keyToLowerCase(Map orgMap) { + Map resultMap = new HashMap(); + if (orgMap != null && !orgMap.isEmpty()) { + Set> entrySet = orgMap.entrySet(); + Iterator var3 = entrySet.iterator(); + + while (var3.hasNext()) { + Map.Entry entry = (Map.Entry) var3.next(); + String key = (String) entry.getKey(); + Object value = entry.getValue(); + resultMap.put(key.toLowerCase(), value); + } + return resultMap; + } else { + return resultMap; + } + } + + + /** + * 根据数据库类型 找到对应数据库 + * + * @param sourceType sourceType 枚举类 + * ETEAMS :数据仓库 + * FORM: ebuilder表单 + * LOGIC: 各模块提供业务数据(逻辑表) + * EXTERNAL: 外部数据源 + * @return + */ + public List> getDataGroups(String sourceType, Boolean flag) { + GroupQuery query = new GroupQuery(); + query.setSourceType(SourceType.valueOf(sourceType)); + query.setShowSqlDataset(flag); + + + DynamicParamDto dynamicParamDto = new DynamicParamDto(); + dynamicParamDto.setUserId(10000L); + dynamicParamDto.setTenantKey("tk"); + + query.setDynamicParamDto(dynamicParamDto); + + return dataSetService.getDataGroups(query); + } + + + /** + * 获取数据表 + * + * @param sourceType + * @param groupId + * @param pageNum + * @param pageSize + * @return + */ + public Map getDataSets(String sourceType, String groupId, Integer pageNum, Integer pageSize) { + + TableQuery tableQuery = new TableQuery(); + tableQuery.setSourceType(SourceType.valueOf(sourceType)); + tableQuery.setGroupId(groupId); + //非必传 + //tableQuery.setName(name); + tableQuery.setPageNo(pageNum); + tableQuery.setPageSize(pageSize); + return dataSetService.getDataSetsByPage(tableQuery); + } + + /** + * 获取表字段 + * sourceType :LOGIC + * sourceId : 8494845523559165780 + * groupId : weaver-crm-service + * + * @param + * @return + */ + public List> getFields(String sourceType, String sourceId, String groupId) { + FieldQuery query = new FieldQuery(); + query.setSourceType(SourceType.valueOf(sourceType)); + query.setSourceId(sourceId); + query.setGroupId(groupId); + return dataSetService.getFields(query); + } + + /** + * 执行sql + * sourceType :LOGIC + * groupId : weaver-ebuilder-app-service + * sql : select * from ebda_app limit 10 + * + * @param + * @return + */ + public Map execute(String sourceType, String groupId, String sql) { + //执行sql 参数sourceType groupId sql + ExecuteSqlEntity executeSqlEntity = new ExecuteSqlEntity(); + executeSqlEntity.setSql(base64(sql)); + executeSqlEntity.setGroupId(groupId); + executeSqlEntity.setSourceType(SourceType.valueOf(sourceType)); + return dataSetService.executeSql(executeSqlEntity); + } + + + public Map executeForQuery(String sourceType, String groupId, String sql,List sqlparam) { + //执行sql 参数sourceType groupId sql sqlparam + ExecuteSqlEntity executeSqlEntity = new ExecuteSqlEntity(); + executeSqlEntity.setSql(base64(sql)); + executeSqlEntity.setGroupId(groupId); + executeSqlEntity.setSourceType(SourceType.valueOf(sourceType)); + executeSqlEntity.setParams(sqlparam); + return dataSetService.executeSql(executeSqlEntity); + } + + public String getMysqlPagedSql(String sql,int pageNo, int pageSize) { + if(pageNo<=0){ + pageNo = 1; + } + + if(pageSize<=0){ + pageSize = 20; + } + + int start = (pageNo-1)*pageSize; + int end = pageNo*pageSize; + + return new StringBuffer().append(sql).append( + " LIMIT "+start+","+(end-start)).toString(); + } + + /** + * 获取sql入参 + * @param list + * @return + */ + public List querySqlParamEntity(List list){ + List sqlparam = new ArrayList(); + for (String str : list){ + SqlParamEntity sqlParamEntity = new SqlParamEntity(); + sqlParamEntity.setParamType(SqlParamType.VARCHAR); + sqlParamEntity.setValue(str); + sqlparam.add(sqlParamEntity); + } + return sqlparam; + } + + /*** + * + * @param sourceType + * @param groupId + * @param dataSql + * @param paramList + * @return + */ + public List> getSqlList(String sourceType,String groupId,String dataSql,List paramList){ + List sqlParamList = querySqlParamEntity(paramList); + Map result = executeForQuery(sourceType, groupId, dataSql, sqlParamList); + List> recordList = getDataSourceList(result); + return recordList; + } + + /*** + * + * @param sourceType + * @param groupId + * @param dataSql + * @param paramList + * @return + */ + public Map getSqlMap(String sourceType,String groupId,String dataSql,List paramList){ + Map recordMap = new HashMap<>(); + List sqlParamList = querySqlParamEntity(paramList); + Map result = executeForQuery(sourceType, groupId, dataSql, sqlParamList); + List> recordList = getDataSourceList(result); + if(recordList.size() > 0){ + recordMap = recordList.get(0); + } + return recordMap; + } + +}