OperationLogServiceImpl.java 19.8 KB
Newer Older
eddie.woo's avatar
eddie.woo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
package pwc.taxtech.atms.service.impl;

import org.apache.ibatis.session.RowBounds;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import pwc.taxtech.atms.common.AuthUserHelper;
import pwc.taxtech.atms.common.CommonConstants;
import pwc.taxtech.atms.common.CommonUtils;
import pwc.taxtech.atms.common.OperateLogType;
import pwc.taxtech.atms.common.OperationAction;
import pwc.taxtech.atms.common.OperationModule;
import pwc.taxtech.atms.constant.EnterpriseAccountConstant;
import pwc.taxtech.atms.constant.OrganizationConstant;
import pwc.taxtech.atms.constant.UserConstant;
import pwc.taxtech.atms.dao.OperationLogBasicDataMapper;
22
import pwc.taxtech.atms.dpo.PagingDto;
eddie.woo's avatar
eddie.woo committed
23 24 25 26
import pwc.taxtech.atms.dto.OperationLogDto;
import pwc.taxtech.atms.dto.PagingResultDto;
import pwc.taxtech.atms.dto.QueryOperateParamDto;
import pwc.taxtech.atms.dto.UpdateLogParams;
27 28
import pwc.taxtech.atms.entity.*;
import pwc.taxtech.atms.entity.OperationLogBasicDataExample.Criteria;
29
import pwc.taxtech.atms.exception.ApplicationException;
eddie.woo's avatar
eddie.woo committed
30 31
import pwc.taxtech.atms.service.OperationLogService;

32 33 34 35 36 37
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

eddie.woo's avatar
eddie.woo committed
38 39 40 41
/**
 */
@Service
public class OperationLogServiceImpl extends AbstractService implements OperationLogService {
42 43 44 45 46 47 48 49 50 51 52 53
	private static final Logger logger = LoggerFactory.getLogger(OperationLogServiceImpl.class);
	@Autowired
	private OperationLogBasicDataMapper operationLogBasicDataMapper;

	@Autowired
	private AuthUserHelper authUserHelper;

	@Override
	public void addOperationLog(OperationLogDto operationLogDto) {
		OperationLogSmart record = new OperationLogSmart();
		Integer logtype = operationLogDto.getLogType();
		Assert.notNull(logtype, "Null logType");
54
		String table = OperateLogType.getTableName(logtype);
55 56 57
		Assert.hasText(table, "Empty tableName");
		record.setTableName(table);

58 59
		record.setId(CommonUtils.getUUID());
		record.setIp(authUserHelper.getClientIp());
60
		record.setOperationUser(authUserHelper.getCurrentAuditor().get());
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
		record.setCreateTime(new Date());

		record.setOperationContent(operationLogDto.getOperationContent());
		Assert.notNull(operationLogDto.getModule(), "Null module");
		String operationModuleString = OperationModule.getName(operationLogDto.getModule());
		Assert.hasText(operationModuleString, "Wrong int value for OperationModule: " + operationLogDto.getModule());
		record.setModuleName(operationModuleString);
		record.setOperationObject(operationLogDto.getOperationObject());
		record.setOperationAction(OperationAction.getName(operationLogDto.getAction()));
		record.setOriginalState(operationLogDto.getOriginalState());
		record.setUpdateState(operationLogDto.getUpdateState());
		record.setComment(operationLogDto.getComment());
		if (Strings.isNotBlank(record.getOperationObject()) && record.getOperationObject().length() > 100) {
			record.setOperationObject(record.getOperationObject().substring(0, 100));
		}
		if (Strings.isNotBlank(record.getOperationContent()) && record.getOperationContent().length() > 1000) {
			record.setOperationContent(record.getOperationContent().substring(0, 1000));
		}
		if (Strings.isNotBlank(record.getOperationAction()) && record.getOperationAction().length() > 100) {
			record.setOperationAction(record.getOperationAction().substring(0, 100));
		}
		if (Strings.isNotBlank(record.getModuleName()) && record.getModuleName().length() > 1000) {
			record.setModuleName(record.getModuleName().substring(0, 100));
		}
		if (Strings.isNotBlank(record.getOperationUser()) && record.getOperationUser().length() > 100) {
			record.setOperationUser(record.getOperationUser().substring(0, 100));
		}
		operationLogBasicDataMapper.insertSmart(record);
	}

