ermcpCommon.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * @Author: zou.yingbin
  3. * @Create : 2021/3/30 14:51
  4. * @Modify : 2021/3/30 14:51
  5. * @Note : Ermcp定义一些公共的方法或变量
  6. */
  7. package models
  8. import (
  9. "encoding/hex"
  10. "fmt"
  11. "math"
  12. "mtp2_if/utils"
  13. )
  14. type ReportType int
  15. // IErmcp 通用接口
  16. type IErmcp interface {
  17. calc() // 相关计算和数据处理
  18. buildSql() string // 生成sql语句
  19. GetDataEx() (interface{}, error) // 获取数据接口
  20. }
  21. // PageEx 分页信息
  22. type PageEx struct {
  23. Page int `json:"-" form:"page"` // 页码
  24. PageSize int `json:"-" form:"pagesize"` // 每页大小
  25. Total int `json:"-" xorm:"'TOTAL'"` // 总条数
  26. }
  27. // DecryptField 解密字段
  28. func DecryptField(oriStr string) string {
  29. nLen := len(oriStr)
  30. if nLen < 32 || nLen%16 != 0 {
  31. // 非加密字符串
  32. return oriStr
  33. }
  34. key := "0d299ce2d4105282f7471074cb0f9f9d"
  35. key2, _ := hex.DecodeString(key)
  36. if oriStr == "" {
  37. return oriStr
  38. }
  39. d, _ := hex.DecodeString(oriStr)
  40. if dst, err := utils.AESDecrypt(d, key2); err == nil {
  41. return string(dst)
  42. }
  43. return oriStr
  44. }
  45. // DecodeStr 类似于问号表达式, true返回第1个 false返回第2个
  46. func DecodeStr(condition bool, strTrue, strFalse string) string {
  47. if condition {
  48. return strTrue
  49. }
  50. return strFalse
  51. }
  52. // IFValue 类似于三元表达式
  53. func IFValue(condition bool, trueValue interface{}, falseValue interface{}) interface{} {
  54. if condition {
  55. return trueValue
  56. } else {
  57. return falseValue
  58. }
  59. }
  60. // InStrBuilder 构建InStr字串
  61. type InStrBuilder struct {
  62. Value []interface{}
  63. }
  64. // Add 增加元素
  65. func (r *InStrBuilder) Add(v interface{}) {
  66. r.Value = append(r.Value, v)
  67. }
  68. // InStr 返回逗号隔开的字串
  69. func (r *InStrBuilder) InStr() string {
  70. var str string
  71. for _, v := range r.Value {
  72. if len(str) > 0 {
  73. str += ","
  74. }
  75. if _, ok := v.(string); ok {
  76. str += fmt.Sprintf("'%v'", v)
  77. } else {
  78. str += fmt.Sprintf("%v", v)
  79. }
  80. }
  81. return str
  82. }
  83. // InStr
  84. func InStr(param ...interface{}) string {
  85. var str string
  86. for i := range param {
  87. if len(str) > 0 {
  88. str += ","
  89. }
  90. str += fmt.Sprintf("%v", param[i])
  91. }
  92. return str
  93. }
  94. type ZOOMFUN func(vList ...*float64)
  95. // NewZoomFun 创建缩放函数
  96. func NewZoomFun(decimalplace int) ZOOMFUN {
  97. return func(vList ...*float64) {
  98. for i := range vList {
  99. *vList[i] = *vList[i] / math.Pow10(decimalplace)
  100. }
  101. }
  102. }
  103. // ZoomByDecimal
  104. func ZoomByDecimal(decimalplace int, vList ...*float64) {
  105. for i := range vList {
  106. *vList[i] = *vList[i] / math.Pow10(decimalplace)
  107. }
  108. }
  109. // SFLOAT64 浮点数Json格式化为字符串, 使用%g
  110. type SFLOAT64 float64
  111. // MarshalJSON
  112. func (r *SFLOAT64) MarshalJSON() ([]byte, error) {
  113. sData := fmt.Sprintf("%g", *r)
  114. return []byte(sData), nil
  115. }