ermcpCommon.go 5.1 KB

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