	@Override
	public void addOperationLogList(List<OperationLogDto> operationLogDtoList) {
		logger.debug("OperationLogService addOperationLogList");
		if (!operationLogDtoList.isEmpty()) {
			for (OperationLogDto operationLogDto : operationLogDtoList) {
				addOperationLog(operationLogDto);
			}
		}
	}

	@Override
	public PagingResultDto<OperationLogDto> getOperationLogList(QueryOperateParamDto queryOperateParamDto) {
		logger.debug("OperationLogService getOperationLogList");
		// validate
		Assert.hasText(queryOperateParamDto.getModuleName(), "Empty moduleName");

		final PagingDto pageInfo = queryOperateParamDto.getPageInfo();

		final PagingResultDto<OperationLogDto> pagingResultDto = new PagingResultDto<>();
		pagingResultDto.setList(new ArrayList<>());
		// build the smart example
		final OperationLogSmartExample example = buildExample(queryOperateParamDto);

		// 查找总记录数
		final int totalCount = (int) operationLogBasicDataMapper.countByExampleSmart(example);

		final PagingDto outputPageInfo = new PagingDto();
		outputPageInfo.setPageIndex(pageInfo.getPageIndex());
		outputPageInfo.setPageSize(pageInfo.getPageSize());
		outputPageInfo.setTotalCount(totalCount);
		pagingResultDto.setPageInfo(outputPageInfo);

		if (totalCount > 0) {
			final RowBounds rowBounds = buildRowBounds(pageInfo);
			List<OperationLogBasicData> operationLogBasicDataList = operationLogBasicDataMapper
					.selectByExampleWithRowboundsSmart(example, rowBounds);
			Assert.notNull(operationLogBasicDataList, "Null list");
			List<OperationLogDto> operationLogDtoList = new ArrayList<>();
			for (OperationLogBasicData operationLogBasicData : operationLogBasicDataList) {
				OperationLogDto operationLogDto = toOperationLogDto(operationLogBasicData);
				operationLogDtoList.add(operationLogDto);
			}
			pagingResultDto.setList(operationLogDtoList);
		}

		return pagingResultDto;
	}

	private OperationLogDto toOperationLogDto(OperationLogBasicData operationLogBasicData) {
		OperationLogDto operationLogDto = new OperationLogDto();
141
		operationLogDto.setId(operationLogBasicData.getId());
142 143 144 145 146 147 148
		operationLogDto.setCreateTime(operationLogBasicData.getCreateTime());
		operationLogDto.setOperationObject(operationLogBasicData.getOperationObject());
		operationLogDto.setOperationContent(operationLogBasicData.getOperationContent());
		operationLogDto.setOriginalState(operationLogBasicData.getOriginalState());
		operationLogDto.setUpdateState(operationLogBasicData.getUpdateState());
		operationLogDto.setOperationUser(operationLogBasicData.getOperationUser());
		operationLogDto.setComment(operationLogBasicData.getComment());
149
		operationLogDto.setIp(operationLogBasicData.getIp());
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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
		// 参考C#代码,logtype为null,所以这里填零
		operationLogDto.setLogType(0);
		try {
			OperationAction actionEnum = OperationAction.valueOf(operationLogBasicData.getOperationAction());
			if (actionEnum != null) {
				operationLogDto.setAction(actionEnum.value());
			}
		} catch (Exception e) {
			logger.warn("Data error: not found the OperationAction by database value:"
					+ operationLogBasicData.getOperationAction());
			operationLogDto.setAction(0);
		}
		try {
			OperationModule moduleEnum = OperationModule.valueOf(operationLogBasicData.getModuleName());
			if (moduleEnum != null) {
				operationLogDto.setModule(moduleEnum.value());
			}
		} catch (Exception e) {
			logger.warn("Data error: not found the OperationModule by database value:"
					+ operationLogBasicData.getModuleName());
			// not found the OperationModule
			operationLogDto.setModule(0);
		}
		return operationLogDto;
	}

