OperatorDataValidator.java 8.7 KB
Newer Older
Z
Zhou Rui 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
package com.x.attendance.assemble.control.processor.thread;

import java.util.Date;
import java.util.List;

import com.x.attendance.assemble.common.date.DateOperation;
import com.x.attendance.assemble.control.processor.EntityImportDataDetail;
import com.x.attendance.assemble.control.processor.ImportOptDefine;
import com.x.attendance.assemble.control.processor.monitor.StatusImportFileDetail;
import com.x.attendance.assemble.control.processor.monitor.StatusSystemImportOpt;
import com.x.attendance.assemble.control.service.UserManagerService;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;

public class OperatorDataValidator implements Runnable {

	private static  Logger logger = LoggerFactory.getLogger( OperatorDataValidator.class );
	
	private UserManagerService userManagerService = null;
	
	private EntityImportDataDetail cacheImportRowDetail = null;
	private Boolean debugger = false;
	
	public OperatorDataValidator( EntityImportDataDetail cacheImportRowDetail, Boolean debugger ) {
		userManagerService = new UserManagerService();
		this.cacheImportRowDetail = cacheImportRowDetail ;
		this.debugger = debugger;
	}
	
	@Override
	public void run() {
		 execute( cacheImportRowDetail );
	}
	
	private void execute( EntityImportDataDetail cacheImportRowDetail ) {
		Integer curRow = cacheImportRowDetail.getCurRow();
		List<String> colmlist = cacheImportRowDetail.getColmlist();
		if( colmlist == null ) {
			return;
		}
Z
zhourui 已提交
41
		if( colmlist != null && !colmlist.get(0).isEmpty() && !colmlist.get(2).isEmpty()){
Z
Zhou Rui 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
			try {
				check( cacheImportRowDetail.getFile_id(), curRow, colmlist );
			}catch( Exception e ) {
				logger.error( e );
			}
		}
	}
	
