@ -1,11 +1,17 @@
@@ -1,11 +1,17 @@
package apelet.tenantadmin.upms.controller ;
import apelet.common.core.exception.MyRuntimeException ;
import apelet.common.core.object.ObjectCollection ;
import apelet.common.core.object.ObjectValue ;
import apelet.common.core.object.TokenData ;
import apelet.common.core.object.ResponseResult ;
import apelet.common.generator.utils.OrmGenDataSourceUtil ;
import apelet.common.online.service.OnlineDictService ;
import apelet.common.orm.impl.Filter ;
import apelet.common.orm.impl.Selector ;
import cn.hutool.core.date.DateUtil ;
import cn.hutool.core.io.IoUtil ;
import cn.hutool.poi.excel.ExcelReader ;
import cn.hutool.poi.excel.ExcelUtil ;
import com.alibaba.fastjson.JSON ;
import com.alibaba.fastjson.JSONArray ;
import com.alibaba.fastjson.JSONObject ;
@ -14,9 +20,12 @@ import org.apache.commons.lang3.StringUtils;
@@ -14,9 +20,12 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException ;
import org.apache.poi.xwpf.usermodel.* ;
import org.docx4j.Docx4J ;
import org.docx4j.fonts.IdentityPlusMapper ;
import org.docx4j.fonts.PhysicalFonts ;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.web.bind.annotation.* ;
import org.springframework.web.multipart.MultipartFile ;
import javax.servlet.http.HttpServletResponse ;
import java.io.* ;
@ -25,11 +34,10 @@ import java.net.URL;
@@ -25,11 +34,10 @@ import java.net.URL;
import java.net.URLEncoder ;
import java.nio.charset.StandardCharsets ;
import java.nio.file.Files ;
import java.util.HashMap ;
import java.util.List ;
import java.util.Map ;
import java.util.* ;
import java.util.regex.Matcher ;
import java.util.regex.Pattern ;
import java.util.stream.Collectors ;
@RestController
@RequestMapping ( "/api/template" )
@ -76,7 +84,7 @@ public class DocxTemplateController {
@@ -76,7 +84,7 @@ public class DocxTemplateController {
hashMap . put ( "name" , objectValue . getString ( "name" ) ) ;
hashMap . put ( "type" , dictMap . get ( String . valueOf ( objectValue . get ( "type" , "" ) ) ) ) ;
hashMap . put ( "number" , objectValue . getString ( "number" ) ) ;
hashMap . put ( "create_time" , objectValue . getString ( "create_time" ) ) ;
hashMap . put ( "create_time" , objectValue . getString ( "create_time" ) . substring ( 0 , 10 ) ) ;
hashMap . put ( "qty" , objectValue . getString ( "qty" ) ) ;
String signatureImage = user . getString ( "signature_image" ) ;
@ -108,6 +116,8 @@ public class DocxTemplateController {
@@ -108,6 +116,8 @@ public class DocxTemplateController {
docxOut . close ( ) ;
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage . load ( new ByteArrayInputStream ( docxBytes ) ) ;
PhysicalFonts . discoverPhysicalFonts ( ) ;
wordMLPackage . setFontMapper ( new IdentityPlusMapper ( ) ) ;
pdfOut = new ByteArrayOutputStream ( ) ;
Docx4J . toPDF ( wordMLPackage , pdfOut ) ;
byte [ ] pdfBytes = pdfOut . toByteArray ( ) ;
@ -208,7 +218,6 @@ public class DocxTemplateController {
@@ -208,7 +218,6 @@ public class DocxTemplateController {
}
private void replaceImageInParagraphs ( XWPFDocument document , String placeholderKey , byte [ ] imageBytes , int width , int height ) throws IOException , InvalidFormatException {
String placeholder = "#{" + placeholderKey + "}" ;
for ( XWPFParagraph paragraph : document . getParagraphs ( ) ) {
@ -257,4 +266,88 @@ public class DocxTemplateController {
@@ -257,4 +266,88 @@ public class DocxTemplateController {
}
}
private static final Map < String , String > HEADER_MAP = new LinkedHashMap < > ( ) ;
static {
HEADER_MAP . put ( "名称" , "name" ) ;
HEADER_MAP . put ( "性别" , "gender" ) ;
HEADER_MAP . put ( "手机号码" , "phone" ) ;
HEADER_MAP . put ( "出生年月" , "birth_date" ) ;
HEADER_MAP . put ( "学历" , "education" ) ;
HEADER_MAP . put ( "民族" , "nation" ) ;
HEADER_MAP . put ( "政治面貌" , "political_status" ) ;
HEADER_MAP . put ( "身份证号码" , "id_card" ) ;
HEADER_MAP . put ( "毕业院校" , "graduate_school" ) ;
HEADER_MAP . put ( "所属专业" , "major" ) ;
HEADER_MAP . put ( "常住地" , "permanent_address" ) ;
}
@PostMapping ( "/expertImport" )
public ResponseResult < ? > expertImport ( @RequestParam ( "file" ) MultipartFile file ) {
List < Map < String , Object > > dataList = new ArrayList < > ( ) ;
try ( InputStream is = file . getInputStream ( ) ) {
ExcelReader reader = ExcelUtil . getReader ( is ) ;
// 第一行作为表头,key 为表头名称,自动兼容 xls/xlsx
List < Map < String , Object > > rawList = reader . readAll ( ) ;
//学历
Map < String , String > educationDictMap = onlineDictService . getDictMap ( 2054094099727781888L ) ;
educationDictMap = educationDictMap . entrySet ( ) . stream ( )
. collect ( Collectors . toMap ( Map . Entry : : getValue , Map . Entry : : getKey ) ) ;
//政治面貌
Map < String , String > politicalDictMap = onlineDictService . getDictMap ( 2054098587905691648L ) ;
politicalDictMap = politicalDictMap . entrySet ( ) . stream ( )
. collect ( Collectors . toMap ( Map . Entry : : getValue , Map . Entry : : getKey ) ) ;
for ( Map < String , Object > row : rawList ) {
Map < String , Object > dataMap = new LinkedHashMap < > ( ) ;
for ( Map . Entry < String , Object > entry : row . entrySet ( ) ) {
String header = entry . getKey ( ) = = null ? "" : entry . getKey ( ) . trim ( ) ;
// 中文表头转英文,映射中没有的表头保留原值
String key = HEADER_MAP . getOrDefault ( header , header ) ;
Object value = entry . getValue ( ) ;
dataMap . put ( key , value ) ;
if ( key . equals ( "gender" ) ) {
dataMap . put ( "gender" , value . equals ( "男" ) ? "1" : "0" ) ;
}
if ( key . equals ( "education" ) ) {
dataMap . put ( "education" , educationDictMap . get ( value . toString ( ) ) ) ;
}
if ( key . equals ( "political_status" ) ) {
dataMap . put ( "political_status" , politicalDictMap . get ( value . toString ( ) ) ) ;
}
if ( "birth_date" . equals ( key ) ) {
if ( value instanceof Date ) {
value = DateUtil . formatDateTime ( ( Date ) value ) ;
} else if ( value instanceof String & & StringUtils . isNotBlank ( ( String ) value ) ) {
value = DateUtil . formatDateTime ( DateUtil . parse ( ( String ) value ) ) ;
}
dataMap . put ( key , value ) ;
}
}
dataMap . put ( "billstatus" , "A" ) ;
dataMap . put ( "apply_time" , DateUtil . formatDateTime ( new Date ( ) ) ) ;
dataList . add ( dataMap ) ;
}
ObjectCollection expertManage = ormGenDataSourceUtil . query ( "expert_manage" , new Filter ( ) , new Selector ( ) ) ;
dataList . forEach ( dataMap - > {
long count = expertManage . stream ( ) . filter ( f - > f . get ( "id_card" ) . equals ( dataMap . get ( "id_card" ) ) ) . count ( ) ;
if ( count > 0 ) {
return ;
}
ObjectValue value = new ObjectValue ( "expert_manage" ) ;
value . setValues ( dataMap ) ;
try {
ormGenDataSourceUtil . addNew ( "expert_manage" , value ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
} ) ;
} catch ( IOException e ) {
throw new MyRuntimeException ( "文件解析失败: " + e . getMessage ( ) ) ;
}
return ResponseResult . success ( "导入成功" ) ;
}
}