@ -1,16 +1,21 @@
package com.msdw.tms.service.impl ;
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.core.metadata.IPage ;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page ;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page ;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl ;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl ;
import com.msdw.tms.common.exception.ExceptionCast ;
import com.msdw.tms.common.exception.ExceptionCast ;
import com.msdw.tms.common.utils.* ;
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.StudentDao ;
import com.msdw.tms.dao.UserInfoDao ;
import com.msdw.tms.dao.UserInfoDao ;
import com.msdw.tms.entity.StudentEntity ;
import com.msdw.tms.entity.StudentEntity ;
import com.msdw.tms.entity.UserInfoEntity ;
import com.msdw.tms.entity.UserInfoEntity ;
import com.msdw.tms.entity.XlsxTemplateEntity ;
import com.msdw.tms.entity.XlsxTemplateEntity ;
import com.msdw.tms.entity.response.CommonCode ;
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.entity.vo.StudentVo ;
import com.msdw.tms.service.AliyunOssService ;
import com.msdw.tms.service.AliyunOssService ;
import com.msdw.tms.service.StudentService ;
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.commons.lang.StringUtils ;
import org.apache.poi.ss.formula.functions.T ;
import org.apache.poi.ss.formula.functions.T ;
import org.springframework.beans.factory.annotation.Autowired ;
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.stereotype.Service ;
import org.springframework.transaction.annotation.Transactional ;
import org.springframework.transaction.annotation.Transactional ;
import org.springframework.web.multipart.MultipartFile ;
import org.springframework.web.multipart.MultipartFile ;
@ -25,9 +33,9 @@ import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource ;
import javax.annotation.Resource ;
import javax.servlet.http.HttpServletResponse ;
import javax.servlet.http.HttpServletResponse ;
import java.io.IOException ;
import java.io.IOException ;
import java.util.ArrayList ;
import java.io.InputStream ;
import java.util.HashMap ;
import java.util.* ;
import java.util.Lis t ;
import java.util.concurrent.TimeUni t ;
@Service ( "studentService" )
@Service ( "studentService" )
public class StudentServiceImpl extends ServiceImpl < StudentDao , StudentEntity > implements StudentService {
public class StudentServiceImpl extends ServiceImpl < StudentDao , StudentEntity > implements StudentService {
@ -44,6 +52,9 @@ public class StudentServiceImpl extends ServiceImpl<StudentDao,StudentEntity> im
@Autowired
@Autowired
private UserInfoDao userInfoDao ;
private UserInfoDao userInfoDao ;
@Resource
StringRedisTemplate stringRedisTemplate ;
@Override
@Override
public Integer queryStudentIdNumber ( String workNumber , Integer schoolId ) {
public Integer queryStudentIdNumber ( String workNumber , Integer schoolId ) {
List < Integer > result = studentDao . queryStudentIdNumber ( workNumber , schoolId ) ;
List < Integer > result = studentDao . queryStudentIdNumber ( workNumber , schoolId ) ;
@ -123,30 +134,40 @@ public class StudentServiceImpl extends ServiceImpl<StudentDao,StudentEntity> im
@Transactional
@Transactional
@Override
@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 < StudentVo > students = ExcelImportHelper . readStudent ( file ) ;
List < StudentImportFailureVo > failVo1 = new ArrayList < > ( ) ;
// 参数合法性校验,只能上传.xlsx后缀的文件
// 参数合法性校验,只能上传.xlsx后缀的文件
if ( StringUtils . isBlank ( file . getOriginalFilename ( ) )
if ( StringUtils . isBlank ( file . getOriginalFilename ( ) )
| | ! file . getOriginalFilename ( ) . substring ( file . getOriginalFilename ( ) . lastIndexOf ( "." ) ) . equals ( Constant . EXCEL_SUFFIX ) ) {
| | ! file . getOriginalFilename ( ) . substring ( file . getOriginalFilename ( ) . lastIndexOf ( "." ) ) . equals ( Constant . EXCEL_SUFFIX ) ) {
ExceptionCast . cast ( CommonCode . EXCEL_FILE_INVALID ) ;
ExceptionCast . cast ( CommonCode . EXCEL_FILE_INVALID ) ;
}
}
int size = students . size ( ) ;
HashMap < String , Object > map = new HashMap < > ( ) ;
Map < String , String > map = new HashMap < > ( ) ;
Long ii = 1L ; //用于记录序列号
try {
try {
for ( int i = 0 ; i < students . size ( ) ; i + + ) {
for ( int i = 0 ; i < students . size ( ) ; i + + ) {
StudentVo student = students . get ( i ) ;
StudentVo student = students . get ( i ) ;
String phone = student . getPhone ( ) ;
String phone = student . getPhone ( ) ;
String workNumber = student . getWorkNumber ( ) ;
String workNumber = student . getWorkNumber ( ) ;
String account = student . getAccount ( ) ;
StudentVo studentVo = new StudentVo ( ) ;
StudentVo studentVo = new StudentVo ( ) ;
String schoolAppellationName = student . getSchoolAppellationName ( ) ;
String schoolAppellationName = student . getSchoolAppellationName ( ) ;
List < Integer > result1 = studentDao . queryStudentIdNumber ( workNumber , schoolId ) ;
List < Integer > result1 = studentDao . queryStudentIdNumber ( workNumber , schoolId ) ;
List < StudentEntity > result4 = userInfoDao . queryStudentAccount ( account , schoolId ) ;
String email = student . getEmail ( ) ;
String email = student . getEmail ( ) ;
if ( phone ! = null & & phone ! = "" ) {
if ( phone ! = null & & phone ! = "" ) {
List < StudentEntity > result2 = userInfoDao . queryStudentPhone ( phone , schoolId ) ;
List < StudentEntity > result2 = userInfoDao . queryStudentPhone ( phone , schoolId ) ;
if ( result2 . size ( ) = = 1 ) {
if ( result2 . size ( ) = = 1 ) {
log . error ( "该号码已被使用" ) ;
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 ) ;
students . remove ( i ) ;
i - - ;
i - - ;
continue ;
continue ;
@ -166,17 +187,45 @@ public class StudentServiceImpl extends ServiceImpl<StudentDao,StudentEntity> im
if ( result1 . size ( ) = = 1 ) {
if ( result1 . size ( ) = = 1 ) {
log . error ( "该学号已存在" ) ;
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 ) ;
students . remove ( i ) ;
i - - ;
i - - ;
continue ;
continue ;
} else if ( result3 . size ( ) ! = 1 ) {
} 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 ) {
log . error ( "不存在该院校" ) ;
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 ) ;
students . remove ( i ) ;
i - - ;
i - - ;
continue ;
continue ;
}
}
+ + ii ;
if ( ! students . isEmpty ( ) & & students . size ( ) > 0 ) {
if ( ! students . isEmpty ( ) & & students . size ( ) > 0 ) {
userInfoDao . batchSaveUserInfo ( studentVo ) ;
userInfoDao . batchSaveUserInfo ( studentVo ) ;
student . setUserId ( studentVo . getUserId ( ) ) . setIsdel ( Constant . IsDel . NOT_DEL . getType ( ) ) . setPlatformId ( ConstantUtils . PLATFORMID ) ;
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 ( ) ;
throw new RuntimeException ( ) ;
}
}
}
}
}
}
} catch ( RuntimeException e ) {
} catch ( RuntimeException e ) {
log . error ( e . getMessage ( ) ) ;
log . error ( e . getMessage ( ) ) ;
map . put ( "failura" , "添加失败" ) ;
throw new RuntimeException ( ) ;
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 ;
return map ;
}
}
@Override
@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) {
// public HashMap<String, Object> readStudent(Integer schoolId, MultipartFile file) {