课程附件表

This commit is contained in:
wsw 2023-07-28 18:01:35 +08:00
parent e0f807909f
commit cd475f080e
6 changed files with 355 additions and 1 deletions

View File

@ -19,3 +19,15 @@ CREATE TABLE `admin_logs`
PRIMARY KEY (`id`),
KEY `a_m_o` (`admin_id`,`module`,`opt`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `course_attachment` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`course_id` int(11) NOT NULL DEFAULT '0' COMMENT '课程ID',
`sort` int(11) NOT NULL DEFAULT '0' COMMENT '升序',
`title` varchar(255) NOT NULL DEFAULT '' COMMENT '附件名',
`type` varchar(20) NOT NULL DEFAULT '' COMMENT '附件类型',
`rid` int(11) NOT NULL DEFAULT '0' COMMENT '资源id',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `course_id` (`course_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

View File

@ -0,0 +1,125 @@
/*
* Copyright (C) 2023 杭州白书科技有限公司
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.playedu.api.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* @TableName course_attachment
*/
@TableName(value = "course_attachment")
@Data
public class CourseAttachment implements Serializable {
/** */
@TableId(type = IdType.AUTO)
private Integer id;
/** 课程ID */
@JsonProperty("course_id")
private Integer courseId;
/** 升序 */
private Integer sort;
/** 附件名 */
private String title;
/** 附件类型 */
private String type;
/** 资源id */
private Integer rid;
/** */
@JsonIgnore private Date createdAt;
@TableField(exist = false)
private static final long serialVersionUID = 1L;
@Override
public boolean equals(Object that) {
if (this == that) {
return true;
}
if (that == null) {
return false;
}
if (getClass() != that.getClass()) {
return false;
}
CourseAttachment other = (CourseAttachment) that;
return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
&& (this.getCourseId() == null
? other.getCourseId() == null
: this.getCourseId().equals(other.getCourseId()))
&& (this.getSort() == null
? other.getSort() == null
: this.getSort().equals(other.getSort()))
&& (this.getTitle() == null
? other.getTitle() == null
: this.getTitle().equals(other.getTitle()))
&& (this.getType() == null
? other.getType() == null
: this.getType().equals(other.getType()))
&& (this.getRid() == null
? other.getRid() == null
: this.getRid().equals(other.getRid()))
&& (this.getCreatedAt() == null
? other.getCreatedAt() == null
: this.getCreatedAt().equals(other.getCreatedAt()));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
result = prime * result + ((getCourseId() == null) ? 0 : getCourseId().hashCode());
result = prime * result + ((getSort() == null) ? 0 : getSort().hashCode());
result = prime * result + ((getTitle() == null) ? 0 : getTitle().hashCode());
result = prime * result + ((getType() == null) ? 0 : getType().hashCode());
result = prime * result + ((getRid() == null) ? 0 : getRid().hashCode());
result = prime * result + ((getCreatedAt() == null) ? 0 : getCreatedAt().hashCode());
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", courseId=").append(courseId);
sb.append(", sort=").append(sort);
sb.append(", title=").append(title);
sb.append(", type=").append(type);
sb.append(", rid=").append(rid);
sb.append(", createdAt=").append(createdAt);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
}
}

View File

@ -0,0 +1,23 @@
/*
* Copyright (C) 2023 杭州白书科技有限公司
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.playedu.api.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import xyz.playedu.api.domain.CourseAttachment;
@Mapper
public interface CourseAttachmentMapper extends BaseMapper<CourseAttachment> {}

View File

@ -0,0 +1,48 @@
/*
* Copyright (C) 2023 杭州白书科技有限公司
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.playedu.api.service;
import com.baomidou.mybatisplus.extension.service.IService;
import xyz.playedu.api.domain.CourseAttachment;
import xyz.playedu.api.exception.NotFoundException;
import java.util.List;
public interface CourseAttachmentService extends IService<CourseAttachment> {
CourseAttachment findOrFail(Integer id, Integer courseId) throws NotFoundException;
void update(CourseAttachment courseAttachment, Integer sort, String title);
List<CourseAttachment> getAttachmentByCourseId(Integer courseId);
CourseAttachment create(
Integer courseId,
Integer sort,
String title,
String type,
Integer rid);
Integer getCountByCourseId(Integer courseId);
void remove(Integer courseId);
void updateSort(List<Integer> ids, Integer cid);
List<Integer> getRidsByCourseId(Integer courseId, String type);
List<CourseAttachment> chunk(List<Integer> attachmentIds);
}

View File

@ -0,0 +1,124 @@
/*
* Copyright (C) 2023 杭州白书科技有限公司
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.playedu.api.service.impl.internal;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.playedu.api.domain.CourseAttachment;
import xyz.playedu.api.exception.NotFoundException;
import xyz.playedu.api.mapper.CourseAttachmentMapper;
import xyz.playedu.api.service.CourseAttachmentService;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class CourseAttachmentServiceImpl extends ServiceImpl<CourseAttachmentMapper, CourseAttachment>
implements CourseAttachmentService {
@Override
public CourseAttachment findOrFail(Integer id, Integer courseId) throws NotFoundException {
CourseAttachment attachment = getOne(query().getWrapper().eq("id", id).eq("course_id", courseId));
if (attachment == null) {
throw new NotFoundException("附件不存在");
}
return attachment;
}
@Override
public void update(
CourseAttachment courseAttachment,
Integer sort,
String title) {
CourseAttachment attachment = new CourseAttachment();
attachment.setId(courseAttachment.getId());
attachment.setSort(sort);
attachment.setTitle(title);
updateById(attachment);
}
@Override
public List<CourseAttachment> getAttachmentByCourseId(Integer courseId) {
return list(query().getWrapper().eq("course_id", courseId).orderByAsc("sort"));
}
@Override
public CourseAttachment create(
Integer courseId,
Integer sort,
String title,
String type,
Integer rid) {
CourseAttachment attachment = new CourseAttachment();
attachment.setCourseId(courseId);
attachment.setSort(sort);
attachment.setTitle(title);
attachment.setType(type);
attachment.setRid(rid);
attachment.setCreatedAt(new Date());
save(attachment);
return attachment;
}
@Override
public Integer getCountByCourseId(Integer courseId) {
return Math.toIntExact(count(query().getWrapper().eq("course_id", courseId)));
}
@Override
public void remove(Integer courseId) {
remove(query().getWrapper().eq("course_id", courseId));
}
@Override
public void updateSort(List<Integer> ids, Integer cid) {
if (ids == null || ids.size() == 0) {
return;
}
List<CourseAttachment> attachments = new ArrayList<>();
final Integer[] sortVal = {0};
for (Integer idVal : ids) {
attachments.add(
new CourseAttachment() {
{
setId(idVal);
setCourseId(cid);
setSort(sortVal[0]++);
}
});
}
updateBatchById(attachments);
}
@Override
public List<Integer> getRidsByCourseId(Integer courseId, String type) {
return list(query().getWrapper().eq("course_id", courseId).eq("type", type)).stream()
.map(CourseAttachment::getRid)
.toList();
}
@Override
public List<CourseAttachment> chunk(List<Integer> attachmentIds) {
if (attachmentIds == null || attachmentIds.size() == 0) {
return new ArrayList<>();
}
return list(query().getWrapper().in("id", attachmentIds));
}
}

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="xyz.playedu.api.mapper.CourseAttachmentMapper">
<resultMap id="BaseResultMap" type="xyz.playedu.api.domain.CourseAttachment">
<id property="id" column="id" jdbcType="INTEGER"/>
<result property="courseId" column="course_id" jdbcType="INTEGER"/>
<result property="sort" column="sort" jdbcType="INTEGER"/>
<result property="title" column="title" jdbcType="VARCHAR"/>
<result property="type" column="type" jdbcType="VARCHAR"/>
<result property="rid" column="rid" jdbcType="INTEGER"/>
<result property="createdAt" column="created_at" jdbcType="TIMESTAMP"/>
</resultMap>
<sql id="Base_Column_List">
id,course_id,
sort,title,type,
rid,created_at
</sql>
</mapper>