	private OperationLogSmartExample buildExample(QueryOperateParamDto queryOperateParamDto) {
		OperationLogSmartExample example = new OperationLogSmartExample();
		Integer logtype = queryOperateParamDto.getLogType();
		Assert.notNull(logtype, "Null logType");
		String tableName = OperateLogType.getName(logtype);
		example.setTableName(tableName);
		example.setOrderByClause("CreateTime desc");
		String searchText = queryOperateParamDto.getSearchText();
		if (StringUtils.hasText(searchText)) {
			String sqlValue = "%" + searchText + "%";
			Criteria criteria1 = example.createCriteria();
			criteria1.andModuleNameEqualTo(queryOperateParamDto.getModuleName());
			criteria1.andCommentLike(sqlValue);
			if (!Lang.isEmpty(queryOperateParamDto.getFilterOperationObjectList())) {
				criteria1.andOperationObjectIn(queryOperateParamDto.getFilterOperationObjectList());
			}

			Criteria criteria2 = example.createCriteria();
			criteria2.andModuleNameEqualTo(queryOperateParamDto.getModuleName());
			criteria2.andOperationObjectLike(sqlValue);
			example.or(criteria2);
			if (!Lang.isEmpty(queryOperateParamDto.getFilterOperationObjectList())) {
				criteria2.andOperationObjectIn(queryOperateParamDto.getFilterOperationObjectList());
			}

			Criteria criteria3 = example.createCriteria();
			criteria3.andModuleNameEqualTo(queryOperateParamDto.getModuleName());
			criteria3.andOperationUserLike(sqlValue);
			example.or(criteria3);
			if (!Lang.isEmpty(queryOperateParamDto.getFilterOperationObjectList())) {
				criteria3.andOperationObjectIn(queryOperateParamDto.getFilterOperationObjectList());
			}

			Criteria criteria4 = example.createCriteria();
			criteria4.andModuleNameEqualTo(queryOperateParamDto.getModuleName());
			criteria4.andUpdateStateLike(sqlValue);
			example.or(criteria4);
			if (!Lang.isEmpty(queryOperateParamDto.getFilterOperationObjectList())) {
				criteria4.andOperationObjectIn(queryOperateParamDto.getFilterOperationObjectList());
			}

			Criteria criteria5 = example.createCriteria();
			criteria5.andModuleNameEqualTo(queryOperateParamDto.getModuleName());
			criteria5.andOriginalStateLike(sqlValue);
			example.or(criteria5);
			if (!Lang.isEmpty(queryOperateParamDto.getFilterOperationObjectList())) {
				criteria5.andOperationObjectIn(queryOperateParamDto.getFilterOperationObjectList());
			}
		} else {
			Criteria criteria = example.createCriteria();
			criteria.andModuleNameEqualTo(queryOperateParamDto.getModuleName());
			if (!Lang.isEmpty(queryOperateParamDto.getFilterOperationObjectList())) {
				criteria.andOperationObjectIn(queryOperateParamDto.getFilterOperationObjectList());
			}
		}

		// @see below C# codes:
		// o.Comment.Contains(paras.SearchText) ||
		// o.OperationObject.Contains(paras.SearchText) ||
		// o.OperationUser.Contains(paras.SearchText) ||
		// o.UpdateState.Contains(paras.SearchText) ||
		// o.OriginalState.Contains(paras.SearchText))
		return example;
	}

