This commit is contained in:
Xlxinxi 2024-12-18 03:08:49 +08:00
parent c2d44e5947
commit 2378d75406
5 changed files with 93 additions and 28 deletions

View File

@ -12,39 +12,21 @@ func NewFacade(tableName string, dbFunc func() *gorm.DB) *Facade {
}
func (t *Facade) NewQuery() *Query {
if t.dbFunc != nil {
return NewQuery(t.tableName).SetDB(t.dbFunc())
}
return NewQuery(t.tableName)
return NewQuery(t.tableName, t.dbFunc())
}
func (t *Facade) NewUpdate() *Update {
if t.dbFunc != nil {
return NewUpdate(t.tableName).SetDB(t.dbFunc())
}
return NewUpdate(t.tableName)
return NewUpdate(t.tableName, t.dbFunc())
}
func (t *Facade) NewInsert() *Insert {
if t.dbFunc != nil {
return NewInsert(t.tableName).SetDB(t.dbFunc())
}
return NewInsert(t.tableName)
return NewInsert(t.tableName, t.dbFunc())
}
func (t *Facade) NewDelete() *Delete {
if t.dbFunc != nil {
return NewDelete(t.tableName).SetDB(t.dbFunc())
}
return NewDelete(t.tableName)
return NewDelete(t.tableName, t.dbFunc())
}
func (t *Facade) Create(val interface{}) error {
if t.dbFunc != nil {
db := t.dbFunc()
if db != nil {
return Create(val, db)
}
}
return Create(val)
return Create(val, t.dbFunc())
}

37
util/amount.go Normal file
View File

@ -0,0 +1,37 @@
package util
import (
"github.com/shopspring/decimal"
"strings"
)
func Fen2Yuan(price int64) string {
d := decimal.New(1, 2) //分除以100得到元
result := decimal.NewFromInt(int64(price)).DivRound(d, 2).String()
//fmt.Printf("输入值为:%d, 分转元后,精度为二的结果为:%s\n", price, result)
return result
}
func FenToYuan00(price int64) string {
d := decimal.New(1, 2) //分除以100得到元
result := decimal.NewFromInt(int64(price)).DivRound(d, 2).String()
if find := strings.Contains(result, "."); find {
return result
} else {
return result + ".00"
}
}
// 元转分,乘以100后保留整数部分
func Yuan2Fen(price float64) int64 {
d := decimal.New(1, 2) //分转元乘以100
d1 := decimal.New(1, 0) //乘完之后保留2为小数需要这么一个中间参数
//df := decimal.NewFromFloat(price).Mul(d).DivRound(d1,2).String()
//df := decimal.NewFromFloat(price).Mul(d).IntPart()
//如下是满足当乘以100后仍然有小数位取四舍五入法后再取整数部分
dff := decimal.NewFromFloat(price).Mul(d).DivRound(d1, 0).IntPart()
//fmt.Printf("输入值为:%f, 简单的元转分后,取整数部分:%d\n", price, df)
//fmt.Printf("输入值为:%f, 元转分后,若还有小数,需做四舍五入后,再取整数:%d\n", price, dff)
return dff
}

View File

@ -1,4 +1,4 @@
package password
package util
import (
"crypto/md5"
@ -6,7 +6,7 @@ import (
"golang.org/x/crypto/bcrypt"
)
func Encode(userPassword string) (string, error) {
func PasswordEncode(userPassword string) (string, error) {
md5Password := md5.Sum([]byte(userPassword))
encode, err := generatePassword(string(md5Password[:]))
if err != nil {
@ -15,18 +15,18 @@ func Encode(userPassword string) (string, error) {
return string(encode), nil
}
}
func ValidateCode(userPassword string, hashed string) (isOK bool, err error) {
func PasswordValidateCode(userPassword string, hashed string) (isOK bool, err error) {
md5Password := md5.Sum([]byte(userPassword))
isOk, err := validatePassword(string(md5Password[:]), hashed)
return isOk, err
}
//generatePassword 给密码就行加密操作
// generatePassword 给密码就行加密操作
func generatePassword(userPassword string) ([]byte, error) {
return bcrypt.GenerateFromPassword([]byte(userPassword), bcrypt.DefaultCost)
}
//validatePassword 密码比对
// validatePassword 密码比对
func validatePassword(userPassword string, hashed string) (isOK bool, err error) {
if err = bcrypt.CompareHashAndPassword([]byte(hashed), []byte(userPassword)); err != nil {
return false, errors.New("密码错误!")

21
util/size.go Normal file
View File

@ -0,0 +1,21 @@
package util
import "fmt"
// 字节的单位转换 保留两位小数
func FormatFileSize(fileSize int64) (size string) {
if fileSize < 1024 {
//return strconv.FormatInt(fileSize, 10) + "B"
return fmt.Sprintf("%.2fB", float64(fileSize)/float64(1))
} else if fileSize < (1024 * 1024) {
return fmt.Sprintf("%.2fKB", float64(fileSize)/float64(1024))
} else if fileSize < (1024 * 1024 * 1024) {
return fmt.Sprintf("%.2fMB", float64(fileSize)/float64(1024*1024))
} else if fileSize < (1024 * 1024 * 1024 * 1024) {
return fmt.Sprintf("%.2fGB", float64(fileSize)/float64(1024*1024*1024))
} else if fileSize < (1024 * 1024 * 1024 * 1024 * 1024) {
return fmt.Sprintf("%.2fTB", float64(fileSize)/float64(1024*1024*1024*1024))
} else { //if fileSize < (1024 * 1024 * 1024 * 1024 * 1024 * 1024)
return fmt.Sprintf("%.2fEB", float64(fileSize)/float64(1024*1024*1024*1024*1024))
}
}

25
util/time.go Normal file
View File

@ -0,0 +1,25 @@
package util
import (
"time"
)
func IsToday(t time.Time) bool {
toDay := time.Now()
if toDay.Year() == t.Year() && toDay.Month() == t.Month() && toDay.Day() == t.Day() {
return true
} else {
return false
}
}
func GetOneDayBetween(t time.Time) (time.Time, time.Time) {
startTime := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
endTime := time.Date(t.Year(), t.Month(), t.Day(), 23, 59, 59, 0, t.Location())
return startTime, endTime
}
func ParseTime(layout, str string) time.Time {
t, _ := time.Parse(layout, str)
return t
}