@ -4,6 +4,17 @@ import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper ;
import com.baomidou.mybatisplus.core.toolkit.Wrappers ;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page ;
import com.itextpdf.io.font.PdfEncodings ;
import com.itextpdf.io.image.ImageData ;
import com.itextpdf.io.image.ImageDataFactory ;
import com.itextpdf.kernel.colors.DeviceRgb ;
import com.itextpdf.kernel.font.PdfFont ;
import com.itextpdf.kernel.font.PdfFontFactory ;
import com.itextpdf.kernel.geom.PageSize ;
import com.itextpdf.kernel.pdf.PdfDocument ;
import com.itextpdf.kernel.pdf.PdfWriter ;
import com.itextpdf.layout.Document ;
import com.itextpdf.layout.element.Image ;
import com.ruoyi.bgt.domain.vo.BgtQuestionResult ;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl ;
import com.ruoyi.common.core.page.TableDataInfo ;
@ -23,20 +34,23 @@ import com.ruoyi.wgz.service.IWgzQuestionBankService;
import com.ruoyi.wgz.service.IWgzQuestionSaveService ;
import com.ruoyi.wgz.service.IWgzQuestionsConfigurationService ;
import com.ruoyi.wgz.service.IWgzUserService ;
import com.sun.scenario.effect.ImageData ;
import org.apache.poi.ss.usermodel.HorizontalAlignment ;
import org.apache.poi.wp.usermodel.Paragraph ;
import com.itextpdf.layout.properties.TextAlignment ;
import com.itextpdf.layout.element.Paragraph ;
import org.aspectj.weaver.patterns.TypePatternQuestions ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.stereotype.Service ;
import org.springframework.transaction.annotation.Transactional ;
import org.springframework.util.StringUtils ;
import java.io.File ;
import java.io.IOException ;
import java.nio.file.Paths ;
import java.util.ArrayList ;
import java.util.Collection ;
import java.util.List ;
import java.util.Map ;
import java.util.regex.Matcher ;
import java.util.regex.Pattern ;
/**
* 用户试卷存储Service业务层处理
@ -209,13 +223,15 @@ public class WgzQuestionSaveServiceImpl extends ServicePlusImpl<WgzQuestionSaveM
/**
* 第二部分:分离出试卷信息
*/
// String two = two(req.getUserId(), configuration);
// System.out.println("????????? "+two);
// System.out.println("????????? "+two);
// System.out.println("????????? "+two);
return res ;
}
// 分离试卷信息
public void two ( Long userId , WgzQuestionsConfiguration configurationEntity ) throws IOException {
public String two ( Long userId , WgzQuestionsConfiguration configurationEntity ) {
//1、组装数据
List < PdfEntity > we = baseMapper . pdfSc ( userId ) ;
if ( ! we . isEmpty ( ) ) {
@ -262,69 +278,106 @@ public class WgzQuestionSaveServiceImpl extends ServicePlusImpl<WgzQuestionSaveM
rs . setMultiple ( two ) ;
rs . setEstimate ( three ) ;
//2、生成pdf
// generatePdf(rs);
try {
return generateExamPaper ( rs ) ;
} catch ( Exception e1 ) {
throw new RuntimeException ( " 生成PDF试卷出现了意外, 请重新提交! " ) ;
}
}
// if len(we) > 0 {
// value, _ := dao.BusQuestionSave.Ctx(ctx).Where("openid", req.Openid).Where("correct", "1").Fields("sum(score)").Value()
// s := configurationEntity.SingleChoice
// m := configurationEntity.MultipleChoice
// e := configurationEntity.Estimate
// var rs *model.ExaminationPaperRes
// rs = &model.ExaminationPaperRes{
// UserName: we[0].UserName,
// Pass: we[0].Pass,
// SumScore: value.String(),
// Sign: signature.Signature,
// Openid: req.Openid,
// }
// var one = new(model.Examinati onPaperOne)
// var two = new(model.Examinati onPaperOne)
// var three = new(model.ExaminationPaperOne)
// one.Topic = "一、单选题,共" + strconv.Itoa(s) + "道题,每小题" + strconv.FormatFloat(configurationEntity.SingleScore, 'f', -1, 64) + "分,共计" + strconv.FormatFloat(float64(s)*configurationEntity.SingleScore, 'f', -1, 64) + "分"
// two.Topic = "二、多选题,共" + strconv.Itoa(m) + "道题,每小题" + strconv.FormatFloat(configurationEntity.MultipleScore, 'f', -1, 64) + "分,共计" + strconv.FormatFloat(float64(m)*configurationEntity.MultipleScore, 'f', -1, 64) + "分"
// three.Topic = "三、判断题,共" + strconv.Itoa(e) + "道题,每小题" + strconv.FormatFloat(configurationEntity.EstimateScore, 'f', -1, 64) + "分,共计" + strconv.FormatFloat(float64(e)*configurationEntity.EstimateScore, 'f', -1, 64) + "分"
// var sEntity []*model.ExaminationPaperTwo
// var mEntity []*model.ExaminationPaperTwo
// var eEntity []*model.ExaminationPaperTwo
// for _, data := range we {
// var sy *model.ExaminationPaperTwo
// sy = &model.ExaminationPaperTwo{
// QuestionText: data.QuestionText,
// Options: data.Options,
// Answer: data.Answer,
// CorrectAnswer: data.CorrectAnswer,
// Correct: data.Correct,
// Score: data.Score,
// }
// if data.QuestionType == "1" {
// sEntity = append(sEntity, sy)
// } else if data.QuestionType == "2" {
// mEntity = append(mEntity, sy)
// } else if data.QuestionType == "3" {
// eEntity = append(eEntity, sy)
// }
// }
// one.List = sEntity
// two.List = mEntity
// three.List = eEntity
// rs.Single = one
// rs.Multiple = two
// rs.Estimate = three
// path, errPath := Wo(rs)
// if errPath != nil {
// errPath = errors.New("生成PDF试卷出现了意外, 请重新提交! ")
// liberr.ErrIsNil(ctx, errPath)
// return
// } else {
// path = strings.Replace(path, "resource/public", "/wxfile", 1)
// _, err = g.DB().Insert(ctx, "bus_question_save_pdf", gdb.Map{
// "openid": req.Openid,
// "path": path,
// })
// res.PdfStr = path
throw new RuntimeException ( " 未获取到试卷信息! " ) ;
}
public String generateExamPaper ( ExaminationPaper rs ) throws IOException {
String fileName = " exam_paper_ " + System . currentTimeMillis ( ) + " .pdf " ;
String outputPath = Paths . get ( System . getProperty ( " user.dir " ) , fileName ) . toString ( ) ;
PdfWriter writer = new PdfWriter ( outputPath ) ;
PdfDocument pdfDoc = new PdfDocument ( writer ) ;
Document document = new Document ( pdfDoc , PageSize . A4 ) ;
// 加载字体(确保 simhei.ttf 可用)
String fontPath = " ruoyi/uploadPath/appResource/font/simhei.ttf " ;
PdfFont simhei = PdfFontFactory . createFont ( f ont Path , PdfEncodings . IDENTITY_H ) ;
PdfFont simheiBold = PdfFontFactory . createFont ( f ont Path , PdfEncodings . IDENTITY_H ) ;
// 标题
document . setFont ( simheiBold ) . setFontSize ( 36 ) ;
document . add ( new Paragraph ( " 《灵活用工岗前培训》 " )
. setTextAlignment ( TextAlignment . CENTER ) ) ;
document . add ( new Paragraph ( " \ n " ) ) ;
// 考生信息
document . setFont ( simhei ) . setFontSize ( 16 ) ;
document . add ( new Paragraph ( " 姓名: " + rs . getUserName ( ) ) ) ;
document . add ( new Paragraph ( " 及格线/总分: " + rs . getPass ( ) ) ) ;
document . add ( new Paragraph ( " 实际得分: " + rs . getSumScore ( ) ) ) ;
document . add ( new Paragraph ( " \ n " ) ) ;
// 处理单选、多选、判断题
for ( int i = 1 ; i < = 3 ; i + + ) {
ExaminationPaperOne ep ;
if ( i = = 1 ) {
ep = rs . getSingle ( ) ;
} else if ( i = = 2 ) {
ep = rs . getMultiple ( ) ;
} else {
ep = rs . getEstimate ( ) ;
}
document . setFont ( simhei ) . setFontSize ( 20 ) ;
document . add ( new Paragraph ( ep . getTopic ( ) ) ) ;
document . setFont ( simhei ) . setFontSize ( 12 ) ;
List < ExaminationPaperTwo > questionList = ep . getList ( ) ;
for ( int qIndex = 0 ; qIndex < questionList . size ( ) ; qIndex + + ) {
ExaminationPaperTwo data = questionList . get ( qIndex ) ;
String questionText = ( i = = 3 ) ?
( qIndex + 1 ) + " 、 " + data . getQuestionText ( ) + " ( " + data . getAnswer ( ) + " ) "
: ( qIndex + 1 ) + " 、 " + replaceBrackets ( data . getQuestionText ( ) , data . getAnswer ( ) ) ;
document . add ( new Paragraph ( questionText ) ) ;
// 选项
String [ ] options = data . getOptions ( ) . split ( " @ " ) ;
for ( String option : options ) {
document . add ( new Paragraph ( " " + option ) ) ;
}
// 标记错误答案
if ( " 2 " . equals ( data . getCorrect ( ) ) ) {
document . add ( new Paragraph ( " 正确答案为: " + data . getCorrectAnswer ( ) )
. setFontColor ( com . itextpdf . kernel . colors . ColorConstants . RED ) ) ;
}
document . add ( new Paragraph ( " \ n " ) ) ;
}
}
// // 签名部分
// document.add(new Paragraph("\n\n\n"));
// document.setFont(simheiBold).setFontSize(16);
// document.add(new Paragraph("签字:").setTextAlignment(TextAlignment.RIGHT));
// // 嵌入签名图片
// if (rs.getSign() != null && !rs.getSign().isEmpty()) {
// String signPath = Paths.get(System.getProperty("user.dir"), rs.getSign()).toString();
// File signFile = new File(signPath);
// if (signFile.exists()) {
// ImageData imageData = ImageDataFactory.create(signPath);
// Image image = new Image(imageData).scaleToFit(100, 50);
// document.add(image.setRotationAngle(Math.PI / 2));
// }
// }
// liberr.ErrIsNil(ctx, err, "添加失败")
document . close ( ) ;
return outputPath ;
}
private static String replaceBrackets ( String text , String replacement ) {
Pattern pattern = Pattern . compile ( " \\ s*( \\ s*) \\ s* " ) ;
Matcher matcher = pattern . matcher ( text ) ;
return matcher . replaceAll ( " ( " + replacement + " ) " ) ;
}
@Override
@ -402,4 +455,5 @@ public class WgzQuestionSaveServiceImpl extends ServicePlusImpl<WgzQuestionSaveM
bgtQuestionResult . setTime ( time ) ;
return bgtQuestionResult ;
}
}