定时计算年龄、工龄、司龄及获取字段id

main
Administrator 3 weeks ago
parent 3783ad2dc7
commit cfea58d150

@ -4,6 +4,7 @@ import com.weaver.common.authority.annotation.WeaPermission;
import com.weaver.ebuilder.form.client.entity.field.ModuleField;
import com.weaver.seconddev.jcl.organization.service.EmployeeRelationService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -126,4 +127,31 @@ public class EmployeeRelationController {
return actionMap;
}
/**
* -fieldId
* @return
*/
@GetMapping("/getFormField")
@WeaPermission(publicPermission = true)
public Map<String, Object> getFormField(
@RequestParam("formId") String formId,
@RequestParam("fieldId") String fieldId,
@RequestParam("title") String title){
log.error("getFormField.formId:{},fieldId:{},title:{}", formId,fieldId,title);
Map<String, Object> actionMap = new HashMap<>();
// 根据表名查询所有字段
String formField = employeeRelationService.getFormField(formId, fieldId, title);
if(StringUtils.isNotBlank(formField)){
log.error("getFormField.formField:{}", formField);
actionMap.put("code","200");
actionMap.put("msg","接口调用成功!");
}else{
actionMap.put("code","201");
actionMap.put("msg","返回数据为空!");
}
actionMap.put("formField",formField);
log.error("getFormField.actionMap:{}", actionMap);
return actionMap;
}
}

@ -0,0 +1,111 @@
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.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @use
* @date 2025327
* @author xuxy
*/
@Service("synAllEmployeeInfo_jcl")
public class SynAllEmployeeInfoAction implements EsbServerlessRpcRemoteInterface {
@Autowired
private DatabaseUtils databaseUtils;
private static final Logger log = LoggerFactory.getLogger(SynAllEmployeeInfoAction.class);
@Override
public WeaResult<Map<String, Object>> execute(Map<String, Object> params) {
Map<String, Object> rs =new HashMap<>();
//查询员工全量信息
List<Map<String, Object>> list = queryEmployeesInfo();
//遍历更新日期不为空的人员:年龄、工龄、司龄
Map<String, Object> result = updateEmployeesInfo(list);
log.error("SynEmployeeAgeGlSlInfoAction.flowV3:"+result);
rs.put("flowV3",result);
return WeaResult.success(rs);
}
public List<Map<String, Object>> queryEmployeesInfo() {
String sql=" SELECT id,birthDate,first_work_date,hiredate,age,work_year,sl from uf_jcl_employee_information where delete_type=0 and TENANT_KEY='t7n9jpeaoa' " ;
log.error("queryEmployeesInfo.sql:{}",sql);
Map<String, Object> rs = databaseUtils.execute("LOGIC", "weaver-ebuilder-form-service", sql);
List<Map<String, Object>> recordList = databaseUtils.getDataSourceList(rs);
log.error("queryEmployeesInfo.recordList:{}",recordList);
return recordList;
}
public Map<String, Object> updateEmployeesInfo(List<Map<String, Object>> list) {
Map<String, Object> rs = new HashMap<>();
//判断调动生效日期是否为当天或者当天之前
LocalDate currentDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
for (Map<String, Object> employee : list) {
String birthDateString = (String) employee.get("birthDate");
String firstWorkDateString = (String) employee.get("first_work_date");
String joinDateString = (String) employee.get("hiredate");
log.error("updateEmployeesInfo.birthDateString:{},firstWorkDateString:{},joinDateString:{}",birthDateString,firstWorkDateString,joinDateString);
if (birthDateString != null && !birthDateString.isEmpty()) {
LocalDate birthDate = LocalDate.parse(birthDateString, formatter);
int age = calculateFullYears(birthDate, currentDate);
employee.put("age", age);
} else {
employee.put("age", null); // 或者可以选择不放入 Map 中
}
if (firstWorkDateString != null && !firstWorkDateString.isEmpty()) {
LocalDate firstWorkDate = LocalDate.parse(firstWorkDateString, formatter);
Double workYears = calculateYears(firstWorkDate, currentDate);
employee.put("work_year", workYears);
} else {
employee.put("work_year", null); // 或者可以选择不放入 Map 中
}
if (joinDateString != null && !joinDateString.isEmpty()) {
LocalDate joinDate = LocalDate.parse(joinDateString, formatter);
Double companyYears = calculateYears(joinDate, currentDate);
employee.put("sl", companyYears);
} else {
employee.put("sl", null); // 或者可以选择不放入 Map 中
}
}
//再全量更新
for (Map<String, Object> objectMap : list) {
String id = (String) objectMap.get("id");
String birthDateString = (String) objectMap.get("age");
String firstWorkDateString = (String) objectMap.get("work_year");
String joinDateString = (String) objectMap.get("sl");
String sql = "update uf_jcl_employee_information set age = '" + birthDateString + "',work_year = '" + firstWorkDateString + "',sl = '" + joinDateString + "'" +
" where id = '" + id + "' and TENANT_KEY = 't7n9jpeaoa' and delete_type=0 ";
log.error("updateEmployeesInfo.sql:{}", sql);
rs = databaseUtils.execute("LOGIC", "weaver-ebuilder-form-service", sql);
log.error("updateEmployeesInfo.rs:{}", rs);
}
return rs;
}
private int calculateFullYears(LocalDate startDate, LocalDate endDate) {
return Period.between(startDate, endDate).getYears();
}
private Double calculateYears(LocalDate startDate, LocalDate endDate) {
Period period = Period.between(startDate, endDate);
double years = period.getYears() + (period.getMonths() / 12.0) + (period.getDays() / 365.0);
return Math.round(years * 100.0) / 100.0; // 保留两位小数
}
}

