package service import ( "DataShare/dao" "DataShare/global" "DataShare/model" "DataShare/service/api_proxy" "DataShare/util/gabs" "database/sql" "encoding/json" "errors" "fmt" "github.com/sirupsen/logrus" "reflect" "regexp" "strings" ) func Business2_GetAccessInfo(accessId string) (string,*model.Access_Service_Struct,*model.Access_Data_Struct,error){ key := fmt.Sprintf("access_%s",accessId) body, err := global.RedisClient.Get(key).Result() if err != nil { return "",nil,nil, err } var tempMap map[string]interface{} err = json.Unmarshal([]byte(body), &tempMap) if err != nil { //panic(err) return "",nil,nil,err } //for k, values := range tempMap { // fmt.Printf("%s: %s\n", k, values) // ... and this line //} if tempMap["resType"]=="service"{ //接口服务 var accessValue model.Access_Service_Struct err = json.NewDecoder(strings.NewReader(body)).Decode(&accessValue) if err != nil{ global.SystemLogger.Log(logrus.ErrorLevel,fmt.Sprintf("json转结构体出错, err:%s",err.Error())) return "",nil,nil, err } return "Service",&accessValue,nil,nil }else{ //数据资源 var accessValue model.Access_Data_Struct err = json.NewDecoder(strings.NewReader(body)).Decode(&accessValue) if err != nil{ global.SystemLogger.Log(logrus.ErrorLevel,fmt.Sprintf("json转结构体出错, err:%s",err.Error())) return "",nil,nil, err } return "Data",nil,&accessValue,nil } //var accessValue model.AccessInfo //err = json.NewDecoder(strings.NewReader(body)).Decode(&accessValue) //if err != nil{ // global.SystemLogger.Log(logrus.ErrorLevel,fmt.Sprintf("json转结构体出错, err:%s",err.Error())) // return nil, err //} // //return &accessValue,nil //colArry := make([]model.ColNumDet,0) // //item := model.ColNumDet{ // Name: "name", // Type: "varchar", // IsDesensitization: 0, // DesensitizationRule: model.DesensitizationRule{ // }, //} //colArry = append(colArry,item) //item = model.ColNumDet{ // Name: "identifyId", // Type: "varchar", // IsDesensitization: 1, // //DesensitizationRule: model.DesensitizationRule{ // // Left: 3, // // Right: 4, // // Fill: "*******", // //}, // DesensitizationRule: model.DesensitizationRule{ // RegStr: "^(.{3}).+(.{4})$", // FillStr: "$1****$2", // }, //} //colArry = append(colArry,item) // //item = model.ColNumDet{ // Name: "rysj", // Type: "varchar", // IsDesensitization: 1, // DesensitizationRule: model.DesensitizationRule{ // RegStr: "^(.{3}).+(.{4})$", // FillStr: "$1****$2", // }, //} //colArry = append(colArry,item) // //info := model.AccessInfo{ // Enable: 1, // DbId: 1, // TbName: "inpatient_info", // Colnum: colArry, //} // //return &info,nil } func getSqlQueryParam(req *model.RequestBody)(string) { orParam := make([]string,0) for i:=0; i%s",keySplit[0],item.KeyVal) orParam = append(orParam,or) } } andParam := make([]string,0) for i:=0;i%s",keySplit[0],item.KeyVal) andParam = append(andParam,or) } } queryParam := "" if len(orParam)>0{ queryParam += "(" for i:=0;i 0 { queryParam += " and " for i:=0;i 0 { // sql += " SQL_CALC_FOUND_ROWS" // } // args := make([]interface{},0) // first := 1 // for i:=0;i 0 && accessInfo.Colnum[i].DesensitizationRule.Right > 0{ // sql += "concat(left(%s, %d),'%s',right(%s, %d)) AS %s" // args = append(args,accessInfo.Colnum[i].Name) // args = append(args,accessInfo.Colnum[i].DesensitizationRule.Left) // args = append(args,accessInfo.Colnum[i].DesensitizationRule.Fill) // args = append(args,accessInfo.Colnum[i].Name) // args = append(args,accessInfo.Colnum[i].DesensitizationRule.Right) // args = append(args,accessInfo.Colnum[i].Name) // }else if accessInfo.Colnum[i].DesensitizationRule.Left > 0 { // sql += "concat(left(%s, %d),'%s') AS %s" // args = append(args,accessInfo.Colnum[i].Name) // args = append(args,accessInfo.Colnum[i].DesensitizationRule.Left) // args = append(args,accessInfo.Colnum[i].DesensitizationRule.Fill) // args = append(args,accessInfo.Colnum[i].Name) // }else if accessInfo.Colnum[i].DesensitizationRule.Right > 0{ // sql += "concat('%s',right(%s, %d)) AS %s" // args = append(args,accessInfo.Colnum[i].DesensitizationRule.Fill) // args = append(args,accessInfo.Colnum[i].Name) // args = append(args,accessInfo.Colnum[i].DesensitizationRule.Right) // args = append(args,accessInfo.Colnum[i].Name) // }else{ // sql +=" %s" // args = append(args,accessInfo.Colnum[i].Name) // } // }else { // sql +=" %s" // args = append(args,accessInfo.Colnum[i].Name) // } // } // sql += " from %s" // args = append(args,accessInfo.TbName) // // queryParam := getSqlQueryParam(req,accessInfo) // if len(queryParam)> 0 { // sql += " where "+queryParam // } // if req.PageSize > 0 { // sql += " limit %d,%d" // args = append(args,(req.PageNo-1)*req.PageSize) // args = append(args,req.PageSize) // } // sqlStr := fmt.Sprintf(sql,args...) // fmt.Println("------------") // fmt.Println(sqlStr) // fmt.Println("------------") // items,_,_,total,_ := dao.GetData(dbInfo.DbHandle,sqlStr) // // result := &model.CommonList{ // Total: total, // Items: items, // } // //todo: // //脱敏处理 // // return result,nil // } // // return nil,nil //} func Business2_QueryTable(req *model.DbQuery,accessInfo *model.Access_Data_Struct)(*model.CommonList,error){ //第一步:去出要查询的字段 dbInfo := getDataBaseInfoById(accessInfo.ResData.DbId) if dbInfo==nil{ return nil,errors.New("数据库不存在!") } if dbInfo.Status == false { return nil,errors.New(fmt.Sprintf("database:%s not connect",dbInfo.DbName)) } //fmt.Println("--------------------------1-------------------------") if dbInfo.SqlType == "mysql" { sql := "select" if req.PageSize > 0 { sql += " SQL_CALC_FOUND_ROWS" } args := make([]interface{},0) first := 1 if req.QueryColn == nil { sql += " *" }else{ for i:=0;i 0 { if where == 0 { sql += " where" where = 1 }else{ sql += " and" } if req.Query[zz].Cond == 0 { sql += " %s='%s'" }else if req.Query[zz].Cond == 1 { sql += " %s>'%s'" }else{ sql += " %s<'%s'" } args = append(args,req.Query[zz].ColName) args = append(args,req.Query[zz].ColVal) } } //拼接默认参数 cols := accessInfo.ResData.Colnums for zz:=0;zz 0 && cols[zz].FilterValue != nil && len(*cols[zz].FilterValue) > 0 { if where == 0 { sql += " where" where = 1 }else{ sql += " and" } //0-等于 1-大于 2-小于 if *cols[zz].FilterOperator == "0" { sql += " %s='%s'" }else if *cols[zz].FilterOperator == "1" { sql += " %s>'%s'" }else if *cols[zz].FilterOperator == "2" { sql += " %s<'%s'" } args = append(args,cols[zz].Name) args = append(args,*cols[zz].FilterValue) } //} } //排序 if len(req.Sort.ColName) > 0 { if req.Sort.IsDesc == 1 { sql += " order by %s desc" }else { sql += " order by %s" } args = append(args,req.Sort.ColName) } //分页 if req.PageSize > 0 { sql += " limit %d,%d" args = append(args,(req.PageNo-1)*req.PageSize) args = append(args,req.PageSize) } sqlStr := fmt.Sprintf(sql,args...) //fmt.Println("---------------------------------------------------") //fmt.Println(sqlStr) //fmt.Println("---------------------------------------------------") //查询 items,_,_,total,_ := dao.GetData2(dbInfo.DbHandle,sqlStr,accessInfo.ResData.Colnums) result := &model.CommonList{ Total: total, Items: items, } //todo: //脱敏处理 return result,nil }else if dbInfo.SqlType == "postgres" || dbInfo.SqlType == "kingbase" { sql := "select" sqlCount := "select count(*)" args_count := make([]interface{},0) args := make([]interface{},0) first := 1 if req.QueryColn == nil { sql += " *" }else{ for i:=0;i 0 { if where == 0 { sql += " where" sqlCount += " where" where = 1 }else{ sql += " and" sqlCount += " and" } if req.Query[zz].Cond == 0 { sql += " %s='%s'" sqlCount += " %s='%s'" }else if req.Query[zz].Cond == 1 { sql += " %s>'%s'" sqlCount += " %s>'%s'" }else{ sql += " %s<'%s'" sqlCount += " %s<'%s'" } args = append(args,req.Query[zz].ColName) args = append(args,req.Query[zz].ColVal) args_count = append(args_count,req.Query[zz].ColName) args_count = append(args_count,req.Query[zz].ColVal) } } //拼接默认参数 cols := accessInfo.ResData.Colnums for zz:=0;zz 0 && cols[zz].FilterValue != nil && len(*cols[zz].FilterValue) > 0 { if where == 0 { sql += " where" sqlCount += " where" where = 1 }else{ sql += " and" sqlCount += " and" } //0-等于 1-大于 2-小于 if *cols[zz].FilterOperator == "0" { sql += " %s='%s'" }else if *cols[zz].FilterOperator == "1" { sql += " %s>'%s'" }else if *cols[zz].FilterOperator == "2" { sql += " %s<'%s'" } args = append(args,cols[zz].Name) args = append(args,*cols[zz].FilterValue) args_count = append(args_count,cols[zz].Name) args_count = append(args_count,*cols[zz].FilterValue) } //} } //统计总数 sqlCntStr := fmt.Sprintf(sqlCount,args_count...) total,_:= dao.GetCount(dbInfo.DbHandle,sqlCntStr) //排序 if len(req.Sort.ColName) > 0 { if req.Sort.IsDesc == 1 { sql += " order by %s desc" }else { sql += " order by %s" } args = append(args,req.Sort.ColName) } //分页 if req.PageSize > 0 { sql += " limit %d,%d" args = append(args,(req.PageNo-1)*req.PageSize) args = append(args,req.PageSize) } sqlStr := fmt.Sprintf(sql,args...) //查询 items,_,_,_,_ := dao.GetData2(dbInfo.DbHandle,sqlStr,accessInfo.ResData.Colnums) result := &model.CommonList{ Total: total, Items: items, } return result,nil } return nil,nil } //func Business2_QueryTable(req *model.RequestBody,accessInfo *model.Access_Data_Struct)(*model.CommonList,error){ // //第一步:去出要查询的字段 // dbInfo := getDataBaseInfoById(accessInfo.ResData.DbId) // if dbInfo==nil{ // return nil,errors.New("数据库不存在!") // } // // if dbInfo.Status == false { // return nil,errors.New(fmt.Sprintf("database:%s not connect",dbInfo.DbName)) // } // // if dbInfo.SqlType == "mysql" { // sql := "select" // if req.PageSize > 0 { // sql += " SQL_CALC_FOUND_ROWS" // } // args := make([]interface{},0) // first := 1 // for i:=0;i 0 { // sql += " where "+queryParam // } // if req.PageSize > 0 { // sql += " limit %d,%d" // args = append(args,(req.PageNo-1)*req.PageSize) // args = append(args,req.PageSize) // } // sqlStr := fmt.Sprintf(sql,args...) // fmt.Println("------------") // fmt.Println(sqlStr) // fmt.Println("------------") // items,_,_,total,_ := dao.GetData2(dbInfo.DbHandle,sqlStr,accessInfo.ResData.Colnums) // // result := &model.CommonList{ // Total: total, // Items: items, // } // //todo: // //脱敏处理 // // return result,nil // } // // return nil,nil //} func getAccessParamByName(key string,access *model.Access_Service_Struct)(*model.Access_Service_Params){ for i:=0;i0 && len(param.DesensitizationRule.FillStr)>0{ reg := regexp.MustCompile(param.DesensitizationRule.RegStr) newValue = reg.ReplaceAllString(strct.Interface().(string),param.DesensitizationRule.FillStr) } } n[key.Interface().(string)]=newValue } } if len(n)>0{ normalRespPacket.Data = append(normalRespPacket.Data, n) } } } //v, err := json.Marshal(normalRespPacket) return normalRespPacket,nil } //数据缓存 func Business2_CacheParser(dataByte []byte,req *model.DataCacheOfRedis) (*model.CQ119Response,[]map[string]interface{},error){ var cq119NormalResp model.CQ119Response err := json.NewDecoder(strings.NewReader(string(dataByte))).Decode(&cq119NormalResp) if err != nil { global.SystemLogger.Log(logrus.ErrorLevel, fmt.Sprintf("json转结构体出错, err:%s", err.Error())) return nil,nil,err } serviceConfig := req.ServiceConfig column_map := serviceConfig.ColumnMap rows_array := make([]map[string]interface{},0) for i := 0; i < len(cq119NormalResp.Data);i++{ fmt.Println("===================index:",i) item := cq119NormalResp.Data[i] v := reflect.ValueOf(item) if v.Kind() == reflect.Map { n := make(map[string]interface{}) for _, key := range v.MapKeys() { strct := v.MapIndex(key) fmt.Println(key.Interface(),":", strct.Interface()) for m:=0;m0{ rows_array = append(rows_array, n) } } } //fmt.Println("------------print rows_array------------------") //for i:=0;i= total{ break } break; } }else if dataCacheConfig.Mode == "add"{ total := 0 pageNo := 1 pageSize := 100 //增量:按照增量标识,获取最大值,作为条件 maxValue,err := dao.GetMaxValue(dataCacheConfig.AddField,dataCacheConfig.DbConfig.TbName,db) if err != nil{ return err } fieldName,err := Business2_GetApiFieldName(dataCacheConfig.AddField,dataCacheConfig) if err != nil { return err } //参数 for ;;{ //var paramMap map[string]interface{} paramMap := make(map[string]interface{},0) err := json.Unmarshal([]byte(serviceConfig.RequestParam),¶mMap) if err != nil { global.SystemLogger.Log(logrus.ErrorLevel,fmt.Sprintf("Business2_DataCache failed, errmsg:"+err.Error())) //return err } paramMap[dataCacheConfig.PageField] = pageNo paramMap[dataCacheConfig.PageSizeField] = pageSize if maxValue != "" { if paramMap[dataCacheConfig.SearchField] != nil { searchNode,err := gabs.ParseJSON([]byte(paramMap[dataCacheConfig.SearchField].(string))) if err != nil { return err } searchNode.Set(maxValue,fmt.Sprintf("%s.gt",fieldName)) paramMap[dataCacheConfig.SearchField] = searchNode.String() }else{ paramMap[dataCacheConfig.SearchField] = fmt.Sprintf("{\"%s.gt\":\"%s\"}",fieldName,maxValue) } }else{ //paramMap[dataCacheConfig.SearchField] = model.NullObj{} } newParam,err := json.Marshal(¶mMap) if err != nil { global.SystemLogger.Log(logrus.ErrorLevel,fmt.Sprintf("Business2_DataCache failed, errmsg:"+err.Error())) return err } fmt.Println("-------------------------------") fmt.Println(string(newParam)) fmt.Println("---------------------------------") _,body,err := api_proxy.CQ119_GetInstance().CQ119_ApiCall(serviceConfig.ServiceUrl,newParam) if err != nil { global.SystemLogger.Log(logrus.ErrorLevel,fmt.Sprintf("serviceUrl:%s ,cache data failed.",serviceConfig.ServiceUrl)) return err } if serviceConfig.AuthWay == "gov" { _,body,err = api_proxy.GovDataFormatToResource(body) } resp,rows_array,err := Business2_CacheParser(body,dataCacheConfig) if len(rows_array)<1{ global.SystemLogger.Log(logrus.InfoLevel,fmt.Sprintf("serviceUrl:%s ,cache data complete.",serviceConfig.ServiceUrl)) break; } if total == 0 { total = resp.Total } //save Business2_DataSave(rows_array,&dataCacheConfig.DbConfig) pageNo += 1 if pageNo*pageSize >= total{ break } break } } } return nil }