Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #301

Merged
merged 5 commits into from
Sep 15, 2024
Merged

Dev #301

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public enum CodeEnums {
APPLY_REPEAT("1024", "apply repeat"),

NOT_FOUND_BOUNTY("1025", "not found bounty"),
NOT_FOUND_COURSE("1026", "not found course"),

// distribute
ID_NEED_EMPTY("6000", "Id need empty"),
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/com/dl/officialsite/course/Course.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.dl.officialsite.course;

import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Data
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "course")
public class Course {
/**
* create table course
* (
* id bigint auto_increment
* primary key,
* course_name varchar(255) null comment '课程名称',
* remark varchar(1024) null comment '课程简介',
* cooperate_community varchar(100) null comment '合作社区',
* creator varchar(64) null comment '创建者',
* updater varchar(64) null comment '更新者',
* create_time bigint null comment '创建时间',
* update_time bigint null comment '更新时间',
* status int null comment '状态,0:进行中,1:已结束'
* )
* comment '课程表';
*/

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
//课程名称
private String courseName;
//课程简介
private String remark;
//合作社区
private String cooperateCommunity;
//创建者
private String creator;
//更新者
private String updater;
//创建时间
@CreatedDate
@Column(updatable = false)
private Long createTime;
//更新时间
@LastModifiedDate
@Column( updatable = false ,nullable = false)
private Long updateTime;
//状态,0:进行中,1:已结束
private Integer status;
}
69 changes: 69 additions & 0 deletions src/main/java/com/dl/officialsite/course/CourseController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.dl.officialsite.course;

import com.dl.officialsite.common.base.BaseResponse;
import com.dl.officialsite.course.vo.CourseAddVo;
import com.dl.officialsite.course.vo.CourseQueryVo;
import com.dl.officialsite.course.vo.CourseSearchVo;
import com.dl.officialsite.course.vo.CourseUpdateVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
* @Description
* @Author xiaoming
* @Date 2024/9/12 7:37 PM
**/
@RestController
@RequestMapping("/course")
@Slf4j
public class CourseController {

@Resource
private CourseService courseService;


@PostMapping("/add")
public BaseResponse add(@RequestBody CourseAddVo courseAddVo, @RequestParam String address) {
return BaseResponse.successWithData(courseService.add(courseAddVo, address));
}

@PostMapping("/update")
public BaseResponse update(@RequestBody CourseUpdateVo courseUpdateVo, @RequestParam String address) {
courseService.update(courseUpdateVo, address);
return BaseResponse.successWithData(null);
}

@PostMapping("/detail")
public BaseResponse detail(@RequestParam Long id, @RequestParam(defaultValue = "1") Integer pageNumber,
@RequestParam(defaultValue = "10") Integer pageSize) {
Pageable pageable = PageRequest.of(pageNumber - 1, pageSize, Sort.by(Sort.Direction.DESC, "createTime"));
return BaseResponse.successWithData(courseService.detail(id, pageable));
}

@PostMapping("/list")
public BaseResponse list(
@RequestParam(defaultValue = "1") Integer pageNumber,
@RequestParam(defaultValue = "10") Integer pageSize) {
Pageable pageable = PageRequest.of(pageNumber - 1, pageSize, Sort.by(Sort.Direction.DESC, "createTime"));
CourseSearchVo courseSearchVo = new CourseSearchVo();
Page<CourseQueryVo> page = courseService.search(courseSearchVo, pageable);
return BaseResponse.successWithData(page);
}

@PostMapping("/delete")
public BaseResponse delete(@RequestParam Long id, @RequestParam String address) {
courseService.delete(id);
return BaseResponse.successWithData(null);
}

}
11 changes: 11 additions & 0 deletions src/main/java/com/dl/officialsite/course/CourseRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.dl.officialsite.course;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;


public interface CourseRepository extends JpaRepository<Course, Long>, JpaSpecificationExecutor<Course> {



}
136 changes: 136 additions & 0 deletions src/main/java/com/dl/officialsite/course/CourseService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package com.dl.officialsite.course;

import cn.hutool.core.collection.CollUtil;
import com.dl.officialsite.common.enums.CodeEnums;
import com.dl.officialsite.common.exception.BizException;
import com.dl.officialsite.course.constant.CourseStatusEnums;
import com.dl.officialsite.course.vo.CourseAddVo;
import com.dl.officialsite.course.vo.CourseQueryVo;
import com.dl.officialsite.course.vo.CourseSearchVo;
import com.dl.officialsite.course.vo.CourseUpdateVo;
import com.dl.officialsite.sharing.Share;
import com.dl.officialsite.sharing.SharingRepository;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import static com.dl.officialsite.common.enums.CodeEnums.NOT_FOUND_COURSE;