@ -0,0 +1,108 @@
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.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.*;
/**
* @use
* @date 2025326
* @author xuxy
*/
@Service("updateEmployeeAgeGlSlInfo_jcl")
public class SynEmployeeAgeGlSlInfoAction implements EsbServerlessRpcRemoteInterface {
@Autowired
private DatabaseUtils databaseUtils;
private static final Logger log = LoggerFactory.getLogger(SynEmployeeAgeGlSlInfoAction.class);
@Override
public WeaResult<Map<String, Object>> execute(Map<String, Object> params) {
Map<String, Object> rs =new HashMap<>();
//查询员工全量信息
List<Map<String, Object>> list = queryEmployeesInfo();
//遍历更新日期不为空的人员:年龄、工龄、司龄
Map<String, Object> result = updateEmployeesInfo(list);
log.error("SynEmployeeAgeGlSlInfoAction.flowV3:"+result);
rs.put("flowV3",result);
return WeaResult.success(rs);
}
public List<Map<String, Object>> queryEmployeesInfo() {
String sql=" SELECT id,birthDate,first_work_date,hiredate,age,work_year,sl from uf_jcl_employee_information where delete_type=0 and TENANT_KEY='t7n9jpeaoa' " ;
log.error("queryEmployeesInfo.sql:{}",sql);
Map<String, Object> rs = databaseUtils.execute("LOGIC", "weaver-ebuilder-form-service", sql);
List<Map<String, Object>> recordList = databaseUtils.getDataSourceList(rs);
log.error("queryEmployeesInfo.recordList:{}",recordList);
return recordList;
}
public Map<String, Object> updateEmployeesInfo(List<Map<String, Object>> list) {
Map<String, Object> rs = new HashMap<>();
//判断调动生效日期是否为当天或者当天之前
LocalDate currentDate = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
for (Map<String, Object> employee : list) {
String birthDateString = (String) employee.get("birthDate");
String firstWorkDateString = (String) employee.get("first_work_date");
String joinDateString = (String) employee.get("hiredate");
log.error("updateEmployeesInfo.birthDateString:{},firstWorkDateString:{},joinDateString:{}",birthDateString,firstWorkDateString,joinDateString);
if (birthDateString != null && !birthDateString.isEmpty()) {
LocalDate birthDate = LocalDate.parse(birthDateString, formatter);
int age = calculateFullYears(birthDate, currentDate);
employee.put("age", age);
} else {
employee.put("age", null); // 或者可以选择不放入 Map 中
}
if (firstWorkDateString != null && !firstWorkDateString.isEmpty()) {
LocalDate firstWorkDate = LocalDate.parse(firstWorkDateString, formatter);
Double workYears = calculateYears(firstWorkDate, currentDate);
employee.put("work_year", workYears);
} else {
employee.put("work_year", null); // 或者可以选择不放入 Map 中
}
if (joinDateString != null && !joinDateString.isEmpty()) {
LocalDate joinDate = LocalDate.parse(joinDateString, formatter);
Double companyYears = calculateYears(joinDate, currentDate);
employee.put("sl", companyYears);
} else {
employee.put("sl", null); // 或者可以选择不放入 Map 中
}
}
//再全量更新
for (Map<String, Object> objectMap : list) {
String id = (String) objectMap.get("id");
String birthDateString = (String) objectMap.get("age");
String firstWorkDateString = (String) objectMap.get("work_year");
String joinDateString = (String) objectMap.get("sl");
String sql = "update uf_jcl_employee_information set age = '" + birthDateString + "',work_year = '" + firstWorkDateString + "',sl = '" + joinDateString + "'" +
" where id = '" + id + "' and TENANT_KEY = 't7n9jpeaoa' and delete_type=0 ";
log.error("updateEmployeesInfo.sql:{}", sql);
rs = databaseUtils.execute("LOGIC", "weaver-ebuilder-form-service", sql);
log.error("updateEmployeesInfo.rs:{}", rs);
}
return rs;
}
private int calculateFullYears(LocalDate startDate, LocalDate endDate) {
return Period.between(startDate, endDate).getYears();
}
private Double calculateYears(LocalDate startDate, LocalDate endDate) {
Period period = Period.between(startDate, endDate);
double years = period.getYears() + (period.getMonths() / 12.0) + (period.getDays() / 365.0);
return Math.round(years * 100.0) / 100.0; // 保留两位小数
}
}

