2024-08-26 09:19:12 +08:00
package com.yem.wm.task ;
import com.yem.ensign.common.DateUtils ;
import com.yem.wm.utils.RequestCmmp ;
import kd.bos.context.RequestContext ;
import kd.bos.dataentity.OperateOption ;
import kd.bos.dataentity.entity.DynamicObject ;
import kd.bos.dataentity.entity.DynamicObjectCollection ;
import kd.bos.dataentity.resource.ResManager ;
import kd.bos.entity.EntityMetadataCache ;
import kd.bos.entity.MainEntityType ;
import kd.bos.entity.operate.result.IOperateInfo ;
import kd.bos.entity.operate.result.OperationResult ;
import kd.bos.exception.KDBizException ;
import kd.bos.exception.KDException ;
import kd.bos.id.ID ;
import kd.bos.orm.query.QCP ;
import kd.bos.orm.query.QFilter ;
import kd.bos.schedule.executor.AbstractTask ;
import kd.bos.servicehelper.BusinessDataServiceHelper ;
import kd.bos.servicehelper.QueryServiceHelper ;
import kd.bos.servicehelper.operation.OperationServiceHelper ;
2024-09-10 19:00:49 +08:00
import kd.bos.servicehelper.operation.SaveServiceHelper ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2024-08-26 09:19:12 +08:00
import java.util.ArrayList ;
import java.util.Date ;
import java.util.List ;
import java.util.Map ;
/ * *
* 库位
* /
public class SynWhLocationCMMPTask extends AbstractTask {
2024-09-10 19:00:49 +08:00
private final static Logger log = LoggerFactory . getLogger ( SynWhLocationCMMPTask . class ) ;
2024-08-26 09:19:12 +08:00
@Override
public void execute ( RequestContext requestContext , Map < String , Object > map ) throws KDException {
//查询CMMP链接
DynamicObjectCollection connObjs = QueryServiceHelper . query ( " yem_cmmpconfig " , " id,yem_belong " , new QFilter [ ] { new QFilter ( " yem_num " , QCP . in , new String [ ] { " ZZJ " , " CC " , " NZ " } ) } ) ;
for ( DynamicObject connObj : connObjs ) {
long id = connObj . getLong ( " id " ) ;
// StringBuilder sql = new StringBuilder("SELECT area_code,position_code,position_name from VIEW_POSITION_WM where 1 = 1");//测试
StringBuilder sql = new StringBuilder ( " SELECT area_code,position_code,position_name from VIEW_BASE_POSITION_DMS where 1 = 1 " ) ; //正式
String isInit = ( String ) map . get ( " isInit " ) ;
if ( ! " true " . equals ( isInit ) ) {
Date dateed = new Date ( ) ;
dateed = DateUtils . getBeginTime ( dateed ) ;
Date datebg = DateUtils . getAddDay ( dateed , - 2 ) ;
dateed = DateUtils . getEndTime ( dateed ) ;
sql . append ( " and plmlastupdate>=to_date(' " + DateUtils . formatString ( datebg ) + " ','yyyy-mm-dd') " + " and plmlastupdate<=to_date(' " + DateUtils . formatString ( dateed ) + " ','yyyy-mm-dd') " ) ;
}
sql . append ( " order by plmlastupdate " ) ;
//执行sql
List < Map > fromCmmpWithConnId = RequestCmmp . getFromCmmpWithConnId ( id , sql . toString ( ) ) ;
for ( Map map1 : fromCmmpWithConnId ) {
//先查询是否存在
ArrayList < QFilter > qFilters = new ArrayList < > ( ) ;
qFilters . add ( new QFilter ( " number " , QCP . equals , map1 . get ( " position_code " ) ) ) ;
2024-09-10 19:00:49 +08:00
// qFilters.add(new QFilter("group.number", QCP.equals, map1.get("area_code")));
// qFilters.add(new QFilter("group.yem_belong", QCP.equals, connObj.getString("yem_belong")));
2024-08-26 09:19:12 +08:00
qFilters . add ( new QFilter ( " yem_belong " , QCP . equals , connObj . getString ( " yem_belong " ) ) ) ;
2024-09-10 19:00:49 +08:00
DynamicObject [ ] whs = BusinessDataServiceHelper . load ( " yem_whlocation " , " id, number, group.number, group.yem_belong " , qFilters . toArray ( new QFilter [ 0 ] ) ) ;
if ( whs . length > 0 ) {
for ( DynamicObject wh : whs ) {
DynamicObject group = wh . getDynamicObject ( " group " ) ;
group = BusinessDataServiceHelper . loadSingle ( group . getPkValue ( ) , group . getDataEntityType ( ) . getName ( ) ) ;
if ( group ! = null ) {
String group_number = group . getString ( " number " ) ;
String group_belong = group . getString ( " yem_belong " ) ;
if ( group_number . equals ( map1 . get ( " area_code " ) ) & & group_belong . equals ( connObj . getString ( " yem_belong " ) ) ) {
log . info ( " 已存在库区数据,执行更新:{} {} " , map1 . get ( " position_code " ) , map1 . get ( " position_name " ) ) ;
//如果存在
wh = BusinessDataServiceHelper . loadSingle ( wh . getString ( " id " ) , " yem_whlocation " ) ;
createWL ( map1 , wh , connObj ) ;
OperationResult operate = OperationServiceHelper . executeOperate ( " save " , " yem_whlocation " , new DynamicObject [ ] { wh } , OperateOption . create ( ) ) ;
String message = operate . getMessage ( ) ;
log . info ( message ) ;
}
}
}
2024-08-26 09:19:12 +08:00
} else {
MainEntityType entityType = EntityMetadataCache . getDataEntityType ( " yem_whlocation " ) ;
DynamicObject wh = new DynamicObject ( entityType ) ;
wh . getDataEntityType ( ) . getPrimaryKey ( ) . setValueFast ( wh , ID . genLongId ( ) ) ;
2024-09-10 19:00:49 +08:00
log . info ( " 新建库区数据:{} {} " , map1 . get ( " position_code " ) , map1 . get ( " position_name " ) ) ;
2024-08-26 09:19:12 +08:00
createWL ( map1 , wh , connObj ) ;
if ( wh . get ( " group " ) = = null ) {
continue ;
}
OperationResult operationResult = OperationServiceHelper . executeOperate ( " save " , " yem_whlocation " , new DynamicObject [ ] { wh } , OperateOption . create ( ) ) ;
2024-09-10 19:00:49 +08:00
log . info ( operationResult . getMessage ( ) ) ;
2024-08-26 09:19:12 +08:00
if ( ! operationResult . isSuccess ( ) ) {
for ( IOperateInfo errInfo1 : operationResult . getAllErrorOrValidateInfo ( ) ) {
String detailMessage1 = errInfo1 . getMessage ( ) ;
throw new KDBizException ( ResManager . loadKDString ( " 生成时保存失败: " , " EtcmanualList_2 " , " createEcatalog_res " ) + detailMessage1 ) ;
}
}
}
}
}
}
private static void createWL ( Map map1 , DynamicObject wh , DynamicObject connObj ) {
wh . set ( " number " , map1 . get ( " position_code " ) . toString ( ) ) ; //仓库编码
wh . set ( " name " , map1 . get ( " position_name " ) . toString ( ) ) ; //仓库名称
wh . set ( " status " , " C " ) ; //数据状态
wh . set ( " enable " , 1 ) ; //是否启用
wh . set ( " yem_belong " , connObj . getString ( " yem_belong " ) ) ; //仓库所属
//库区获取
DynamicObject waObj = BusinessDataServiceHelper . loadSingle ( " yem_warearea " ,
new QFilter [ ] { new QFilter ( " number " , QCP . equals , map1 . get ( " area_code " ) ) ,
new QFilter ( " yem_belong " , QCP . equals , connObj . getString ( " yem_belong " ) ) } ) ;
wh . set ( " group " , waObj ) ;
}
}