@ -1,16 +1,21 @@
package com.msdw.tms.service.impl ;
import com.alibaba.fastjson.JSON ;
import com.alibaba.fastjson.TypeReference ;
import com.baomidou.mybatisplus.core.metadata.IPage ;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page ;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl ;
import com.msdw.tms.common.exception.ExceptionCast ;
import com.msdw.tms.common.utils.* ;
import com.msdw.tms.common.utils.poi.ExcelExportUtil ;
import com.msdw.tms.dao.StudentDao ;
import com.msdw.tms.dao.UserInfoDao ;
import com.msdw.tms.entity.StudentEntity ;
import com.msdw.tms.entity.UserInfoEntity ;
import com.msdw.tms.entity.XlsxTemplateEntity ;
import com.msdw.tms.entity.response.CommonCode ;
import com.msdw.tms.entity.vo.QuestionsImportFailureVO ;
import com.msdw.tms.entity.vo.StudentImportFailureVo ;
import com.msdw.tms.entity.vo.StudentVo ;
import com.msdw.tms.service.AliyunOssService ;
import com.msdw.tms.service.StudentService ;
@ -18,6 +23,9 @@ import com.msdw.tms.service.XlsxTemplateService;
import org.apache.commons.lang.StringUtils ;
import org.apache.poi.ss.formula.functions.T ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.core.io.ClassPathResource ;
import org.springframework.data.redis.core.StringRedisTemplate ;
import org.springframework.data.redis.core.ValueOperations ;
import org.springframework.stereotype.Service ;
import org.springframework.transaction.annotation.Transactional ;
import org.springframework.web.multipart.MultipartFile ;
@ -25,9 +33,9 @@ import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource ;
import javax.servlet.http.HttpServletResponse ;
import java.io.IOException ;
import java.util.ArrayList ;
import java.util.HashMap ;
import java.util.Lis t ;
import java.io.InputStream ;
import java.util.* ;
import java.util.concurrent.TimeUni t ;
@Service ( "studentService" )
public class StudentServiceImpl extends ServiceImpl < StudentDao , StudentEntity > implements StudentService {
@ -44,6 +52,9 @@ public class StudentServiceImpl extends ServiceImpl<StudentDao,StudentEntity> im
@Autowired
private UserInfoDao userInfoDao ;
@Resource
StringRedisTemplate stringRedisTemplate ;
@Override
public Integer queryStudentIdNumber ( String workNumber , Integer schoolId ) {
List < Integer > result = studentDao . queryStudentIdNumber ( workNumber , schoolId ) ;
@ -88,7 +99,7 @@ public class StudentServiceImpl extends ServiceImpl<StudentDao,StudentEntity> im
public boolean updateStudent ( StudentVo studentVo ) {
try {
Integer update = studentDao . updateStudent ( studentVo ) ;
return true ;
return true ;
} catch ( RuntimeException e ) {
e . printStackTrace ( ) ;
return false ;
@ -105,14 +116,14 @@ public class StudentServiceImpl extends ServiceImpl<StudentDao,StudentEntity> im
R r = new R ( ) ;
boolean result = studentDao . addStudentInfo ( userInfoEntity ) ;
if ( result = = false ) {
r . put ( "code" , 300 ) ;
r . put ( "errmessage" , "注册失败!" ) ;
return r ;
} else {
r . put ( "code" , 200 ) ;
r . put ( "errmessage" , "注册成功!" ) ;
return r ;
}
r . put ( "code" , 300 ) ;
r . put ( "errmessage" , "注册失败!" ) ;
return r ;
} else {
r . put ( "code" , 200 ) ;
r . put ( "errmessage" , "注册成功!" ) ;
return r ;
}
}
@Override
@ -123,30 +134,40 @@ public class StudentServiceImpl extends ServiceImpl<StudentDao,StudentEntity> im
@Transactional
@Override
public Hash Map< String , Object > upload ( MultipartFile file , Integer schoolId ) throws IOException {
public Map < String , String > upload ( MultipartFile file , Integer schoolId ) throws IOException {
List < StudentVo > students = ExcelImportHelper . readStudent ( file ) ;
List < StudentImportFailureVo > failVo1 = new ArrayList < > ( ) ;
// 参数合法性校验,只能上传.xlsx后缀的文件
if ( StringUtils . isBlank ( file . getOriginalFilename ( ) )
| | ! file . getOriginalFilename ( ) . substring ( file . getOriginalFilename ( ) . lastIndexOf ( "." ) ) . equals ( Constant . EXCEL_SUFFIX ) ) {
ExceptionCast . cast ( CommonCode . EXCEL_FILE_INVALID ) ;
}
HashMap < String , Object > map = new HashMap < > ( ) ;
int size = students . size ( ) ;
Map < String , String > map = new HashMap < > ( ) ;
Long ii = 1L ; //用于记录序列号
try {
for ( int i = 0 ; i < students . size ( ) ; i + + ) {
StudentVo student = students . get ( i ) ;
String phone = student . getPhone ( ) ;
String workNumber = student . getWorkNumber ( ) ;
String account = student . getAccount ( ) ;
StudentVo studentVo = new StudentVo ( ) ;
String schoolAppellationName = student . getSchoolAppellationName ( ) ;
List < Integer > result1 = studentDao . queryStudentIdNumber ( workNumber , schoolId ) ;
List < StudentEntity > result4 = userInfoDao . queryStudentAccount ( account , schoolId ) ;
String email = student . getEmail ( ) ;
if ( phone ! = null & & phone ! = "" ) {
List < StudentEntity > result2 = userInfoDao . queryStudentPhone ( phone , schoolId ) ;
if ( result2 . size ( ) = = 1 ) {
log . error ( "该号码已被使用" ) ;
map . put ( "被占用电话号码序列:" + System . currentTimeMillis ( ) , "电话号码:" + phone ) ;
StudentImportFailureVo vo = new StudentImportFailureVo ( ) ;
+ + ii ;
vo . setIndex ( ii ) . setUserName ( student . getUserName ( ) ) . setSchoolAppellationName ( student . getSchoolAppellationName ( ) )
. setAccount ( student . getAccount ( ) ) . setWorkNumber ( student . getWorkNumber ( ) ) . setPhone ( student . getPhone ( ) + " (重复的号码)" ) . setEmail ( student . getEmail ( ) )
;
// .setFailureMsg("重复的号码");
failVo1 . add ( vo ) ;
// map.put("被占用电话号码序列:"+System.currentTimeMillis(),"电话号码:"+phone);
students . remove ( i ) ;
i - - ;
continue ;
@ -161,22 +182,50 @@ public class StudentServiceImpl extends ServiceImpl<StudentDao,StudentEntity> im
studentVo . setUniqueIdentificationAccount ( String . valueOf ( System . currentTimeMillis ( ) ) ) .
setPassword ( ConstantUtils . INITIAL_PASSWORD )
. setSchoolId ( ConstantUtils . Keda_schoolId ) . setRoleId ( ConstantUtils . STUDENT_ROLE )
;
;
studentVo . setAccount ( student . getAccount ( ) ) . setSchoolAppellationName ( schoolAppellationName ) . setUserName ( student . getUserName ( ) ) . setIsdel ( Constant . IsDel . NOT_DEL . getType ( ) ) ;
if ( result1 . size ( ) = = 1 ) {
log . error ( "该学号已存在" ) ;
map . put ( "学号已存在序列:" + System . currentTimeMillis ( ) , "学号:" + workNumber ) ;
StudentImportFailureVo vo = new StudentImportFailureVo ( ) ;
+ + ii ;
vo . setIndex ( ii ) . setUserName ( student . getUserName ( ) ) . setSchoolAppellationName ( student . getSchoolAppellationName ( ) )
. setAccount ( student . getAccount ( ) ) . setWorkNumber ( student . getWorkNumber ( ) + " (重复的学号)" ) . setPhone ( student . getPhone ( ) ) . setEmail ( student . getEmail ( ) )
;
// .setFailureMsg("重复的学号");
failVo1 . add ( vo ) ;
// map.put("学号已存在序列:"+System.currentTimeMillis(),"学号:"+workNumber);
students . remove ( i ) ;
i - - ;
continue ;
} else if ( result4 . size ( ) = = 1 ) {
log . error ( "账号已存在" ) ;
StudentImportFailureVo vo = new StudentImportFailureVo ( ) ;
+ + ii ;
vo . setIndex ( ii ) . setUserName ( student . getUserName ( ) ) . setSchoolAppellationName ( student . getSchoolAppellationName ( ) )
. setAccount ( student . getAccount ( ) + " (重复的账号)" ) . setWorkNumber ( student . getWorkNumber ( ) ) . setPhone ( student . getPhone ( ) ) . setEmail ( student . getEmail ( ) )
;
// .setFailureMsg("重复的账号");
failVo1 . add ( vo ) ;
// map.put("学号已存在序列:"+System.currentTimeMillis(),"学号:"+workNumber);
students . remove ( i ) ;
i - - ;
continue ;
} else if ( result3 . size ( ) ! = 1 ) {
}
else if ( result3 . size ( ) ! = 1 ) {
log . error ( "不存在该院校" ) ;
map . put ( "错误的学校名称序列:" + System . currentTimeMillis ( ) , "学校名称:" + schoolAppellationName ) ;
StudentImportFailureVo vo = new StudentImportFailureVo ( ) ;
+ + ii ;
vo . setIndex ( ii ) . setUserName ( student . getUserName ( ) ) . setSchoolAppellationName ( student . getSchoolAppellationName ( ) )
. setAccount ( student . getAccount ( ) ) . setWorkNumber ( student . getWorkNumber ( ) ) . setPhone ( student . getPhone ( ) ) . setEmail ( student . getEmail ( ) )
. setFailureMsg ( "不存在的院校" ) ;
failVo1 . add ( vo ) ;
// map.put("错误的学校名称序列:"+System.currentTimeMillis(),"学校名称:"+schoolAppellationName);
students . remove ( i ) ;
i - - ;
continue ;
}
+ + ii ;
if ( ! students . isEmpty ( ) & & students . size ( ) > 0 ) {
userInfoDao . batchSaveUserInfo ( studentVo ) ;
student . setUserId ( studentVo . getUserId ( ) ) . setIsdel ( Constant . IsDel . NOT_DEL . getType ( ) ) . setPlatformId ( ConstantUtils . PLATFORMID ) ;
@ -185,18 +234,56 @@ public class StudentServiceImpl extends ServiceImpl<StudentDao,StudentEntity> im
throw new RuntimeException ( ) ;
}
}
}
} catch ( RuntimeException e ) {
log . error ( e . getMessage ( ) ) ;
map . put ( "failura" , "添加失败" ) ;
throw new RuntimeException ( ) ;
}
String token = "" ;
// 有导入失败的数据,才会存入redis
if ( failVo1 . size ( ) > 0 ) {
//生成token
token = "FAILURE_IMPORT" + UUID . randomUUID ( ) . toString ( ) . replace ( "-" , "" ) ;
ValueOperations < String , String > ops = stringRedisTemplate . opsForValue ( ) ;
String failureVOJson = JSON . toJSONString ( failVo1 ) ;
ops . set ( token , failureVOJson , 30 * 60 , TimeUnit . SECONDS ) ;
}
map . put ( "token" , token ) ;
map . put ( "successNum" , ( size - failVo1 . size ( ) ) + "" ) ;
map . put ( "failureNum" , failVo1 . size ( ) + "" ) ;
return map ;
}
@Override
public void exportFailureRecord ( HttpServletResponse response , String token ) {
public void exportFailureRecord ( HttpServletResponse response , String token ) throws Exception {
if ( StringUtils . isEmpty ( token ) ) {
return ;
}
ValueOperations < String , String > ops = stringRedisTemplate . opsForValue ( ) ;
//获取数据
String record = ops . get ( token ) ;
if ( StringUtils . isEmpty ( record ) ) {
return ;
}
List < StudentImportFailureVo > parse = JSON . parseObject ( record , new TypeReference < List < StudentImportFailureVo > > ( ) {
} ) ;
parse . sort ( Comparator . comparing ( StudentImportFailureVo : : getIndex ) ) ;
//2.加载模板流数据
org . springframework . core . io . Resource resource = new ClassPathResource ( "excel-template/学生导入失败数据导出模板.xlsx" ) ;
InputStream inputStream = resource . getInputStream ( ) ;
//3、通过工具类下载文件
new ExcelExportUtil ( StudentImportFailureVo . class , Constant . ROW_INDEX , Constant . STYLE_INDEX ) .
export ( response , inputStream , parse , "学生信息导入失败表.xlsx" ) ;
}
// public HashMap<String, Object> readStudent(Integer schoolId, MultipartFile file) {