@ -14,4 +14,6 @@ public interface EmployeeRelationService {
List<Map<String, Object>> getTableColumnsOfTableName(String tableSchema,String tableName);
List<ModuleField> getTableColumns(String tableName);
String getFormField(String formId,String fieldId,String title);
}

@ -75,7 +75,7 @@ public class EmployeeRelationServiceImpl implements EmployeeRelationService {
@Override
public List<Map<String, Object>> getTableColumnsOfTableName(String tableSchema,String tableName) {
String sql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS\n" +
String sql = "SELECT COLUMN_NAME,DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS\n" +
"WHERE TABLE_SCHEMA = '" + tableSchema + "' AND TABLE_NAME = '" + tableName + "' ";
log.error("getTableColumnsOfTableName.sql:{}", sql);
Map<String, Object> rs = databaseUtils.execute("LOGIC", "weaver-ebuilder-form-service", sql);
@ -85,7 +85,10 @@ public class EmployeeRelationServiceImpl implements EmployeeRelationService {
}
@Override
public List<ModuleField> getTableColumns(String tableName) {
//saveFormFieldNew
//todo (500报错)
//// SQL语句向表A添加一个新字段
// String sql = "ALTER TABLE A ADD COLUMN new_column_name VARCHAR(255)"; // 替换为你的字段名和类型
Long aLong = Long.valueOf(tableName);
FieldsQueryDto fieldsQueryDto = new FieldsQueryDto();
fieldsQueryDto.setObjId(aLong);
@ -98,6 +101,31 @@ public class EmployeeRelationServiceImpl implements EmployeeRelationService {
return fields;
}
@Override
public String getFormField(String formId, String fieldId, String title) {
//1.先根据id查询title
String sql = "select title from form_field where id = '" + fieldId + "' ";
log.error("getFormField.sql:{}", sql);
Map<String, Object> rs = databaseUtils.execute("LOGIC", "weaver-ebuilder-form-service", sql);
List<Map<String, Object>> recordList = databaseUtils.getDataSourceList(rs);
log.error("getFormField.recordList:{}", recordList);
String titleNew = "";
if(CollectionUtils.isNotEmpty(recordList)){
titleNew = String.valueOf(recordList.get(0));
}
//2.根据titleNew和formId查询id
String sql2 = "select id from form_field where form_id = '" + fieldId + "' and title = '" + titleNew + "'";
log.error("getFormField.sql2:{}", sql2);
Map<String, Object> rs2 = databaseUtils.execute("LOGIC", "weaver-ebuilder-form-service", sql2);
List<Map<String, Object>> recordList2 = databaseUtils.getDataSourceList(rs2);
log.error("getFormField.recordList2:{}", recordList2);
String fieldIdNew = "";
if(CollectionUtils.isNotEmpty(recordList2)){
fieldIdNew = String.valueOf(recordList2.get(0));
}
return fieldIdNew;
}
private void GetTableAlterStatements() {
//1.获取连接池实例

Loading…
Cancel
Save