feat: add school default task service
parent
78f5324a4e
commit
a20905f638
@ -0,0 +1,9 @@
|
||||
CREATE TABLE IF NOT EXISTS school_default_task_initialization (
|
||||
school_id varchar(64) NOT NULL,
|
||||
status varchar(16) NOT NULL,
|
||||
last_error varchar(1000) NULL,
|
||||
attempt_count int NOT NULL DEFAULT 0,
|
||||
create_time datetime NOT NULL,
|
||||
update_time datetime NOT NULL,
|
||||
PRIMARY KEY (school_id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='学校默认实训任务初始化状态';
|
||||
@ -0,0 +1,15 @@
|
||||
package com.sztzjy.linkCommerce.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
public class SchoolDefaultTaskInitialization {
|
||||
private String schoolId;
|
||||
private String status;
|
||||
private String lastError;
|
||||
private Integer attemptCount;
|
||||
private Date createTime;
|
||||
private Date updateTime;
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
package com.sztzjy.linkCommerce.mapper;
|
||||
|
||||
import com.sztzjy.linkCommerce.entity.SchoolDefaultTaskInitialization;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface SchoolDefaultTaskInitializationMapper {
|
||||
SchoolDefaultTaskInitialization selectByPrimaryKey(String schoolId);
|
||||
|
||||
int insert(SchoolDefaultTaskInitialization record);
|
||||
|
||||
int updateByPrimaryKeySelective(SchoolDefaultTaskInitialization record);
|
||||
}
|
||||
@ -0,0 +1,20 @@
|
||||
package com.sztzjy.linkCommerce.service;
|
||||
|
||||
import com.sztzjy.linkCommerce.entity.SchoolDefaultTaskInitialization;
|
||||
import com.sztzjy.linkCommerce.entity.TaskAllocation;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface SchoolDefaultTaskService {
|
||||
String PLATFORM_DEFAULT_CLASS_ID = "999999999";
|
||||
|
||||
List<TaskAllocation> resolveForTeachingClass(String teachingClassId);
|
||||
|
||||
List<TaskAllocation> getSchoolDefault(String schoolId);
|
||||
|
||||
SchoolDefaultTaskInitialization getInitialization(String schoolId);
|
||||
|
||||
void replaceSchoolDefault(String schoolId, List<TaskAllocation> tasks);
|
||||
|
||||
void initializeSchoolDefault(String schoolId);
|
||||
}
|
||||
@ -0,0 +1,128 @@
|
||||
package com.sztzjy.linkCommerce.service.impl;
|
||||
|
||||
import com.sztzjy.linkCommerce.entity.SchoolClass;
|
||||
import com.sztzjy.linkCommerce.entity.SchoolDefaultTaskInitialization;
|
||||
import com.sztzjy.linkCommerce.entity.TaskAllocation;
|
||||
import com.sztzjy.linkCommerce.entity.TaskAllocationExample;
|
||||
import com.sztzjy.linkCommerce.mapper.SchoolClassMapper;
|
||||
import com.sztzjy.linkCommerce.mapper.SchoolDefaultTaskInitializationMapper;
|
||||
import com.sztzjy.linkCommerce.mapper.TaskAllocationMapper;
|
||||
import com.sztzjy.linkCommerce.service.SchoolDefaultTaskService;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
public class SchoolDefaultTaskServiceImpl implements SchoolDefaultTaskService {
|
||||
private static final String SCHOOL_DEFAULT_PREFIX = "SCHOOL_DEFAULT:";
|
||||
|
||||
@Autowired
|
||||
TaskAllocationMapper taskAllocationMapper;
|
||||
@Autowired
|
||||
SchoolClassMapper schoolClassMapper;
|
||||
@Autowired(required = false)
|
||||
SchoolDefaultTaskInitializationMapper initializationMapper;
|
||||
|
||||
public static String schoolDefaultClassId(String schoolId) {
|
||||
return SCHOOL_DEFAULT_PREFIX + schoolId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TaskAllocation> resolveForTeachingClass(String teachingClassId) {
|
||||
List<TaskAllocation> teachingClassTasks = listByClassId(teachingClassId);
|
||||
if (!teachingClassTasks.isEmpty()) {
|
||||
return teachingClassTasks;
|
||||
}
|
||||
SchoolClass schoolClass = schoolClassMapper == null ? null : schoolClassMapper.selectByPrimaryKey(teachingClassId);
|
||||
if (schoolClass != null && StringUtils.isNotBlank(schoolClass.getSchoolId())) {
|
||||
List<TaskAllocation> schoolTasks = getSchoolDefault(schoolClass.getSchoolId());
|
||||
if (!schoolTasks.isEmpty()) {
|
||||
return schoolTasks;
|
||||
}
|
||||
}
|
||||
return listByClassId(PLATFORM_DEFAULT_CLASS_ID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<TaskAllocation> getSchoolDefault(String schoolId) {
|
||||
if (StringUtils.isBlank(schoolId)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return listByClassId(schoolDefaultClassId(schoolId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public SchoolDefaultTaskInitialization getInitialization(String schoolId) {
|
||||
return initializationMapper == null || StringUtils.isBlank(schoolId)
|
||||
? null : initializationMapper.selectByPrimaryKey(schoolId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void replaceSchoolDefault(String schoolId, List<TaskAllocation> tasks) {
|
||||
if (StringUtils.isBlank(schoolId)) {
|
||||
throw new IllegalArgumentException("学校不能为空");
|
||||
}
|
||||
String classId = schoolDefaultClassId(schoolId);
|
||||
TaskAllocationExample example = new TaskAllocationExample();
|
||||
example.createCriteria().andClassIdEqualTo(classId);
|
||||
taskAllocationMapper.deleteByExample(example);
|
||||
for (TaskAllocation task : tasks == null ? Collections.<TaskAllocation>emptyList() : tasks) {
|
||||
task.setId(UUID.randomUUID().toString());
|
||||
task.setClassId(classId);
|
||||
task.setSchoolId(schoolId);
|
||||
taskAllocationMapper.insert(task);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initializeSchoolDefault(String schoolId) {
|
||||
if (StringUtils.isBlank(schoolId) || hasSchoolDefault(schoolId)) {
|
||||
return;
|
||||
}
|
||||
List<TaskAllocation> platformTasks = listByClassId(PLATFORM_DEFAULT_CLASS_ID);
|
||||
for (TaskAllocation source : platformTasks) {
|
||||
TaskAllocation copy = new TaskAllocation();
|
||||
copy.setId(UUID.randomUUID().toString());
|
||||
copy.setClassId(schoolDefaultClassId(schoolId));
|
||||
copy.setSchoolId(schoolId);
|
||||
copy.setModule(source.getModule());
|
||||
copy.setDisabledStatus(source.getDisabledStatus());
|
||||
copy.setSort(source.getSort());
|
||||
taskAllocationMapper.insert(copy);
|
||||
}
|
||||
markReady(schoolId);
|
||||
}
|
||||
|
||||
private boolean hasSchoolDefault(String schoolId) {
|
||||
TaskAllocationExample example = new TaskAllocationExample();
|
||||
example.createCriteria().andClassIdEqualTo(schoolDefaultClassId(schoolId));
|
||||
return taskAllocationMapper.countByExample(example) > 0;
|
||||
}
|
||||
|
||||
private List<TaskAllocation> listByClassId(String classId) {
|
||||
if (taskAllocationMapper == null || StringUtils.isBlank(classId)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
TaskAllocationExample example = new TaskAllocationExample();
|
||||
example.createCriteria().andClassIdEqualTo(classId);
|
||||
example.setOrderByClause("sort asc");
|
||||
List<TaskAllocation> tasks = taskAllocationMapper.selectByExample(example);
|
||||
return tasks == null ? Collections.emptyList() : tasks;
|
||||
}
|
||||
|
||||
private void markReady(String schoolId) {
|
||||
if (initializationMapper == null) {
|
||||
return;
|
||||
}
|
||||
SchoolDefaultTaskInitialization record = new SchoolDefaultTaskInitialization();
|
||||
record.setSchoolId(schoolId);
|
||||
record.setStatus("READY");
|
||||
record.setUpdateTime(new Date());
|
||||
initializationMapper.updateByPrimaryKeySelective(record);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,30 @@
|
||||
<?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="com.sztzjy.linkCommerce.mapper.SchoolDefaultTaskInitializationMapper">
|
||||
<resultMap id="BaseResultMap" type="com.sztzjy.linkCommerce.entity.SchoolDefaultTaskInitialization">
|
||||
<id column="school_id" jdbcType="VARCHAR" property="schoolId"/>
|
||||
<result column="status" jdbcType="VARCHAR" property="status"/>
|
||||
<result column="last_error" jdbcType="VARCHAR" property="lastError"/>
|
||||
<result column="attempt_count" jdbcType="INTEGER" property="attemptCount"/>
|
||||
<result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
|
||||
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
|
||||
</resultMap>
|
||||
<sql id="BaseColumns">school_id, status, last_error, attempt_count, create_time, update_time</sql>
|
||||
<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
|
||||
SELECT <include refid="BaseColumns"/> FROM school_default_task_initialization WHERE school_id = #{schoolId}
|
||||
</select>
|
||||
<insert id="insert" parameterType="com.sztzjy.linkCommerce.entity.SchoolDefaultTaskInitialization">
|
||||
INSERT INTO school_default_task_initialization (<include refid="BaseColumns"/>)
|
||||
VALUES (#{schoolId}, #{status}, #{lastError}, #{attemptCount}, #{createTime}, #{updateTime})
|
||||
</insert>
|
||||
<update id="updateByPrimaryKeySelective" parameterType="com.sztzjy.linkCommerce.entity.SchoolDefaultTaskInitialization">
|
||||
UPDATE school_default_task_initialization
|
||||
<set>
|
||||
<if test="status != null">status = #{status},</if>
|
||||
<if test="lastError != null">last_error = #{lastError},</if>
|
||||
<if test="attemptCount != null">attempt_count = #{attemptCount},</if>
|
||||
<if test="updateTime != null">update_time = #{updateTime},</if>
|
||||
</set>
|
||||
WHERE school_id = #{schoolId}
|
||||
</update>
|
||||
</mapper>
|
||||
@ -0,0 +1,59 @@
|
||||
package com.sztzjy.linkCommerce.service.impl;
|
||||
|
||||
import com.sztzjy.linkCommerce.entity.SchoolClass;
|
||||
import com.sztzjy.linkCommerce.entity.TaskAllocation;
|
||||
import com.sztzjy.linkCommerce.mapper.SchoolClassMapper;
|
||||
import com.sztzjy.linkCommerce.mapper.TaskAllocationMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class SchoolDefaultTaskServiceImplTest {
|
||||
|
||||
@Test
|
||||
void resolvesSchoolDefaultBeforePlatformDefaultWhenTeachingClassHasNoConfiguration() {
|
||||
TaskAllocationMapper taskMapper = mock(TaskAllocationMapper.class);
|
||||
SchoolClassMapper classMapper = mock(SchoolClassMapper.class);
|
||||
SchoolDefaultTaskServiceImpl service = new SchoolDefaultTaskServiceImpl();
|
||||
ReflectionTestUtils.setField(service, "taskAllocationMapper", taskMapper);
|
||||
ReflectionTestUtils.setField(service, "schoolClassMapper", classMapper);
|
||||
|
||||
SchoolClass schoolClass = new SchoolClass();
|
||||
schoolClass.setSchoolClassId("class-1");
|
||||
schoolClass.setSchoolId("school-1");
|
||||
schoolClass.setClassType("TEACHING");
|
||||
when(classMapper.selectByPrimaryKey("class-1")).thenReturn(schoolClass);
|
||||
when(taskMapper.selectByExample(any())).thenReturn(
|
||||
Collections.emptyList(), List.of(task("学校默认任务")), List.of(task("平台默认任务")));
|
||||
|
||||
List<TaskAllocation> result = service.resolveForTeachingClass("class-1");
|
||||
|
||||
assertEquals(List.of("学校默认任务"), result.stream().map(TaskAllocation::getModule).collect(java.util.stream.Collectors.toList()));
|
||||
verify(taskMapper, never()).insert(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void initializationDoesNotOverwriteExistingSchoolDefaultTasks() {
|
||||
TaskAllocationMapper taskMapper = mock(TaskAllocationMapper.class);
|
||||
SchoolDefaultTaskServiceImpl service = new SchoolDefaultTaskServiceImpl();
|
||||
ReflectionTestUtils.setField(service, "taskAllocationMapper", taskMapper);
|
||||
when(taskMapper.countByExample(any())).thenReturn(1L);
|
||||
|
||||
service.initializeSchoolDefault("school-1");
|
||||
|
||||
verify(taskMapper, never()).insert(any());
|
||||
}
|
||||
|
||||
private TaskAllocation task(String module) {
|
||||
TaskAllocation task = new TaskAllocation();
|
||||
task.setModule(module);
|
||||
task.setDisabledStatus((byte) 0);
|
||||
return task;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue