浏览代码

Merge branch 'feature/cms增加添加文档的阅读记录接口' into 'wrdp'

[内容管理]增加管理员添加文档的阅读记录接口

See merge request o2oa/o2oa!2859
o2null 5 年之前
父节点
当前提交
6f44d696a0

+ 90 - 0
o2server/x_cms_assemble_control/src/main/java/com/x/cms/assemble/control/jaxrs/document/ActionPersistViewRecord.java

@@ -0,0 +1,90 @@
+package com.x.cms.assemble.control.jaxrs.document;
+
+import com.google.gson.JsonElement;
+import com.x.base.core.project.annotation.FieldDescribe;
+import com.x.base.core.project.annotation.FieldTypeDescribe;
+import com.x.base.core.project.gson.GsonPropertyObject;
+import com.x.base.core.project.http.ActionResult;
+import com.x.base.core.project.http.EffectivePerson;
+import com.x.base.core.project.jaxrs.WrapBoolean;
+import com.x.base.core.project.logger.Logger;
+import com.x.base.core.project.logger.LoggerFactory;
+import com.x.base.core.project.organization.Person;
+import com.x.base.core.project.tools.DateTools;
+import com.x.base.core.project.tools.ListTools;
+import com.x.cms.core.entity.Document;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.List;
+
+public class ActionPersistViewRecord extends BaseAction {
+
+	private static  Logger logger = LoggerFactory.getLogger(ActionPersistViewRecord.class);
+
+	protected ActionResult<Wo> execute(HttpServletRequest request, String id, JsonElement jsonElement, EffectivePerson effectivePerson) throws Exception {
+		ActionResult<Wo> result = new ActionResult<>();
+
+		Document document  = documentQueryService.get(id);
+		if (null == document) {
+			throw new ExceptionDocumentNotExists(id);
+		}
+
+		Wi wi = this.convertToWrapIn(jsonElement, Wi.class );
+		if(ListTools.isNotEmpty(wi.getRecordList())){
+			for (ViewRecordWi viewRecordWi : wi.getRecordList()){
+				Person person = this.userManagerService.getPerson(viewRecordWi.getPerson());
+				if (person != null) {
+					documentViewRecordServiceAdv.addViewRecord(id, person.getDistinguishedName(), DateTools.parse(viewRecordWi.getViewTime()));
+				}
+			}
+		}
+		Wo wo = new Wo();
+		wo.setValue(true);
+
+		return result;
+	}
+
+	public static class Wo extends WrapBoolean {
+
+	}
+
+	public static class Wi extends GsonPropertyObject {
+		@FieldDescribe("阅读记录列表")
+		@FieldTypeDescribe(fieldType = "class", fieldTypeName = "Module", fieldValue = "{\"person\": \"阅读人员\", \"viewTime\": \"阅读时间(格式:2020-08-08)\"}")
+		private List<ViewRecordWi> recordList;
+
+		public List<ViewRecordWi> getRecordList() {
+			return recordList;
+		}
+
+		public void setRecordList(List<ViewRecordWi> recordList) {
+			this.recordList = recordList;
+		}
+	}
+
+	public static class ViewRecordWi extends GsonPropertyObject {
+
+		@FieldDescribe("阅读人员")
+		private String person;
+		@FieldDescribe("阅读时间(格式:2020-08-08)")
+		private String viewTime;
+
+		public String getPerson() {
+			return person;
+		}
+
+		public void setPerson(String person) {
+			this.person = person;
+		}
+
+		public String getViewTime() {
+			return viewTime;
+		}
+
+		public void setViewTime(String viewTime) {
+			this.viewTime = viewTime;
+		}
+	}
+
+
+}

+ 23 - 3
o2server/x_cms_assemble_control/src/main/java/com/x/cms/assemble/control/jaxrs/document/DocumentCipherAction.java