	private RowBounds buildRowBounds(PagingDto pageInfo) {
		Assert.notNull(pageInfo, "Null pageInfo");
		Assert.notNull(pageInfo.getPageIndex(), "Null pageInfo.pageIndex");
		Assert.notNull(pageInfo.getPageSize(), "Null pageInfo.pagesize");
		int offset = Math.max(0, pageInfo.getPageSize() * (pageInfo.getPageIndex() - 1));
		int limit = pageInfo.getPageSize();
		return new RowBounds(offset, limit);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * pwc.taxtech.atms.service.OperationLogService#updateDataAddLog(java.util.List)
	 */
	@Override
	public void updateDataAddLog(List<UpdateLogParams> updateLogParamsList) {

		for (UpdateLogParams updateLogParams : updateLogParamsList) {
			updateDataAddLog(updateLogParams);
		}

	}

	// check how many properties being updated and the log number should be same
	@Override
	public void updateDataAddLog(UpdateLogParams updateLogParams) {
		logger.debug("OperationLogService updateDataAddLog, parameter: {}", updateLogParams);
		if (updateLogParams != null && updateLogParams.getOriginalState() != null
				&& updateLogParams.getUpdateState() != null) {
			Object originObject = updateLogParams.getOriginalState();
			Object updateObject = updateLogParams.getUpdateState();
			// logger.debug("origin object: {}", JSON.toJSONString(originObject, true));
			// logger.debug("update object: {}", JSON.toJSONString(updateObject, true));
			Class<?> cls = originObject.getClass();
			Field[] fileds = cls.getDeclaredFields();
			if (fileds != null && fileds.length > 0) {
				for (Field field : fileds) {
					field.setAccessible(true);
280
					// skip create time, update time and serialVersionUId
281 282
					if (CommonConstants.CREATETIME.equalsIgnoreCase(field.getName())
							|| CommonConstants.UPDATETIME.equalsIgnoreCase(field.getName())
283
							|| CommonConstants.SERIALVERSIONUId.equalsIgnoreCase(field.getName())) {
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
						continue;
					}

					try {
						Object originValue = field.get(originObject);
						String originalState = getValue(field.getName(), originValue, cls);
						Object updateValue = field.get(updateObject);
						String updateState = getValue(field.getName(), updateValue, cls);

						if (!Objects.equals(originalState, updateState)) {
							logger.debug("value is updated for field: {}, origin value: {}, updated value: {}",
									field.getName(), originValue, updateValue);
							OperationLogDto operationLogDto = new OperationLogDto();
							operationLogDto.setOperationObject(updateLogParams.getOperationObject());
							operationLogDto.setOperationContent(Strings.upperFirst(field.getName()));
							operationLogDto.setOriginalState(originalState);
							operationLogDto.setUpdateState(updateState);
							operationLogDto.setModule(updateLogParams.getOperationModule());
							operationLogDto.setComment(updateLogParams.getComment());
							operationLogDto.setLogType(updateLogParams.getOperateLogType());
							operationLogDto.setAction(updateLogParams.getOperationAction());
							addOperationLog(operationLogDto);
						}
					} catch (IllegalArgumentException | IllegalAccessException e) {
						throw new ApplicationException(
								"exception happened during comparing object properties, Object name: " + cls.getName()
										+ " exception: " + e.getMessage());
					}
				}
			}
		}
	}

	private String getValue(String fieldName, Object fieldValue, Class objClass) {
		String value = fieldValue == null ? "" : toDotNetStyleString(fieldValue);
		try {
			if (objClass.equals(EnterpriseAccount.class)) {
				if (EnterpriseAccountConstant.DirectionField.equalsIgnoreCase(fieldName)) {
					value = EnterpriseAccountConstant.DirectionMap.get(value);
					return value;
				}
				if (EnterpriseAccountConstant.IgnoreFieldList.stream().anyMatch(s -> fieldName.equalsIgnoreCase(s))) {
					return "";
				}
				if (EnterpriseAccountConstant.AcctPropField.equalsIgnoreCase(fieldName)) {
					int temp = Integer.parseInt(value);
					if (temp < EnterpriseAccountConstant.acctPropLogList.size()) {
						return EnterpriseAccountConstant.acctPropLogList.get(temp - 1);
					}
				}
			} else if (objClass.equals(Organization.class)) {
				return getOrganizationSpecialName(fieldName, value);
			} else if (objClass.equals(User.class)) {
				return getUserSpecialName(fieldName, value);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return value;
	}

	private String getOrganizationSpecialName(String fieldName, String value) {
		if (OrganizationConstant.IgnoreFieldList.stream().anyMatch(sa -> fieldName.equalsIgnoreCase(sa))) {
			return null;
		}
349
		if (OrganizationConstant.ParentIdField.equalsIgnoreCase(fieldName)) {
350 351 352 353
			Organization temp = organizationMapper.selectByPrimaryKey(value);
			if (temp != null) {
				return temp.getName();
			}
354
		} else if (OrganizationConstant.RegionIdField.equalsIgnoreCase(fieldName)) {
355 356 357 358
			Region temp = regionMapper.selectByPrimaryKey(value);
			if (temp != null) {
				return temp.getName();
			}
359
		} else if (OrganizationConstant.StructureIdField.equalsIgnoreCase(fieldName)) {
360 361 362 363
			OrganizationStructure temp = organizationStructureMapper.selectByPrimaryKey(value);
			if (temp != null) {
				return temp.getName();
			}
364
		} else if (OrganizationConstant.BusinessUnitIdField.equalsIgnoreCase(fieldName)) {
365 366 367 368
			BusinessUnit temp = businessUnitMapper.selectByPrimaryKey(value);
			if (temp != null) {
				return temp.getName();
			}
369
		} else if (OrganizationConstant.IndustryIdField.equalsIgnoreCase(fieldName)) {
370 371 372 373
			Industry temp = industryMapper.selectByPrimaryKey(value);
			if (temp != null) {
				return temp.getName();
			}
374
		} else if (OrganizationConstant.AreaIdField.equalsIgnoreCase(fieldName)) {
375 376 377 378 379 380 381 382 383
			Area temp = areaMapper.selectByPrimaryKey(value);
			if (temp != null) {
				return temp.getName();
			}
		}
		return value;
	}

	private String getUserSpecialName(String fieldName, String value) {
384
		if (UserConstant.OrganizationIdField.equalsIgnoreCase(fieldName)) {
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
			Organization temp = organizationMapper.selectByPrimaryKey(value);
			if (temp != null) {
				return temp.getName();
			}
		} else if (UserConstant.IsAdminField.equalsIgnoreCase(fieldName)) {
		}
		return value;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * pwc.taxtech.atms.service.OperationLogService#addOrDeleteDataAddLog(java.util.
	 * List)
	 */
	@Override
	public void addOrDeleteDataAddLog(List<UpdateLogParams> updateLogParamsList) {

		for (UpdateLogParams updateLogParams : updateLogParamsList) {
			addOrDeleteDataAddLog(updateLogParams);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see pwc.taxtech.atms.service.OperationLogService#addOrDeleteDataAddLog(pwc.
	 * taxtech.atms.service.dto.UpdateLogParams)
	 */
	@Override
	public void addOrDeleteDataAddLog(UpdateLogParams updateLogParams) {

		logger.debug("OperationLogService addOrDeleteDataAddLog, parameter: {}", updateLogParams);

		OperationLogDto operationLogDto = new OperationLogDto();
		CommonUtils.copyProperties(updateLogParams, operationLogDto);
		operationLogDto.setModule(updateLogParams.getOperationModule());
		operationLogDto.setLogType(updateLogParams.getOperateLogType());
		operationLogDto.setAction(updateLogParams.getOperationAction());
		addOperationLog(operationLogDto);
	}

	private String toDotNetStyleString(Object inputObject) {
		if (inputObject == null) {
			return "";
		}
		boolean upcaseFirstFlag = false;
		if (inputObject instanceof Boolean) {
			upcaseFirstFlag = true;
		}
		String result = inputObject.toString();
		return upcaseFirstFlag ? Strings.upperFirst(result) : result;
	}

	@Override
441 442
	public void addDataAddLog(Object newObj, OperationModule operationModule, String userName, String operationDesc,
							  String operationContent, String OperationObject, OperateLogType logType) {
443 444
		OperationLogDto addLog = new OperationLogDto();

445
		addLog.setId(CommonUtils.getUUID());
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
		addLog.setOperationContent(operationContent);
		addLog.setOperationObject(OperationObject);
		addLog.setOriginalState("");
		addLog.setUpdateState("");
		addLog.setOperationUser(userName);
		addLog.setIp(authUserHelper.getClientIp());
		addLog.setModule(operationModule.value());
		addLog.setComment(operationDesc);
		addLog.setAction(OperationAction.New.value());
		addLog.setLogType(logType.value());

		this.addOperationLog(addLog);
	}

	@Override
461 462
	public void updateDataAddLog(Object originalObj, Object updateObj, OperationModule operationModule, String userName,
								 String comment, String operationObject, String operationContent, OperateLogType logType) {
463 464 465 466 467 468 469 470 471 472 473 474 475 476
		UpdateLogParams params = new UpdateLogParams();
		params.setOriginalState(originalObj);
		params.setOperationContent(operationContent);
		params.setOperationObject(operationObject);
		params.setUpdateState(updateObj);
		params.setOperationModule(operationModule.value());
		params.setOperationUser(userName);
		params.setComment(comment);
		params.setOperationAction(OperationAction.Update.value());
		params.setOperateLogType(logType.value());
		this.updateDataAddLog(params);
	}

	@Override
477 478
	public void deleteDataAddLog(Object deleteObj, OperationModule operationModule, String userName, String comment,
								 String operationContent, String OperationObject, OperateLogType logType) {
479
		OperationLogDto addLog = new OperationLogDto();
480
		addLog.setId(CommonUtils.getUUID());
481 482 483 484 485 486 487 488 489 490 491 492
		addLog.setOperationContent(operationContent);
		addLog.setOperationObject(OperationObject);
		addLog.setOriginalState("");
		addLog.setUpdateState("");
		addLog.setOperationUser(userName);
		addLog.setIp(authUserHelper.getClientIp());
		addLog.setModule(operationModule.value());
		addLog.setComment(comment);
		addLog.setAction(OperationAction.New.value());
		addLog.setLogType(logType.value());
		this.addOperationLog(addLog);
	}
eddie.woo's avatar
eddie.woo committed
493 494

}