123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- package cn.com.qmth.scancentral.service.impl;
- import cn.com.qmth.scancentral.dao.OmrGroupDao;
- import cn.com.qmth.scancentral.entity.OmrGroupEntity;
- import cn.com.qmth.scancentral.entity.OmrTaskEntity;
- import cn.com.qmth.scancentral.enums.ConditionType;
- import cn.com.qmth.scancentral.enums.LockType;
- import cn.com.qmth.scancentral.enums.Stage;
- import cn.com.qmth.scancentral.enums.TaskStatus;
- import cn.com.qmth.scancentral.model.OmrCondition;
- import cn.com.qmth.scancentral.service.OmrGroupService;
- import cn.com.qmth.scancentral.service.OmrTaskService;
- import cn.com.qmth.scancentral.service.StudentPaperService;
- import cn.com.qmth.scancentral.util.BatchSetDataUtil;
- import cn.com.qmth.scancentral.vo.OmrConditionVo;
- import cn.com.qmth.scancentral.vo.OmrGroupVo;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.qmth.boot.core.concurrent.service.ConcurrentService;
- import com.qmth.boot.core.exception.ParameterException;
- import com.qmth.boot.core.exception.ReentrantException;
- import com.qmth.boot.core.exception.StatusException;
- import org.apache.commons.collections4.CollectionUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- @Service
- public class OmrGroupServiceImpl extends ServiceImpl<OmrGroupDao, OmrGroupEntity> implements OmrGroupService {
- protected static Logger log = LoggerFactory.getLogger(OmrGroupService.class);
- @Autowired
- private ConcurrentService concurrentService;
- @Autowired
- private OmrTaskService taskService;
- @Autowired
- private StudentPaperService studentPaperService;
- @Transactional
- @Override
- public void deleteById(Long id) {
- taskService.deleteByGroupId(id);
- this.removeById(id);
- }
- @Transactional
- @Override
- public void resetById(Long id) {
- OmrGroupEntity group = this.getById(id);
- if (group == null) {
- throw new ParameterException("分组不存在");
- }
- taskService.resetByGroup(group);
- }
- @Override
- public void buildTaskById(Long groupId) {
- OmrGroupEntity group = this.getById(groupId);
- long start = System.currentTimeMillis();
- List<Long> studentIds = studentPaperService.findStudentIdByExamId(group.getExamId());
- long end = System.currentTimeMillis();
- if (CollectionUtils.isEmpty(studentIds)) {
- log.info("OmrGroup[" + groupId + "] get studentIds finish | time cost(s):" + ((end - start) / 1000)
- + " | count:0");
- return;
- }
- log.info("OmrGroup[" + groupId + "] get studentIds finish | time cost(s):" + ((end - start) / 1000)
- + " | count:" + studentIds.size());
- new BatchSetDataUtil<Long>() {
- @Override
- protected void setData(List<Long> dataList) {
- buildTaskByStudent(group, dataList);
- }
- }.setDataForBatch(studentIds, 1000);
- }
- private void buildTaskByStudent(OmrGroupEntity group, List<Long> studentIds) {
- if (CollectionUtils.isEmpty(studentIds)) {
- return;
- }
- List<OmrTaskEntity> saveList = new ArrayList<OmrTaskEntity>();
- for (Long studentId : studentIds) {
- try {
- concurrentService.getLock(LockType.STUDENT + "-" + studentId).lock();
- List<OmrTaskEntity> task = taskService.buildTask(group, studentId);
- if (task != null && !task.isEmpty()) {
- saveList.addAll(task);
- }
- } finally {
- concurrentService.getLock(LockType.STUDENT + "-" + studentId).unlock();
- }
- }
- taskService.saveBatch(saveList);
- this.updateTotalCount(group.getId());
- }
- @Override
- public List<OmrGroupVo> listByExamId(Long examId) {
- QueryWrapper<OmrGroupEntity> wrapper = new QueryWrapper<>();
- LambdaQueryWrapper<OmrGroupEntity> lw = wrapper.lambda();
- lw.eq(OmrGroupEntity::getExamId, examId);
- List<OmrGroupEntity> groups = this.list(wrapper);
- List<OmrGroupVo> list = new ArrayList<OmrGroupVo>();
- for (OmrGroupEntity group : groups) {
- OmrGroupVo groupVo = new OmrGroupVo();
- groupVo.setId(group.getId());
- List<OmrConditionVo> conditions = new ArrayList<OmrConditionVo>();
- for (OmrCondition c : group.getConditions()) {
- conditions.add(new OmrConditionVo(c));
- }
- groupVo.setConditions(conditions);
- groupVo.setStage(group.getStage());
- groupVo.setFixed(group.getFixed());
- groupVo.setBuilding(
- !concurrentService.getSemaphore(LockType.OMR_GROUP_BUILD + "-" + group.getId()).isAvailable());
- groupVo.setDeleting(
- !concurrentService.getSemaphore(LockType.OMR_GROUP_DELETE + "-" + group.getId()).isAvailable());
- groupVo.setReseting(
- !concurrentService.getSemaphore(LockType.OMR_GROUP_RESET + "-" + group.getId()).isAvailable());
- groupVo.setTotalCount(group.getTotalCount());
- groupVo.setFinishCount(
- taskService.getCountByExamAndGroupAndStatus(examId, group.getId(), TaskStatus.PROCESSED));
- groupVo.setUnarbitrateCount(
- taskService.getCountByExamAndGroupAndStatus(examId, group.getId(), TaskStatus.WAIT_ARBITRATE));
- groupVo.setArbitratedCount(
- taskService.getCountByExamAndGroupAndStatus(examId, group.getId(), TaskStatus.ARBITRATED));
- groupVo.setUpdateTime(group.getUpdateTime());
- list.add(groupVo);
- }
- return list;
- }
- @Transactional
- @Override
- public void updateStage(Long id, Long userId) {
- OmrGroupEntity group = this.getById(id);
- if (group == null) {
- throw new ParameterException("分组不存在");
- }
- if (!Stage.FIRST.equals(group.getStage())) {
- throw new StatusException("分组不在第一阶段,无法切换");
- }
- if (group.getTotalCount() == 0) {
- throw new StatusException("分组下没有任务,无法切换");
- }
- Integer finishCount = taskService.getCountByExamAndGroupAndStatus(group.getExamId(), id, TaskStatus.PROCESSED,
- TaskStatus.ARBITRATED);
- if (group.getTotalCount() - finishCount != 0) {
- throw new StatusException("识别对照任务未完成,无法切换");
- }
- group.setStage(Stage.SECOND);
- group.setUpdaterId(userId);
- group.setUpdateTime(System.currentTimeMillis());
- this.saveOrUpdate(group);
- taskService.waitingByGroupToggle(id);
- }
- @Override
- public OmrGroupEntity save(Long id, Long examId, List<OmrCondition> conditions, Long userId) {
- if (examId == null) {
- throw new ParameterException("examId不能为空");
- }
- if (conditions == null || conditions.isEmpty()) {
- throw new ParameterException("识别对照条件不能为空");
- }
- for (OmrCondition omrCondition : conditions) {
- if (ConditionType.FILL_SUSPECT.equals(omrCondition.getCode())) {
- throw new ParameterException("识别对照条件不能重复");
- }
- if (ConditionType.QUESTION_SINGLE_BLANK.equals(omrCondition.getCode())
- || ConditionType.QUESTION_MULTI_BLANK.equals(omrCondition.getCode())
- || ConditionType.SELECTIVE_EXCEED.equals(omrCondition.getCode())
- || ConditionType.SELECTIVE_BLANK.equals(omrCondition.getCode())) {
- if (omrCondition.getValue() == null) {
- throw new ParameterException("参数不能为空");
- }
- if (omrCondition.getValue() < 0) {
- throw new ParameterException("参数不能小于0");
- }
- }
- }
- checkOmrCondition(id, examId, conditions);
- if (id != null) {
- OmrGroupEntity group = this.getById(id);
- if (group.getFixed()) {
- throw new ParameterException("默认分组不能修改");
- }
- if (group.getTotalCount() != null && group.getTotalCount() > 0) {
- throw new ParameterException("已生成任务的分组不能修改");
- }
- if (concurrentService.getLock(LockType.OMR_GROUP + "-" + id).tryLock()) {
- group.setConditions(conditions);
- group.setUpdaterId(userId);
- group.setUpdateTime(System.currentTimeMillis());
- this.saveOrUpdate(group);
- concurrentService.getLock(LockType.OMR_GROUP + "-" + id).unlock();
- return group;
- } else {
- throw new ReentrantException("该分组数据操作繁忙,请稍后重试");
- }
- } else {
- OmrGroupEntity group = new OmrGroupEntity();
- group.setExamId(examId);
- group.setConditions(conditions);
- group.setCreateTime(System.currentTimeMillis());
- group.setFixed(false);
- group.setStage(Stage.FIRST);
- group.setTotalCount(0);
- group.setCreatorId(userId);
- group.setCreateTime(System.currentTimeMillis());
- this.save(group);
- return group;
- }
- }
- @Override
- public List<OmrGroupEntity> findByExamIdAndFixed(Long examId, Boolean fixed) {
- QueryWrapper<OmrGroupEntity> wrapper = new QueryWrapper<>();
- LambdaQueryWrapper<OmrGroupEntity> lw = wrapper.lambda();
- lw.eq(OmrGroupEntity::getExamId, examId);
- lw.eq(OmrGroupEntity::getFixed, fixed);
- return baseMapper.selectList(wrapper);
- }
- private void checkOmrCondition(Long id, Long examId, List<OmrCondition> conditions) {
- List<OmrGroupEntity> list = findByExamId(examId);
- if (CollectionUtils.isEmpty(list)) {
- return;
- }
- Set<String> codes = new HashSet<>();
- for (OmrCondition oc : conditions) {
- codes.add(oc.getCode().name());
- }
- for (OmrGroupEntity g : list) {
- for (OmrCondition oc : g.getConditions()) {
- if (codes.contains(oc.getCode().name()) && (id == null || g.getId().longValue() != id.longValue())) {
- throw new ParameterException("已存在识别对照条件");
- }
- }
- }
- }
- private List<OmrGroupEntity> findByExamId(Long examId) {
- QueryWrapper<OmrGroupEntity> wrapper = new QueryWrapper<>();
- LambdaQueryWrapper<OmrGroupEntity> lw = wrapper.lambda();
- lw.eq(OmrGroupEntity::getExamId, examId);
- return baseMapper.selectList(wrapper);
- }
- @Transactional
- @Override
- public void updateTotalCount(Long id) {
- if (id == null) {
- throw new ParameterException("Id不能为空");
- }
- baseMapper.updateTotalCount(id);
- }
- @Override
- public void addFixOmrCondition(Long userId, Long examId) {
- OmrGroupEntity group = new OmrGroupEntity();
- group.setExamId(examId);
- List<OmrCondition> conditions = new ArrayList<OmrCondition>();
- OmrCondition omrCondition = new OmrCondition();
- omrCondition.setCode(ConditionType.FILL_SUSPECT);
- conditions.add(omrCondition);
- group.setConditions(conditions);
- group.setCreateTime(System.currentTimeMillis());
- group.setFixed(true);
- group.setStage(Stage.FIRST);
- group.setTotalCount(0);
- group.setCreatorId(userId);
- group.setCreateTime(System.currentTimeMillis());
- this.save(group);
- }
- @Transactional
- @Override
- public void updateTotalCountByExamId(Long examId) {
- List<OmrGroupEntity> list = this.findByExamId(examId);
- for (OmrGroupEntity omrGroupEntity : list) {
- this.updateTotalCount(omrGroupEntity.getId());
- }
- }
- }
|