@ -19,15 +19,20 @@ import com.tz.platform.feign.exam.vo.QuestionVo;
import com.tz.platform.feign.vo.GradeVo ;
import com.tz.platform.feign.vo.GradeVo ;
import com.tz.platform.feign.vo.SchoolVo ;
import com.tz.platform.feign.vo.SchoolVo ;
import com.tz.platform.repository.UserDao ;
import com.tz.platform.repository.UserDao ;
import lombok.extern.slf4j.Slf4j ;
import org.apache.commons.collections4.list.GrowthList ;
import org.apache.commons.collections4.list.GrowthList ;
import org.checkerframework.checker.units.qual.A ;
import org.checkerframework.checker.units.qual.A ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.data.redis.core.RedisCallback ;
import org.springframework.data.redis.core.RedisTemplate ;
import org.springframework.stereotype.Component ;
import org.springframework.stereotype.Component ;
import org.springframework.util.StringUtils ;
import org.springframework.util.StringUtils ;
import javax.annotation.Resource ;
import java.util.* ;
import java.util.* ;
@Component
@Component
@Slf4j
public class ZhiyunAccountBiz {
public class ZhiyunAccountBiz {
private ZhiYunApi zhiYunApi = new ZhiYunApi ( ) ;
private ZhiYunApi zhiYunApi = new ZhiYunApi ( ) ;
@ -43,6 +48,14 @@ public class ZhiyunAccountBiz {
@Autowired
@Autowired
private IFeignQuestion feignQuestion ;
private IFeignQuestion feignQuestion ;
@Resource
private RedisTemplate redisTemplate ;
/ * *
* 过 期 时 间 毫 秒
* /
private long ms = 3600000 * 4 ;
@Autowired
@Autowired
@ -60,6 +73,15 @@ public class ZhiyunAccountBiz {
} ) ;
} ) ;
}
}
public void synSingle ( SchoolVo schoolVo ) {
String gradeContent = zhiYunApi . findAllGrade ( schoolVo . getOuterId ( ) ) ;
dealGrade ( gradeContent , schoolVo ) ;
String studentContent = zhiYunApi . findAllStudent ( schoolVo . getOuterId ( ) ) ;
dealStudent ( studentContent , schoolVo ) ;
String teacherCountent = zhiYunApi . findTeacherBySchoolId ( schoolVo . getOuterId ( ) ) ;
dealTeacher ( teacherCountent , schoolVo ) ;
}
public void synGrade ( ) {
public void synGrade ( ) {
List < SchoolVo > schoolVoList = feignSchool . list ( ) ;
List < SchoolVo > schoolVoList = feignSchool . list ( ) ;
schoolVoList . forEach ( schoolVo - > {
schoolVoList . forEach ( schoolVo - > {
@ -70,6 +92,33 @@ public class ZhiyunAccountBiz {
} ) ;
} ) ;
}
}
public void synJob ( ) {
Thread t = new Thread ( ( ) - > {
String jobName = "zhiyunSynJob2" ;
boolean lr = lock ( jobName , ms ) ;
if ( lr ) {
try {
synSchool ( ) ;
List < SchoolVo > schoolVoList = feignSchool . list ( ) ;
schoolVoList . forEach ( schoolVo - > {
try {
synSingle ( schoolVo ) ;
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
}
} ) ;
} catch ( Exception ex ) {
ex . printStackTrace ( ) ;
log . error ( "大赛状态更新任务失败: {}" , ex . getMessage ( ) ) ;
} finally {
unLock ( jobName ) ;
}
}
} ) ;
t . setName ( "zhiyunSynJob2" ) ;
t . start ( ) ;
}
public void synSchool ( ) {
public void synSchool ( ) {
String content = zhiYunApi . findAuthSchool ( ) ;
String content = zhiYunApi . findAuthSchool ( ) ;
if ( ! content . isEmpty ( ) ) {
if ( ! content . isEmpty ( ) ) {
@ -122,7 +171,7 @@ public class ZhiyunAccountBiz {
if ( questionVo = = null ) {
if ( questionVo = = null ) {
questionVo = new QuestionVo ( ) ;
questionVo = new QuestionVo ( ) ;
CourseVO courseVO = feignCourse . getByOuterId ( courseId ) ;
CourseVO courseVO = feignCourse . getByOuterId ( courseId ) ;
if ( courseVO = = null ) return ;
if ( courseVO = = null ) { return ; }
questionVo . setCourseId ( courseVO . getId ( ) ) ;
questionVo . setCourseId ( courseVO . getId ( ) ) ;
questionVo . setCourseName ( courseVO . getName ( ) ) ;
questionVo . setCourseName ( courseVO . getName ( ) ) ;
}
}
@ -289,6 +338,7 @@ public class ZhiyunAccountBiz {
if ( gradeVo ! = null ) {
if ( gradeVo ! = null ) {
user . setClassName ( gradeVo . getName ( ) ) ;
user . setClassName ( gradeVo . getName ( ) ) ;
user . setClassId ( gradeVo . getOuterId ( ) ) ;
user . setClassId ( gradeVo . getOuterId ( ) ) ;
user . setClassNo ( gradeVo . getClassNo ( ) ) ;
user . setGmtCreate ( new Date ( ) ) ;
user . setGmtCreate ( new Date ( ) ) ;
}
}
user . setLevelId ( school . getLevelId ( ) ) ;
user . setLevelId ( school . getLevelId ( ) ) ;
@ -317,5 +367,39 @@ public class ZhiyunAccountBiz {
}
}
}
}
/ * *
* 获 取 一 个 redis 分 布 锁
*
* @param lockKey 锁 住 的 key
* @param lockExpireMils 锁 住 的 时 长 。 如 果 超 时 未 解 锁 , 视 为 加 锁 线 程 死 亡 , 其 他 线 程 可 夺 取 锁
* @return
* /
public boolean lock ( String lockKey , long lockExpireMils ) {
return ( Boolean ) redisTemplate . execute ( ( RedisCallback ) connection - > {
long nowTime = System . currentTimeMillis ( ) ;
Boolean acquire = connection . setNX ( lockKey . getBytes ( ) , String . valueOf ( nowTime + lockExpireMils + 1 ) . getBytes ( ) ) ;
if ( acquire ) {
return Boolean . TRUE ;
} else {
byte [ ] value = connection . get ( lockKey . getBytes ( ) ) ;
if ( Objects . nonNull ( value ) & & value . length > 0 ) {
long oldTime = Long . parseLong ( new String ( value ) ) ;
if ( oldTime < nowTime ) {
//connection.getSet: 返回这个key的旧值并设置新值。
byte [ ] oldValue = connection . getSet ( lockKey . getBytes ( ) , String . valueOf ( nowTime + lockExpireMils + 1 ) . getBytes ( ) ) ;
//当key不存时会返回空, 表示key不存在或者已在管道中使用
return oldValue = = null ? false : Long . parseLong ( new String ( oldValue ) ) < nowTime ;
}
}
}
return Boolean . FALSE ;
} ) ;
}
public void unLock ( String key ) {
redisTemplate . execute ( ( RedisCallback ) redisConnection - > {
redisConnection . del ( key . getBytes ( ) ) ;
return true ;
} ) ;
}
}
}