You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
4.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package CacheUtil
import (
"dsDataex/Utils/ErrorConst"
"dsDataex/Utils/LogUtil"
"dsDataex/Utils/RedisUtil"
"strings"
"time"
)
var GBT2260 map[string]string
/**
* @Author zhangjun
* @Description 组织机构树缓存初始化,【机构下属学校】递归查询、并缓存 Redis
* @Description TODO如果性能还不行使用全局静态变量存储【机构下属学校】
* @Date 2020-06-16 10:06
* @Param
* @return
**/
func OrgtreeCacheInit(){
//删除Redis缓存
RedisUtil.RedisClient.FlushDB()
//机构树缓存
sql := "SELECT id from t_dataex_orgtree where delete_flag = -1 and enable_flag = 1 and parent_id = '-1'"
list, count, _ := Page(sql, 1000,0)
if count>0{
for no:=0;no<int(count);no++{
//TODO:目前只支持教育局下属学校的数据访问控制,暂不支持其他机构类型(如:大学区、主校分校...
if list[no]["org_type"].(string)=="1"{
sql = "SELECT id from t_dataex_orgtree where delete_flag = -1 and enable_flag = 1 and parent_id = '"+list[no]["id"].(string)+"'"
list2, count2, _ := Page(sql, 1000,0)
if count2>0{
Orgtree2Redis(list2,"Dataex_Scope_"+list[no]["id"].(string))
OrgCacheCreate(list[no]["id"].(string),list[no]["id"].(string))
}
}
}
}
//行政区划缓存
sql = "SELECT area_code from t_dataex_gbt2260"
list,_,_= Page(sql, 5000,0)
GBT2260=make( map[string]string)
for no:=0;no< len(list);no++{
GBT2260[list[no]["area_code"].(string)]=list[no]["area_name"].(string)
}
}
/**
* @Author zhangjun
* @Description 刷新机构下属学校缓存
* @Date 2020-06-16 10:14
* @Param orgID string 教育局机构ID
* @return
**/
func OrgCacheCreate(orgID string,scope... string) {
sql := "SELECT id from t_dataex_orgtree where delete_flag = -1 and enable_flag = 1 and parent_id = '" + orgID + "'"
list, count, _ := Page(sql, 1000,0)
if count>0{
for no:=0;no<int(count);no++{
//TODO:目前只支持教育局下属学校的数据访问控制,暂不支持其他机构类型(如:大学区、主校分校...
if list[no]["org_type"].(string)=="1"{
sql = "SELECT id from t_dataex_orgtree where delete_flag = -1 and enable_flag = 1 and parent_id = '"+list[no]["id"].(string)+"'"
list2, count2, _ := Page(sql, 1000,0)
if count2>0{
Orgtree2Redis(list2,"Dataex_Scope_"+list[no]["id"].(string))
var newscope =append( scope ,list[no]["id"].(string))
for no:=0;no<len(scope);no++{
Orgtree2Redis(list2,"Dataex_Scope_"+scope[no])
}
OrgCacheCreate(list[no]["id"].(string),newscope...)
}
}
}
}
}
/**
* @Author zhangjun
* @Description
* @Date 2020-06-16 10:49
* @Param list 【】map 机构范围数据【】数组
* @Param cacheName string 缓存名称
* @return
**/
func Orgtree2Redis(list []map[string]interface{}, cacheName string) {
var ids string
for i := 0; i < len(list); i++ {
bean := list[i]
ids=ids+"##"+bean["id"].(string)
}
RedisUtil.RedisClient.Set(cacheName, ids,10*time.Hour)
}
func Orgtree2Redis2(list []map[string]interface{}, cacheName string) {
//声明管道
pipe := RedisUtil.RedisClient.Pipeline()
for i := 0; i < len(list); i++ {
bean := list[i]
key := cacheName + ":" + bean["id"].(string)
pipe.HSet(key, bean)
pipe.Expire(key, 24*time.Hour)
}
_, err := pipe.Exec()
if err != nil {
LogUtil.Error(ErrorConst.SqlQueryError, err.Error())
}
}
/**
* @Author zhangjun
* @Description
* @Date 2020-06-16 11:21
* @Param
* @return
**/
func OrgtreeCheckScope(provideID string,orgID string)bool{
result,err:= RedisUtil.RedisClient.Get("Dataex_Scope_"+provideID).Result()
if err == nil{
if strings.Contains(result,orgID){
return true
}else {
return false
}
}
return false
}
func OrgtreeCheckScope2(provideID string,orgID string)bool{
count,_:= RedisUtil.RedisClient.Exists("Dataex_Scope_"+provideID+":"+orgID).Result()
if count>0{
return true
}
return false
}
func OrgtreeGetScope(orgId string) []string {
result,_:= RedisUtil.RedisClient.Get("Dataex_Scope_"+orgId).Result()
if result != ""{
return strings.Split(result,"##")
}
return nil
}