token.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package net
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "time"
  7. )
  8. //..
  9. const (
  10. TableToken = "net_token"
  11. TableTokenBind = "net_token_bind"
  12. //BindTypeFlow 绑定flow
  13. BindTypeFlow = int8(1)
  14. //BindTypeTransition 绑定单个审核操作
  15. BindTypeTransition = int8(2)
  16. //BindTypeTranBatch 绑定批量审核操作
  17. BindTypeTranBatch = int8(3)
  18. //BindTypeTranHelp 绑定单个非审核操作,提交后不流转
  19. BindTypeTranHelp = int8(4)
  20. //BindTypeTranHelpBatch 绑定批量非审核操作,提交后不流转
  21. BindTypeTranHelpBatch = int8(5)
  22. BindLogTemp = "%s(%d)=%s" //中文名=表达式
  23. BindBatchPrefix = "批量"
  24. BindBatchOptAll = int8(0)
  25. BindBatchOnly = int8(1)
  26. BindBatchNot = int8(2)
  27. )
  28. //TokenCompareAssign 运算符.
  29. const (
  30. TokenCompareAssign = int8(0)
  31. TokenCompareEqual = int8(1)
  32. TokenCompareInequal = int8(2)
  33. TokenCompareGreater = int8(3)
  34. TokenCompareGreaterEqual = int8(4)
  35. TokenCompareLower = int8(5)
  36. TokenCompareLowerEqual = int8(6)
  37. TokenCompareBetween = int8(7)
  38. TokenCompareNotBetween = int8(8)
  39. TokenCompareIn = int8(9)
  40. TokenCompareNotIn = int8(10)
  41. TokenCompareExist = int8(11)
  42. TokenCompareNotExist = int8(12)
  43. //TokenTypeInt8值类型 .
  44. TokenTypeInt8 = int8(0)
  45. TokenTypeInt16 = int8(1)
  46. TokenTypeInt32 = int8(2)
  47. TokenTypeInt64 = int8(3)
  48. TokenTypeString = int8(4)
  49. TokenTypeBool = int8(5)
  50. )
  51. //BindTranType 变迁绑定令牌类型
  52. var BindTranType = []int8{
  53. BindTypeTransition,
  54. BindTypeTranBatch,
  55. BindTypeTranHelp,
  56. BindTypeTranHelpBatch,
  57. }
  58. //BindTypes .
  59. var BindTypes = []int8{
  60. BindTypeFlow,
  61. BindTypeTransition,
  62. BindTypeTranBatch,
  63. BindTypeTranHelp,
  64. BindTypeTranHelpBatch,
  65. }
  66. // TokenCompareDesc .
  67. var TokenCompareDesc = map[int8]string{
  68. TokenCompareAssign: "=",
  69. //TokenCompareEqual: "==",
  70. //TokenCompareInequal: "!=",
  71. //TokenCompareGreater: ">",
  72. //TokenCompareGreaterEqual: ">=",
  73. //TokenCompareLower: "<",
  74. //TokenCompareLowerEqual: "<=",
  75. //TokenCompareBetween: "between", //值有2个数字
  76. //TokenCompareNotBetween: "not between", //值有2个数字
  77. //TokenCompareIn: "in", //a in []
  78. //TokenCompareNotIn: "not in", //a not in []
  79. }
  80. // TokenValueTypeDesc ..
  81. var TokenValueTypeDesc = map[int8]string{
  82. TokenTypeInt8: "int8",
  83. TokenTypeInt16: "int16",
  84. TokenTypeInt32: "int32",
  85. TokenTypeInt64: "int64",
  86. TokenTypeString: "string",
  87. TokenTypeBool: "bool",
  88. }
  89. // Token .
  90. type Token struct {
  91. ID int64 `gorm:"primary_key" json:"id" form:"id" validate:"omitempty,gt=0"`
  92. NetID int64 `gorm:"column:net_id" json:"net_id" form:"net_id" validate:"omitempty,gt=0"`
  93. ChName string `gorm:"column:ch_name" json:"ch_name" form:"ch_name" validate:"required,max=16"`
  94. Name string `gorm:"column:name" json:"name" form:"name" validate:"required,max=10"`
  95. Compare int8 `gorm:"column:compare" json:"compare" form:"compare" validate:"omitempty,min=0,max=10" default:"-1"`
  96. Value string `gorm:"column:value" json:"value" form:"value"`
  97. Type int8 `gorm:"column:type" json:"type" form:"type" default:"-1"`
  98. UID int64 `gorm:"column:uid" json:"uid"`
  99. Ctime time.Time `gorm:"column:ctime" json:"ctime"`
  100. Mtime time.Time `gorm:"column:mtime" json:"mtime"`
  101. }
  102. // TokenBind .
  103. type TokenBind struct {
  104. ID int64 `gorm:"primary_key" json:"id"`
  105. Type int8 `gorm:"column:type" json:"type"`
  106. ElementID int64 `gorm:"column:element_id" json:"element_id"`
  107. TokenID string `gorm:"column:token_id" json:"token_id"`
  108. ChName string `gorm:"column:ch_name" json:"ch_name" `
  109. UID int64 `gorm:"column:uid" json:"uid"`
  110. DisableTime time.Time `gorm:"column:disable_time" json:"disable_time"`
  111. Ctime time.Time `gorm:"column:ctime" json:"ctime"`
  112. Mtime time.Time `gorm:"column:mtime" json:"mtime"`
  113. }
  114. // TokenBindDetail .
  115. type TokenBindDetail struct {
  116. ID int64 `json:"id"`
  117. Type int8 `json:"type"`
  118. ElementID int64 `json:"element_id"`
  119. TokenID string `json:"token_id"`
  120. ChName string `json:"ch_name"`
  121. DisableTime time.Time `json:"disable_time"`
  122. Tokens []*Token `json:"tokens"`
  123. }
  124. // TableName .
  125. func (tk *Token) TableName() string {
  126. return TableToken
  127. }
  128. // IsAssign .
  129. func (tk *Token) IsAssign() bool {
  130. return tk.Compare == TokenCompareAssign
  131. }
  132. // FormatValue .
  133. func (tk *Token) FormatValue() (vt interface{}, err error) {
  134. var (
  135. it int64
  136. ib bool
  137. )
  138. switch tk.Type {
  139. case TokenTypeInt8:
  140. if it, err = strconv.ParseInt(tk.Value, 10, 8); err != nil {
  141. return
  142. }
  143. vt = int8(it)
  144. case TokenTypeInt16:
  145. if it, err = strconv.ParseInt(tk.Value, 10, 16); err != nil {
  146. return
  147. }
  148. vt = int16(it)
  149. case TokenTypeInt32:
  150. if it, err = strconv.ParseInt(tk.Value, 10, 32); err != nil {
  151. return
  152. }
  153. vt = int32(it)
  154. case TokenTypeInt64:
  155. if it, err = strconv.ParseInt(tk.Value, 10, 8); err != nil {
  156. return
  157. }
  158. vt = it
  159. case TokenTypeString:
  160. vt = tk.Value
  161. case TokenTypeBool:
  162. if ib, err = strconv.ParseBool(tk.Value); err != nil {
  163. return
  164. }
  165. vt = ib
  166. default:
  167. err = fmt.Errorf("token not support tp(%d)!", tk.Type)
  168. }
  169. return
  170. }
  171. //FormatLog 日志形式
  172. func (tk *Token) FormatLog() string {
  173. var (
  174. cm, tp string
  175. exist bool
  176. )
  177. if cm, exist = TokenCompareDesc[tk.Compare]; !exist {
  178. cm = strconv.Itoa(int(tk.Compare))
  179. }
  180. if tp, exist = TokenValueTypeDesc[tk.Type]; !exist {
  181. tp = strconv.Itoa(int(tk.Type))
  182. }
  183. return fmt.Sprintf("%s%s%s(%s)", tk.Name, cm, tk.Value, tp)
  184. }
  185. // TableName .
  186. func (tb *TokenBind) TableName() string {
  187. return TableTokenBind
  188. }
  189. // IsAvailable .
  190. func (tb *TokenBind) IsAvailable() bool {
  191. return tb.DisableTime.IsZero()
  192. }
  193. // IsBatch .
  194. func (tb *TokenBind) IsBatch() bool {
  195. return tb.Type == BindTypeTranBatch || tb.Type == BindTypeTranHelpBatch
  196. }
  197. //FormatLog 日志形式
  198. func (tb *TokenBindDetail) FormatLog() string {
  199. var (
  200. logs = []string{}
  201. )
  202. for _, tk := range tb.Tokens {
  203. logs = append(logs, tk.FormatLog())
  204. }
  205. return fmt.Sprintf("{%s: %s}", tb.ChName, strings.Join(logs, ","))
  206. }
  207. // IsAvailable .
  208. func (tb *TokenBindDetail) IsAvailable() bool {
  209. return tb.DisableTime.IsZero()
  210. }
  211. // GetTokenCompare .
  212. func GetTokenCompare(compare int8) string {
  213. v, exist := TokenCompareDesc[compare]
  214. if !exist {
  215. return ""
  216. }
  217. return v
  218. }
  219. // GetTokenValueType .
  220. func GetTokenValueType(tp int8) string {
  221. v, exist := TokenValueTypeDesc[tp]
  222. if !exist {
  223. return ""
  224. }
  225. return v
  226. }
  227. // Int64Slice data.Less and data.Swap
  228. type Int64Slice []int64
  229. func (sl Int64Slice) Len() int {
  230. return len(sl)
  231. }
  232. func (sl Int64Slice) Less(i, j int) bool {
  233. return sl[i] < sl[j]
  234. }
  235. func (sl Int64Slice) Swap(i, j int) {
  236. sl[i], sl[j] = sl[j], sl[i]
  237. }