|
|
package com.ibeetl.jlw.service;
|
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
import cn.hutool.core.lang.Assert;
|
|
|
import cn.hutool.core.util.NumberUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.RandomUtil;
|
|
|
import cn.jlw.Interceptor.SCoreUser;
|
|
|
import cn.jlw.aliPay.utils.StringUtils;
|
|
|
import com.ibeetl.admin.core.entity.CoreUser;
|
|
|
import com.ibeetl.admin.core.service.CoreBaseService;
|
|
|
import com.ibeetl.admin.core.util.PlatformException;
|
|
|
import com.ibeetl.jlw.dao.CourseInfoDao;
|
|
|
import com.ibeetl.jlw.dao.ResourcesInfoDao;
|
|
|
import com.ibeetl.jlw.dao.TeacherOpenCourseMergeResourcesInfoDao;
|
|
|
import com.ibeetl.jlw.entity.CourseInfo;
|
|
|
import com.ibeetl.jlw.entity.ResourcesInfo;
|
|
|
import com.ibeetl.jlw.entity.TeacherOpenCourseMergeResourcesInfo;
|
|
|
import com.ibeetl.jlw.enums.GlobalUpStatusEnum;
|
|
|
import com.ibeetl.jlw.enums.MoveEnum;
|
|
|
import com.ibeetl.jlw.web.query.ResourcesInfoQuery;
|
|
|
import com.ibeetl.jlw.web.query.TeacherOpenCourseMergeResourcesInfoQuery;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
import org.beetl.sql.core.SqlId;
|
|
|
import org.beetl.sql.core.engine.PageQuery;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import javax.validation.constraints.NotNull;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.math.RoundingMode;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import static cn.hutool.core.util.ObjectUtil.defaultIfNull;
|
|
|
import static cn.jlw.util.ConvertM3U8.booleanMap;
|
|
|
import static java.lang.Math.min;
|
|
|
import static java.math.BigDecimal.ROUND_HALF_UP;
|
|
|
|
|
|
/**
|
|
|
* ResourcesInfo Service
|
|
|
*/
|
|
|
|
|
|
@Service
|
|
|
@Slf4j
|
|
|
@Transactional
|
|
|
public class ResourcesInfoService extends CoreBaseService<ResourcesInfo> {
|
|
|
|
|
|
@Autowired
|
|
|
private ResourcesInfoDao resourcesInfoDao;
|
|
|
|
|
|
@Autowired
|
|
|
private CourseInfoService courseInfoService;
|
|
|
|
|
|
@Autowired
|
|
|
private CourseInfoDao courseInfoDao;
|
|
|
|
|
|
@Autowired
|
|
|
private TeacherOpenCourseMergeResourcesInfoDao teacherOpenCourseMergeResourcesInfoDao;
|
|
|
|
|
|
|
|
|
public PageQuery<ResourcesInfo> queryByCondition(PageQuery query) {
|
|
|
PageQuery ret = resourcesInfoDao.queryByCondition(query);
|
|
|
queryListAfter(ret.getList());
|
|
|
|
|
|
List<ResourcesInfo> resourcesInfoList = ret.getList();
|
|
|
for (int i = 0; i < resourcesInfoList.size(); i++) {
|
|
|
List<CourseInfo> courseInfoList = new ArrayList<>();
|
|
|
|
|
|
Long courseInfoId = resourcesInfoList.get(i).getCourseInfoId();
|
|
|
CourseInfo c = courseInfoService.queryById(courseInfoId);
|
|
|
if (null != c) {
|
|
|
if (!((Integer) 1).equals(c.getCourseInfoType())) {
|
|
|
courseInfoId = c.getCourseInfoParentId();
|
|
|
for (int j = 0; j < 25; j++) {
|
|
|
CourseInfo courseInfoParent = courseInfoService.queryById(courseInfoId);
|
|
|
courseInfoList.add(courseInfoParent);
|
|
|
if (((Integer) 1).equals(courseInfoParent.getCourseInfoType())) {
|
|
|
break;
|
|
|
} else {
|
|
|
courseInfoId = courseInfoParent.getCourseInfoParentId();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (courseInfoList.size() >= 2) {
|
|
|
resourcesInfoList.get(i).set("courseInfoParentId", courseInfoList.get(courseInfoList.size() - 2).getCourseInfoId());
|
|
|
resourcesInfoList.get(i).set("courseInfoParentName", courseInfoList.get(courseInfoList.size() - 2).getCourseInfoName());
|
|
|
resourcesInfoList.get(i).set("courseInfoParentParentId", courseInfoList.get(courseInfoList.size() - 1).getCourseInfoId());
|
|
|
resourcesInfoList.get(i).set("courseInfoParentParentName", courseInfoList.get(courseInfoList.size() - 1).getCourseInfoName());
|
|
|
} else {
|
|
|
resourcesInfoList.get(i).set("courseInfoParentId", null);
|
|
|
resourcesInfoList.get(i).set("courseInfoParentName", null);
|
|
|
resourcesInfoList.get(i).set("courseInfoParentParentId", null);
|
|
|
resourcesInfoList.get(i).set("courseInfoParentParentName", null);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取课程资源管理列表
|
|
|
*
|
|
|
* @param query
|
|
|
* @return
|
|
|
*/
|
|
|
public PageQuery<ResourcesInfo> getCurriculumResourceManagementList(PageQuery query) {
|
|
|
return resourcesInfoDao.getCurriculumResourceManagementList(query);
|
|
|
}
|
|
|
|
|
|
public ResourcesInfo getById(Long resourcesInfoId) {
|
|
|
ResourcesInfo resourcesInfo = queryById(resourcesInfoId);
|
|
|
if (null != resourcesInfo) {
|
|
|
try {
|
|
|
String path = resourcesInfo.getResourcesInfoContent().replace("\\", "/");
|
|
|
String fileName = path.substring(path.lastIndexOf("/") + 1);
|
|
|
String formatSpot = fileName.substring(fileName.lastIndexOf("."));
|
|
|
|
|
|
System.out.println(path);
|
|
|
path = path
|
|
|
.replace("/filesystem/temp/", "/filesystem/temp/" + fileName.replace(formatSpot, "") + "/")
|
|
|
.replace("server/", "")
|
|
|
.replace(formatSpot, ".m3u8");
|
|
|
System.out.println(path);
|
|
|
|
|
|
Boolean flag = booleanMap.get(fileName.replace(formatSpot, ""));
|
|
|
if (null == flag) {
|
|
|
flag = false;
|
|
|
}
|
|
|
if (flag) {
|
|
|
resourcesInfo.setResourcesInfoContent(path);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
return resourcesInfo;
|
|
|
}
|
|
|
|
|
|
public void deleteResourcesInfo(String ids) {
|
|
|
for (int i = 0; i < ids.split(",").length; i++) {
|
|
|
ResourcesInfo oldResourcesInfo = queryById(Long.parseLong(ids.split(",")[i]));
|
|
|
if (ObjectUtil.isNotEmpty(oldResourcesInfo)) {
|
|
|
CourseInfo oldCourseInfo = courseInfoDao.single(oldResourcesInfo.getCourseInfoId());
|
|
|
if (null != oldCourseInfo) {
|
|
|
//资源类型(1视频 2课件 3附件)
|
|
|
if (((Integer) 1).equals(oldResourcesInfo.getResourcesInfoType())) {
|
|
|
CourseInfo c = new CourseInfo();
|
|
|
c.setCourseInfoId(oldCourseInfo.getCourseInfoId());
|
|
|
c.setCourseVideoId(null != oldCourseInfo.getCourseVideoId() && oldCourseInfo.getCourseVideoId().equals(oldResourcesInfo.getResourcesInfoId()) ? null : oldCourseInfo.getCourseVideoId());
|
|
|
courseInfoDao.updateVideoIdById(c);
|
|
|
} else if (((Integer) 2).equals(oldResourcesInfo.getResourcesInfoType())) {
|
|
|
CourseInfo c = new CourseInfo();
|
|
|
c.setCourseInfoId(oldCourseInfo.getCourseInfoId());
|
|
|
c.setCourseCoursewareId(null != oldCourseInfo.getCourseCoursewareId() && oldCourseInfo.getCourseCoursewareId().equals(oldResourcesInfo.getResourcesInfoId()) ? null : oldCourseInfo.getCourseCoursewareId());
|
|
|
courseInfoDao.updateCoursewareIdById(c);
|
|
|
} else if (((Integer) 3).equals(oldResourcesInfo.getResourcesInfoType())) {
|
|
|
CourseInfo c = new CourseInfo();
|
|
|
c.setCourseInfoId(oldCourseInfo.getCourseInfoId());
|
|
|
c.setCourseEnclosureIds(StringUtils.isNotBlank(oldCourseInfo.getCourseEnclosureIds()) ? (oldCourseInfo.getCourseEnclosureIds().replace(oldResourcesInfo.getResourcesInfoId().toString(), "").replace(",,", ",")) : oldCourseInfo.getCourseEnclosureIds());
|
|
|
courseInfoDao.updateEnclosureIdsById(c);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
resourcesInfoDao.deleteResourcesInfoByIds(ids);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 课程资源删除
|
|
|
* @param ids
|
|
|
* @param coreUser
|
|
|
*/
|
|
|
public void deleteCourseSystemOAndUniversities(String ids, CoreUser coreUser) {
|
|
|
if (StringUtils.isNotEmpty(ids)) {
|
|
|
String[] split = ids.split(",");
|
|
|
for (String s : split) {
|
|
|
ResourcesInfo resourcesInfo = getById(Long.parseLong(s));
|
|
|
if (!ObjectUtil.isNull(resourcesInfo)) {
|
|
|
if (coreUser.isAdmin()){
|
|
|
courseInfoService.flushCache(resourcesInfo.getCourseInfoId());
|
|
|
CourseInfo oldCourseInfo = courseInfoDao.single(resourcesInfo.getCourseInfoId());
|
|
|
if (null != oldCourseInfo) {
|
|
|
//资源类型(1视频 2课件 3附件)
|
|
|
if (((Integer) 1).equals(resourcesInfo.getResourcesInfoType())) {
|
|
|
CourseInfo c = new CourseInfo();
|
|
|
c.setCourseInfoId(oldCourseInfo.getCourseInfoId());
|
|
|
c.setCourseVideoId(null != oldCourseInfo.getCourseVideoId() && oldCourseInfo.getCourseVideoId().equals(resourcesInfo.getResourcesInfoId()) ? null : oldCourseInfo.getCourseVideoId());
|
|
|
courseInfoDao.updateVideoIdById(c);
|
|
|
} else if (((Integer) 2).equals(resourcesInfo.getResourcesInfoType())) {
|
|
|
CourseInfo c = new CourseInfo();
|
|
|
c.setCourseInfoId(oldCourseInfo.getCourseInfoId());
|
|
|
c.setCourseCoursewareId(null != oldCourseInfo.getCourseCoursewareId() && oldCourseInfo.getCourseCoursewareId().equals(resourcesInfo.getResourcesInfoId()) ? null : oldCourseInfo.getCourseCoursewareId());
|
|
|
courseInfoDao.updateCoursewareIdById(c);
|
|
|
} else if (((Integer) 3).equals(resourcesInfo.getResourcesInfoType())) {
|
|
|
CourseInfo c = new CourseInfo();
|
|
|
c.setCourseInfoId(oldCourseInfo.getCourseInfoId());
|
|
|
c.setCourseEnclosureIds(StringUtils.isNotBlank(oldCourseInfo.getCourseEnclosureIds()) ? (oldCourseInfo.getCourseEnclosureIds().replace(resourcesInfo.getResourcesInfoId().toString(), "").replace(",,", ",")) : oldCourseInfo.getCourseEnclosureIds());
|
|
|
courseInfoDao.updateEnclosureIdsById(c);
|
|
|
}
|
|
|
}
|
|
|
resourcesInfoDao.deleteResourcesInfoByIds(s);
|
|
|
}
|
|
|
} else {
|
|
|
|
|
|
TeacherOpenCourseMergeResourcesInfo teacherOpenCourseMergeResourcesInfo = teacherOpenCourseMergeResourcesInfoDao.getSQLManager()
|
|
|
.lambdaQuery(TeacherOpenCourseMergeResourcesInfo.class)
|
|
|
.andEq(TeacherOpenCourseMergeResourcesInfo::getTeacherOpenCourseMergeResourcesInfoId,s)
|
|
|
.single();
|
|
|
|
|
|
if (!Objects.isNull(teacherOpenCourseMergeResourcesInfo)) {
|
|
|
TeacherOpenCourseMergeResourcesInfo updateQuery = new TeacherOpenCourseMergeResourcesInfo();
|
|
|
updateQuery.setResourcesInfoStatus(GlobalUpStatusEnum.DEL);
|
|
|
updateQuery.setTeacherOpenCourseMergeResourcesInfoId(teacherOpenCourseMergeResourcesInfo.getTeacherOpenCourseMergeResourcesInfoId());
|
|
|
teacherOpenCourseMergeResourcesInfoDao.upsertByTemplate(updateQuery);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
public boolean addAll(List<ResourcesInfo> resourcesInfoList) {
|
|
|
resourcesInfoList.forEach(item -> {
|
|
|
item.setOrderIndex(BigDecimal.valueOf(RandomUtil.randomDouble(100, 200, 6, RoundingMode.HALF_UP)));
|
|
|
});
|
|
|
insertBatch(resourcesInfoList);
|
|
|
|
|
|
List<CourseInfo> courseInfoList = new ArrayList<>();
|
|
|
|
|
|
for (int i = 0; i < resourcesInfoList.size(); i++) {
|
|
|
|
|
|
CourseInfo courseInfo = courseInfoDao.single(resourcesInfoList.get(i).getCourseInfoId());
|
|
|
if (null != courseInfo) {
|
|
|
//资源类型(1视频 2课件 3附件)
|
|
|
if (((Integer) 1).equals(resourcesInfoList.get(i).getResourcesInfoType())) {
|
|
|
CourseInfo c = new CourseInfo();
|
|
|
c.setCourseInfoId(courseInfo.getCourseInfoId());
|
|
|
c.setCourseVideoId(null != courseInfo.getCourseVideoId() ? courseInfo.getCourseVideoId() : resourcesInfoList.get(i).getResourcesInfoId());
|
|
|
courseInfoList.add(c);
|
|
|
} else if (((Integer) 2).equals(resourcesInfoList.get(i).getResourcesInfoType())) {
|
|
|
CourseInfo c = new CourseInfo();
|
|
|
c.setCourseInfoId(courseInfo.getCourseInfoId());
|
|
|
c.setCourseCoursewareId(null != courseInfo.getCourseCoursewareId() ? courseInfo.getCourseCoursewareId() : resourcesInfoList.get(i).getResourcesInfoId());
|
|
|
courseInfoList.add(c);
|
|
|
} else if (((Integer) 3).equals(resourcesInfoList.get(i).getResourcesInfoType())) {
|
|
|
CourseInfo c = new CourseInfo();
|
|
|
c.setCourseInfoId(courseInfo.getCourseInfoId());
|
|
|
c.setCourseEnclosureIds(StringUtils.isNotBlank(courseInfo.getCourseEnclosureIds()) ? (courseInfo.getCourseEnclosureIds() + "," + resourcesInfoList.get(i).getResourcesInfoId()) : resourcesInfoList.get(i).getResourcesInfoId().toString());
|
|
|
courseInfoList.add(c);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (courseInfoList.size() > 0) {
|
|
|
courseInfoService.updateBatchTemplate(courseInfoList);
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
public boolean add(ResourcesInfo resourcesInfo) {
|
|
|
|
|
|
resourcesInfo.setOrderIndex(BigDecimal.valueOf(RandomUtil.randomDouble(100, 200, 6, RoundingMode.HALF_UP)));
|
|
|
insert(resourcesInfo);
|
|
|
|
|
|
List<CourseInfo> courseInfoList = new ArrayList<>();
|
|
|
|
|
|
CourseInfo courseInfo = courseInfoDao.single(resourcesInfo.getCourseInfoId());
|
|
|
|
|
|
if (null != courseInfo) {
|
|
|
//资源类型(1视频 2课件 3附件)
|
|
|
if (((Integer) 1).equals(resourcesInfo.getResourcesInfoType())) {
|
|
|
CourseInfo c = new CourseInfo();
|
|
|
c.setCourseInfoId(courseInfo.getCourseInfoId());
|
|
|
c.setCourseVideoId(null != courseInfo.getCourseVideoId() ? courseInfo.getCourseVideoId() : resourcesInfo.getResourcesInfoId());
|
|
|
courseInfoList.add(c);
|
|
|
} else if (((Integer) 2).equals(resourcesInfo.getResourcesInfoType())) {
|
|
|
CourseInfo c = new CourseInfo();
|
|
|
c.setCourseInfoId(courseInfo.getCourseInfoId());
|
|
|
c.setCourseCoursewareId(null != courseInfo.getCourseCoursewareId() ? courseInfo.getCourseCoursewareId() : resourcesInfo.getResourcesInfoId());
|
|
|
courseInfoList.add(c);
|
|
|
} else if (((Integer) 3).equals(resourcesInfo.getResourcesInfoType())) {
|
|
|
CourseInfo c = new CourseInfo();
|
|
|
c.setCourseInfoId(courseInfo.getCourseInfoId());
|
|
|
c.setCourseEnclosureIds(StringUtils.isNotBlank(courseInfo.getCourseEnclosureIds()) ? (courseInfo.getCourseEnclosureIds() + "," + resourcesInfo.getResourcesInfoId()) : resourcesInfo.getResourcesInfoId().toString());
|
|
|
courseInfoList.add(c);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (courseInfoList.size() > 0) {
|
|
|
courseInfoService.updateBatchTemplate(courseInfoList);
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
public boolean edit(ResourcesInfo resourcesInfo) {
|
|
|
|
|
|
ResourcesInfo oldResourcesInfo = queryById(resourcesInfo.getResourcesInfoId());
|
|
|
CourseInfo oldCourseInfo = courseInfoDao.single(oldResourcesInfo.getCourseInfoId());
|
|
|
if (null != oldCourseInfo) {
|
|
|
//资源类型(1视频 2课件 3附件)
|
|
|
if (((Integer) 1).equals(oldResourcesInfo.getResourcesInfoType())) {
|
|
|
CourseInfo c = new CourseInfo();
|
|
|
c.setCourseInfoId(oldCourseInfo.getCourseInfoId());
|
|
|
c.setCourseVideoId(null != oldCourseInfo.getCourseVideoId() && oldCourseInfo.getCourseVideoId().equals(resourcesInfo.getResourcesInfoId()) ? null : oldCourseInfo.getCourseVideoId());
|
|
|
courseInfoDao.updateVideoIdById(c);
|
|
|
} else if (((Integer) 2).equals(oldResourcesInfo.getResourcesInfoType())) {
|
|
|
CourseInfo c = new CourseInfo();
|
|
|
c.setCourseInfoId(oldCourseInfo.getCourseInfoId());
|
|
|
c.setCourseCoursewareId(null != oldCourseInfo.getCourseCoursewareId() && oldCourseInfo.getCourseCoursewareId().equals(resourcesInfo.getResourcesInfoId()) ? null : oldCourseInfo.getCourseCoursewareId());
|
|
|
courseInfoDao.updateCoursewareIdById(c);
|
|
|
} else if (((Integer) 3).equals(oldResourcesInfo.getResourcesInfoType())) {
|
|
|
CourseInfo c = new CourseInfo();
|
|
|
c.setCourseInfoId(oldCourseInfo.getCourseInfoId());
|
|
|
c.setCourseEnclosureIds(StringUtils.isNotBlank(oldCourseInfo.getCourseEnclosureIds()) ? (oldCourseInfo.getCourseEnclosureIds().replace(resourcesInfo.getResourcesInfoId().toString(), "").replace(",,", ",")) : oldCourseInfo.getCourseEnclosureIds());
|
|
|
courseInfoDao.updateEnclosureIdsById(c);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
updateTemplate(resourcesInfo);
|
|
|
|
|
|
List<CourseInfo> courseInfoList = new ArrayList<>();
|
|
|
|
|
|
CourseInfo courseInfo = courseInfoDao.single(resourcesInfo.getCourseInfoId());
|
|
|
if (null != courseInfo) {
|
|
|
//资源类型(1视频 2课件 3附件)
|
|
|
if (((Integer) 1).equals(resourcesInfo.getResourcesInfoType())) {
|
|
|
CourseInfo c = new CourseInfo();
|
|
|
c.setCourseInfoId(courseInfo.getCourseInfoId());
|
|
|
c.setCourseVideoId(null != courseInfo.getCourseVideoId() ? courseInfo.getCourseVideoId() : resourcesInfo.getResourcesInfoId());
|
|
|
courseInfoList.add(c);
|
|
|
} else if (((Integer) 2).equals(resourcesInfo.getResourcesInfoType())) {
|
|
|
CourseInfo c = new CourseInfo();
|
|
|
c.setCourseInfoId(courseInfo.getCourseInfoId());
|
|
|
c.setCourseCoursewareId(null != courseInfo.getCourseCoursewareId() ? courseInfo.getCourseCoursewareId() : resourcesInfo.getResourcesInfoId());
|
|
|
courseInfoList.add(c);
|
|
|
} else if (((Integer) 3).equals(resourcesInfo.getResourcesInfoType())) {
|
|
|
CourseInfo c = new CourseInfo();
|
|
|
c.setCourseInfoId(courseInfo.getCourseInfoId());
|
|
|
c.setCourseEnclosureIds(StringUtils.isNotBlank(courseInfo.getCourseEnclosureIds()) ? (courseInfo.getCourseEnclosureIds() + "," + resourcesInfo.getResourcesInfoId()) : resourcesInfo.getResourcesInfoId().toString());
|
|
|
courseInfoList.add(c);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (courseInfoList.size() > 0) {
|
|
|
courseInfoService.updateBatchTemplate(courseInfoList);
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
public List<ResourcesInfo> getValuesByQuery(ResourcesInfoQuery resourcesInfoQuery) {
|
|
|
return resourcesInfoDao.getValuesByQuery(resourcesInfoQuery);
|
|
|
}
|
|
|
|
|
|
public List<ResourcesInfo> getValues(Object paras) {
|
|
|
return sqlManager.select(SqlId.of("jlw.resourcesInfo.getResourcesInfoValues"), ResourcesInfo.class, paras);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 从系统资源中拷贝
|
|
|
*
|
|
|
* @param resourcesInfoIds
|
|
|
* @param courseId
|
|
|
*/
|
|
|
public void copy(String resourcesInfoIds, Long courseId) {
|
|
|
ResourcesInfoQuery resourcesInfoQuery = new ResourcesInfoQuery();
|
|
|
resourcesInfoQuery.setResourcesInfoIds(resourcesInfoIds);
|
|
|
List<ResourcesInfo> list = getValuesByQuery(resourcesInfoQuery);
|
|
|
|
|
|
List<ResourcesInfo> insertList = CollectionUtil.emptyIfNull(list).stream().map(item -> {
|
|
|
item.setCourseInfoId(courseId);
|
|
|
item.setResourcesInfoId(null);
|
|
|
return item;
|
|
|
}).collect(Collectors.toList());
|
|
|
|
|
|
if (ObjectUtil.isNotEmpty(insertList)) {
|
|
|
insertBatch(insertList);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 位置移动
|
|
|
* 小于0的数字,是代表置顶。大于0代表非置顶
|
|
|
*
|
|
|
* @param id
|
|
|
* @param moveType
|
|
|
*/
|
|
|
public void move(@NotNull(message = "ID不能为空!") Long id, @NotNull(message = "移动类型不能为空!") MoveEnum moveType) {
|
|
|
List<ResourcesInfo> links = resourcesInfoDao.createLambdaQuery().andIn(ResourcesInfo::getResourcesInfoId, Arrays.asList(id))
|
|
|
.select();
|
|
|
|
|
|
Assert.notEmpty(links, "未查询到要移动的元素!");
|
|
|
|
|
|
ResourcesInfo mergeCourseInfo = links.get(0);
|
|
|
|
|
|
// 定义排序值
|
|
|
Double calcedOrder = null;
|
|
|
|
|
|
switch (moveType) {
|
|
|
|
|
|
case MOVE_TOP: {
|
|
|
List<ResourcesInfo> result = resourcesInfoDao.createLambdaQuery()
|
|
|
.andEq(ResourcesInfo::getCourseInfoId, mergeCourseInfo.getCourseInfoId())
|
|
|
.andNotEq(ResourcesInfo::getResourcesInfoId, id)
|
|
|
.asc(ResourcesInfo::getOrderIndex)
|
|
|
.limit(1, 1)
|
|
|
.select(ResourcesInfo::getOrderIndex);
|
|
|
|
|
|
if (CollectionUtil.isEmpty(result)) {
|
|
|
throw new PlatformException("已处于置顶状态!");
|
|
|
}
|
|
|
|
|
|
ResourcesInfo minOrderClientLink = result.get(0);
|
|
|
// 最小的排序值
|
|
|
BigDecimal minLinkOrder = minOrderClientLink.getOrderIndex();
|
|
|
// 置顶就现有的最小值减1。但是需要确保结果小于0
|
|
|
calcedOrder = NumberUtil.sub(min(minLinkOrder.floatValue(), 0), 1);
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
|
|
|
case MOVE_LEFT: {
|
|
|
List<ResourcesInfo> result = resourcesInfoDao.createLambdaQuery()
|
|
|
.andEq(ResourcesInfo::getCourseInfoId, mergeCourseInfo.getCourseInfoId())
|
|
|
.andLess(ResourcesInfo::getOrderIndex, defaultIfNull(mergeCourseInfo.getOrderIndex(), 50))
|
|
|
.andNotEq(ResourcesInfo::getResourcesInfoId, id)
|
|
|
.desc(ResourcesInfo::getOrderIndex)
|
|
|
.limit(1, 2)
|
|
|
.select(ResourcesInfo::getOrderIndex);
|
|
|
|
|
|
if (CollectionUtil.isEmpty(result)) {
|
|
|
throw new PlatformException("已处于置顶状态!");
|
|
|
}
|
|
|
|
|
|
// 找到应该安插的位置
|
|
|
BigDecimal LinkOrder0 = result.get(0).getOrderIndex();
|
|
|
BigDecimal LinkOrder1 = LinkOrder0.subtract(BigDecimal.ONE);
|
|
|
if (result.size() == 2) {
|
|
|
LinkOrder1 = result.get(1).getOrderIndex();
|
|
|
}
|
|
|
|
|
|
BigDecimal min = NumberUtil.min(LinkOrder0, LinkOrder1);
|
|
|
BigDecimal max = NumberUtil.max(LinkOrder0, LinkOrder1);
|
|
|
|
|
|
// 左移位后的值
|
|
|
calcedOrder = RandomUtil.randomDouble(min.doubleValue(), max.doubleValue(), 6, RoundingMode.HALF_UP);
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
|
|
|
case MOVE_RIGHT: {
|
|
|
// 要查询这个章节节点的数据集合 故:.andEq(ResourcesInfo::getCourseInfoId, mergeCourseInfo.getCourseInfoId())
|
|
|
List<ResourcesInfo> result = resourcesInfoDao.createLambdaQuery()
|
|
|
.andGreat(ResourcesInfo::getOrderIndex, defaultIfNull(mergeCourseInfo.getOrderIndex(), 50))
|
|
|
.andEq(ResourcesInfo::getCourseInfoId, mergeCourseInfo.getCourseInfoId())
|
|
|
.andNotEq(ResourcesInfo::getResourcesInfoId, id)
|
|
|
.asc(ResourcesInfo::getOrderIndex)
|
|
|
.limit(1, 2)
|
|
|
.select(ResourcesInfo::getOrderIndex);
|
|
|
|
|
|
if (CollectionUtil.isEmpty(result)) {
|
|
|
throw new PlatformException("已处于置底状态!");
|
|
|
}
|
|
|
|
|
|
// 找到应该安插的位置
|
|
|
BigDecimal LinkOrder0 = result.get(0).getOrderIndex();
|
|
|
BigDecimal LinkOrder1 = LinkOrder0.add(BigDecimal.ONE);
|
|
|
if (result.size() == 2) {
|
|
|
LinkOrder1 = result.get(1).getOrderIndex();
|
|
|
}
|
|
|
|
|
|
BigDecimal min = NumberUtil.min(LinkOrder0, LinkOrder1);
|
|
|
BigDecimal max = NumberUtil.max(LinkOrder0, LinkOrder1);
|
|
|
|
|
|
// 右移位后的值
|
|
|
calcedOrder = RandomUtil.randomDouble(min.doubleValue(), max.doubleValue(), 6, RoundingMode.HALF_UP);
|
|
|
}
|
|
|
break;
|
|
|
default:
|
|
|
log.error("不支持的移动类型!");
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
if (calcedOrder != null) {
|
|
|
// 改变排序记录值
|
|
|
ResourcesInfo updatePO = new ResourcesInfo();
|
|
|
updatePO.setResourcesInfoId(mergeCourseInfo.getResourcesInfoId());
|
|
|
updatePO.setOrderIndex(BigDecimal.valueOf(calcedOrder));
|
|
|
updateTemplate(updatePO);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 功能描述: <br>
|
|
|
* 拖动
|
|
|
* 只传左元素ID,表示:拖动元素 在 左元素的左侧;
|
|
|
* 只传右元素ID,表示:拖动元素 在 右元素的右侧;
|
|
|
* 左右元素ID都传,则拖动元素放置在两个元素的中间。
|
|
|
*
|
|
|
* @param id 拖动元素的ID
|
|
|
* @param leftId 左元素ID 偏小数值对应的元素ID
|
|
|
* @param rightId 右元素ID 偏大数值对应的元素ID
|
|
|
* <p>
|
|
|
* 左侧(上) ==> 右侧(下)
|
|
|
* 小数值 ==> 大数值
|
|
|
* <p>
|
|
|
* 数值越小,越靠前
|
|
|
* @Author: 87966
|
|
|
* @Date: 2023/1/9 16:44
|
|
|
*/
|
|
|
public void drag(@NotNull(message = "要移动的元素 ID不能为空!") final Long id, final Long leftId, final Long rightId) {
|
|
|
|
|
|
// 查询出来所有元素
|
|
|
List<ResourcesInfo> allElement = resourcesInfoDao.createLambdaQuery().andIn(ResourcesInfo::getResourcesInfoId, Arrays.asList(id, leftId, rightId))
|
|
|
.select();
|
|
|
|
|
|
// 自身ID是否存在
|
|
|
boolean hasSelf = allElement.stream().anyMatch(item -> item.getResourcesInfoId().equals(id));
|
|
|
// 是否有左元素
|
|
|
boolean hasLeft = allElement.stream().anyMatch(item -> item.getResourcesInfoId().equals(leftId));
|
|
|
// 是否有元素
|
|
|
boolean hasRight = allElement.stream().anyMatch(item -> item.getResourcesInfoId().equals(rightId));
|
|
|
|
|
|
// 断言
|
|
|
Assert.isTrue(hasSelf, "未查询到要移动的元素!");
|
|
|
Assert.isFalse(!hasLeft && !hasRight, "左右元素ID不能同时为空!");
|
|
|
|
|
|
// 自身元素
|
|
|
ResourcesInfo selfElement = allElement.stream()
|
|
|
.filter(item -> item.getResourcesInfoId().equals(id)).findFirst().get();
|
|
|
// 左侧元素
|
|
|
ResourcesInfo leftElement = allElement.stream()
|
|
|
.filter(item -> item.getResourcesInfoId().equals(leftId)).findFirst().orElse(null);
|
|
|
// 右侧元素
|
|
|
ResourcesInfo rightElement = allElement.stream()
|
|
|
.filter(item -> item.getResourcesInfoId().equals(rightId)).findFirst().orElse(null);
|
|
|
|
|
|
// 定义排序值
|
|
|
BigDecimal clacedOrder = null;
|
|
|
|
|
|
// 左移动,无法确定右侧边界,适用于置顶
|
|
|
if (hasLeft && !hasRight) {
|
|
|
BigDecimal leftOrder = defaultIfNull(leftElement.getOrderIndex(), BigDecimal.ZERO);
|
|
|
// 数据库里存小数点后六位的排序值
|
|
|
// 随机取值 0.0001 到 0.0005之间
|
|
|
BigDecimal randomNum = RandomUtil.randomBigDecimal(new BigDecimal("0.0001"), new BigDecimal("0.0005")).setScale(6, ROUND_HALF_UP);
|
|
|
// 计算后的排序值
|
|
|
clacedOrder = leftOrder.subtract(randomNum);
|
|
|
}
|
|
|
|
|
|
// 右移动,无法确定左侧边界,适用于置底
|
|
|
else if (!hasLeft && hasRight) {
|
|
|
BigDecimal rightOrder = defaultIfNull(rightElement.getOrderIndex(), BigDecimal.ZERO);
|
|
|
// 数据库里存小数点后六位的排序值
|
|
|
// 随机取值 0.0001 到 0.0005之间
|
|
|
BigDecimal randomNum = RandomUtil.randomBigDecimal(new BigDecimal("0.0001"), new BigDecimal("0.0005")).setScale(6, ROUND_HALF_UP);
|
|
|
// 计算后的排序值
|
|
|
clacedOrder = rightOrder.add(randomNum);
|
|
|
|
|
|
}
|
|
|
|
|
|
// 可以确定左右两侧边界,移动的比较精准
|
|
|
else if (hasLeft && hasRight) {
|
|
|
BigDecimal leftOrder = defaultIfNull(leftElement.getOrderIndex(), BigDecimal.ZERO);
|
|
|
BigDecimal rightOrder = defaultIfNull(rightElement.getOrderIndex(), BigDecimal.ONE);
|
|
|
|
|
|
BigDecimal min = NumberUtil.min(leftOrder, rightOrder);
|
|
|
BigDecimal max = NumberUtil.max(leftOrder, rightOrder);
|
|
|
// 随机取值
|
|
|
BigDecimal randomNum = RandomUtil.randomBigDecimal(min, max).setScale(6, ROUND_HALF_UP);
|
|
|
// 计算后的排序值
|
|
|
clacedOrder = min.add(randomNum);
|
|
|
}
|
|
|
|
|
|
// 最后更新排序值
|
|
|
if (clacedOrder != null) {
|
|
|
ResourcesInfo updatePO = new ResourcesInfo();
|
|
|
updatePO.setResourcesInfoId(selfElement.getResourcesInfoId());
|
|
|
updatePO.setOrderIndex(clacedOrder);
|
|
|
updateTemplate(updatePO);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
public List<ResourcesInfo> getByCourseInfoIds(List<Long> courseInfoIds) {
|
|
|
if (CollectionUtils.isEmpty(courseInfoIds)) {
|
|
|
return Collections.emptyList();
|
|
|
}
|
|
|
return resourcesInfoDao.createLambdaQuery()
|
|
|
.andIn(ResourcesInfo::getCourseInfoId, courseInfoIds)
|
|
|
.select();
|
|
|
}
|
|
|
} |