	private void check( String fileKey, Integer curRow, List<String> colmlist ) {

		StatusImportFileDetail cacheImportFileStatus = StatusSystemImportOpt.getInstance().getCacheImportFileStatus( fileKey );
		
		cacheImportFileStatus.setCurrentProcessName( ImportOptDefine.VALIDATE );
		
		cacheImportFileStatus.setProcessing_validate( true );
		
		cacheImportFileStatus.setProcessing( true );
		
Z
zhourui 已提交
60
		if( colmlist!= null && colmlist.size() > 0 ){
Z
Zhou Rui 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
			
			Boolean checkSuccess = true;
			Boolean personExists = true;
			Date datetime = null;
			DateOperation dateOperation = new DateOperation();
			
			EntityImportDataDetail cacheImportRowDetail = new EntityImportDataDetail();
			
			cacheImportRowDetail.setCurRow( curRow );
			
			if( !colmlist.get(0).isEmpty() && !colmlist.get(2).isEmpty() ){
				
				cacheImportRowDetail.setEmployeeName( colmlist.get(0).trim() );  //员工姓名
				
				if( colmlist.get(1) != null && !colmlist.get(1).trim().isEmpty()){
					cacheImportRowDetail.setEmployeeNo( colmlist.get(1).trim() );    //员工号
				}
				
				cacheImportRowDetail.setRecordDateString( colmlist.get(2) ); //打卡日期
				
				if( colmlist.size() > 3 ){
					cacheImportRowDetail.setOnDutyTime( colmlist.get(3) );    //上班打卡时间
				}
				
				if( colmlist.size() > 4 ){
					cacheImportRowDetail.setOffDutyTime( colmlist.get(4) );   //下班打卡时间
				}
				
				cacheImportRowDetail.setCheckStatus( "success" );         //设置数据检查状态为正常
				
				if( checkSuccess ) {
					//查询人员是否存在
					personExists = checkPersonExists( cacheImportFileStatus, cacheImportRowDetail.getEmployeeName() );
					if( !personExists ) {
						checkSuccess = false;
						cacheImportRowDetail.setCheckStatus("error");
						cacheImportRowDetail.setDescription( cacheImportRowDetail.getDescription() + ", 员工不存在!" );
						logger.info("step 2, data check on row "+curRow+", found an error! person '"+ cacheImportRowDetail.getEmployeeName() +"' not exists." );
					}
				}
				
				if( checkSuccess ) {
					if( cacheImportRowDetail.getEmployeeName() == null || cacheImportRowDetail.getEmployeeName().isEmpty() ) {
						checkSuccess = false;
						cacheImportRowDetail.setCheckStatus("error");
						cacheImportRowDetail.setDescription( cacheImportRowDetail.getDescription() + ", '员工标识'为空!" );
						logger.info("step 2, data check on row "+curRow+", found an error! field 'employeeName' is null." );
					}
				}
				
				if( checkSuccess ) {
					if( cacheImportRowDetail.getRecordDateString() == null || cacheImportRowDetail.getRecordDateString().isEmpty() ) {
						checkSuccess = false;
						cacheImportRowDetail.setCheckStatus("error");
						cacheImportRowDetail.setDescription( cacheImportRowDetail.getDescription() + ", '打卡日期'为空!" );
						logger.info("step 2, data check on row "+curRow+", found an error! field 'recordDateString' is null." );
					}
				}
				
				if( checkSuccess ) {
					//下面检查三个日期和时间格式是否正常,并且进行转换
					try{
						datetime = dateOperation.getDateFromString( cacheImportRowDetail.getRecordDateString() );
						cacheImportRowDetail.setRecordDate( datetime );
						cacheImportRowDetail.setRecordDateStringFormated( dateOperation.getDateStringFromDate( datetime, "YYYY-MM-DD") ); //打卡日期
						cacheImportRowDetail.setRecordYearString( dateOperation.getYear( datetime ) );
						cacheImportRowDetail.setRecordMonthString( dateOperation.getMonth(datetime) );
						if( Integer.parseInt( cacheImportRowDetail.getRecordYearString() ) > dateOperation.getYearNumber( new Date() )
								|| Integer.parseInt( cacheImportRowDetail.getRecordYearString() ) < 2000 
						) {
							throw new Exception("record date error:" + cacheImportRowDetail.getRecordDateString() );
						}
						//设置一下该文件中所有数据的日期范围
						cacheImportFileStatus.sendStartTime(datetime);
						cacheImportFileStatus.sendEndTime( datetime );
					}catch( Exception e ){
						checkSuccess = false;
						cacheImportRowDetail.setCheckStatus("error");
						cacheImportRowDetail.setDescription( cacheImportRowDetail.getDescription() + "打卡日期格式异常:" + cacheImportRowDetail.getRecordDateString() );
						logger.info("step 2, data check on row "+curRow+", found an error! format on field 'recordDate':" + cacheImportRowDetail.getRecordDateString(), e);
					}
				}
				
				if( checkSuccess ) {
					if( cacheImportRowDetail.getOnDutyTime() != null && cacheImportRowDetail.getOnDutyTime().trim().length() > 0 ){
						try{
							datetime = dateOperation.getDateFromString( cacheImportRowDetail.getOnDutyTime() );
							cacheImportRowDetail.setOnDutyTimeFormated( dateOperation.getDateStringFromDate( datetime, "HH:mm:ss") ); //上班打卡时间
						}catch( Exception e ){
							checkSuccess = false;
							cacheImportRowDetail.setCheckStatus("error");
							cacheImportRowDetail.setDescription( cacheImportRowDetail.getDescription() + "上班打卡时间格式异常:" + cacheImportRowDetail.getOnDutyTime() );
							logger.info("step 2, data check on row "+curRow+", found an error!format on field 'onDutyTime':" + cacheImportRowDetail.getOnDutyTime(), e);
						}
					}
				}
				
				if( checkSuccess ) {
					if( cacheImportRowDetail.getOffDutyTime() != null && cacheImportRowDetail.getOffDutyTime().trim().length() > 0 ){
						try{
							datetime = dateOperation.getDateFromString( cacheImportRowDetail.getOffDutyTime() );
							cacheImportRowDetail.setOffDutyTimeFormated( dateOperation.getDateStringFromDate( datetime, "HH:mm:ss") ); //下班打卡时间
						}catch( Exception e ){
							checkSuccess = false;
							cacheImportRowDetail.setCheckStatus("error");
							cacheImportRowDetail.setDescription( cacheImportRowDetail.getDescription() + "下班打卡时间格式异常:" + cacheImportRowDetail.getOffDutyTime() );
							logger.info("step 2, data check on row "+curRow+", found an error!format on field 'offDutyTime':" + cacheImportRowDetail.getOffDutyTime(), e);
						}
					}
				}
				
				if( !checkSuccess ){
					cacheImportFileStatus.setCheckStatus( "error" );
					cacheImportFileStatus.setCheckStatus( "error" );
					cacheImportFileStatus.increaseErrorCount( 1 );
					logger.debug( debugger, ">>>>>>>>>>record check error:" + cacheImportRowDetail.getDescription() );
					cacheImportFileStatus.addErrorList( cacheImportRowDetail );
				}
				
				cacheImportFileStatus.addDetailList( cacheImportRowDetail );
				cacheImportFileStatus.increaseProcess_validate_count( 1 );
			}
		}
	}

	private Boolean checkPersonExists( StatusImportFileDetail cacheImportFileStatus, String personName ) {
		if( !cacheImportFileStatus.getPersonList().isEmpty() && cacheImportFileStatus.getPersonList().contains( personName ) ) {
			//如果已经缓存了,那么肯定存在的
			return true;
		}else {
			//检查人员是否存在
			try{
				personName = userManagerService.checkPersonExists( personName );
				if( personName == null || personName.isEmpty() ) {
					return false;
				}else {
					cacheImportFileStatus.addPersonList( personName );
					return true;
				}
			}catch(Exception e) {
				return false;
			}
		}
	}

	
}