@ -5,9 +5,7 @@ import com.github.pagehelper.PageInfo;
import com.sztzjy.resource_center.annotation.AnonymousAccess ;
import com.sztzjy.resource_center.entity.* ;
import com.sztzjy.resource_center.entity.dto.SysObjectiveQuestionsDto ;
import com.sztzjy.resource_center.mapper.SysObjectiveQuestionsMapper ;
import com.sztzjy.resource_center.mapper.SysOneCatalogMapper ;
import com.sztzjy.resource_center.mapper.SysTopicAndCourseMapper ;
import com.sztzjy.resource_center.mapper.* ;
import com.sztzjy.resource_center.util.PageUtil ;
import com.sztzjy.resource_center.util.file.IFileUtil ;
import io.swagger.annotations.Api ;
@ -19,6 +17,7 @@ import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.BeanUtils ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Value ;
import org.springframework.transaction.annotation.Transactional ;
import org.springframework.web.bind.annotation.* ;
import org.springframework.web.multipart.MultipartFile ;
@ -42,7 +41,10 @@ public class ObjectiveApi {
IFileUtil fileUtil ;
@Value ( "${file.path}" )
private String filePath ;
@Autowired
private SysTwoCatalogMapper sysTwoCatalogMapper ;
@Autowired
private SysThreeCatalogMapper sysThreeCatalogMapper ;
private SysOneCatalog getSysOneCatalogs ( String systemOwner ) {
SysOneCatalogExample example = new SysOneCatalogExample ( ) ;
@ -252,7 +254,8 @@ public class ObjectiveApi {
@AnonymousAccess
@ApiOperation ( "本地excel导入试题" )
@PostMapping ( "insertObjectiveByExcel" )
private Boolean importTopicByLocal ( @RequestParam MultipartFile file ,
@Transactional
Boolean importTopicByLocal ( @RequestParam MultipartFile file ,
@RequestParam String schoolId ) throws IOException , InvalidFormatException {
if ( file = = null ) {
return false ;
@ -264,9 +267,17 @@ public class ObjectiveApi {
//如果是老师增加 则只需要提供课程信息
public Boolean insertObjectiveByExcel ( MultipartFile file , String schoolId ) throws IOException , InvalidFormatException {
List < SysObjectiveQuestions > objectiveQuestionList = new ArrayList < > ( ) ;
List < SysTopicAndCourse > topicAndCourseList = new ArrayList < > ( ) ;
Workbook workbook = WorkbookFactory . create ( file . getInputStream ( ) ) ;
Sheet sheet = workbook . getSheetAt ( 0 ) ;
//创建三级的ID和名称Map
Map < String , String > oneIdMap = new HashMap < > ( ) ;
Map < String , String > twoIdMap = new HashMap < > ( ) ;
Map < String , String > threeIdMap = new HashMap < > ( ) ;
// 迭代每一行
Iterator < Row > iterator = sheet . iterator ( ) ;
iterator . next ( ) ;
@ -284,6 +295,9 @@ public class ObjectiveApi {
Cell cell7 = row . getCell ( 7 ) ;
Cell cell8 = row . getCell ( 8 ) ;
Cell cell9 = row . getCell ( 9 ) ;
Cell cell10 = row . getCell ( 10 ) ;
Cell cell11 = row . getCell ( 11 ) ;
Cell cell12 = row . getCell ( 12 ) ;
//判断题的BCDE选项和解析可能为空
if ( cell0 = = null | | cell1 = = null | | cell2 = = null | | cell6 = = null | | cell7 = = null | | cell9 = = null ) {
return false ;
@ -295,6 +309,9 @@ public class ObjectiveApi {
String type = this . getCellValue ( cell6 ) ; //题型
String answer = this . getCellValue ( cell7 ) ; //答案
String score = this . getCellValue ( cell9 ) ; //分值
String oneName = this . getCellValue ( cell10 ) ; //一级目录名称
String twoName = this . getCellValue ( cell11 ) ; //二级目录名称
String threeName = this . getCellValue ( cell12 ) ; //三级目录名称
String c = cell3 ! = null ? this . getCellValue ( cell3 ) : null ;
String d = cell4 ! = null ? this . getCellValue ( cell4 ) : null ;
String e = cell5 ! = null ? this . getCellValue ( cell5 ) : null ;
@ -302,7 +319,8 @@ public class ObjectiveApi {
SysObjectiveQuestions obj = new SysObjectiveQuestions ( ) ;
obj . setObjectiveId ( String . valueOf ( UUID . randomUUID ( ) ) ) ;
String topicId = IdUtil . randomUUID ( ) ;
obj . setObjectiveId ( topicId ) ;
obj . setContent ( content ) ;
obj . setType ( type ) ;
obj . setSource ( schoolId ) ;
@ -317,7 +335,40 @@ public class ObjectiveApi {
obj . setCreateTime ( new Date ( ) ) ;
obj . setIsDelete ( false ) ;
objectiveQuestionList . add ( obj ) ;
//判断名称是否被查出来过
String oneId = oneIdMap . get ( oneName ) ;
if ( oneId = = null ) {
oneId = oneCatalogMapper . getIdByName ( oneName ) ;
oneIdMap . put ( oneName , oneId ) ;
}
String twoId = twoIdMap . get ( twoName ) ;
if ( twoId = = null ) {
twoId = sysTwoCatalogMapper . getIdByName ( twoName ) ;
twoIdMap . put ( twoName , twoId ) ;
}
String threeId = threeIdMap . get ( threeName ) ;
if ( threeId = = null ) {
threeId = sysThreeCatalogMapper . getIdByName ( threeName , twoName ) ;
threeIdMap . put ( threeName , threeId ) ;
}
SysTopicAndCourse sysTopicAndCourse = new SysTopicAndCourse ( ) ;
sysTopicAndCourse . setId ( IdUtil . randomUUID ( ) ) ;
sysTopicAndCourse . setTopicId ( topicId ) ;
sysTopicAndCourse . setTopicType ( "0" ) ;
sysTopicAndCourse . setOneName ( oneName ) ;
sysTopicAndCourse . setOneId ( oneId ) ;
sysTopicAndCourse . setTwoName ( twoName ) ;
sysTopicAndCourse . setTwoId ( twoId ) ;
sysTopicAndCourse . setThreeId ( threeId ) ;
sysTopicAndCourse . setThreeName ( threeName ) ;
topicAndCourseList . add ( sysTopicAndCourse ) ;
}
sysTopicAndCourseMapper . batchInsert ( topicAndCourseList ) ;
int result = sysObjectiveQuestionMapper . insertBatch ( objectiveQuestionList ) ;
if ( result > 0 ) {
return true ;