From cd475f080eea132191cfb7ad614b18ed3b19ee77 Mon Sep 17 00:00:00 2001 From: wsw Date: Fri, 28 Jul 2023 18:01:35 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BE=E7=A8=8B=E9=99=84=E4=BB=B6=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- databases/v1.2.sql | 14 +- .../playedu/api/domain/CourseAttachment.java | 125 ++++++++++++++++++ .../api/mapper/CourseAttachmentMapper.java | 23 ++++ .../api/service/CourseAttachmentService.java | 48 +++++++ .../internal/CourseAttachmentServiceImpl.java | 124 +++++++++++++++++ .../mapper/CourseAttachmentMapper.xml | 22 +++ 6 files changed, 355 insertions(+), 1 deletion(-) create mode 100644 src/main/java/xyz/playedu/api/domain/CourseAttachment.java create mode 100644 src/main/java/xyz/playedu/api/mapper/CourseAttachmentMapper.java create mode 100644 src/main/java/xyz/playedu/api/service/CourseAttachmentService.java create mode 100644 src/main/java/xyz/playedu/api/service/impl/internal/CourseAttachmentServiceImpl.java create mode 100644 src/main/resources/mapper/CourseAttachmentMapper.xml diff --git a/databases/v1.2.sql b/databases/v1.2.sql index 36238e1..21429ea 100644 --- a/databases/v1.2.sql +++ b/databases/v1.2.sql @@ -18,4 +18,16 @@ CREATE TABLE `admin_logs` `created_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`), KEY `a_m_o` (`admin_id`,`module`,`opt`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; \ No newline at end of file +) 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; \ No newline at end of file diff --git a/src/main/java/xyz/playedu/api/domain/CourseAttachment.java b/src/main/java/xyz/playedu/api/domain/CourseAttachment.java new file mode 100644 index 0000000..9edb2ae --- /dev/null +++ b/src/main/java/xyz/playedu/api/domain/CourseAttachment.java @@ -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(); + } +} diff --git a/src/main/java/xyz/playedu/api/mapper/CourseAttachmentMapper.java b/src/main/java/xyz/playedu/api/mapper/CourseAttachmentMapper.java new file mode 100644 index 0000000..fe25ebb --- /dev/null +++ b/src/main/java/xyz/playedu/api/mapper/CourseAttachmentMapper.java @@ -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 {} diff --git a/src/main/java/xyz/playedu/api/service/CourseAttachmentService.java b/src/main/java/xyz/playedu/api/service/CourseAttachmentService.java new file mode 100644 index 0000000..f054fd0 --- /dev/null +++ b/src/main/java/xyz/playedu/api/service/CourseAttachmentService.java @@ -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 findOrFail(Integer id, Integer courseId) throws NotFoundException; + + void update(CourseAttachment courseAttachment, Integer sort, String title); + + List 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 ids, Integer cid); + + List getRidsByCourseId(Integer courseId, String type); + + List chunk(List attachmentIds); +} diff --git a/src/main/java/xyz/playedu/api/service/impl/internal/CourseAttachmentServiceImpl.java b/src/main/java/xyz/playedu/api/service/impl/internal/CourseAttachmentServiceImpl.java new file mode 100644 index 0000000..eca5c54 --- /dev/null +++ b/src/main/java/xyz/playedu/api/service/impl/internal/CourseAttachmentServiceImpl.java @@ -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 + 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 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 ids, Integer cid) { + if (ids == null || ids.size() == 0) { + return; + } + List 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 getRidsByCourseId(Integer courseId, String type) { + return list(query().getWrapper().eq("course_id", courseId).eq("type", type)).stream() + .map(CourseAttachment::getRid) + .toList(); + } + + @Override + public List chunk(List attachmentIds) { + if (attachmentIds == null || attachmentIds.size() == 0) { + return new ArrayList<>(); + } + return list(query().getWrapper().in("id", attachmentIds)); + } +} diff --git a/src/main/resources/mapper/CourseAttachmentMapper.xml b/src/main/resources/mapper/CourseAttachmentMapper.xml new file mode 100644 index 0000000..77065cd --- /dev/null +++ b/src/main/resources/mapper/CourseAttachmentMapper.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + id,course_id, + sort,title,type, + rid,created_at + +