@ -3,16 +3,21 @@ package com.storeroom.modules.dictionary.service.impl;
import com.storeroom.exception.BaseException ;
import com.storeroom.modules.dictionary.domain.ArchivesDictionary ;
import com.storeroom.modules.dictionary.domain.ArchivesType ;
import com.storeroom.modules.dictionary.repository.ArchivesDictionaryRepository ;
import com.storeroom.modules.dictionary.repository.ArchivesTypeRepository ;
import com.storeroom.modules.dictionary.service.ArchivesDictionaryService ;
import com.storeroom.modules.dictionary.service.dto.* ;
import com.storeroom.modules.dictionary.service.mapstruct.ArchivesDictionaryMapper ;
import com.storeroom.utils.NanoIdUtils ;
import com.storeroom.utils.StringUtils ;
import com.storeroom.utils.ValidationUtil ;
import lombok.RequiredArgsConstructor ;
import org.springframework.stereotype.Service ;
import org.springframework.transaction.annotation.Transactional ;
import javax.persistence.EntityManager ;
import javax.persistence.PersistenceContext ;
import java.util.ArrayList ;
import java.util.Comparator ;
import java.util.List ;
@ -22,6 +27,9 @@ import java.util.Set;
@RequiredArgsConstructor
public class ArchivesDictionaryImpl implements ArchivesDictionaryService {
@PersistenceContext
EntityManager entityManager ;
private final ArchivesTypeRepository archivesTypeRepository ;
private final ArchivesDictionaryRepository archivesDictionaryRepository ;
private final ArchivesDictionaryMapper archivesDictionaryMapper ;
@ -111,6 +119,17 @@ public class ArchivesDictionaryImpl implements ArchivesDictionaryService {
arc . setId ( NanoIdUtils . randomNanoId ( ) ) ;
arc . setIsInput ( true ) ;
archivesDictionaryRepository . save ( arc ) ;
String colType = arcdicDto . getIsDataType ( ) = = 1 ? "varchar" : "int" ;
colType = colType + "(" + arcdicDto . getIsColumnLength ( ) + ") " ;
String adddefault = " " ;
if ( ! StringUtils . isEmpty ( arcdicDto . getIsDefaultValue ( ) ) ) {
adddefault = " default " ;
adddefault = arcdicDto . getIsDataType ( ) = = 2 ? adddefault + arcdicDto . getIsDefaultValue ( ) : adddefault + "'" + arcdicDto . getIsDefaultValue ( ) + "'" ;
}
ArchivesType archivesType = archivesTypeRepository . findById ( arcdicDto . getCategoryId ( ) ) . get ( ) ;
String sql = "alter table " + archivesType . getEnName ( ) + " add " + arc . getFieldName ( ) + " " + colType + adddefault + " comment '" + arc . getFieldCnName ( ) + "'" ;
entityManager . createNativeQuery ( sql ) . executeUpdate ( ) ;
} else {
throw new BaseException ( "字段名称不能重复" ) ;
}
@ -119,7 +138,11 @@ public class ArchivesDictionaryImpl implements ArchivesDictionaryService {
@Override
@Transactional
public void delete ( String id ) {
ArchivesDictionary archivesDictionary = archivesDictionaryRepository . findById ( id ) . get ( ) ;
ArchivesType archivesType = archivesTypeRepository . findById ( archivesDictionary . getCategoryId ( ) ) . get ( ) ;
archivesDictionaryRepository . deleteById ( id ) ;
String sql = "alter table " + archivesType . getEnName ( ) + " DROP COLUMN " + archivesDictionary . getFieldName ( ) ;
entityManager . createNativeQuery ( sql ) . executeUpdate ( ) ;
}
@Override