zhangzengfei
2023-09-05 63645d248c765244488cd34dbc1bb6528ca6b7c7
system-service/models/cluster.go
@@ -1,131 +1,131 @@
package models
import (
   "fmt"
   "strconv"
   "time"
   "vamicro/config"
   "vamicro/extend/util"
   "basic.com/syncdb.git"
)
type Cluster struct {
   ClusterId   string `gorm:"primary_key;column:cluster_id" json:"cluster_id"`
   ClusterName string `gorm:"column:cluster_name" json:"cluster_name"`
   Password    string `gorm:"column:password" json:"password"`
   VirtualIp   string `gorm:"column:virtual_ip" json:"virtual_ip"`
}
func (Cluster) TableName() string {
   return "cluster"
}
func (c *Cluster) FindAll() (arr []Cluster, err error) {
   if err = db.Table("cluster").Find(&arr).Error; err != nil {
      return nil, err
   }
   return arr, nil
}
func (c *Cluster) FindByClusterId() (result Cluster, err error) {
   if c.ClusterId == "" {
      return Cluster{}, fmt.Errorf("param clusterId is nil error")
   }
   if err = db.Table("cluster").Where("cluster_id=?", c.ClusterId).First(&result).Error; err != nil {
      return Cluster{}, err
   }
   return result, nil
}
func (c *Cluster) FindByVirtualIp() (result Cluster, err error) {
   if c.VirtualIp == "" {
      return Cluster{}, fmt.Errorf("param virtual_ip is nil error")
   }
   if err = db.Table("cluster").Where("virtual_ip=?", c.VirtualIp).First(&result).Error; err != nil {
      return Cluster{}, err
   }
   return result, nil
}
func (c *Cluster) Create() bool {
   var localConfig LocalConfig
   e := localConfig.Select()
   if e != nil {
      return false
   }
   serverId := config.Server.AnalyServerId
   if serverId == "" {
      return false
   }
   serverIp, _, e := util.GetLocalIP(config.Server.NetworkAdapter)
   if e != nil {
      return false
   }
   var err error
   tx := db.Begin()
   defer func() {
      if err != nil && tx != nil {
         tx.Rollback()
      }
   }()
   sql := "insert into cluster (cluster_id,cluster_name,password,virtual_ip) values ('" + c.ClusterId + "','" + c.ClusterName + "','" + c.Password + "','" + c.VirtualIp + "')"
   if err = tx.Exec(sql).Error; err != nil {
      return false
   }
   timeUnix := time.Now().Unix()
   fmtTimeStr := time.Unix(timeUnix, 0).Format("2006-01-02 15:04:05")
   //添加本身节点
   sql = "insert into cluster_node (id,cluster_id,node_name,node_id,node_ip,create_time,isDelete,device_type) values ('" + serverId + "','" + c.ClusterId + "','" + localConfig.ServerName + "','" + serverId + "','" + (serverIp + ":" + strconv.Itoa(syncdb.DefaultBindPort)) + "','" + fmtTimeStr + "',0,'" + config.Server.DeviceType + "')"
   if err = tx.Exec(sql).Error; err != nil {
      return false
   }
   tx.Commit()
   return true
}
func (c *Cluster) UpdateClusterName(clusterName string, virtualIp string) bool {
   arr, e := c.FindAll()
   if e == nil && arr != nil && len(arr) > 0 {
      result := db.Exec("update cluster set cluster_name='" + clusterName + "',virtual_ip='" + virtualIp + "' where cluster_id='" + arr[0].ClusterId + "'")
      if result.Error != nil {
         return false
      }
      if result.RowsAffected > 0 {
         return true
      }
   }
   return false
}
func (c *Cluster) UpdateNodeName(nodeName string, nodeId string) bool {
   arr, _ := c.FindAll()
   if arr != nil && len(arr) > 0 {
      c := arr[0]
      result := db.Exec("update cluster_node set node_name='" + nodeName + "' where cluster_id='" + c.ClusterId + "' and id='" + nodeId + "'")
      if result.Error != nil {
         return false
      }
      return result.RowsAffected > 0
   }
   return false
}
func (c *Cluster) FindAllNodeMap() map[string]Node {
   m := make(map[string]Node, 0)
   arr, err := c.FindAll()
   if err == nil && arr != nil {
      var nodeE Node
      for _, ct := range arr {
         nodes, e := nodeE.FindNodesByClusterId(ct.ClusterId)
         if e == nil && nodes != nil {
            for _, nd := range nodes {
               m[nd.NodeId] = nd
            }
         }
      }
   }
   return m
}
package models
import (
   "fmt"
   "strconv"
   "time"
   "vamicro/config"
   "vamicro/extend/util"
   "basic.com/syncdb.git"
)
type Cluster struct {
   ClusterId   string `gorm:"primary_key;column:cluster_id" json:"cluster_id"`
   ClusterName string `gorm:"column:cluster_name" json:"cluster_name"`
   Password    string `gorm:"column:password" json:"password"`
   VirtualIp   string `gorm:"column:virtual_ip" json:"virtual_ip"`
}
func (Cluster) TableName() string {
   return "cluster"
}
func (c *Cluster) FindAll() (arr []Cluster, err error) {
   if err = db.Table("cluster").Find(&arr).Error; err != nil {
      return nil, err
   }
   return arr, nil
}
func (c *Cluster) FindByClusterId() (result Cluster, err error) {
   if c.ClusterId == "" {
      return Cluster{}, fmt.Errorf("param clusterId is nil error")
   }
   if err = db.Table("cluster").Where("cluster_id=?", c.ClusterId).First(&result).Error; err != nil {
      return Cluster{}, err
   }
   return result, nil
}
func (c *Cluster) FindByVirtualIp() (result Cluster, err error) {
   if c.VirtualIp == "" {
      return Cluster{}, fmt.Errorf("param virtual_ip is nil error")
   }
   if err = db.Table("cluster").Where("virtual_ip=?", c.VirtualIp).First(&result).Error; err != nil {
      return Cluster{}, err
   }
   return result, nil
}
func (c *Cluster) Create() bool {
   var localConfig LocalConfig
   e := localConfig.Select()
   if e != nil {
      return false
   }
   serverId := config.Server.AnalyServerId
   if serverId == "" {
      return false
   }
   serverIp, _, e := util.GetLocalIP(config.Server.NetworkAdapter)
   if e != nil {
      return false
   }
   var err error
   tx := db.Begin()
   defer func() {
      if err != nil && tx != nil {
         tx.Rollback()
      }
   }()
   sql := "insert into cluster (cluster_id,cluster_name,password,virtual_ip) values ('" + c.ClusterId + "','" + c.ClusterName + "','" + c.Password + "','" + c.VirtualIp + "')"
   if err = tx.Exec(sql).Error; err != nil {
      return false
   }
   timeUnix := time.Now().Unix()
   fmtTimeStr := time.Unix(timeUnix, 0).Format("2006-01-02 15:04:05")
   //添加本身节点
   sql = "insert into cluster_node (id,cluster_id,node_name,node_id,node_ip,create_time,isDelete,device_type) values ('" + serverId + "','" + c.ClusterId + "','" + localConfig.ServerName + "','" + serverId + "','" + (serverIp + ":" + strconv.Itoa(syncdb.DefaultBindPort)) + "','" + fmtTimeStr + "',0,'" + config.Server.DeviceType + "')"
   if err = tx.Exec(sql).Error; err != nil {
      return false
   }
   tx.Commit()
   return true
}
func (c *Cluster) UpdateClusterName(clusterName string, virtualIp string) bool {
   arr, e := c.FindAll()
   if e == nil && arr != nil && len(arr) > 0 {
      result := db.Exec("update cluster set cluster_name='" + clusterName + "',virtual_ip='" + virtualIp + "' where cluster_id='" + arr[0].ClusterId + "'")
      if result.Error != nil {
         return false
      }
      if result.RowsAffected > 0 {
         return true
      }
   }
   return false
}
func (c *Cluster) UpdateNodeName(nodeName string, nodeId string) bool {
   arr, _ := c.FindAll()
   if arr != nil && len(arr) > 0 {
      c := arr[0]
      result := db.Exec("update cluster_node set node_name='" + nodeName + "' where cluster_id='" + c.ClusterId + "' and id='" + nodeId + "'")
      if result.Error != nil {
         return false
      }
      return result.RowsAffected > 0
   }
   return false
}
func (c *Cluster) FindAllNodeMap() map[string]Node {
   m := make(map[string]Node, 0)
   arr, err := c.FindAll()
   if err == nil && arr != nil {
      var nodeE Node
      for _, ct := range arr {
         nodes, e := nodeE.FindNodesByClusterId(ct.ClusterId)
         if e == nil && nodes != nil {
            for _, nd := range nodes {
               m[nd.NodeId] = nd
            }
         }
      }
   }
   return m
}