/**
* @Description
* @Author xiaoming
* @Date 2024/9/12 7:32 PM
**/
@Service
@Slf4j
public class CourseService {

@Resource
private CourseRepository courseRepository;
@Resource
private SharingRepository sharingRepository;
public Course add(CourseAddVo courseAddVo, String address) {
Course course = new Course();
BeanUtils.copyProperties(courseAddVo, course);
course.setCreator(address);
course.setUpdater(address);
course.setStatus(CourseStatusEnums.ING.getCode());
courseRepository.save(course);
Long courseId = course.getId();

List<Long> shareIdList = courseAddVo.getShareIdList();
shareIdList.forEach(shareId -> {
Share share = sharingRepository.findById(shareId).orElseThrow(() -> new BizException(CodeEnums.SHARING_NOT_FOUND.getCode(), CodeEnums.SHARING_NOT_FOUND.getMsg()));
share.setCourseId(courseId);
sharingRepository.save(share);
});
return course;
}

@Transactional(rollbackFor = Exception.class)
public void update(CourseUpdateVo courseUpdateVo, String address) {
Long courseId = courseUpdateVo.getId();
Course course = courseRepository.findById(courseUpdateVo.getId()).orElseThrow(() -> new BizException(NOT_FOUND_COURSE.getCode(), NOT_FOUND_COURSE.getMsg()));
BeanUtils.copyProperties(courseUpdateVo, course);
course.setUpdater(address);
courseRepository.save(course);

List<Long> shareIdList = courseUpdateVo.getShareIdList();
//先将分享的课程id设置为null
List<Share> oldShareList = sharingRepository.findByCourseId(courseId);
List<Long> oldIdList = oldShareList.stream().map(Share::getId).collect(Collectors.toList());
if(CollUtil.isNotEmpty(oldShareList)){
Collection<Long> subtract = CollUtil.subtract(oldIdList, shareIdList);
if(CollUtil.isNotEmpty(subtract)){
sharingRepository.updateCourseIdToNull(Lists.newArrayList(subtract));
}
}
//设置新的分享课程id
shareIdList.forEach(shareId -> {
Share share = sharingRepository.findById(shareId).orElseThrow(() -> new BizException(CodeEnums.SHARING_NOT_FOUND.getCode(), CodeEnums.SHARING_NOT_FOUND.getMsg()));
share.setCourseId(courseId);
sharingRepository.save(share);
});
}

public Page<CourseQueryVo> search(CourseSearchVo courseSearchVo, Pageable pageable) {
Page<Course> all = courseRepository.findAll((root, query, criteriaBuilder) -> {
List<Predicate> predicates = new ArrayList<>();
if (StringUtils.isNotBlank(courseSearchVo.getCourseName())) {
predicates.add(criteriaBuilder.like(root.get("courseName"), "%" + courseSearchVo.getCourseName() + "%"));
}
if (StringUtils.isNotBlank(courseSearchVo.getRemark())) {
predicates.add(criteriaBuilder.like(root.get("remark"), "%" + courseSearchVo.getRemark() + "%"));
}
if (StringUtils.isNotBlank(courseSearchVo.getCooperateCommunity())) {
predicates.add(criteriaBuilder.like(root.get("cooperateCommunity"), "%" + courseSearchVo.getCooperateCommunity() + "%"));
}
if (courseSearchVo.getStatus() != null) {
predicates.add(criteriaBuilder.equal(root.get("status"), courseSearchVo.getStatus()));
}
return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
}, pageable);
return all.map(course -> {
CourseQueryVo courseQueryVo = new CourseQueryVo();
BeanUtils.copyProperties(course, courseQueryVo);
return courseQueryVo;
});
}

public CourseQueryVo detail(Long id, Pageable pageable) {
Course course = courseRepository.findById(id).orElseThrow(() -> new BizException(NOT_FOUND_COURSE.getCode(), NOT_FOUND_COURSE.getMsg()));
CourseQueryVo courseQueryVo = new CourseQueryVo();
BeanUtils.copyProperties(course, courseQueryVo);
//通过courseId分页查找share
Page<Share> all = sharingRepository.findAll((root, query, criteriaBuilder) -> {
List<Predicate> predicates = new ArrayList<>();
predicates.add(criteriaBuilder.equal(root.get("courseId"), id));
return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
}, pageable);
courseQueryVo.setSharePage(all);
return courseQueryVo;
}

public void delete(Long id) {
Course course = courseRepository.findById(id).orElseThrow(() -> new BizException(NOT_FOUND_COURSE.getCode(), NOT_FOUND_COURSE.getMsg()));
courseRepository.delete(course);

//先将分享的课程id设置为null
List<Share> oldShareList = sharingRepository.findByCourseId(id);
List<Long> oldIdList = oldShareList.stream().map(Share::getId).collect(Collectors.toList());
if(CollUtil.isNotEmpty(oldShareList)){
sharingRepository.updateCourseIdToNull(oldIdList);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.dl.officialsite.course.constant;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
* @Description
* @Author xiaoming
* @Date 2024/9/12 7:40 PM
**/
@Getter
@AllArgsConstructor
public enum CourseStatusEnums {
/**
* 状态,0:进行中,1:已结束
*/
ING(0, "进行中"),
END(1, "已结束");


private int code;

private String message;
}
26 changes: 26 additions & 0 deletions src/main/java/com/dl/officialsite/course/vo/CourseAddVo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.dl.officialsite.course.vo;

import lombok.Data;

import java.util.List;

/**
* @Description
* @Author xiaoming
* @Date 2024/9/12 7:35 PM
**/
@Data
public class CourseAddVo {

//课程名称
private String courseName;
//课程简介
private String remark;
//合作社区
private String cooperateCommunity;

/**
* 分享id列表
*/
private List<Long> shareIdList;
}
40 changes: 40 additions & 0 deletions src/main/java/com/dl/officialsite/course/vo/CourseQueryVo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.dl.officialsite.course.vo;

import com.dl.officialsite.sharing.Share;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Data;
import org.springframework.data.domain.Page;

/**
* @Description
* @Author xiaoming
* @Date 2024/9/12 7:50 PM
**/
@Data
public class CourseQueryVo {

// @JsonSerialize(using = ToStringSerializer.class)
private Long id;
//课程名称
private String courseName;
//课程简介
private String remark;
//合作社区
private String cooperateCommunity;
//创建者
private String creator;
//更新者
private String updater;
//创建时间
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Long createTime;
//更新时间
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Long updateTime;
// //状态,0:进行中,1:已结束
// private Integer status;

private Page<Share> sharePage;
}
Loading