ermcpCommon.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. "strconv"
  14. "strings"
  15. "github.com/shopspring/decimal"
  16. )
  17. type ReportType int
  18. // IErmcp 通用接口
  19. type IErmcp interface {
  20. calc() // 相关计算和数据处理
  21. buildSql() string // 生成sql语句
  22. GetDataEx() (interface{}, error) // 获取数据接口
  23. }
  24. // PageEx 分页信息
  25. type PageEx struct {
  26. Page int `json:"-" form:"page"` // 页码
  27. PageSize int `json:"-" form:"pagesize"` // 每页大小
  28. Total int `json:"-" xorm:"'TOTAL'"` // 总条数
  29. }
  30. // DecryptField 解密字段
  31. func DecryptField(oriStr string) string {
  32. nLen := len(oriStr)
  33. if nLen < 32 || nLen%16 != 0 {
  34. // 非加密字符串
  35. return oriStr
  36. }
  37. key := "0d299ce2d4105282f7471074cb0f9f9d"
  38. key2, _ := hex.DecodeString(key)
  39. if oriStr == "" {
  40. return oriStr
  41. }
  42. d, _ := hex.DecodeString(oriStr)
  43. if dst, err := utils.AESDecrypt(d, key2); err == nil {
  44. return string(dst)
  45. }
  46. return oriStr
  47. }
  48. // DecodeStr 类似于问号表达式, true返回第1个 false返回第2个
  49. func DecodeStr(condition bool, strTrue, strFalse string) string {
  50. if condition {
  51. return strTrue
  52. }
  53. return strFalse
  54. }
  55. // IFValue 类似于三元表达式
  56. func IFValue(condition bool, trueValue interface{}, falseValue interface{}) interface{} {
  57. if condition {
  58. return trueValue
  59. } else {
  60. return falseValue
  61. }
  62. }
  63. // InStrBuilder 构建InStr字串
  64. type InStrBuilder struct {
  65. Value []interface{}
  66. }
  67. // Add 增加元素
  68. func (r *InStrBuilder) Add(v interface{}) {
  69. r.Value = append(r.Value, v)
  70. }
  71. // InStr 返回逗号隔开的字串
  72. func (r *InStrBuilder) InStr() string {
  73. var str string
  74. for _, v := range r.Value {
  75. if len(str) > 0 {
  76. str += ","
  77. }
  78. if _, ok := v.(string); ok {
  79. str += fmt.Sprintf("'%v'", v)
  80. } else {
  81. str += fmt.Sprintf("%v", v)
  82. }
  83. }
  84. return str
  85. }
  86. // InStr
  87. func InStr(param ...interface{}) string {
  88. var str string
  89. for i := range param {
  90. if len(str) > 0 {
  91. str += ","
  92. }
  93. str += fmt.Sprintf("%v", param[i])
  94. }
  95. return str
  96. }
  97. type ZOOMFUN func(vList ...*float64)
  98. // NewZoomFun 创建缩放函数
  99. func NewZoomFun(decimalplace int) ZOOMFUN {
  100. return func(vList ...*float64) {
  101. for i := range vList {
  102. *vList[i] = *vList[i] / math.Pow10(decimalplace)
  103. }
  104. }
  105. }
  106. // ZoomByDecimal
  107. func ZoomByDecimal(decimalplace int, vList ...*float64) {
  108. for i := range vList {
  109. *vList[i] = *vList[i] / math.Pow10(decimalplace)
  110. }
  111. }
  112. // SFLOAT64 浮点数Json格式化为字符串, 使用%g
  113. type SFLOAT64 float64
  114. // MarshalJSON
  115. func (r *SFLOAT64) MarshalJSON() ([]byte, error) {
  116. return []byte(r.string()), nil
  117. }
  118. // Float64
  119. func (r *SFLOAT64) Float64() float64 {
  120. return float64(*r)
  121. }
  122. func (r *SFLOAT64) string() string {
  123. return strconv.FormatFloat(r.Float64(), 'f', -1, 64)
  124. }
  125. // Set
  126. func (r *SFLOAT64) Set(v float64) {
  127. *r = SFLOAT64(v)
  128. }
  129. // SetByInt64
  130. func (r *SFLOAT64) SetByInt64(v int64, place int) {
  131. if place == 0 {
  132. *r = SFLOAT64(v)
  133. } else if place > 0 {
  134. str := fmt.Sprintf("%v", *r)
  135. if len(str) >= place {
  136. pos := len(str) - place
  137. str = str[0:pos] + "." + str[pos:]
  138. if d, err := strconv.ParseFloat(str, 64); err == nil {
  139. *r = SFLOAT64(d)
  140. }
  141. }
  142. }
  143. }
  144. // Round
  145. func (r *SFLOAT64) Round(places int32) *SFLOAT64 {
  146. d, _ := decimal.NewFromFloat(float64(*r)).Round(places).Float64()
  147. *r = SFLOAT64(d)
  148. return r
  149. }
  150. // Power10
  151. func (r *SFLOAT64) Power10(n int) *SFLOAT64 {
  152. *r = *r * SFLOAT64(math.Pow10(n))
  153. return r
  154. }
  155. // FtItemName 解析逗号隔开的字符串
  156. func FtItemName(dgItemName string) ([]string, bool) {
  157. if len(dgItemName) < 2 {
  158. return nil, false
  159. }
  160. sItem := strings.Split(dgItemName, ",")
  161. if len(sItem) > 0 {
  162. return sItem, true
  163. }
  164. return nil, false
  165. }
  166. // EncryptByStar 名字脱敏处理
  167. func EncryptByStar(str string) string {
  168. if str == "" {
  169. return ""
  170. }
  171. d := []rune(str)
  172. var strName string
  173. nLen := len(d)
  174. switch nLen {
  175. case 1:
  176. strName = "*" + string(d[0])
  177. case 2:
  178. strName = "*" + string(d[1])
  179. case 3:
  180. strName = string(d[0]) + "*" + string(d[2])
  181. case 4:
  182. strName = string(d[0]) + "**" + string(d[3])
  183. default:
  184. strName = string(d[0]) + "***" + string(d[nLen-1])
  185. }
  186. return strName
  187. }
  188. func EncryptByStar2(str string) string {
  189. if str == "" {
  190. return ""
  191. }
  192. d := []rune(str)
  193. var strName string
  194. nLen := len(d)
  195. switch nLen {
  196. case 1:
  197. strName = "*" + string(d[0])
  198. case 2:
  199. strName = "*" + string(d[1])
  200. case 3:
  201. strName = string(d[0]) + "**"
  202. case 4:
  203. strName = string(d[0]) + "***"
  204. default:
  205. strName = string(d[0]) + "****"
  206. }
  207. return strName
  208. }