@@ -24,9 +24,9 @@ import java.util.List;
 @Path("document/cipher")
 @JaxrsDescribe("信息发布信息文档管理(Cipher)")
 public class DocumentCipherAction extends StandardJaxrsAction{
-	
+
 	private static  Logger logger = LoggerFactory.getLogger( DocumentCipherAction.class );
-	
+
 	@JaxrsMethodDescribe(value = "直接发布文档信息.", action = ActionPersistPublishByWorkFlow.class)
 	@PUT
 	@Path("publish/content")
@@ -96,4 +96,24 @@ public class DocumentCipherAction extends StandardJaxrsAction{
 		}
 		asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));
 	}
-}
+
+	@JaxrsMethodDescribe(value = "添加文档的阅读记录.", action = ActionPersistViewRecord.class)
+	@POST
+	@Path("{id}/persist/view/record")
+	@Produces(HttpMediaType.APPLICATION_JSON_UTF_8)
+	@Consumes(MediaType.APPLICATION_JSON)
+	public void persist_documentViewRecord( @Suspended final AsyncResponse asyncResponse, @Context HttpServletRequest request,
+											@JaxrsParameterDescribe("文档ID") @PathParam("id") String id, JsonElement jsonElement ) {
+		EffectivePerson effectivePerson = this.effectivePerson( request );
+		ActionResult<ActionPersistViewRecord.Wo> result = new ActionResult<>();
+
+		try {
+			result = new ActionPersistViewRecord().execute( request, id, jsonElement, effectivePerson );
+		} catch (Exception e) {
+			result.error( e );
+			logger.error( e, effectivePerson, request, null);
+		}
+
+		asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));
+	}
+}

+ 95 - 29
o2server/x_cms_assemble_control/src/main/java/com/x/cms/assemble/control/service/DocumentViewRecordServiceAdv.java

