瀏覽代碼

优化查询sql

hgw 3 年之前
父節點
當前提交
942858f2c2

+ 32 - 13
mec-biz/src/main/java/com/ym/mec/biz/service/impl/TempLittleArtistTrainingCampServiceImpl.java

@@ -75,19 +75,7 @@ public class TempLittleArtistTrainingCampServiceImpl extends ServiceImpl<TempLit
     public void add(TempLittleArtistTrainingCampDto dto) {
         SysUser user = getUser();
         Date now = DateUtil.toDate(LocalDate.now().toString());
-        if (dto.getApplyEndDate().getTime() < now.getTime()) {
-            throw new BizException("报名结束时间不能小于今天");
-        }
-        if (dto.getApplyStartDate().getTime() > dto.getApplyEndDate().getTime()) {
-            throw new BizException("报名开始时间不能大于结束时间");
-        }
-        if (dto.getTrainStartDate().getTime() > dto.getTrainEndDate().getTime()) {
-            throw new BizException("训练开始时间不能大于训练结束时间");
-        }
-        //报名结束时间必需小于训练开始时间
-        if (dto.getTrainStartDate().getTime() < dto.getApplyEndDate().getTime()) {
-            throw new BizException("报名结束时间必需小于训练开始时间");
-        }
+        checkDateTime(dto, now);
         TempLittleArtistTrainingCamp entity = new TempLittleArtistTrainingCamp();
         BeanUtils.copyProperties(dto, entity);
         entity.setState(TempLittleArtistTrainingCamp.NOT_START);
@@ -107,9 +95,16 @@ public class TempLittleArtistTrainingCampServiceImpl extends ServiceImpl<TempLit
     @Override
     public void update(TempLittleArtistTrainingCampDto dto) {
         SysUser user = getUser();
+        Date now = DateUtil.toDate(LocalDate.now().toString());
+        checkDateTime(dto, now);
         TempLittleArtistTrainingCamp entity = Optional.ofNullable(dto.getId())
                 .map(this::getById)
                 .orElseThrow(() -> new BizException("训练营不存在"));
+        int count = tempLittleArtistTrainingCampUserRelationService.count(Wrappers.<TempLittleArtistTrainingCampUserRelation>lambdaQuery()
+                .eq(TempLittleArtistTrainingCampUserRelation::getActivityId, entity.getId()));
+        if (count > 0) {
+            throw new BizException("训练营已有学生报名,无法删除");
+        }
         entity.setName(dto.getName());
         entity.setApplyStartDate(dto.getApplyStartDate());
         entity.setApplyEndDate(dto.getApplyEndDate());
@@ -122,6 +117,25 @@ public class TempLittleArtistTrainingCampServiceImpl extends ServiceImpl<TempLit
     }
 
     /**
+     * 校验时间
+     */
+    private void checkDateTime(TempLittleArtistTrainingCampDto dto, Date now) {
+        if (dto.getApplyEndDate().getTime() < now.getTime()) {
+            throw new BizException("报名结束时间不能小于今天");
+        }
+        if (dto.getApplyStartDate().getTime() > dto.getApplyEndDate().getTime()) {
+            throw new BizException("报名开始时间不能大于结束时间");
+        }
+        if (dto.getTrainStartDate().getTime() > dto.getTrainEndDate().getTime()) {
+            throw new BizException("训练开始时间不能大于训练结束时间");
+        }
+        //报名结束时间必需小于训练开始时间
+        if (dto.getTrainStartDate().getTime() < dto.getApplyEndDate().getTime()) {
+            throw new BizException("报名结束时间必需小于训练开始时间");
+        }
+    }
+
+    /**
      * 删除训练营
      */
     @Transactional(rollbackFor = Exception.class)
@@ -130,6 +144,11 @@ public class TempLittleArtistTrainingCampServiceImpl extends ServiceImpl<TempLit
         TempLittleArtistTrainingCamp entity = Optional.ofNullable(id)
                 .map(this::getById)
                 .orElseThrow(() -> new BizException("训练营不存在"));
+        int count = tempLittleArtistTrainingCampUserRelationService.count(Wrappers.<TempLittleArtistTrainingCampUserRelation>lambdaQuery()
+                .eq(TempLittleArtistTrainingCampUserRelation::getActivityId, entity.getId()));
+        if (count > 0) {
+            throw new BizException("训练营已有学生报名,无法删除");
+        }
         entity.setDelFlag(1);
         this.updateById(entity);
     }

+ 1 - 1
mec-biz/src/main/java/com/ym/mec/biz/service/impl/TempLittleArtistTrainingCampUserRelationServiceImpl.java

@@ -52,7 +52,7 @@ public class TempLittleArtistTrainingCampUserRelationServiceImpl extends Service
         SysUser user = getUser();
         TempLittleArtistTrainingCampUserRelation one = this.getOne(Wrappers.<TempLittleArtistTrainingCampUserRelation>lambdaQuery()
                 .eq(TempLittleArtistTrainingCampUserRelation::getUserId, user.getId()));
-        if (Objects.nonNull(one)) {
+        if (Objects.nonNull(one) && Objects.nonNull(one.getActivityId())) {
             throw new BizException("您已登记训练营!");
         }
         int count = this.count(Wrappers.<TempLittleArtistTrainingCampUserRelation>lambdaUpdate()