Browse Source

计算课程学习时长

yangfeng 1 year ago
parent
commit
a44f0a67f4

+ 14 - 0
web/src/main/java/com/ynfy/buss/course/usercoursecatalog/controller/UserCourseCatalogController.java

@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.ynfy.buss.course.usercoursecatalog.entity.UserCourseCatalog;
+import com.ynfy.buss.course.usercoursecatalog.entity.vo.CatalogTimeVO;
 import com.ynfy.buss.course.usercoursecatalog.service.IUserCourseCatalogService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
@@ -159,4 +160,17 @@ public class UserCourseCatalogController extends JeecgController<UserCourseCatal
         return super.importExcel(request, response, UserCourseCatalog.class);
     }
 
+    /**
+     * 计算任务学习时长
+     *
+     * @return
+     */
+    @AutoLog(value = "计算任务学习时长")
+    @ApiOperation(value = "计算任务学习时长", notes = "计算任务学习时长")
+    @PostMapping(value = "/calcStudyTime")
+    public Result<String> calcStudyTime(@RequestBody CatalogTimeVO catalogTimeVO) {
+        userCourseCatalogService.calcStudyTime(catalogTimeVO);
+        return Result.OK();
+    }
+
 }

+ 34 - 0
web/src/main/java/com/ynfy/buss/course/usercoursecatalog/entity/vo/CatalogTimeVO.java

@@ -0,0 +1,34 @@
+package com.ynfy.buss.course.usercoursecatalog.entity.vo;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import org.springframework.format.annotation.DateTimeFormat;
+
+import java.io.Serializable;
+import java.util.Date;
+
+/**
+ * 学习时长计算vo
+ */
+@Data
+public class CatalogTimeVO implements Serializable {
+    private static final long serialVersionUID = 1L;
+
+    private String courseCatalogId;
+
+    /**
+     * /**
+     * 创建日期
+     */
+    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
+    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    @ApiModelProperty(value = "创建日期")
+    private Date startLearnTime;
+
+    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
+    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    @ApiModelProperty(value = "创建日期")
+    private Date endLearnTime;
+
+}

+ 7 - 0
web/src/main/java/com/ynfy/buss/course/usercoursecatalog/service/IUserCourseCatalogService.java

@@ -2,6 +2,7 @@ package com.ynfy.buss.course.usercoursecatalog.service;
 
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.ynfy.buss.course.usercoursecatalog.entity.UserCourseCatalog;
+import com.ynfy.buss.course.usercoursecatalog.entity.vo.CatalogTimeVO;
 
 /**
  * @Description: 用户课程任务学习情况
@@ -11,4 +12,10 @@ import com.ynfy.buss.course.usercoursecatalog.entity.UserCourseCatalog;
  */
 public interface IUserCourseCatalogService extends IService<UserCourseCatalog> {
 
+    /**
+     * 计算任务学习时长
+     *
+     * @return
+     */
+    void calcStudyTime(CatalogTimeVO catalogTimeVO);
 }

+ 44 - 0
web/src/main/java/com/ynfy/buss/course/usercoursecatalog/service/impl/UserCourseCatalogServiceImpl.java

@@ -1,10 +1,20 @@
 package com.ynfy.buss.course.usercoursecatalog.service.impl;
 
+import cn.hutool.core.date.DateUnit;
+import cn.hutool.core.date.DateUtil;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ynfy.buss.course.usercoursecatalog.entity.UserCourseCatalog;
+import com.ynfy.buss.course.usercoursecatalog.entity.vo.CatalogTimeVO;
 import com.ynfy.buss.course.usercoursecatalog.mapper.UserCourseCatalogMapper;
 import com.ynfy.buss.course.usercoursecatalog.service.IUserCourseCatalogService;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.shiro.SecurityUtils;
+import org.jeecg.common.system.vo.LoginUser;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.Objects;
 
 /**
  * @Description: 用户课程任务学习情况
@@ -15,4 +25,38 @@ import org.springframework.stereotype.Service;
 @Service
 public class UserCourseCatalogServiceImpl extends ServiceImpl<UserCourseCatalogMapper, UserCourseCatalog> implements IUserCourseCatalogService {
 
+    /**
+     * 计算任务学习时长
+     *
+     * @return
+     */
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public void calcStudyTime(CatalogTimeVO catalogTimeVO) {
+        if (StringUtils.isBlank(catalogTimeVO.getCourseCatalogId()) || Objects.isNull(catalogTimeVO.getStartLearnTime())
+                || Objects.isNull(catalogTimeVO.getEndLearnTime())) {
+            return;
+        }
+        if (DateUtil.compare(catalogTimeVO.getEndLearnTime(), catalogTimeVO.getStartLearnTime()) > 0) {
+            LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
+
+            UserCourseCatalog courseCatalog = findUserCatalog(sysUser.getId(), catalogTimeVO.getCourseCatalogId());
+            if (Objects.isNull(courseCatalog)) {
+                courseCatalog = new UserCourseCatalog();
+            }
+            courseCatalog.setUserId(sysUser.getId());
+            courseCatalog.setCourseCatalogId(catalogTimeVO.getCourseCatalogId());
+            long between = DateUtil.between(catalogTimeVO.getStartLearnTime(), catalogTimeVO.getEndLearnTime(), DateUnit.SECOND);
+            courseCatalog.setTotalLearnTime(Integer.parseInt(String.valueOf(((!Objects.isNull(courseCatalog.getTotalLearnTime())
+                    ? courseCatalog.getTotalLearnTime() : 0) + between))));
+            saveOrUpdate(courseCatalog);
+        }
+
+    }
+
+    public UserCourseCatalog findUserCatalog(String userId, String courseCatalogId) {
+        LambdaQueryWrapper<UserCourseCatalog> query = new LambdaQueryWrapper<UserCourseCatalog>()
+                .eq(UserCourseCatalog::getUserId, userId).eq(UserCourseCatalog::getCourseCatalogId, courseCatalogId);
+        return getOne(query);
+    }
 }