@ -22,20 +22,30 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.elink.esua.epdc.commons.mybatis.service.impl.BaseServiceImpl ;
import com.elink.esua.epdc.commons.tools.constant.FieldConstant ;
import com.elink.esua.epdc.commons.tools.page.PageData ;
import com.elink.esua.epdc.commons.tools.security.user.SecurityUser ;
import com.elink.esua.epdc.commons.tools.utils.ConvertUtils ;
import com.elink.esua.epdc.commons.tools.utils.Result ;
import com.elink.esua.epdc.dto.evaluate.EvaluateOfficerDTO ;
import com.elink.esua.epdc.modules.evaluate.dao.EvaluateOfficerDao ;
import com.elink.esua.epdc.modules.evaluate.entity.EvaluateOfficerEntity ;
import com.elink.esua.epdc.modules.evaluate.redis.EvaluateOfficerRedis ;
import com.elink.esua.epdc.modules.evaluate.service.EvaluateOfficerService ;
import org.apache.commons.lang3.StringUtils ;
import org.apache.poi.hssf.usermodel.HSSFCell ;
import org.apache.poi.hssf.usermodel.HSSFWorkbook ;
import org.apache.poi.ss.usermodel.Cell ;
import org.apache.poi.ss.usermodel.Row ;
import org.apache.poi.ss.usermodel.Sheet ;
import org.apache.poi.ss.usermodel.Workbook ;
import org.apache.poi.xssf.usermodel.XSSFWorkbook ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.stereotype.Service ;
import org.springframework.transaction.annotation.Transactional ;
import org.springframework.web.multipart.MultipartFile ;
import java.util.Arrays ;
import java.util.List ;
import java.util.Map ;
import java.io.InputStream ;
import java.text.SimpleDateForma t ;
import java.util.* ;
/ * *
* 干部信息表 干部信息表
@ -46,6 +56,12 @@ import java.util.Map;
@Service
public class EvaluateOfficerServiceImpl extends BaseServiceImpl < EvaluateOfficerDao , EvaluateOfficerEntity > implements EvaluateOfficerService {
private int totalRows = 0 ; // 总行数
private int totalCells = 0 ; // 总条数
public static int Guid = 100 ; //id自增值
@Autowired
private EvaluateOfficerRedis evaluateOfficerRedis ;
@ -73,7 +89,16 @@ public class EvaluateOfficerServiceImpl extends BaseServiceImpl<EvaluateOfficerD
@Override
public List < EvaluateOfficerDTO > list ( Map < String , Object > params ) {
List < EvaluateOfficerEntity > entityList = baseDao . selectList ( getWrapper ( params ) ) ;
for ( int i = 0 ; i < entityList . size ( ) ; i + + ) {
EvaluateOfficerEntity entity = entityList . get ( i ) ;
//转型性别
String sex = entity . getSex ( ) ;
if ( "0" . equals ( sex ) ) {
entity . setSex ( "女" ) ;
} else {
entity . setSex ( "男" ) ;
}
}
return ConvertUtils . sourceToTarget ( entityList , EvaluateOfficerDTO . class ) ;
}
@ -113,4 +138,198 @@ public class EvaluateOfficerServiceImpl extends BaseServiceImpl<EvaluateOfficerD
baseDao . deleteBatchIds ( Arrays . asList ( ids ) ) ;
}
@Override
public Result insertPartyList ( MultipartFile file ) {
//获取excle版本
String isExcel2003 = getExcelInfo ( file ) ;
Result result = new Result ( ) ;
//需存储的实体
List < EvaluateOfficerEntity > list = new ArrayList < EvaluateOfficerEntity > ( ) ;
try {
Workbook wb = null ;
InputStream is = file . getInputStream ( ) ;
if ( "true" . equals ( isExcel2003 ) ) {
// 当excel是2003时,创建excel2003
wb = new HSSFWorkbook ( is ) ;
} else if ( "false" . equals ( isExcel2003 ) ) {
// 当excel是2007时,创建excel2007
wb = new XSSFWorkbook ( is ) ;
} else {
result . setMsg ( "defeat" ) ;
result . setCode ( 1 ) ;
result . setData ( "excle格式错误!" ) ;
return result ;
}
// 得到第一个shell
Sheet sheet = wb . getSheetAt ( 0 ) ;
// 得到Excel的行数
this . totalRows = sheet . getPhysicalNumberOfRows ( ) ;
// 得到Excel的列数(前提是有行数)
if ( totalRows > 1 & & sheet . getRow ( 0 ) ! = null ) {
this . totalCells = sheet . getRow ( 0 ) . getPhysicalNumberOfCells ( ) ;
}
// 循环Excel行数(不要列名,从1开始) ;0是列名
for ( int r = 1 ; r < totalRows ; r + + ) {
Row row = sheet . getRow ( r ) ;
if ( row = = null ) {
continue ;
}
// 循环Excel的列
for ( int c = 0 ; c < 5 ; c + + ) {
EvaluateOfficerEntity evaluateOfficerEntity = new EvaluateOfficerEntity ( ) ;
Cell cell = row . getCell ( c ) ;
if ( null ! = cell ) {
if ( c = = 0 ) {
// 干部姓名
String value = getCellContent ( cell ) ;
evaluateOfficerEntity . setFullName ( value ) ;
} else if ( c = = 1 ) {
// 性别
String value = getCellContent ( cell ) ;
if ( "女" . equals ( value ) ) {
value = "0" ;
} else {
value = "1" ;
}
evaluateOfficerEntity . setSex ( value ) ;
} else if ( c = = 2 ) {
// 单位职务
String value = getCellContent ( cell ) ;
evaluateOfficerEntity . setPosition ( value ) ;
} else if ( c = = 3 ) {
// 点赞次数
String value = getCellContent ( cell ) ;
evaluateOfficerEntity . setLikesCount ( Integer . parseInt ( value ) ) ;
} else if ( c = = 4 ) {
// 被踩次数
String value = getCellContent ( cell ) ;
evaluateOfficerEntity . setOpposeCount ( Integer . parseInt ( value ) ) ;
} else if ( c = = 5 ) {
// 排序
String value = getCellContent ( cell ) ;
evaluateOfficerEntity . setSort ( Integer . parseInt ( value ) ) ;
}
evaluateOfficerEntity . setDeptId ( "11231" ) ;
String userId = SecurityUser . getUserId ( ) = = null ? "" : SecurityUser . getUserId ( ) . toString ( ) ;
;
evaluateOfficerEntity . setId ( getLongRandom ( ) ) ;
evaluateOfficerEntity . setDelFlag ( "0" ) ; //删除标识 0:未删除 1:删除
evaluateOfficerEntity . setRevision ( 0 ) ;
evaluateOfficerEntity . setCreatedBy ( userId ) ;
evaluateOfficerEntity . setUpdatedBy ( userId ) ;
evaluateOfficerEntity . setCreatedTime ( new Date ( ) ) ;
evaluateOfficerEntity . setUpdatedTime ( new Date ( ) ) ;
}
list . add ( evaluateOfficerEntity ) ;
}
}
if ( list . size ( ) > 0 ) {
EvaluateOfficerEntity [ ] evaluateOfficerEntities = new EvaluateOfficerEntity [ list . size ( ) ] ;
for ( int k = 0 ; k < list . size ( ) ; k + + ) {
evaluateOfficerEntities [ k ] = list . get ( k ) ;
}
baseDao . insertList ( evaluateOfficerEntities ) ;
}
result . setMsg ( "success" ) ;
result . setCode ( 0 ) ;
result . setData ( "数据导入成功!" ) ;
return result ;
} catch ( Exception e ) {
}
result . setMsg ( "defeat" ) ;
result . setCode ( 1 ) ;
result . setData ( "数据导入失败!" ) ;
return result ;
}
/ * *
* 获取19位随机数
* @return
* /
public String getLongRandom ( ) {
Guid + = 1 ;
long now = System . currentTimeMillis ( ) ;
SimpleDateFormat dateFormat = new SimpleDateFormat ( "MMdd" ) ;
String time = dateFormat . format ( now ) ;
String currentTimeMillis = now + "" ;
int ran = 0 ;
if ( Guid > 999 ) {
Guid = 100 ;
}
ran = Guid ;
String str = time + currentTimeMillis . substring ( 1 , currentTimeMillis . length ( ) ) + ran ;
return str ;
}
/ * *
* 获取单元格内容
* @param cell
* @return
* /
public String getCellContent ( Cell cell ) {
String value = "" ;
if ( cell . getCellType ( ) = = HSSFCell . CELL_TYPE_NUMERIC ) {
value = String . valueOf ( cell . getNumericCellValue ( ) ) ;
} else {
value = cell . getStringCellValue ( ) ;
}
//内容超过25个字进行长度限制,否则数据库会超限
/ * if ( value . length ( ) > 25 ) {
value = value . substring ( 0 , 25 ) + "..." ;
} * /
return value ;
}
/ * *
* 获取excle版本
* @param mFile
* @return
* /
public String getExcelInfo ( MultipartFile mFile ) {
String fileName = mFile . getOriginalFilename ( ) ; // 获取文件名
String isExcel2003 = "true" ; // 根据文件名判断文件是2003版本还是2007版本
try {
if ( ! validateExcel ( fileName ) ) { // 验证文件名是否合格
return "" ;
}
if ( isExcel2007 ( fileName ) ) {
isExcel2003 = "false" ;
}
return isExcel2003 ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
return "" ;
}
/ * *
* 验证EXCEL文件
* @param filePath
* @return
* /
public boolean validateExcel ( String filePath ) {
if ( filePath = = null | | ! ( isExcel2003 ( filePath ) | | isExcel2007 ( filePath ) ) ) {
return false ;
}
return true ;
}
// @描述:是否是2003的excel,返回true是2003
public static boolean isExcel2003 ( String filePath ) {
return filePath . matches ( "^.+\\.(?i)(xls)$" ) ;
}
// @描述:是否是2007的excel,返回true是2007
public static boolean isExcel2007 ( String filePath ) {
return filePath . matches ( "^.+\\.(?i)(xlsx)$" ) ;
}
}