@ -3,22 +3,29 @@ package com.epmet.model;
import com.alibaba.excel.context.AnalysisContext ;
import com.alibaba.excel.event.AnalysisEventListener ;
import com.epmet.commons.tools.constant.NumConstant ;
import com.epmet.commons.tools.constant.StrConstant ;
import com.epmet.commons.tools.enums.OrgTypeEnum ;
import com.epmet.commons.tools.exception.EpmetException ;
import com.epmet.commons.tools.exception.ExceptionUtils ;
import com.epmet.commons.tools.exception.ValidateException ;
import com.epmet.commons.tools.utils.ConvertUtils ;
import com.epmet.commons.tools.utils.PidUtils ;
import com.epmet.commons.tools.validator.ValidatorUtils ;
import com.epmet.dto.CustomerAgencyDTO ;
import com.epmet.dto.CustomerGridDTO ;
import com.epmet.entity.IcDangerousChemicalsEntity ;
import com.epmet.service.CoverageService ;
import com.epmet.service.CustomerAgencyService ;
import com.epmet.service.CustomerGridService ;
import com.epmet.service.IcDangerousChemicalsService ;
import lombok.Data ;
import lombok.extern.slf4j.Slf4j ;
import org.apache.commons.collections4.CollectionUtils ;
import org.apache.commons.lang3.StringUtils ;
import java.util.ArrayList ;
import java.util.List ;
import java.util.Map ;
import java.util.stream.Collectors ;
/ * *
* @Author zxc
@ -31,6 +38,7 @@ public class DangerousChemicalsImportListener extends AnalysisEventListener<Dang
private IcDangerousChemicalsService icDangerousChemicalsService ;
private CoverageService coverageService ;
private CustomerAgencyService customerAgencyService ;
private CustomerGridService customerGridService ;
private String currentUserId ;
private String customerId ;
@ -39,11 +47,11 @@ public class DangerousChemicalsImportListener extends AnalysisEventListener<Dang
private List < DangerousChemicalsModel . RowRemarkMessage > errorRows = new ArrayList < > ( ) ;
private List < DangerousChemicalsModel . RowRemarkMessage > otherRows = new ArrayList < > ( ) ;
private List < IcDangerousChemicalsEntity > insertList = new ArrayList < > ( ) ;
private Map < String , String > existMap = null ;
/ * *
* 危化品种类
* /
private Map < String , String > dangerTypeMap ;
private List < IcDangerousChemicalsEntity > updateList = new ArrayList < > ( ) ;
// /**
// * 危化品种类
// */
// private Map<String, String> dangerTypeMap;
/ * *
* 企业类别
* /
@ -60,6 +68,7 @@ public class DangerousChemicalsImportListener extends AnalysisEventListener<Dang
String staffAgencyPath ,
IcDangerousChemicalsService icDangerousChemicalsService ,
CoverageService coverageService ,
CustomerAgencyService customerAgencyService ,
CustomerGridService customerGridService ,
Map < String , String > dangerTypeMap ,
Map < String , String > categoryDictMap ) {
@ -69,8 +78,9 @@ public class DangerousChemicalsImportListener extends AnalysisEventListener<Dang
this . staffAgencyPath = staffAgencyPath ;
this . icDangerousChemicalsService = icDangerousChemicalsService ;
this . coverageService = coverageService ;
this . customerAgencyService = customerAgencyService ;
this . customerGridService = customerGridService ;
this . dangerTypeMap = dangerTypeMap ;
// this.dangerTypeMap=dangerTypeMap;
this . categoryDictMap = categoryDictMap ;
}
@ -80,24 +90,72 @@ public class DangerousChemicalsImportListener extends AnalysisEventListener<Dang
data . setRowNum ( rowNum ) ;
try {
ValidatorUtils . validateEntity ( data ) ;
//todo
// 所属组织,orgIdPath等等。。。。
IcDangerousChemicalsEntity e = ConvertUtils . sourceToTarget ( data , IcDangerousChemicalsEntity . class ) ;
//验证所属组织填写的是否正确
CustomerAgencyDTO customerAgencyDTO = customerAgencyService . getByAgencyName ( customerId , data . getAgencyName ( ) ) ;
if ( null = = customerAgencyDTO ) {
throw new ValidateException ( String . format ( "当前客户下,不存在【%s】" , data . getAgencyName ( ) ) ) ;
}
//验证所属网格
if ( StringUtils . isNotBlank ( data . getGridName ( ) ) ) {
CustomerGridDTO gridFormDto = new CustomerGridDTO ( ) ;
gridFormDto . setCustomerId ( customerId ) ;
gridFormDto . setPid ( customerAgencyDTO . getId ( ) ) ;
gridFormDto . setGridName ( data . getGridName ( ) ) ;
CustomerGridDTO gridDTO = customerGridService . get ( gridFormDto ) ;
if ( null = = gridDTO ) {
throw new ValidateException ( String . format ( "%s下,不存在【%s】" , data . getAgencyName ( ) , data . getGridName ( ) ) ) ;
}
// 企业属于网格
e . setOrgId ( gridDTO . getId ( ) ) ;
e . setOrgType ( OrgTypeEnum . GRID . getCode ( ) ) ;
e . setOrgIdPath ( PidUtils . convertPid2OrgIdPath ( gridDTO . getId ( ) , gridDTO . getPids ( ) ) ) ;
} else {
// 企业属于组织
e . setOrgId ( customerAgencyDTO . getId ( ) ) ;
e . setOrgType ( OrgTypeEnum . AGENCY . getCode ( ) ) ;
e . setOrgIdPath ( PidUtils . convertPid2OrgIdPath ( customerAgencyDTO . getId ( ) , customerAgencyDTO . getPids ( ) ) ) ;
}
// 验证企业类别
String categoryKey = StrConstant . EPMETY_STR ;
for ( Map . Entry < String , String > entry : categoryDictMap . entrySet ( ) ) {
if ( data . getCategoryName ( ) . equals ( entry . getValue ( ) ) ) {
categoryKey = entry . getKey ( ) ;
break ;
}
}
if ( StringUtils . isBlank ( categoryKey ) ) {
throw new ValidateException ( "【" + data . getCategoryName ( ) + "】此企业类别不存在" ) ;
}
e . setCategory ( categoryKey ) ;
e . setSourceType ( "import" ) ;
e . setAgencyId ( staffAgencyId ) ;
e . setCustomerId ( customerId ) ;
e . setAgencyId ( staffAgencyId ) ;
e . setAgencyIdPath ( staffAgencyPath ) ;
e . setCreatedBy ( currentUserId ) ;
e . setUpdatedBy ( currentUserId ) ;
insertList . add ( e ) ;
if ( insertList . size ( ) = = NumConstant . ONE_HUNDRED ) {
e . setSecurityFlag ( "是" . equals ( data . getSecurityFlagName ( ) ) ? true : false ) ;
// 判断是否存在
IcDangerousChemicalsEntity origin = icDangerousChemicalsService . get ( e . getOrgId ( ) , e . getName ( ) ) ;
if ( null ! = origin ) {
e . setId ( origin . getId ( ) ) ;
updateList . add ( origin ) ;
} else {
insertList . add ( e ) ;
}
if ( insertList . size ( ) = = NumConstant . ONE_HUNDRED ) {
execPersist ( ) ;
}
if ( updateList . size ( ) = = NumConstant . ONE_HUNDRED ) {
execPersist ( ) ;
}
} catch ( Exception e ) {
String errorMsg = null ;
if ( e instanceof ValidateException ) {
errorMsg = ( ( ValidateException ) e ) . getMsg ( ) ;
} else {
} else if ( e instanceof EpmetException ) {
errorMsg = ( ( EpmetException ) e ) . getMsg ( ) ;
} else {
errorMsg = "未知错误" ;
log . error ( "【重点危化品企业导入】出错:{}" , ExceptionUtils . getErrorStackTrace ( e ) ) ;
}
@ -118,43 +176,15 @@ public class DangerousChemicalsImportListener extends AnalysisEventListener<Dang
private void execPersist ( ) {
try {
if ( CollectionUtils . isNotEmpty ( insertList ) ) {
insertList . forEach ( i - > {
dangerTypeMap . forEach ( ( k , v ) - > {
if ( i . getDangerTypeName ( ) . equals ( v ) ) {
i . setDangerType ( k ) ;
}
} ) ;
categoryDictMap . forEach ( ( k , v ) - > {
if ( i . getCategoryName ( ) . equals ( v ) ) {
i . setCategory ( k ) ;
}
} ) ;
} ) ;
Map < String , Long > groupByName = insertList . stream ( ) . collect ( Collectors . groupingBy ( IcDangerousChemicalsEntity : : getName , Collectors . counting ( ) ) ) ;
groupByName . forEach ( ( k , v ) - > {
if ( v . compareTo ( NumConstant . ONE_L ) ! = NumConstant . ZERO ) {
insertList . forEach ( i - > {
if ( i . getName ( ) . equals ( k ) ) {
i . setExistStatus ( true ) ;
}
} ) ;
}
} ) ;
Map < Boolean , List < IcDangerousChemicalsEntity > > groupByStatus = insertList . stream ( ) . collect ( Collectors . groupingBy ( IcDangerousChemicalsEntity : : getExistStatus ) ) ;
if ( CollectionUtils . isNotEmpty ( groupByStatus . get ( true ) ) ) {
groupByStatus . get ( true ) . forEach ( t - > {
DangerousChemicalsModel . RowRemarkMessage errorRow = ConvertUtils . sourceToTarget ( t , DangerousChemicalsModel . RowRemarkMessage . class ) ;
errorRow . setErrorInfo ( "表格中存在重复数据" ) ;
errorRows . add ( errorRow ) ;
} ) ;
}
if ( CollectionUtils . isNotEmpty ( groupByStatus . get ( false ) ) ) {
icDangerousChemicalsService . importInsert ( groupByStatus . get ( false ) ) ;
}
icDangerousChemicalsService . insertBatch ( insertList ) ;
}
if ( CollectionUtils . isNotEmpty ( updateList ) ) {
icDangerousChemicalsService . updateBatchById ( updateList ) ;
}
} finally {
insertList . clear ( ) ;
existMap = null ;
updateList . clear ( ) ;
}
}