@ -3,6 +3,7 @@ package com.storeroom.modules.archives.service.impl;
import cn.hutool.json.JSONArray ;
import cn.hutool.json.JSONObject ;
import com.storeroom.modules.archives.domain.ArchivesSummary ;
import com.storeroom.modules.archives.domain.vo.ArchivesDetailsVO ;
import com.storeroom.modules.archives.repository.ArchivesSummaryRepository ;
import com.storeroom.modules.archives.service.ArchivesService ;
import com.storeroom.modules.archives.service.dto.ArchivesDTO ;
@ -24,6 +25,7 @@ import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager ;
import javax.persistence.PersistenceContext ;
import java.io.File ;
import java.text.SimpleDateFormat ;
import java.util.* ;
import java.util.stream.Collectors ;
@ -47,7 +49,9 @@ public class ArchivesServiceImpl implements ArchivesService {
}
@Override
public Object initArchivesView ( String categoryId , String parentsId , String query , Integer archivesState , boolean isdel , Pageable page ) {
public Object initArchivesView ( String categoryId , String parentsId , String query , Integer archivesState ,
String archiveYear , String department , String retention , String securityClass , String organizationMatter ,
boolean isdel , Pageable page ) {
Optional < ArchivesType > optional = archivesTypeRepository . findById ( categoryId ) ;
if ( ! optional . isPresent ( ) )
return "未查询到到门类信息!" ;
@ -74,12 +78,18 @@ public class ArchivesServiceImpl implements ArchivesService {
String [ ] splits = obj . toString ( ) . split ( ":" ) ;
quertOrder + = splits [ 0 ] . trim ( ) + " " + splits [ 1 ] . trim ( ) + "," ;
}
String additional = isdel ? " t where is_delete_time is not null " : " where is_delete_time is null " ;
String additional = isdel ? " t where is_delete_time is not null " : " t where is_delete_time is null " ;
String queryparentsId = StringUtils . isEmpty ( parentsId ) ? " " : " and parent_id = '" + parentsId + "' " ;
additional = StringUtils . isEmpty ( archiveYear ) ? archiveYear : additional + " and archive_year = '" + archiveYear + "'" ;
additional = StringUtils . isEmpty ( department ) ? department : additional + " and department = '" + department + "'" ;
additional = StringUtils . isEmpty ( retention ) ? retention : additional + " and retention = '" + retention + "'" ;
additional = StringUtils . isEmpty ( securityClass ) ? securityClass : additional + " and security_class = '" + securityClass + "'" ;
additional = StringUtils . isEmpty ( organizationMatter ) ? organizationMatter : additional + " and organization_matter = '" + organizationMatter + "'" ;
additional = StringUtils . isEmpty ( query ) ? additional : additional
+ " and (maintitle like '%" + query + "%' or archive_no like '%" + query + "%' or archive_year like '%" + query + "%') " ;
if ( objects . length > 0 )
quertOrder = " order by " + quertOrder . substring ( 0 , quertOrder . length ( ) - 1 ) ;
@ -95,14 +105,81 @@ public class ArchivesServiceImpl implements ArchivesService {
}
returnlist . add ( map ) ;
}
return PageUtil . toPage ( returnlist , count ) ;
List < Object [ ] > yearGroup = entityManager . createNativeQuery ( "select archive_year from " + queryTable + additional + queryparentsId
+ " and archive_year is not null group by archive_year order by archive_year asc" ) . getResultList ( ) ;
JSONObject object = new JSONObject ( ) ;
object . put ( "list" , PageUtil . toPage ( returnlist , count ) ) ;
object . put ( "yearGroup" , yearGroup ) ;
return object ;
}
@Override
public List < ArchivesDetailsVO > archiveDetails ( String categoryId , String archivesId ) {
List < ArchivesDetailsVO > returnVo = new ArrayList < > ( ) ;
ArchivesType archivesType = archivesTypeRepository . findById ( categoryId ) . get ( ) ;
List < String > queryFields = archivesDictionaryRepository . findDisPlayField ( categoryId ) ;
List < String > queryCnFields = archivesDictionaryRepository . findDisPlayCnField ( categoryId ) ;
String queryField = queryFields . stream ( ) . map ( String : : valueOf ) . collect ( Collectors . joining ( "," ) ) ;
String queryTable = archivesType . getEnName ( ) ;
String appendQuery = ",tid,(select group_concat(case_name) from archives_case t inner join archives_case_cartoning t1 on t.id = t1.case_id where archives_id = z.id)," +
"folder_location,borrow_type,barcode " ;
List < Object [ ] > list = entityManager . createNativeQuery ( "select " + queryField + appendQuery + " from " + queryTable + " z where z.id = '" + archivesId + "'" ) . getResultList ( ) ;
queryFields . add ( "tid" ) ; queryCnFields . add ( "TID" ) ;
queryFields . add ( "case_name" ) ; queryCnFields . add ( "盒名称" ) ;
queryFields . add ( "folder_location" ) ; queryCnFields . add ( "库房位置" ) ;
queryFields . add ( "borrow_type" ) ; queryCnFields . add ( "借阅状态" ) ;
queryFields . add ( "barcode" ) ; queryCnFields . add ( "条形码" ) ;
for ( int i = 0 ; i < queryFields . size ( ) ; i + + ) {
Object [ ] obj = list . get ( 0 ) ;
ArchivesDetailsVO vo = new ArchivesDetailsVO ( ) ;
vo . setFieldName ( queryFields . get ( i ) ) ;
vo . setFieldCnName ( queryCnFields . get ( i ) ) ;
vo . setContext ( obj [ i ] ) ;
returnVo . add ( vo ) ;
}
return returnVo ;
}
@Override
public List < Object [ ] > exportArchivesData ( String categoryId , String archivesId , Pageable page ) {
ArchivesType archivesType = archivesTypeRepository . findById ( categoryId ) . get ( ) ;
List < String > queryFields = archivesDictionaryRepository . findDisPlayField ( categoryId ) ;
String queryField = queryFields . stream ( ) . map ( String : : valueOf ) . collect ( Collectors . joining ( "," ) ) ;
String queryTable = archivesType . getEnName ( ) ;
String quertOrder = "" ;
String prependQuery = "" ;
if ( archivesType . getIsType ( ) = = ArchivesTypeEnum . project . getCode ( ) | | archivesType . getIsType ( ) = = ArchivesTypeEnum . archives . getCode ( ) ) {
String childrenTable = archivesTypeRepository . findEnNameByPid ( archivesType . getId ( ) ) ;
prependQuery = StringUtils . isEmpty ( childrenTable ) ? "0," : "(select count(1) from " + childrenTable + " c where c.parent_id = t.id)," ;
} else {
prependQuery = "(select count(1) from file_" + queryTable + " c where c.archive_id = t.id)," ;
}
Sort sort = page . getSort ( ) ;
Object [ ] objects = sort . get ( ) . toArray ( ) ;
for ( Object obj : objects ) {
String [ ] splits = obj . toString ( ) . split ( ":" ) ;
quertOrder + = splits [ 0 ] . trim ( ) + " " + splits [ 1 ] . trim ( ) + "," ;
}
String appendQuery = ",if(tid is null||LENGTH(trim(tid))=0,'未绑','已绑')" +
",if(case_no is null||LENGTH(trim(case_no))=0,'未装','已装')" +
",'未入'" +
",'未借'" ;
String additional = " t where is_delete_time is null " ;
String queryparentsId = StringUtils . isEmpty ( archivesId ) ? " " : " and parent_id = '" + archivesId + "' " ;
if ( objects . length > 0 )
quertOrder = " order by " + quertOrder . substring ( 0 , quertOrder . length ( ) - 1 ) ;
List < Object [ ] > list = entityManager . createNativeQuery ( "select " + prependQuery + queryField + appendQuery + " from " + queryTable + additional + queryparentsId + quertOrder ) . getResultList ( ) ;
return list ;
}
@Override
public Object doedit ( String categoryId , String archivesId ) {
JSONObject json = new JSONObject ( ) ;
List < ArchivesDictionary > allFiled = archivesDictionaryRepository . findAllByCategoryIdOrderByIsSequenceAsc ( categoryId ) ;
List < ArchivesDictionary > showFiled = archivesDictionaryRepository . findAllByCategoryIdAndIsInputOrderByIsSequenceAsc ( categoryId , true ) ;
List < ArchivesDictionary > showFiled = archivesDictionaryRepository . findAllByCategoryIdAndIsTypeAndIs InputOrderByIsSequenceAsc ( categoryId , 2 , true ) ;
ArchivesDictionary showappendFiled = archivesDictionaryRepository . findFirstByCategoryIdAndFieldName ( categoryId , "barcode" ) ;
showFiled . add ( showappendFiled ) ;
if ( ! StringUtils . isEmpty ( archivesId ) ) {
@ -151,7 +228,7 @@ public class ArchivesServiceImpl implements ArchivesService {
ArchivesSummary archivesSummary = new ArchivesSummary ( ) ;
JSONObject json = new JSONObject ( dto . getJsonString ( ) ) ;
ArchivesType archivesType = archivesTypeRepository . findById ( dto . getCategoryId ( ) ) . get ( ) ;
List < ArchivesDictionary > showFiled = archivesDictionaryRepository . findAllByCategoryIdAndIsInputOrderByIsSequenceAsc ( dto . getCategoryId ( ) , true ) ;
List < ArchivesDictionary > showFiled = archivesDictionaryRepository . findAllByCategoryIdAndIsTypeAndIs InputOrderByIsSequenceAsc ( dto . getCategoryId ( ) , 2 , true ) ;
List < String > filedNameList = new ArrayList < > ( ) ;
String insertFiled = "category_id,category_name," ;
String insertValue = "'" + dto . getCategoryId ( ) + "','" + archivesType . getCnName ( ) + "'," ;
@ -159,13 +236,13 @@ public class ArchivesServiceImpl implements ArchivesService {
for ( ArchivesDictionary archivesDictionary : showFiled ) {
filedNameList . add ( archivesDictionary . getFieldName ( ) ) ;
/ / 是否必填
if ( archivesDictionary . getIsRequired ( ) ) {
if ( null ! = archivesDictionary . getIsRequired ( ) & & archivesDictionary . getIsRequired ( ) ) {
if ( null = = json . get ( archivesDictionary . getFieldName ( ) ) | | "null" . equals ( json . get ( archivesDictionary . getFieldName ( ) ) ) | | "" . equals ( json . get ( archivesDictionary . getFieldName ( ) ) ) ) {
return ApiResponse . error ( 50001 , archivesDictionary . getFieldName ( ) + "不可为空!" ) ;
}
}
/ / 是否重复
if ( archivesDictionary . getIsRepeat ( ) ) {
if ( null ! = archivesDictionary . getIsRepeat ( ) & & archivesDictionary . getIsRepeat ( ) ) {
/ / 判断是否为空
if ( null ! = json . get ( archivesDictionary . getFieldName ( ) ) & & ! "" . equals ( json . get ( archivesDictionary . getFieldName ( ) ) ) & & ! "null" . equals ( json . get ( archivesDictionary . getFieldName ( ) ) ) ) {
String thissql = "select id," + archivesDictionary . getFieldName ( ) + " from " + archivesType . getEnName ( ) + " where " +
@ -198,9 +275,15 @@ public class ArchivesServiceImpl implements ArchivesService {
insertFiled = insertFiled . length ( ) ! = 0 ? insertFiled . substring ( 0 , insertFiled . length ( ) - 1 ) : "" ;
insertValue = insertValue . length ( ) ! = 0 ? insertValue . substring ( 0 , insertValue . length ( ) - 1 ) : "" ;
updateValue = updateValue . length ( ) ! = 0 ? updateValue . substring ( 0 , updateValue . length ( ) - 1 ) : "" ;
insertFiled = "id," + insertFiled ;
insertFiled = "id,create_time," + insertFiled ;
if ( ! StringUtils . isEmpty ( dto . getParentsId ( ) ) ) {
insertFiled = insertFiled + ",parent_id " ;
insertValue = insertValue + ",'" + dto . getParentsId ( ) + "' " ;
updateValue = updateValue + ",parent_id='" + dto . getParentsId ( ) + "' " ;
}
String insertId = NanoIdUtils . randomNanoId ( ) ;
insertValue = "'" + insertId + "'," + insertValue ;
String now = sdf . format ( new Date ( ) ) ;
insertValue = "'" + insertId + "','" + now + "'," + insertValue ;
String sql = StringUtils . isEmpty ( dto . getId ( ) ) ? "insert into " + archivesType . getEnName ( ) + "(" + insertFiled + ") values (" + insertValue + ")"
: "update " + archivesType . getEnName ( ) + " set " + updateValue + " where id = '" + dto . getId ( ) + "'" ;
int result = entityManager . createNativeQuery ( sql ) . executeUpdate ( ) ;
@ -329,12 +412,23 @@ public class ArchivesServiceImpl implements ArchivesService {
@Override
@Transactional ( rollbackFor = Exception . class )
public Object deleteFile ( ArchivesFileDTO dto ) {
public Object deleteFile ( ArchivesFileDTO dto , String prefixPath ) {
ArchivesType archivesType = archivesTypeRepository . findById ( dto . getCategoryId ( ) ) . get ( ) ;
String ids = dto . getIds ( ) . stream ( ) . map ( String : : valueOf ) . collect ( Collectors . joining ( "','" ) ) ;
ids = ids . length ( ) > 0 ? "'" + ids + "'" : ids ;
String sql = "delete from " + archivesType . getEnName ( ) + " where id in (" + ids + ")" ;
/ / ids . replaceAll ( "\"" , "" ) ;
String queryPath = "select file_path from file_" + archivesType . getEnName ( ) + " where id in (" + ids + ")" ;
List < String > list = entityManager . createNativeQuery ( queryPath ) . getResultList ( ) ;
String sql = "delete from file_" + archivesType . getEnName ( ) + " where id in (" + ids + ")" ;
int result = entityManager . createNativeQuery ( sql ) . executeUpdate ( ) ;
for ( String paths : list ) {
if ( ! StringUtils . isEmpty ( paths ) ) {
File file = new File ( prefixPath + paths ) ;
if ( file . isFile ( ) & & file . exists ( ) ) {
file . delete ( ) ;
}
}
}
return result ;
}
@ -350,6 +444,34 @@ public class ArchivesServiceImpl implements ArchivesService {
return result ;
}
@Override
@Transactional ( rollbackFor = Exception . class )
public Object reduction ( ArchivesDTO dto ) {
List < String > ids = dto . getIds ( ) ;
int success = 0 ;
ArchivesType archivesType = archivesTypeRepository . findById ( dto . getCategoryId ( ) ) . get ( ) ;
List < String > fieldNames = archivesDictionaryRepository . findFieldNameByCategoryIdAndIsTypeAndIsRepeat ( dto . getCategoryId ( ) , 2 , true ) ;
for ( String id : ids ) {
boolean isRepeat = true ;
for ( String fieldName : fieldNames ) {
String querysql = "select id from " + archivesType . getEnName ( ) + " where is_delete_time is null and " + fieldName +
"= (select " + fieldName + " from " + archivesType . getEnName ( ) + " where id = '" + id + "')" ;
List < Object [ ] > list = entityManager . createNativeQuery ( querysql ) . getResultList ( ) ;
if ( list . size ( ) > 0 ) {
isRepeat = false ;
}
}
/ / 当没有重复
if ( isRepeat ) {
String sql = "update " + archivesType . getEnName ( ) + " set is_delete_time = null,is_delete_man = null " +
"where id = '" + id + "'" ;
int result = entityManager . createNativeQuery ( sql ) . executeUpdate ( ) ;
success + = result ;
}
}
return "成功还原" + success + "条数据" ;
}
@Override
@Transactional ( rollbackFor = Exception . class )
public Object completelyDelete ( ArchivesDTO dto ) {
@ -365,4 +487,18 @@ public class ArchivesServiceImpl implements ArchivesService {
}
return result + "," + result1 ;
}
@Override
public String getcategoryAllParentsId ( String categoryId ) {
List < String > categoryIds = new ArrayList < > ( ) ;
String thisCategoryId = categoryId ;
categoryIds . add ( thisCategoryId ) ;
do {
thisCategoryId = archivesTypeRepository . findPidById ( thisCategoryId ) ;
if ( ! StringUtils . isEmpty ( thisCategoryId ) ) {
categoryIds . add ( thisCategoryId ) ;
}
} while ( thisCategoryId ! = null ) ;
return categoryIds . stream ( ) . map ( String : : valueOf ) . collect ( Collectors . joining ( "," ) ) ;
}
}