123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907 |
- 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<len(req.Search.OR);i++{
- item := req.Search.OR[i]
- keySplit := strings.Split(item.KeyStr,".")
- if len(keySplit) < 2 {
- continue
- }
- if keySplit[1] == "like" {
- or := fmt.Sprintf("%s like %s",keySplit[0],item.KeyVal)
- orParam = append(orParam,or)
- }else if keySplit[1] == "eq"{
- or := fmt.Sprintf("%s=%s",keySplit[0],item.KeyVal)
- orParam = append(orParam,or)
- }else if keySplit[1] == "lt"{
- or := fmt.Sprintf("%s<%s",keySplit[0],item.KeyVal)
- orParam = append(orParam,or)
- }else if keySplit[1] == "gt"{
- or := fmt.Sprintf("%s>%s",keySplit[0],item.KeyVal)
- orParam = append(orParam,or)
- }
- }
- andParam := make([]string,0)
- for i:=0;i<len(req.Search.AND);i++{
- item := req.Search.AND[i]
- keySplit := strings.Split(item.KeyStr,".")
- if len(keySplit) < 2 {
- continue
- }
- if keySplit[1] == "like" {
- or := fmt.Sprintf("%s like %s",keySplit[0],item.KeyVal)
- andParam = append(andParam,or)
- }else if keySplit[1] == "eq"{
- or := fmt.Sprintf("%s=%s",keySplit[0],item.KeyVal)
- andParam = append(andParam,or)
- }else if keySplit[1] == "lt"{
- or := fmt.Sprintf("%s<%s",keySplit[0],item.KeyVal)
- andParam = append(andParam,or)
- }else if keySplit[1] == "gt"{
- or := fmt.Sprintf("%s>%s",keySplit[0],item.KeyVal)
- andParam = append(andParam,or)
- }
- }
- queryParam := ""
- if len(orParam)>0{
- queryParam += "("
- for i:=0;i<len(orParam);i++{
- if i < len(orParam)-1{
- queryParam += orParam[i] + " or "
- }else{
- queryParam += orParam[i]
- }
- }
- queryParam += ")"
- }
- if len(andParam) > 0 {
- queryParam += " and "
- for i:=0;i<len(andParam);i++{
- if i < len(andParam)-1{
- queryParam += orParam[i] + " and "
- }else{
- queryParam += orParam[i]
- }
- }
- }
- return queryParam
- }
- //func Business2_QueryTable(req *model.RequestBody,accessInfo *model.AccessInfo)(*model.CommonList,error){
- // //第一步:去出要查询的字段
- // dbInfo := getDataBaseInfoById(accessInfo.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<len(accessInfo.Colnum);i++{
- // if first == 0{
- // sql += ","
- // }else{
- // first = 0
- // }
- //
- // if accessInfo.Colnum[i].IsDesensitization == 1 {
- // if accessInfo.Colnum[i].DesensitizationRule.Left > 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<len(req.QueryColn);i++{
- if first == 0{
- sql += ","
- }else{
- first = 0
- }
- sql +=" %s"
- args = append(args,req.QueryColn[i])
- }
- if len(req.QueryColn) == 0 {
- sql += " *"
- }
- }
- //fmt.Println("--------------------------2-------------------------")
- sql += " from %s"
- args = append(args,accessInfo.ResData.TbName)
- where := 0
- //用户查询参数
- for zz:=0;zz<len(req.Query);zz++{
- if len(req.Query[zz].ColName) > 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<len(cols);zz++{
- //if cols[zz].IsSearchable != nil && *cols[zz].IsSearchable == 1 {
- if cols[zz].FilterOperator != nil && len(*cols[zz].FilterOperator) > 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<len(req.QueryColn);i++{
- if first == 0{
- sql += ","
- }else{
- first = 0
- }
- sql +=" %s"
- args = append(args,req.QueryColn[i])
- }
- if len(req.QueryColn) == 0 {
- sql += " *"
- }
- }
- sql += " from %s"
- args = append(args,accessInfo.ResData.TbName)
- args_count = append(args_count,accessInfo.ResData.TbName)
- where := 0
- //用户查询参数
- for zz:=0;zz<len(req.Query);zz++{
- if len(req.Query[zz].ColName) > 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<len(cols);zz++{
- //if cols[zz].IsSearchable != nil && *cols[zz].IsSearchable == 1 {
- if cols[zz].FilterOperator != nil && len(*cols[zz].FilterOperator) > 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<len(accessInfo.ResData.Colnums);i++{
- // if first == 0{
- // sql += ","
- // }else{
- // first = 0
- // }
- //
- // sql +=" %s"
- // args = append(args,accessInfo.ResData.Colnums[i].Name)
- // }
- // sql += " from %s"
- // args = append(args,accessInfo.ResData.TbName)
- //
- // queryParam := getSqlQueryParam(req)
- // 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.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;i<len(access.ResData.Params);i++{
- if key == access.ResData.Params[i].ParamName {
- return &access.ResData.Params[i]
- }
- }
- return nil
- }
- //总队标准接口服务解析校验
- func Business2_Cq119NormalApiVerify(dataByte []byte,access *model.Access_Service_Struct)(*model.DataShareNormalResponse,error) {
- fmt.Println("---resp:", string(dataByte))
- 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, err
- }
- normalRespPacket := &model.DataShareNormalResponse{
- Code: cq119NormalResp.Code,
- Page: cq119NormalResp.Page,
- Size: cq119NormalResp.Size,
- Total: cq119NormalResp.Total,
- Columns: cq119NormalResp.Columns,
- }
- 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())
- newValue := strct.Interface()
- param := getAccessParamByName(key.Interface().(string),access)
- if param != nil {
- if param.DesensitizationRule != nil {
- //脱敏
- if len(param.DesensitizationRule.RegStr)>0 && 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;m<len(column_map);m++{
- if key.Interface().(string) == column_map[m].FieldName {
- n[column_map[m].ColumnName] = strct.Interface()
- break
- }
- }
- }
- if len(n)>0{
- rows_array = append(rows_array, n)
- }
- }
- }
- //fmt.Println("------------print rows_array------------------")
- //for i:=0;i<len(rows_array);i++{
- // item := rows_array[i]
- // for k,v := range item {
- // fmt.Println("key:",k,"value:",v)
- // }
- //}
- //fmt.Println("--------------end print-----------------------")
- return &cq119NormalResp,rows_array,nil
- }
- func Business2_DataCache_DbConnect(dbInfo *model.CacheDbConfig) (*sql.DB,error){
- find := false
- for i:=0;i<len(global.Databases);i++{
- if dbInfo.DbId == global.Databases[i].Id{
- find = true
- return global.Databases[i].DbHandle,nil
- }
- }
- if find == false {
- //连接数据库
- db, err := dao.DbConnect(dbInfo.DbType,dbInfo.ConnectionString)
- if err != nil {
- return nil,err
- }
- info := global.DbInfo{
- DbHandle: db,
- ConnInfo: dbInfo.ConnectionString,
- Status: true,
- SqlType: dbInfo.DbType,
- DbName: "",
- Id: dbInfo.DbId,
- }
- global.Databases = append(global.Databases, info)
- return db,nil
- }
- return nil,errors.New("连接数据库失败")
- }
- //数据保存
- func Business2_DataSave(rows_array []map[string]interface{},dbInfo *model.CacheDbConfig)(error){
- //写数据表
- column_data := model.ColnDataInsert{
- Id: dbInfo.DbId,
- TbName: dbInfo.TbName,
- }
- for i:=0;i<len(rows_array);i++{
- item := rows_array[i]
- for k, _ := range item {
- //fmt.Printf("--------%s: %s\n", k, values) // ... and this line
- column_data.ColNameArray = append(column_data.ColNameArray,k)
- }
- break
- }
- for i:=0;i<len(rows_array);i++{
- var row model.ColnItem
- item := rows_array[i]
- for j:=0;j<len(column_data.ColNameArray);j++ {
- name := column_data.ColNameArray[j]
- row.List = append(row.List,item[name].(string))
- }
- //for k, values := range item {
- // fmt.Printf("%s: %s\n", k, values) // ... and this line
- // row.List = append(row.List,values.(string))
- //}
- column_data.Rows = append(column_data.Rows,row)
- }
- return DatabaseInsert(&column_data)
- }
- func Business2_DataCacheTest(){
- data_cache_list := dao.LoadDataCacheList()
- for i:=0;i< len(data_cache_list);i++{
- _,err := Business2_DataCache_DbConnect(&data_cache_list[i].DbConfig)
- if err != nil {
- continue
- }
- serviceConfig := data_cache_list[i].ServiceConfig
- if serviceConfig.AuthWay == "cq119"{
- _,body,err := api_proxy.CQ119_GetInstance().CQ119_ApiCall(serviceConfig.ServiceUrl,[]byte(serviceConfig.RequestParam))
- if err != nil {
- global.SystemLogger.Log(logrus.ErrorLevel,fmt.Sprintf("serviceUrl:%s ,cache data failed.",serviceConfig.ServiceUrl))
- continue
- }
- _,rows_array,err := Business2_CacheParser(body,&data_cache_list[i])
- //save
- Business2_DataSave(rows_array,&data_cache_list[i].DbConfig)
- }
- }
- }
- func Business2_GetApiFieldName(columnName string,dataCacheConfig *model.DataCacheOfRedis) (string,error){
- for i:=0;i<len(dataCacheConfig.ServiceConfig.ColumnMap);i++{
- if dataCacheConfig.ServiceConfig.ColumnMap[i].ColumnName == columnName{
- return dataCacheConfig.ServiceConfig.ColumnMap[i].FieldName,nil
- }
- }
- return "",errors.New("未找到搜索参数名称")
- }
- func Business2_DataCache(dataCacheConfig *model.DataCacheOfRedis) error{
- db,err := Business2_DataCache_DbConnect(&dataCacheConfig.DbConfig)
- if err != nil {
- return err
- }
- serviceConfig := dataCacheConfig.ServiceConfig
- if serviceConfig.AuthWay == "119"{
- //组装参数
- if dataCacheConfig.Mode == "full"{
- //全量:删除数据表,重新同步数据
- pageNo := 1
- pageSize := 10000
- total := 0
- for ;;{
- //参数
- 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
- newParam,err := json.Marshal(¶mMap)
- if err != nil {
- global.SystemLogger.Log(logrus.ErrorLevel,fmt.Sprintf("Business2_DataCache failed, errmsg:"+err.Error()))
- return err
- }
- _,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;
- }
- }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 != "<nil>" {
- 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
- }
|