| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- /**
- * @Author: zou.yingbin
- * @Create : 2021/3/30 14:51
- * @Modify : 2021/3/30 14:51
- * @Note : Ermcp定义一些公共的方法或变量
- */
- package models
- import (
- "encoding/hex"
- "fmt"
- "math"
- "mtp2_if/utils"
- "strconv"
- "strings"
- "github.com/shopspring/decimal"
- )
- type ReportType int
- // IErmcp 通用接口
- type IErmcp interface {
- calc() // 相关计算和数据处理
- buildSql() string // 生成sql语句
- GetDataEx() (interface{}, error) // 获取数据接口
- }
- // PageEx 分页信息
- type PageEx struct {
- Page int `json:"-" form:"page"` // 页码
- PageSize int `json:"-" form:"pagesize"` // 每页大小
- Total int `json:"-" xorm:"'TOTAL'"` // 总条数
- }
- // DecryptField 解密字段
- func DecryptField(oriStr string) string {
- nLen := len(oriStr)
- if nLen < 32 || nLen%16 != 0 {
- // 非加密字符串
- return oriStr
- }
- key := "0d299ce2d4105282f7471074cb0f9f9d"
- key2, _ := hex.DecodeString(key)
- if oriStr == "" {
- return oriStr
- }
- d, _ := hex.DecodeString(oriStr)
- if dst, err := utils.AESDecrypt(d, key2); err == nil {
- return string(dst)
- }
- return oriStr
- }
- // DecodeStr 类似于问号表达式, true返回第1个 false返回第2个
- func DecodeStr(condition bool, strTrue, strFalse string) string {
- if condition {
- return strTrue
- }
- return strFalse
- }
- // IFValue 类似于三元表达式
- func IFValue(condition bool, trueValue interface{}, falseValue interface{}) interface{} {
- if condition {
- return trueValue
- } else {
- return falseValue
- }
- }
- // InStrBuilder 构建InStr字串
- type InStrBuilder struct {
- Value []interface{}
- }
- // Add 增加元素
- func (r *InStrBuilder) Add(v interface{}) {
- r.Value = append(r.Value, v)
- }
- // InStr 返回逗号隔开的字串
- func (r *InStrBuilder) InStr() string {
- var str string
- for _, v := range r.Value {
- if len(str) > 0 {
- str += ","
- }
- if _, ok := v.(string); ok {
- str += fmt.Sprintf("'%v'", v)
- } else {
- str += fmt.Sprintf("%v", v)
- }
- }
- return str
- }
- // InStr
- func InStr(param ...interface{}) string {
- var str string
- for i := range param {
- if len(str) > 0 {
- str += ","
- }
- str += fmt.Sprintf("%v", param[i])
- }
- return str
- }
- type ZOOMFUN func(vList ...*float64)
- // NewZoomFun 创建缩放函数
- func NewZoomFun(decimalplace int) ZOOMFUN {
- return func(vList ...*float64) {
- for i := range vList {
- *vList[i] = *vList[i] / math.Pow10(decimalplace)
- }
- }
- }
- // ZoomByDecimal
- func ZoomByDecimal(decimalplace int, vList ...*float64) {
- for i := range vList {
- *vList[i] = *vList[i] / math.Pow10(decimalplace)
- }
- }
- // SFLOAT64 浮点数Json格式化为字符串, 使用%g
- type SFLOAT64 float64
- // MarshalJSON
- func (r *SFLOAT64) MarshalJSON() ([]byte, error) {
- return []byte(r.string()), nil
- }
- // Float64
- func (r *SFLOAT64) Float64() float64 {
- return float64(*r)
- }
- func (r *SFLOAT64) string() string {
- return strconv.FormatFloat(r.Float64(), 'f', -1, 64)
- }
- // Set
- func (r *SFLOAT64) Set(v float64) {
- *r = SFLOAT64(v)
- }
- // SetByInt64
- func (r *SFLOAT64) SetByInt64(v int64, place int) {
- if place == 0 {
- *r = SFLOAT64(v)
- } else if place > 0 {
- str := fmt.Sprintf("%v", *r)
- if len(str) >= place {
- pos := len(str) - place
- str = str[0:pos] + "." + str[pos:]
- if d, err := strconv.ParseFloat(str, 64); err == nil {
- *r = SFLOAT64(d)
- }
- }
- }
- }
- // Round
- func (r *SFLOAT64) Round(places int32) *SFLOAT64 {
- d, _ := decimal.NewFromFloat(float64(*r)).Round(places).Float64()
- *r = SFLOAT64(d)
- return r
- }
- // Power10
- func (r *SFLOAT64) Power10(n int) *SFLOAT64 {
- *r = *r * SFLOAT64(math.Pow10(n))
- return r
- }
- // FtItemName 解析逗号隔开的字符串
- func FtItemName(dgItemName string) ([]string, bool) {
- if len(dgItemName) < 2 {
- return nil, false
- }
- sItem := strings.Split(dgItemName, ",")
- if len(sItem) > 0 {
- return sItem, true
- }
- return nil, false
- }
- // EncryptByStar 名字脱敏处理
- func EncryptByStar(str string) string {
- if str == "" {
- return ""
- }
- d := []rune(str)
- var strName string
- nLen := len(d)
- switch nLen {
- case 1:
- strName = "*" + string(d[0])
- case 2:
- strName = "*" + string(d[1])
- case 3:
- strName = string(d[0]) + "*" + string(d[2])
- case 4:
- strName = string(d[0]) + "**" + string(d[3])
- default:
- strName = string(d[0]) + "***" + string(d[nLen-1])
- }
- return strName
- }
- func EncryptByStar2(str string) string {
- if str == "" {
- return ""
- }
- d := []rune(str)
- var strName string
- nLen := len(d)
- switch nLen {
- case 1:
- strName = "*" + string(d[0])
- case 2:
- strName = "*" + string(d[1])
- case 3:
- strName = string(d[0]) + "**"
- case 4:
- strName = string(d[0]) + "***"
- default:
- strName = string(d[0]) + "****"
- }
- return strName
- }
|