@@ -21,14 +21,14 @@ import java.util.List;
 /**
  * 对文档访问记录信息进行管理的服务类(高级)
  * 高级服务器可以利用Service完成事务控制
- * 
+ *
  * @author O2LEE
  */
 public class DocumentViewRecordServiceAdv {
-	
+
 	private UserManagerService userManagerService = new UserManagerService();
 	private DocumentViewRecordService documentViewRecordService = new DocumentViewRecordService();
-	
+
 	public List<DocumentViewRecord> list( List<String> ids ) throws Exception {
 		try ( EntityManagerContainer emc = EntityManagerContainerFactory.instance().create() ) {
 			return documentViewRecordService.list( emc, ids );
@@ -44,7 +44,7 @@ public class DocumentViewRecordServiceAdv {
 			throw e;
 		}
 	}
-	
+
 	public List<String> listByPerson( String personName, Integer maxCount ) throws Exception {
 		try ( EntityManagerContainer emc = EntityManagerContainerFactory.instance().create() ) {
 			List<String> ids =  documentViewRecordService.listByPerson( emc, personName, maxCount );
@@ -56,7 +56,7 @@ public class DocumentViewRecordServiceAdv {
 			throw e;
 		}
 	}
-	
+
 	public List<String> listDocIdsByPerson( String personName, Integer maxCount ) throws Exception {
 		try ( EntityManagerContainer emc = EntityManagerContainerFactory.instance().create() ) {
 			List<String> ids =  documentViewRecordService.listDocIdsByPerson( emc, personName, maxCount );
@@ -68,7 +68,7 @@ public class DocumentViewRecordServiceAdv {
 			throw e;
 		}
 	}
-	
+
 	public void deleteByDocument( String docId ) throws Exception {
 		try ( EntityManagerContainer emc = EntityManagerContainerFactory.instance().create() ) {
 			documentViewRecordService.deleteByDocument( emc, docId );
@@ -79,8 +79,8 @@ public class DocumentViewRecordServiceAdv {
 
 	/**
 	 * TODO:记录访问日志,一个用户一篇文档只保留一条记录,更新访问次数和最后访问时间
-	 * 
-	 * @param document
+	 *
+	 * @param docId
 	 * @param personName
 	 * @throws Exception
 	 */
@@ -93,14 +93,14 @@ public class DocumentViewRecordServiceAdv {
 		try ( EntityManagerContainer emc = EntityManagerContainerFactory.instance().create() ) {
 			business = new Business( emc );
 			emc.beginTransaction( DocumentViewRecord.class );
-			
+
 			ids = business.documentViewRecordFactory().listByDocAndPerson( docId, personName );
 			document = emc.find( docId, Document.class );
-			
+
 			if( document == null ) {
 				throw new Exception("document not exists!ID:" + docId );
 			}
-			
+
 			if( ListTools.isNotEmpty( ids )){
 				int i = 0;
 				for( String id : ids ){
@@ -112,7 +112,7 @@ public class DocumentViewRecordServiceAdv {
 							documentViewRecord.setViewCount( 1 );
 						}
 						documentViewRecord.setViewCount( documentViewRecord.getViewCount() + 1 );
-						emc.check( documentViewRecord, CheckPersistType.all ); 
+						emc.check( documentViewRecord, CheckPersistType.all );
 					}else{
 						//删除多余的日志数据
 						emc.remove( documentViewRecord, CheckRemoveType.all );
@@ -132,23 +132,89 @@ public class DocumentViewRecordServiceAdv {
 					documentViewRecord.setViewCount( 1 );
 					documentViewRecord.setViewerTopUnitName( userManagerService.getTopUnitNameWithPerson( personName ));
 					documentViewRecord.setViewerUnitName( userManagerService.getUnitNameWithPerson( personName ));
-					emc.persist( documentViewRecord, CheckPersistType.all ); 
+					emc.persist( documentViewRecord, CheckPersistType.all );
+				}
+			}
+			emc.commit();
+
+			emc.beginTransaction( Document.class );
+			viewCount = business.documentViewRecordFactory().sumWithDocmentId( docId );
+			document.setViewCount( viewCount );
+			emc.check( document, CheckPersistType.all );
+			emc.commit();
+
+		} catch ( Exception e ) {
+			throw e;
+		}
+		return viewCount;
+	}
+
+	public Long addViewRecord( String docId, String personName, Date viewTime) throws Exception {
+		DocumentViewRecord documentViewRecord = null;
+		Document document = null;
+		Business business = null;
+		Long viewCount = 0L;
+		List<String> ids = null;
+		try ( EntityManagerContainer emc = EntityManagerContainerFactory.instance().create() ) {
+			business = new Business( emc );
+			emc.beginTransaction( DocumentViewRecord.class );
+
+			ids = business.documentViewRecordFactory().listByDocAndPerson( docId, personName );
+			document = emc.find( docId, Document.class );
+
+			if( document == null ) {
+				throw new Exception("document not exists!ID:" + docId );
+			}
+
+			if( ListTools.isNotEmpty( ids )){
+				int i = 0;
+				for( String id : ids ){
+					i++;
+					documentViewRecord = emc.find( id, DocumentViewRecord.class );
+					if( i == 1 ){
+						documentViewRecord.setLastViewTime(viewTime);
+						if( documentViewRecord.getViewCount() == null || documentViewRecord.getViewCount() == 0 ){
+							documentViewRecord.setViewCount( 1 );
+						}
+						documentViewRecord.setViewCount( documentViewRecord.getViewCount() + 1 );
+						emc.check( documentViewRecord, CheckPersistType.all );
+					}else{
+						//删除多余的日志数据
+						emc.remove( documentViewRecord, CheckRemoveType.all );
+					}
+				}
+			}else{
+				if( document != null ){
+					documentViewRecord = new DocumentViewRecord();
+					documentViewRecord.setCreateTime(viewTime);
+					documentViewRecord.setAppId( document.getAppId() );
+					documentViewRecord.setCategoryId( document.getCategoryId() );
+					documentViewRecord.setDocumentId( document.getId() );
+					documentViewRecord.setViewerName( personName );
+					documentViewRecord.setAppName( document.getAppName() );
+					documentViewRecord.setCategoryName( document.getCategoryName() );
+					documentViewRecord.setTitle( document.getTitle() );
+					documentViewRecord.setLastViewTime(viewTime);
+					documentViewRecord.setViewCount( 1 );
+					documentViewRecord.setViewerTopUnitName( userManagerService.getTopUnitNameWithPerson( personName ));
+					documentViewRecord.setViewerUnitName( userManagerService.getUnitNameWithPerson( personName ));
+					emc.persist( documentViewRecord, CheckPersistType.all );
 				}
 			}
 			emc.commit();
-			
+
 			emc.beginTransaction( Document.class );
 			viewCount = business.documentViewRecordFactory().sumWithDocmentId( docId );
 			document.setViewCount( viewCount );
-			emc.check( document, CheckPersistType.all ); 
+			emc.check( document, CheckPersistType.all );
 			emc.commit();
-			
+
 		} catch ( Exception e ) {
 			throw e;
-		}		
+		}
 		return viewCount;
 	}
-	
+
 	public List<DocumentViewRecord> listNextWithDocIds( String id, String docId, Integer count, String order) throws Exception {
 		if( docId == null ){
 			throw new Exception("docId is null!");
@@ -162,7 +228,7 @@ public class DocumentViewRecordServiceAdv {
 			throw e;
 		}
 	}
-	
+
 	public Long countWithDocIds( String docId ) throws Exception {
 		if( docId == null ){
 			throw new Exception("docId is null!");
@@ -173,12 +239,12 @@ public class DocumentViewRecordServiceAdv {
 			throw e;
 		}
 	}
-	
+
 	/**
 	 * 对文档访问日志信息进行清理
 	 * @param stay_yeanumr_viewRecord 日志保留年份
 	 * @param stay_count_viewRecord 日志保留条目数
-	 * @throws Exception 
+	 * @throws Exception
 	 */
 	public void clean( Integer stay_yeanumr_viewRecord, Integer stay_count_viewRecord ) throws Exception {
 		//先按时间清理
@@ -188,8 +254,8 @@ public class DocumentViewRecordServiceAdv {
 
 	/**
 	 * 按最大保留条目数进行清理
-	 * @param stay_count_operationLog
-	 * @throws Exception 
+	 * @param stay_count_viewRecord
+	 * @throws Exception
 	 */
 	private void cleanWithMaxCount(Integer stay_count_viewRecord ) throws Exception {
 		Business business = null;
@@ -218,8 +284,8 @@ public class DocumentViewRecordServiceAdv {
 
 	/**
 	 * 按保留年份对日志进行清理
-	 * @param stay_yearnum_operationLog
-	 * @throws Exception 
+	 * @param stay_yeanumr_viewRecord
+	 * @throws Exception
 	 */
 	private void cleanWithStayYearNumber( Integer stay_yeanumr_viewRecord ) throws Exception {
 		if( stay_yeanumr_viewRecord == null ) {
@@ -230,12 +296,12 @@ public class DocumentViewRecordServiceAdv {
 		Date limitDate = DateOperation.getDateFromString( ( year-4 ) + "-01-01 00:00:00");
 		cleanWithDate( limitDate );
 	}
-	
-	
+
+
 	/**
 	 * 按保留年份对日志进行清理
 	 * @param overTime
-	 * @throws Exception 
+	 * @throws Exception
 	 */
 	private void cleanWithDate( Date overTime ) throws Exception {
 		if( overTime == null ) {
@@ -267,7 +333,7 @@ public class DocumentViewRecordServiceAdv {
 	 * @param ids
 	 * @param effectivePerson
 	 * @return
-	 * @throws Exception 
+	 * @throws Exception
 	 */
 	public List<String> listReadDocId( List<String> ids, EffectivePerson effectivePerson ) throws Exception {
 		if( ListTools.isEmpty(  ids ) ) {