ermcpCommon.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. "github.com/shopspring/decimal"
  12. "math"
  13. "mtp2_if/utils"
  14. "strings"
  15. )
  16. type ReportType int
  17. // IErmcp 通用接口
  18. type IErmcp interface {
  19. calc() // 相关计算和数据处理
  20. buildSql() string // 生成sql语句
  21. GetDataEx() (interface{}, error) // 获取数据接口
  22. }
  23. // PageEx 分页信息
  24. type PageEx struct {
  25. Page int `json:"-" form:"page"` // 页码
  26. PageSize int `json:"-" form:"pagesize"` // 每页大小
  27. Total int `json:"-" xorm:"'TOTAL'"` // 总条数
  28. }
  29. // DecryptField 解密字段
  30. func DecryptField(oriStr string) string {
  31. nLen := len(oriStr)
  32. if nLen < 32 || nLen%16 != 0 {
  33. // 非加密字符串
  34. return oriStr
  35. }
  36. key := "0d299ce2d4105282f7471074cb0f9f9d"
  37. key2, _ := hex.DecodeString(key)
  38. if oriStr == "" {
  39. return oriStr
  40. }
  41. d, _ := hex.DecodeString(oriStr)
  42. if dst, err := utils.AESDecrypt(d, key2); err == nil {
  43. return string(dst)
  44. }
  45. return oriStr
  46. }
  47. // DecodeStr 类似于问号表达式, true返回第1个 false返回第2个
  48. func DecodeStr(condition bool, strTrue, strFalse string) string {
  49. if condition {
  50. return strTrue
  51. }
  52. return strFalse
  53. }
  54. // IFValue 类似于三元表达式
  55. func IFValue(condition bool, trueValue interface{}, falseValue interface{}) interface{} {
  56. if condition {
  57. return trueValue
  58. } else {
  59. return falseValue
  60. }
  61. }
  62. // InStrBuilder 构建InStr字串
  63. type InStrBuilder struct {
  64. Value []interface{}
  65. }
  66. // Add 增加元素
  67. func (r *InStrBuilder) Add(v interface{}) {
  68. r.Value = append(r.Value, v)
  69. }
  70. // InStr 返回逗号隔开的字串
  71. func (r *InStrBuilder) InStr() string {
  72. var str string
  73. for _, v := range r.Value {
  74. if len(str) > 0 {
  75. str += ","
  76. }
  77. if _, ok := v.(string); ok {
  78. str += fmt.Sprintf("'%v'", v)
  79. } else {
  80. str += fmt.Sprintf("%v", v)
  81. }
  82. }
  83. return str
  84. }
  85. // InStr
  86. func InStr(param ...interface{}) string {
  87. var str string
  88. for i := range param {
  89. if len(str) > 0 {
  90. str += ","
  91. }
  92. str += fmt.Sprintf("%v", param[i])
  93. }
  94. return str
  95. }
  96. type ZOOMFUN func(vList ...*float64)
  97. // NewZoomFun 创建缩放函数
  98. func NewZoomFun(decimalplace int) ZOOMFUN {
  99. return func(vList ...*float64) {
  100. for i := range vList {
  101. *vList[i] = *vList[i] / math.Pow10(decimalplace)
  102. }
  103. }
  104. }
  105. // ZoomByDecimal
  106. func ZoomByDecimal(decimalplace int, vList ...*float64) {
  107. for i := range vList {
  108. *vList[i] = *vList[i] / math.Pow10(decimalplace)
  109. }
  110. }
  111. // SFLOAT64 浮点数Json格式化为字符串, 使用%g
  112. type SFLOAT64 float64
  113. // MarshalJSON
  114. func (r *SFLOAT64) MarshalJSON() ([]byte, error) {
  115. sData := fmt.Sprintf("%g", *r)
  116. return []byte(sData), nil
  117. }
  118. // Float64
  119. func (r *SFLOAT64) Float64() float64 {
  120. return float64(*r)
  121. }
  122. // Set
  123. func (r *SFLOAT64) Set(v float64) {
  124. *r = SFLOAT64(v)
  125. }
  126. // Round
  127. func (r *SFLOAT64) Round(places int32) *SFLOAT64 {
  128. d, ok := decimal.NewFromFloat(float64(*r)).Round(places).Float64()
  129. if ok {
  130. *r = SFLOAT64(d)
  131. return r
  132. }
  133. return r
  134. }
  135. // Power10
  136. func (r *SFLOAT64) Power10(n int) *SFLOAT64 {
  137. *r = *r * SFLOAT64(math.Pow10(n))
  138. return r
  139. }
  140. // FtItemName 解析逗号隔开的字符串
  141. func FtItemName(dgItemName string) ([]string, bool) {
  142. if len(dgItemName) < 2 {
  143. return nil, false
  144. }
  145. sItem := strings.Split(dgItemName, ",")
  146. if len(sItem) > 0 {
  147. return sItem, true
  148. }
  149. return nil, false
  150. }