package com.weaver.seconddev.util ;
import com.alibaba.fastjson.JSONArray ;
import com.alibaba.fastjson.JSONObject ;
import com.weaver.common.hrm.util.Util ;
import com.weaver.common.i18n.tool.util.I18nContextUtil ;
import com.weaver.ebuilder.datasource.api.entity.ExecuteSqlEntity ;
import com.weaver.ebuilder.datasource.api.enums.SourceType ;
import com.weaver.ebuilder.datasource.api.service.DataSetService ;
import com.weaver.framework.rpc.context.impl.TenantRpcContext ;
import com.weaver.seconddev.entity.DataOptions ;
import com.weaver.seconddev.entity.FormDataDuty ;
import com.weaver.verupgrade.conn.RecordSet ;
import lombok.extern.slf4j.Slf4j ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.stereotype.Component ;
import javax.sql.DataSource ;
import java.util.ArrayList ;
import java.util.List ;
import java.util.Map ;
/ * *
* @Author : calyrex
* @CreateTime : 2025 - 04 - 25
* @Description : 数 据 操 作 工 具
* /
@Component
@Slf4j
public class DataOperateUtil {
@Autowired
private DataSetService dataSetService ;
public List < Long > getDetailFormId ( String workflowId ) {
log . error ( "getDetailFormId start : " + workflowId ) ;
List < Long > formIdList = new ArrayList ( ) ;
String sourceType = String . valueOf ( SourceType . LOGIC ) ;
String groupId = "weaver-basic-schedule-service" ;
try {
String sql = "select form_id from ecology10.dbo.form_table where form_id in (select id from ecology10.dbo.sub_form where form_id in (select relatekey from ecology10.dbo.wfp_relateform where workflowid = '" + workflowId + "'))" ;
log . info ( "getConfig sql-->" + sql ) ;
Map < String , Object > datas = executeForQuery ( sourceType , groupId , sql ) ;
// log.info("getConfig datas-->" + datas);
if ( String . valueOf ( datas . get ( "status" ) ) . equals ( "OK" ) ) {
List < Map < String , Object > > records = ( List < Map < String , Object > > ) datas . get ( "records" ) ;
for ( int i = 0 ; i < records . size ( ) ; i + + ) {
Map < String , Object > map = records . get ( i ) ;
String formId = map . get ( "form_id" ) . toString ( ) ;
formIdList . add ( Long . valueOf ( formId ) ) ;
}
return formIdList ;
} else {
log . error ( "getConfig status-->" + datas . get ( "status" ) ) ;
return formIdList ;
}
} catch ( Exception e ) {
log . error ( "getConfig e--> " , e ) ;
return formIdList ;
}
}
public String getConfig ( String key , String tenantKey ) {
String sourceType = String . valueOf ( SourceType . LOGIC ) ;
String groupId = "weaver-basic-schedule-service" ;
try {
String sql = "select configvalue from ecology10.dbo.uf_config where configkey = '" + key + "' and zhkey = '" + tenantKey + "'" ;
log . info ( "getConfig sql-->" + sql ) ;
Map < String , Object > datas = executeForQuery ( sourceType , groupId , sql ) ;
// log.info("getConfig datas-->" + datas);
if ( String . valueOf ( datas . get ( "status" ) ) . equals ( "OK" ) ) {
List < Map < String , Object > > records = ( List < Map < String , Object > > ) datas . get ( "records" ) ;
Map < String , Object > map = records . get ( 0 ) ;
return map . get ( "config_value" ) . toString ( ) ;
} else {
log . error ( "getConfig status-->" + datas . get ( "status" ) ) ;
return "" ;
}
} catch ( Exception e ) {
log . error ( "getConfig e--> " , e ) ;
return "" ;
}
}
public Map < String , Object > executeForQuery ( String sourceType , String groupId , String sql ) {
// log.info("executeForQuery sourceType-->" + sourceType + ",groupId-->" + groupId + ",sql-->" + sql);
TenantRpcContext . setTargetTenantKey ( "temkc46eme" ) ;
ExecuteSqlEntity executeSqlEntity = new ExecuteSqlEntity ( ) ;
executeSqlEntity . setSql ( cn . hutool . core . codec . Base64 . encode ( sql ) ) ;
executeSqlEntity . setGroupId ( groupId ) ; //groupid,可以访问 E10地址/api/datasource/ds/group?sourceType=LOGIC 获取
executeSqlEntity . setSourceType ( SourceType . valueOf ( sourceType ) ) ;
// log.info("executeForQuery executeSqlEntity-->"+executeSqlEntity.getSql()+"-->"+executeSqlEntity.getGroupId()+"-->"+executeSqlEntity.getSourceType());
Map < String , Object > datas = dataSetService . executeSql ( executeSqlEntity ) ;
// log.info("executeForQuery datas-->" + datas);
TenantRpcContext . removeTargetTenantKey ( ) ;
return datas ;
}
/ * *
* 表 单 字 段 构 建
* @return
* /
public static JSONObject getFromDataByDataDetails ( List < FormDataDuty > formDataDutyList ) {
JSONObject formData = new JSONObject ( ) ;
formData . put ( "module" , "workflow" ) ;
JSONArray dataDetails = new JSONArray ( ) ;
formData . put ( "dataDetails" , dataDetails ) ;
formDataDutyList . forEach ( formDataDuty - > {
if ( formDataDuty . getSubFormId ( ) ! = null ) {
//明细表
if ( formDataDuty . getDataOptions ( ) = = null ) {
//普通字段
JSONObject inputDataKey = new JSONObject ( ) ;
inputDataKey . put ( "dataKey" , formDataDuty . getDataKey ( ) ) ;
inputDataKey . put ( "dataIndex" , formDataDuty . getDataIndex ( ) ) ;
inputDataKey . put ( "content" , formDataDuty . getContent ( ) ) ;
inputDataKey . put ( "subFormId" , formDataDuty . getSubFormId ( ) ) ;
dataDetails . add ( inputDataKey ) ;
} else {
JSONObject details = new JSONObject ( ) ;
dataDetails . add ( details ) ;
JSONArray dataOptions = new JSONArray ( ) ;
details . put ( "dataKey" , formDataDuty . getDataKey ( ) ) ;
details . put ( "dataIndex" , formDataDuty . getDataIndex ( ) ) ;
details . put ( "subFormId" , formDataDuty . getSubFormId ( ) ) ;
details . put ( "dataOptions" , dataOptions ) ;
List < DataOptions > dataOptions1 = formDataDuty . getDataOptions ( ) ;
dataOptions1 . forEach ( e - > {
JSONObject option = new JSONObject ( ) ;
option . put ( "optionId" , e . getOptionId ( ) ) ;
dataOptions . add ( option ) ;
} ) ;
}
} else {
//主表
if ( formDataDuty . getDataOptions ( ) = = null ) {
//普通字段
JSONObject inputDataKey = new JSONObject ( ) ;
inputDataKey . put ( "dataKey" , formDataDuty . getDataKey ( ) ) ;
inputDataKey . put ( "content" , formDataDuty . getContent ( ) ) ;
dataDetails . add ( inputDataKey ) ;
} else {
//浏览按钮字段
// 浏览按钮
JSONObject details = new JSONObject ( ) ;
dataDetails . add ( details ) ;
JSONArray dataOptions = new JSONArray ( ) ;
details . put ( "dataKey" , formDataDuty . getDataKey ( ) ) ;
details . put ( "dataOptions" , dataOptions ) ;
List < DataOptions > dataOptions1 = formDataDuty . getDataOptions ( ) ;
dataOptions1 . forEach ( e - > {
JSONObject option = new JSONObject ( ) ;
option . put ( "optionId" , e . getOptionId ( ) ) ;
//fieldType 3为附件
if ( formDataDuty . getFieldType ( ) = = 3 ) {
option . put ( "content" , e . getContent ( ) ) ;
}
dataOptions . add ( option ) ;
} ) ;
}
}
} ) ;
return formData ;
}
public static String null2String ( String s ) {
return s = = null ? "" : s ;
}
public static String null2String ( Object o ) {
return o = = null ? "" : o . toString ( ) ;
}
public static String null2String ( String s1 , String s2 ) {
return s1 = = null ? ( s2 = = null ? "" : s2 ) : s1 ;
}
}