ermcpCommon.go 3.1 KB

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