@ -60,492 +60,454 @@ import java.util.regex.Pattern;
*/
@Service
public class WgzQuestionSaveServiceImpl extends ServiceImpl < WgzQuestionSaveMapper , WgzQuestionSave > implements IWgzQuestionSaveService {
@Autowired
private IWgzQuestionsConfigurationService iWgzQuestionsConfigurationService ;
@Autowired
private IWgzQuestionBankService iWgzQuestionBankService ;
@Autowired
private IWgzQuestionSavePdfService wgzQuestionSavePdfService ;
@Resource
private ISysOssService ossService ;
@Override
public WgzQuestionSave queryById ( Long id ) {
return null ;
return getById ( id ) ;
}
@Override
public TableDataInfo < WgzQuestionSaveVo > queryPageList ( WgzQuestionSaveBo bo , PageQuery pageQuery ) {
return null ;
LambdaQueryWrapper < WgzQuestionSave > lqw = buildQueryWrapper ( bo ) ;
Page < WgzQuestionSaveVo > result = baseMapper . selectVoPage ( pageQuery . build ( ) , lqw ) ;
return TableDataInfo . build ( result ) ;
}
@Override
public List < WgzQuestionSaveVo > queryList ( WgzQuestionSaveBo bo ) {
return List . of ( ) ;
LambdaQueryWrapper < WgzQuestionSave > lqw = buildQueryWrapper ( bo ) ;
return baseMapper . selectVoList ( lqw ) ;
}
private LambdaQueryWrapper < WgzQuestionSave > buildQueryWrapper ( WgzQuestionSaveBo bo ) {
Map < String , Object > params = bo . getParams ( ) ;
LambdaQueryWrapper < WgzQuestionSave > lqw = Wrappers . lambdaQuery ( ) ;
lqw . orderByDesc ( WgzQuestionSave : : getId ) ;
lqw . eq ( bo . getUserId ( ) ! = null , WgzQuestionSave : : getUserId , bo . getUserId ( ) ) ;
lqw . eq ( bo . getBankId ( ) ! = null , WgzQuestionSave : : getBankId , bo . getBankId ( ) ) ;
lqw . eq ( StringUtils . isNotBlank ( bo . getAnswer ( ) ) , WgzQuestionSave : : getAnswer , bo . getAnswer ( ) ) ;
lqw . eq ( StringUtils . isNotBlank ( bo . getCorrect ( ) ) , WgzQuestionSave : : getCorrect , bo . getCorrect ( ) ) ;
lqw . eq ( bo . getScore ( ) ! = null , WgzQuestionSave : : getScore , bo . getScore ( ) ) ;
lqw . eq ( StringUtils . isNotBlank ( bo . getSign ( ) ) , WgzQuestionSave : : getSign , bo . getSign ( ) ) ;
lqw . eq ( bo . getTakeTime ( ) ! = null , WgzQuestionSave : : getTakeTime , bo . getTakeTime ( ) ) ;
lqw . eq ( bo . getTimeOut ( ) ! = null , WgzQuestionSave : : getTimeOut , bo . getTimeOut ( ) ) ;
lqw . eq ( StringUtils . isNotBlank ( bo . getPass ( ) ) , WgzQuestionSave : : getPass , bo . getPass ( ) ) ;
return lqw ;
}
@Override
public Boolean insert ( WgzQuestionSave bo ) {
return null ;
WgzQuestionSave add = BeanUtil . toBean ( bo , WgzQuestionSave . class ) ;
validEntityBeforeSave ( add ) ;
return save ( add ) ;
}
@Override
public Boolean update ( WgzQuestionSave bo ) {
return null ;
WgzQuestionSave update = BeanUtil . toBean ( bo , WgzQuestionSave . class ) ;
validEntityBeforeSave ( update ) ;
return updateById ( update ) ;
}
/**
* 保存前的数据校验
*
* @param entity 实体类数据
*/
private void validEntityBeforeSave ( WgzQuestionSave entity ) {
//TODO 做一些数据校验,如唯一约束
}
@Override
public Boolean deleteWithValidByIds ( Collection < Long > ids , Boolean isValid ) {
return null ;
if ( isValid ) {
//TODO 做一些业务上的校验,判断是否需要校验
}
return removeByIds ( ids ) ;
}
/**
* APP相关
* =================================================================================================================
* =================================================================================================================
* =================================================================================================================
*/
@Override
@Transactional ( rollbackFor = Exception . class )
public WgzAppSubmitATestPaperRes userSubmitATestPaper ( WgzAppSubmitATestPaperReq req ) {
return null ;
String coryPass = " " ;
int coryTimeOut = 0 ;
Long coryTakeTime = 0L ;
Double corySumScore = 0 . 00 ;
/**
* 第一部分:计算并保存最高分试卷
*/
// 初始化第一次分数和当前考试的分数
double scoreOne = 0 . 00 ;
double scoreTwo = 0 . 00 ;
//1、获取上次考试的分数
Map < String , Object > spMap = baseMapper . getSumScoreAndPassAndSign ( req . getUserId ( ) ) ;
if ( spMap ! = null ) {
String pass = ( String ) spMap . get ( " pass " ) ;
if ( pass ! = null & & ! pass . isEmpty ( ) ) {
String [ ] split = pass . split ( " , " ) ;
double float1 = Double . parseDouble ( split [ 1 ] ) ;
double sumScore = ( double ) spMap . get ( " sumScore " ) ;
scoreOne = 100 . 00 / float1 * sumScore ;
}
}
//2、获取配置信息
WgzQuestionsConfiguration configuration = iWgzQuestionsConfigurationService . appQueryLimitOne ( ) ;
//3、计算这次用户提交的试卷信息所得到的分数
List < WgzQuestionSave > bqs = new ArrayList < > ( ) ;
Double fenshu = 0 . 0 ;
Integer number = 0 ;
Integer index = 0 ;
List < WgzAppSubmitATestPaperTwo > list = req . getList ( ) ;
for ( WgzAppSubmitATestPaperTwo data : list ) {
String correct = " " ;
String pass = " " ;
// 获取题库的正确答案,然后和用户的答案作对比
WgzQuestionBank bqb = iWgzQuestionBankService . selectById ( data . getBankId ( ) ) ;
if ( bqb = = null ) {
throw new RuntimeException ( " 您的试卷被外星人卷走了! " ) ;
} else {
if ( StringUtils . isEmpty ( data . getAnswer ( ) ) ) {
throw new RuntimeException ( " 您还有题未答完! " ) ;
}
if ( bqb . getCorrectAnswer ( ) . toLowerCase ( ) . contains ( data . getAnswer ( ) . toLowerCase ( ) ) ) {
correct = " 1 " ;
fenshu = fenshu + data . getScore ( ) ;
number = number + 1 ;
} else {
correct = " 2 " ;
}
}
// 记录这次的及格线和总分
pass = configuration . getPassingScore ( ) . toString ( ) + " , " + configuration . getFullMark ( ) . toString ( ) ;
WgzQuestionSave bqsTwo = new WgzQuestionSave ( ) ;
bqsTwo . setUserId ( req . getUserId ( ) ) ;
bqsTwo . setBankId ( data . getBankId ( ) ) ;
bqsTwo . setAnswer ( data . getAnswer ( ) ) ;
bqsTwo . setCorrect ( correct ) ;
bqsTwo . setScore ( data . getScore ( ) ) ;
//这几条数据只在每张试卷的第一条数据中存储(重复的没必要每条都存储)
if ( index = = 0 ) {
bqsTwo . setTakeTime ( req . getTakeTime ( ) ) ;
bqsTwo . setTimeOut ( configuration . getAnswerTime ( ) ) ;
bqsTwo . setPass ( pass ) ;
coryTakeTime = req . getTakeTime ( ) ;
coryTimeOut = configuration . getAnswerTime ( ) ;
coryPass = pass ;
}
bqs . add ( bqsTwo ) ;
index = index + 1 ;
}
Double fullMark = configuration . getFullMark ( ) ;
scoreTwo = 100 . 00 / fullMark * fenshu ;
WgzAppSubmitATestPaperRes res = new WgzAppSubmitATestPaperRes ( ) ;
res . setAnswerTime ( configuration . getAnswerTime ( ) ) ;
res . setTakeTime ( req . getTakeTime ( ) ) ;
res . setFullMark ( fullMark ) ;
res . setPassingScore ( configuration . getPassingScore ( ) ) ;
res . setScore ( fenshu ) ;
res . setNumber ( number ) ;
//4、两次卷子的分数对比,第一次的分数比第二次大那么第二次只返回结果;第二次的分数比第一次大那么删除第一次的数据再重新插入
if ( scoreOne > scoreTwo ) {
return res ;
} else {
corySumScore = scoreTwo ;
if ( scoreOne = = 0 ) {
// 插入新数据
super . saveBatch ( bqs ) ;
}
// 先查询当前用户是否有答题,有就把之前的删除(真删)
int deleteResult = baseMapper . delete ( new LambdaQueryWrapper < WgzQuestionSave > ( ) . eq ( WgzQuestionSave : : getUserId , req . getUserId ( ) ) ) ;
if ( deleteResult > 0 ) {
// 删除pdf
wgzQuestionSavePdfService . deleteByUserId ( req . getUserId ( ) ) ;
// 插入新数据
super . saveBatch ( bqs ) ;
}
}
/**
* 第二部分:分离出试卷信息
*/
ExaminationPaper examinationPaper = separateTestInformation ( req . getUserId ( ) , configuration ) ;
/**
* 第三部分: 生成pdf并存储到数据库中
*/
try {
String xdPath = generateExamPaper ( examinationPaper ) ;
res . setPdfStr ( xdPath ) ;
WgzQuestionSavePdf wgzQuestionSavePdf = new WgzQuestionSavePdf ( )
. setType ( " 1 " )
. setUserId ( req . getUserId ( ) )
. setPath ( xdPath )
. setPass ( coryPass )
. setTimeOut ( coryTimeOut )
. setTakeTime ( coryTakeTime )
. setSumScore ( corySumScore ) ;
wgzQuestionSavePdfService . insert ( wgzQuestionSavePdf ) ;
} catch ( Exception e1 ) {
throw new RuntimeException ( " 生成PDF试卷出现了意外, 请重新提交! " ) ;
}
return res ;
}
// 分离试卷信息
private ExaminationPaper separateTestInformation ( Long userId , WgzQuestionsConfiguration configurationEntity ) {
//1、组装数据
List < PdfEntity > we = baseMapper . pdfSc ( userId ) ;
if ( ! we . isEmpty ( ) ) {
String sumScore = baseMapper . pdfSumScore ( userId ) ;
Integer s = configurationEntity . getSingleChoice ( ) ;
Integer m = configurationEntity . getMultipleChoice ( ) ;
Integer e = configurationEntity . getEstimate ( ) ;
ExaminationPaper rs = new ExaminationPaper ( )
. setPass ( we . get ( 0 ) . getPass ( ) )
. setSumScore ( sumScore )
. setSign ( we . get ( 0 ) . getSign ( ) )
. setUserId ( userId . toString ( ) )
. setUserName ( we . get ( 0 ) . getUserName ( ) ) ;
ExaminationPaperOne one = new ExaminationPaperOne ( ) ;
ExaminationPaperOne two = new ExaminationPaperOne ( ) ;
ExaminationPaperOne three = new ExaminationPaperOne ( ) ;
one . setTopic ( " 一、单选题,共 " + s + " 道题,每小题 " + configurationEntity . getSingleScore ( ) + " 分,共计 " + configurationEntity . getSingleScore ( ) * s + " 分 " ) ;
two . setTopic ( " 二、多选题,共 " + m + " 道题,每小题 " + configurationEntity . getMultipleScore ( ) + " 分,共计 " + configurationEntity . getMultipleScore ( ) * m + " 分 " ) ;
three . setTopic ( " 三、判断题,共 " + e + " 道题,每小题 " + configurationEntity . getEstimateScore ( ) + " 分,共计 " + configurationEntity . getEstimateScore ( ) * e + " 分 " ) ;
List < ExaminationPaperTwo > sEntity = new ArrayList < > ( ) ;
List < ExaminationPaperTwo > mEntity = new ArrayList < > ( ) ;
List < ExaminationPaperTwo > eEntity = new ArrayList < > ( ) ;
for ( PdfEntity data : we ) {
ExaminationPaperTwo sy = new ExaminationPaperTwo ( ) ;
sy . setQuestionText ( data . getQuestionText ( ) ) ;
sy . setOptions ( data . getOptions ( ) ) ;
sy . setAnswer ( data . getAnswer ( ) ) ;
sy . setCorrectAnswer ( data . getCorrectAnswer ( ) ) ;
sy . setCorrect ( data . getCorrect ( ) ) ;
sy . setScore ( data . getScore ( ) ) ;
if ( data . getQuestionType ( ) . equals ( " 1 " ) ) {
sEntity . add ( sy ) ;
}
if ( data . getQuestionType ( ) . equals ( " 2 " ) ) {
mEntity . add ( sy ) ;
}
if ( data . getQuestionType ( ) . equals ( " 3 " ) ) {
eEntity . add ( sy ) ;
}
}
one . setList ( sEntity ) ;
two . setList ( mEntity ) ;
three . setList ( eEntity ) ;
rs . setSingle ( one ) ;
rs . setMultiple ( two ) ;
rs . setEstimate ( three ) ;
return rs ;
}
throw new RuntimeException ( " 未获取到试卷信息! " ) ;
}
// pdf生成
private String generateExamPaper ( ExaminationPaper rs ) throws IOException {
String fileName = DateUtils . datePath ( ) + File . separator + rs . getUserId ( ) + IdUtil . fastUUID ( ) + " .pdf " ;
File desc = new File ( " file " + File . separator + " exam " + File . separator + fileName ) ;
// 创建PDF文档
PdfWriter writer = new PdfWriter ( desc ) ;
PdfDocument pdfDoc = new PdfDocument ( writer ) ;
Document document = new Document ( pdfDoc , PageSize . A4 ) ;
// 加载字体(确保 simhei.ttf 可用)
String fontPath = " fonts/simhei.ttf " ;
PdfFont simhei = PdfFontFactory . createFont ( fontPath , PdfEncodings . IDENTITY_H ) ;
PdfFont simheiBold = PdfFontFactory . createFont ( fontPath , 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 ( 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));
// }
// }
document . close ( ) ;
SysOssVo upload = ossService . upload ( desc ) ;
// 刪除本地文件
try {
boolean delete = desc . delete ( ) ;
if ( ! delete ) {
log . error ( " 删除本地文件失败 " ) ;
}
} catch ( Exception e ) {
log . error ( " 删除本地文件失败 " , e ) ;
}
return upload . getOssId ( ) . toString ( ) ;
}
// 括号填充
private String replaceBrackets ( String text , String replacement ) {
Pattern pattern = Pattern . compile ( " \\ s*( \\ s*) \\ s* " ) ;
Matcher matcher = pattern . matcher ( text ) ;
return matcher . replaceAll ( " ( " + replacement + " ) " ) ;
}
@Override
public WgzAppUserScoreQuery userScoreQuery ( long userId ) {
return null ;
WgzAppUserScoreQuery res = new WgzAppUserScoreQuery ( ) ;
//1、查询及格分和满分,为空表示暂无记录
LambdaQueryWrapper < WgzQuestionSave > wra = new LambdaQueryWrapper < WgzQuestionSave > ( )
. eq ( WgzQuestionSave : : getUserId , userId ) ;
List < WgzQuestionSave > savaList = baseMapper . selectList ( wra ) ;
if ( savaList . isEmpty ( ) ) {
res . setIsPass ( " 3 " ) ;
return res ;
}
//2、查询用户答对的分数
float sumScore = baseMapper . getSumScore ( userId ) ;
//3、计算上次分数是否及格
String pass = savaList . get ( 0 ) . getPass ( ) ;
if ( pass ! = null & & ! pass . isEmpty ( ) ) {
String [ ] split = pass . split ( " , " ) ;
float fullMark = Float . parseFloat ( split [ 1 ] ) ;
float passingGrade = Float . parseFloat ( split [ 0 ] ) ;
// // 100分 / 满分 * 答对的分数 = 当前分数
// double score = 100.00 / fullMark * sumScore;
// // 使用 DecimalFormat 保证分数最多1位小数
// DecimalFormat df = new DecimalFormat("#.#");
// score = Double.parseDouble(df.format(score));
// 当前分数 >= 及格线 = 及格 否则 不及格
if ( sumScore > = passingGrade ) {
res . setIsPass ( " 1 " ) ;
} else {
res . setIsPass ( " 2 " ) ;
}
res . setFullMark ( fullMark ) ;
res . setPassingScore ( passingGrade ) ;
}
res . setCurrentMinute ( sumScore ) ;
//4、获取pdf试卷信息
WgzQuestionSavePdf wgzQuestionSavePdf = wgzQuestionSavePdfService . queryByUserId ( userId ) ;
if ( wgzQuestionSavePdf ! = null ) {
res . setPdfStr ( wgzQuestionSavePdf . getPath ( ) ) ;
}
return res ;
}
//
// @Autowired
// private IWgzQuestionsConfigurationService iWgzQuestionsConfigurationService;
//
// @Autowired
// private IWgzQuestionBankService iWgzQuestionBankService;
//
// @Autowired
// private IWgzQuestionSavePdfService wgzQuestionSavePdfService;
//
// @Resource
// private ISysOssService ossService;
//
// @Override
// public Wgz QuestionSave queryById (Long i d) {
// return getById(id);
// }
// public Bgt QuestionResult getResult (Long userI d) {
//
// @Override
// public TableDataInfo<WgzQuestionSaveVo> queryPageList(WgzQuestionSaveBo bo, PageQuery pageQuery) {
// LambdaQueryWrapper<WgzQuestionSave> lqw = buildQueryWrapper(bo);
// Page<WgzQuestionSaveVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
// return TableDataInfo.build(result);
// }
// BgtQuestionResult bgtQuestionResult = new BgtQuestionResult();
//
// @Override
// public List<WgzQuestionSaveVo> queryList(WgzQuestionSaveBo bo) {
// LambdaQueryWrapper<WgzQuestionSave> lqw = buildQueryWrapper(bo );
// return baseMapper.selectVoList(lqw);
// WgzUser byUserId = wgzUserService.findByUserId(userId);
// if(byUserId == null) {
// throw new BaseException("用户不存在" );
// }
// bgtQuestionResult.setAvatarName(byUserId.getAvatarName());
//
// private LambdaQueryWrapper<WgzQuestionSave> buildQueryWrapper(WgzQuestionSaveBo bo) {
// Map<String, Object> params = bo.getParams();
// LambdaQueryWrapper<WgzQuestionSave> lqw = Wrappers.lambdaQuery();
// lqw.orderByDesc(WgzQuestionSave::getId);
// lqw.eq(bo.getUserId() != null, WgzQuestionSave::getUserId, bo.getUserId());
// lqw.eq(bo.getBankId() != null, WgzQuestionSave::getBankId, bo.getBankId());
// lqw.eq(StringUtils.isNotBlank(bo.getAnswer()), WgzQuestionSave::getAnswer, bo.getAnswer());
// lqw.eq(StringUtils.isNotBlank(bo.getCorrect()), WgzQuestionSave::getCorrect, bo.getCorrect());
// lqw.eq(bo.getScore() != null, WgzQuestionSave::getScore, bo.getScore());
// lqw.eq(StringUtils.isNotBlank(bo.getSign()), WgzQuestionSave::getSign, bo.getSign());
// lqw.eq(bo.getTakeTime() != null, WgzQuestionSave::getTakeTime, bo.getTakeTime());
// lqw.eq(bo.getTimeOut() != null, WgzQuestionSave::getTimeOut, bo.getTimeOut());
// lqw.eq(StringUtils.isNotBlank(bo.getPass()), WgzQuestionSave::getPass, bo.getPass());
// return lqw;
// }
//
// @Override
// public Boolean insert(WgzQuestionSave bo) {
// WgzQuestionSave add = BeanUtil.toBean(bo, WgzQuestionSave.class);
// validEntityBeforeSave(add);
// return save(add);
// }
//
// @Override
// public Boolean update(WgzQuestionSave bo) {
// WgzQuestionSave update = BeanUtil.toBean(bo, WgzQuestionSave.class);
// validEntityBeforeSave(update);
// return updateById(update);
// }
//
// /**
// * 保存前的数据校验
// *
// * @param entity 实体类数据
// */
// private void validEntityBeforeSave(WgzQuestionSave entity) {
// //TODO 做一些数据校验,如唯一约束
// }
//
// @Override
// public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
// if (isValid) {
// //TODO 做一些业务上的校验,判断是否需要校验
// }
// return removeByIds(ids);
// }
//
// /**
// * APP相关
// * =================================================================================================================
// * =================================================================================================================
// * =================================================================================================================
// */
//
// @Override
// @Transactional(rollbackFor = Exception.class)
// public WgzAppSubmitATestPaperRes userSubmitATestPaper(WgzAppSubmitATestPaperReq req) {
// String coryPass = "";
// int coryTimeOut = 0;
// Long coryTakeTime = 0L;
// Double corySumScore = 0.00;
// /**
// * 第一部分:计算并保存最高分试卷
// */
// // 初始化第一次分数和当前考试的分数
// double scoreOne = 0.00;
// double scoreTwo = 0.00;
// //1、获取上次考试的分数
// Map<String, Object> spMap = baseMapper.getSumScoreAndPassAndSign(req.getUserId());
// if (spMap != null) {
// String pass = (String) spMap.get("pass");
// if (pass != null && !pass.isEmpty()) {
// String[] split = pass.split(",");
// double float1 = Double.parseDouble(split[1]);
// double sumScore = (double) spMap.get("sumScore");
// scoreOne = 100.00 / float1 * sumScore;
// }
// }
// //2、获取配置信息
// WgzQuestionsConfiguration configuration = iWgzQuestionsConfigurationService.appQueryLimitOne();
// //3、计算这次用户提交的试卷信息所得到的分数
// List<WgzQuestionSave> bqs = new ArrayList<>();
// Double fenshu = 0.0;
// Integer number = 0;
// Integer index = 0;
// List<WgzAppSubmitATestPaperTwo> list = req.getList();
// for (WgzAppSubmitATestPaperTwo data : list) {
// String correct = "";
// String pass = "";
// // 获取题库的正确答案,然后和用户的答案作对比
// WgzQuestionBank bqb = iWgzQuestionBankService.selectById(data.getBankId());
// if (bqb == null) {
// throw new RuntimeException("您的试卷被外星人卷走了!");
// } else {
// if (StringUtils.isEmpty(data.getAnswer())) {
// throw new RuntimeException("您还有题未答完!");
// }
// if (bqb.getCorrectAnswer().toLowerCase().contains(data.getAnswer().toLowerCase())) {
// correct = "1";
// fenshu = fenshu + data.getScore();
// number = number + 1;
// } else {
// correct = "2";
// }
// }
// // 记录这次的及格线和总分
// pass = configuration.getPassingScore().toString() + "," + configuration.getFullMark().toString();
// WgzQuestionSave bqsTwo = new WgzQuestionSave();
// bqsTwo.setUserId(req.getUserId());
// bqsTwo.setBankId(data.getBankId());
// bqsTwo.setAnswer(data.getAnswer());
// bqsTwo.setCorrect(correct);
// bqsTwo.setScore(data.getScore());
// //这几条数据只在每张试卷的第一条数据中存储(重复的没必要每条都存储)
// if (index == 0) {
// bqsTwo.setTakeTime(req.getTakeTime());
// bqsTwo.setTimeOut(configuration.getAnswerTime());
// bqsTwo.setPass(pass);
//
// coryTakeTime = req.getTakeTime();
// coryTimeOut = configuration.getAnswerTime();
// coryPass = pass;
// }
// bqs.add(bqsTwo);
// index = index + 1;
// }
// Double fullMark = configuration.getFullMark();
// scoreTwo = 100.00 / fullMark * fenshu;
// WgzAppSubmitATestPaperRes res = new WgzAppSubmitATestPaperRes();
// res.setAnswerTime(configuration.getAnswerTime());
// res.setTakeTime(req.getTakeTime());
// res.setFullMark(fullMark);
// res.setPassingScore(configuration.getPassingScore());
// res.setScore(fenshu);
// res.setNumber(number);
// //4、两次卷子的分数对比,第一次的分数比第二次大那么第二次只返回结果;第二次的分数比第一次大那么删除第一次的数据再重新插入
// if (scoreOne > scoreTwo) {
// return res;
// } else {
// corySumScore = scoreTwo;
// if (scoreOne == 0) {
// // 插入新数据
// super.saveBatch(bqs);
// }
// // 先查询当前用户是否有答题,有就把之前的删除(真删)
// int deleteResult = baseMapper.delete(new LambdaQueryWrapper<WgzQuestionSave>().eq(WgzQuestionSave::getUserId, req.getUserId()));
// if (deleteResult > 0) {
// // 删除pdf
// wgzQuestionSavePdfService.deleteByUserId(req.getUserId());
// // 插入新数据
// super.saveBatch(bqs);
// }
// }
// /**
// * 第二部分:分离出试卷信息
// */
// ExaminationPaper examinationPaper = separateTestInformation(req.getUserId(), configuration);
// /**
// * 第三部分: 生成pdf并存储到数据库中
// */
// try {
// String xdPath = generateExamPaper(examinationPaper);
// res.setPdfStr(xdPath);
// WgzQuestionSavePdf wgzQuestionSavePdf = new WgzQuestionSavePdf()
// .setType("1")
// .setUserId(req.getUserId())
// .setPath(xdPath)
// .setPass(coryPass)
// .setTimeOut(coryTimeOut)
// .setTakeTime(coryTakeTime)
// .setSumScore(corySumScore);
// wgzQuestionSavePdfService.insert(wgzQuestionSavePdf);
// } catch (Exception e1) {
// throw new RuntimeException("生成PDF试卷出现了意外, 请重新提交! ");
// }
// return res;
// }
//
// // 分离试卷信息
// private ExaminationPaper separateTestInformation(Long userId, WgzQuestionsConfiguration configurationEntity) {
// //1、组装数据
// List<PdfEntity> we = baseMapper.pdfSc(userId);
// if (!we.isEmpty()) {
// String sumScore = baseMapper.pdfSumScore(userId);
// Integer s = configurationEntity.getSingleChoice();
// Integer m = configurationEntity.getMultipleChoice();
// Integer e = configurationEntity.getEstimate();
// ExaminationPaper rs = new ExaminationPaper()
// .setPass(we.get(0).getPass())
// .setSumScore(sumScore)
// .setSign(we.get(0).getSign())
// .setUserId(userId.toString())
// .setUserName(we.get(0).getUserName());
// ExaminationPaperOne one = new ExaminationPaperOne();
// ExaminationPaperOne two = new ExaminationPaperOne();
// ExaminationPaperOne three = new ExaminationPaperOne();
// one.setTopic("一、单选题,共" + s + "道题,每小题" + configurationEntity.getSingleScore() + "分,共计" + configurationEntity.getSingleScore() * s + "分");
// two.setTopic("二、多选题,共" + m + "道题,每小题" + configurationEntity.getMultipleScore() + "分,共计" + configurationEntity.getMultipleScore() * m + "分");
// three.setTopic("三、判断题,共" + e + "道题,每小题" + configurationEntity.getEstimateScore() + "分,共计" + configurationEntity.getEstimateScore() * e + "分");
// List<ExaminationPaperTwo> sEntity = new ArrayList<>();
// List<ExaminationPaperTwo> mEntity = new ArrayList<>();
// List<ExaminationPaperTwo> eEntity = new ArrayList<>();
// for (PdfEntity data : we) {
// ExaminationPaperTwo sy = new ExaminationPaperTwo();
// sy.setQuestionText(data.getQuestionText());
// sy.setOptions(data.getOptions());
// sy.setAnswer(data.getAnswer());
// sy.setCorrectAnswer(data.getCorrectAnswer());
// sy.setCorrect(data.getCorrect());
// sy.setScore(data.getScore());
// if (data.getQuestionType().equals("1")) {
// sEntity.add(sy);
// }
// if (data.getQuestionType().equals("2")) {
// mEntity.add(sy);
// }
// if (data.getQuestionType().equals("3")) {
// eEntity.add(sy);
// }
// }
// one.setList(sEntity);
// two.setList(mEntity);
// three.setList(eEntity);
// rs.setSingle(one);
// rs.setMultiple(two);
// rs.setEstimate(three);
// return rs;
// }
// throw new RuntimeException("未获取到试卷信息!");
// }
//
// // pdf生成
// private String generateExamPaper(ExaminationPaper rs) throws IOException {
// String fileName = DateUtils.datePath() + File.separator + rs.getUserId() + IdUtil.fastUUID() + ".pdf";
// File desc = new File("file" + File.separator + "exam" + File.separator + fileName);
// // 创建PDF文档
// PdfWriter writer = new PdfWriter(desc);
// PdfDocument pdfDoc = new PdfDocument(writer);
// Document document = new Document(pdfDoc, PageSize.A4);
//
// // 加载字体(确保 simhei.ttf 可用)
// String fontPath = "fonts/simhei.ttf";
// PdfFont simhei = PdfFontFactory.createFont(fontPath, PdfEncodings.IDENTITY_H);
// PdfFont simheiBold = PdfFontFactory.createFont(fontPath, 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(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));
//// }
//// }
//
// document.close();
// SysOssVo upload = ossService.upload(desc);
// // 刪除本地文件
// try {
// boolean delete = desc.delete();
// if (!delete) {
// log.error("删除本地文件失败");
// }
// } catch (Exception e) {
// log.error("删除本地文件失败", e);
// }
// return upload.getOssId().toString();
// }
//
// // 括号填充
// private String replaceBrackets(String text, String replacement) {
// Pattern pattern = Pattern.compile("\\s*( \\s*) \\s*");
// Matcher matcher = pattern.matcher(text);
// return matcher.replaceAll("( " + replacement + " ) ");
// }
//
// @Override
// public WgzAppUserScoreQuery userScoreQuery(long userId) {
// WgzAppUserScoreQuery res = new WgzAppUserScoreQuery();
// //1、查询及格分和满分,为空表示暂无记录
// LambdaQueryWrapper<WgzQuestionSave> wra = new LambdaQueryWrapper<WgzQuestionSave>()
// .eq(WgzQuestionSave::getUserId, userId);
// List<WgzQuestionSave> savaList = baseMapper.selectList(wra);
// if (savaList.isEmpty()) {
// res.setIsPass("3") ;
// return res ;
// }
// //2、查询用户答对的分数
// float sumScore = baseMapper.getSumScore(userId);
// //3、计算上次分数是否及格
// String pass = savaList.get(0).getPass();
// if (pass != null && !pass.isEmpty()) {
// String[] split = pass.split(",") ;
// float fullMark = Float.parseFloat(split[1]);
// float passingGrade = Float.parseFloat(split[0]);
//// // 100分 / 满分 * 答对的分数 = 当前分数
//// double score = 100.00 / fullMark * sumScore;
//// // 使用 DecimalFormat 保证分数最多1位小数
//// DecimalFormat df = new DecimalFormat("#.#");
//// score = Double.parseDouble(df.format(score));
// // 当前分数 >= 及格线 = 及格 否则 不及格
// if (sumScore >= passingGrade) {
// res.setIsPass("1");
//
// double score= 0d ;
// int successNum = 0 ;
// int errorNum= 0;
// long time= 0L;
//
// for (WgzQuestionSave wgzQuestionSave : savaList){
// if(wgzQuestionSave.getCorrect().equals("1")){
// score += wgzQuestionSave.getScore();
// successNum++ ;
// }else{
// res.setIsPass("2") ;
// errorNum++ ;
// }
// res.setFullMark(fullMark);
// res.setPassingScore(passingGrade );
// if(wgzQuestionSave.getTakeTime() != null){
// time += wgzQuestionSave.getTakeTime( );
// }
// res.setCurrentMinute(sumScore);
// //4、获取pdf试卷信息
// WgzQuestionSavePdf wgzQuestionSavePdf = wgzQuestionSavePdfService.queryByUserId(userId);
// if (wgzQuestionSavePdf != null) {
// res.setPdfStr(wgzQuestionSavePdf.getPath());
// }
// return res;
// }
//
//// @Override
//// public B gtQuestionResult getResult(Long userId) {
////
//// BgtQuestionResult bgtQuestionResult = new BgtQuestionResult( );
////
//// WgzUser byUserId = wgzUserService.findByUserId(userId);
//// if(byUserId == null){
//// throw new BaseException("用户不存在");
//// }
//// bgtQuestionResult.setAvatarName(byUserId.getAvatarName());
////
//// LambdaQueryWrapper<WgzQuestionSave> wra = new LambdaQueryWrapper<WgzQuestionSave>()
//// .eq(WgzQuestionSave::getUserId, userId);
//// List<WgzQuestionSave> savaList = baseMapper.selectList(wra);
////
//// double score= 0d;
//// int successNum = 0;
//// int errorNum= 0;
//// long time= 0L;
////
//// for (WgzQuestionSave wgzQuestionSave : savaList){
//// if(wgzQuestionSave.getCorrect().equals("1")){
//// score += wgzQuestionSave.getScore();
//// successNum++;
//// }else{
//// errorNum++;
//// }
//// if(wgzQuestionSave.getTakeTime() != null){
//// time += wgzQuestionSave.getTakeTime();
//// }
//// }
////
//// bgtQuestionResult.setScore(score);
//// bgtQuestionResult.setSuccessNum(successNum);
//// bgtQuestionResult.setErrorNum(errorNum);
//// bgtQuestionResult.setTime(time);
//// return bgtQuestionResult;
//// }
// bgtQuestionResult.setScore(score);
// b gtQuestionResult.setSuccessNum(successNum);
// bgtQuestionResult.setErrorNum(errorNum);
// bgtQuestionResult.setTime(time );
// return bgtQuestionResult;
// }
}