@@ -3,9 +3,9 @@ package zyos_go_coupon | |||
import ( | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/db" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/db/model" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/e" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/md" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/utils" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_tools.git/e" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_tools.git/utils" | |||
"fmt" | |||
"time" | |||
"xorm.io/xorm" | |||
@@ -17,22 +17,22 @@ func AddCoupon(session *xorm.Session, req *md.CouponCheckRequest) error { | |||
req.PvdType = "ordinary" | |||
} | |||
//查出该用户有的券额 | |||
list, err := db.GetCouponUserTotalByOne(session, utils.StrToInt(req.Uid), req.PvdType) | |||
list, err := db.GetCouponUserTotalByOne(session, zhios_tool_utils.StrToInt(req.Uid), req.PvdType) | |||
if err != nil { | |||
return err | |||
} else if list == nil { | |||
list = &model.CommCouponUserTotal{ | |||
Uid: utils.StrToInt(req.Uid), | |||
Uid: zhios_tool_utils.StrToInt(req.Uid), | |||
CreateAt: time.Now(), | |||
UpdateAt: time.Now(), | |||
PvdType: req.PvdType, | |||
} | |||
} | |||
LeaveCouponAmountValue := list.LeaveCouponAmountValue | |||
list.LeaveCouponAmountValue = utils.Float64ToStr(utils.FloatFormat(utils.StrToFloat64(list.LeaveCouponAmountValue)+utils.StrToFloat64(req.CouponAmount), 2)) | |||
list.LeaveCouponAmountValue = zhios_tool_utils.Float64ToStr(zhios_tool_utils.FloatFormat(zhios_tool_utils.StrToFloat64(list.LeaveCouponAmountValue)+zhios_tool_utils.StrToFloat64(req.CouponAmount), 2)) | |||
ordType := 4 | |||
if req.Type == "" { | |||
list.CouponAmountValue = utils.Float64ToStr(utils.FloatFormat(utils.StrToFloat64(list.CouponAmountValue)+utils.StrToFloat64(req.CouponAmount), 2)) | |||
list.CouponAmountValue = zhios_tool_utils.Float64ToStr(zhios_tool_utils.FloatFormat(zhios_tool_utils.StrToFloat64(list.CouponAmountValue)+zhios_tool_utils.StrToFloat64(req.CouponAmount), 2)) | |||
} | |||
if req.Type == "refund" { | |||
ordType = 5 | |||
@@ -65,7 +65,7 @@ func AddCoupon(session *xorm.Session, req *md.CouponCheckRequest) error { | |||
} | |||
bools := db.CouponUserOrdByInsert(session, tmp) | |||
if bools == false { | |||
return e.NewErr(400, "失败") | |||
return zhios_tool_e.NewErr(400, "失败") | |||
} | |||
return nil | |||
} |
@@ -3,7 +3,7 @@ package zyos_go_coupon | |||
import ( | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/db" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/md" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/utils" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_tools.git/utils" | |||
"strings" | |||
"xorm.io/xorm" | |||
) | |||
@@ -17,7 +17,7 @@ func CommCheck(eg *xorm.Engine, req md.CouponCheckRequest) map[string]string { | |||
"base_coupon_time": "1440", | |||
} | |||
//查查剩余额度 | |||
userCouponAmount, err := db.GetCouponUserTotalByLeaveSumWithType(eg, utils.StrToInt(req.Uid), req.PvdType, "leave_coupon_amount_value") | |||
userCouponAmount, err := db.GetCouponUserTotalByLeaveSumWithType(eg, zhios_tool_utils.StrToInt(req.Uid), req.PvdType, "leave_coupon_amount_value") | |||
if err != nil { | |||
return r | |||
} | |||
@@ -32,15 +32,15 @@ func CommCheck(eg *xorm.Engine, req md.CouponCheckRequest) map[string]string { | |||
isCanBuy := 1 | |||
isShowCouponList := 1 | |||
//判断额度够不够 并且有没有开启 | |||
if utils.StrToFloat64(req.CouponAmount) > userCouponAmount { | |||
if zhios_tool_utils.StrToFloat64(req.CouponAmount) > userCouponAmount { | |||
isCanBuy = 0 | |||
} | |||
if utils.StrToFloat64(req.CouponAmount) == 0 { | |||
if utils.StrToInt(req.CheckAmount) == 1 { | |||
if zhios_tool_utils.StrToFloat64(req.CouponAmount) == 0 { | |||
if zhios_tool_utils.StrToInt(req.CheckAmount) == 1 { | |||
isCanBuy = 1 | |||
isShowCouponList = 0 | |||
} | |||
if utils.StrToInt(req.CheckAmount) == 0 && userCouponAmount == 0 { | |||
if zhios_tool_utils.StrToInt(req.CheckAmount) == 0 && userCouponAmount == 0 { | |||
isCanBuy = 0 | |||
isShowCouponList = 1 | |||
} | |||
@@ -58,12 +58,12 @@ func CommCheck(eg *xorm.Engine, req md.CouponCheckRequest) map[string]string { | |||
} | |||
r = map[string]string{ | |||
"user_coupon_amount": utils.Float64ToStr(userCouponAmount), | |||
"is_show_coupon_list": utils.IntToStr(isShowCouponList), | |||
"is_can_buy": utils.IntToStr(isCanBuy), | |||
"base_coupon_time": utils.IntToStr(baseList.RefundTime), | |||
"user_coupon_amount": zhios_tool_utils.Float64ToStr(userCouponAmount), | |||
"is_show_coupon_list": zhios_tool_utils.IntToStr(isShowCouponList), | |||
"is_can_buy": zhios_tool_utils.IntToStr(isCanBuy), | |||
"base_coupon_time": zhios_tool_utils.IntToStr(baseList.RefundTime), | |||
} | |||
if utils.IntToStr(baseList.RefundTime) == "0" { | |||
if zhios_tool_utils.IntToStr(baseList.RefundTime) == "0" { | |||
r["base_coupon_time"] = "1440" | |||
} | |||
return r | |||
@@ -3,9 +3,9 @@ package zyos_go_coupon | |||
import ( | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/db" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/db/model" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/e" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/md" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/utils" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_tools.git/e" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_tools.git/utils" | |||
"fmt" | |||
"time" | |||
"xorm.io/xorm" | |||
@@ -16,31 +16,31 @@ func ReduceCoupon(eg *xorm.Engine, req md.CouponCheckRequest) error { | |||
r := CommCheck(eg, req) | |||
fmt.Println(r) | |||
if r["is_can_buy"] == "0" && req.IsMustReduce != "1" { | |||
return e.NewErr(400, "暂不能购买") | |||
return zhios_tool_e.NewErr(400, "暂不能购买") | |||
} | |||
if r["is_show_coupon_list"] == "0" && req.IsMustReduce != "1" { | |||
return nil | |||
} | |||
//判断额度够不够 | |||
sum, _ := db.GetCouponUserTotalByLeaveSumWithType(eg, utils.StrToInt(req.Uid), req.PvdType, "leave_coupon_amount_value") | |||
sum, _ := db.GetCouponUserTotalByLeaveSumWithType(eg, zhios_tool_utils.StrToInt(req.Uid), req.PvdType, "leave_coupon_amount_value") | |||
fmt.Println(sum) | |||
if sum < utils.StrToFloat64(req.CouponAmount) && req.IsMustReduce != "1" { | |||
return e.NewErr(400, "额度不足") | |||
if sum < zhios_tool_utils.StrToFloat64(req.CouponAmount) && req.IsMustReduce != "1" { | |||
return zhios_tool_e.NewErr(400, "额度不足") | |||
} | |||
session := eg.NewSession() | |||
defer session.Close() | |||
//查出该用户有的券额 | |||
list, err := db.GetCouponUserTotalByLeaveSumWithTypeSession(session, utils.StrToInt(req.Uid), req.PvdType) | |||
list, err := db.GetCouponUserTotalByLeaveSumWithTypeSession(session, zhios_tool_utils.StrToInt(req.Uid), req.PvdType) | |||
if list == nil || err != nil { | |||
session.Rollback() | |||
return err | |||
} | |||
//负数的记录 | |||
negative, _ := db.GetCouponUserTotalByNegative(eg, utils.StrToInt(req.Uid), req.PvdType, "leave_coupon_amount_value") | |||
negative, _ := db.GetCouponUserTotalByNegative(eg, zhios_tool_utils.StrToInt(req.Uid), req.PvdType, "leave_coupon_amount_value") | |||
//循环出来判断扣钱 | |||
var leaveAmount = utils.StrToFloat64(req.CouponAmount) | |||
var leaveAmount = zhios_tool_utils.StrToFloat64(req.CouponAmount) | |||
num := 0 | |||
one, err := db.CouponUserOrdByGid(session, utils.StrToInt(req.Uid), "1", req.Gid, req.Pvd) | |||
one, err := db.CouponUserOrdByGid(session, zhios_tool_utils.StrToInt(req.Uid), "1", req.Gid, req.Pvd) | |||
if err != nil { | |||
session.Rollback() | |||
return err | |||
@@ -54,13 +54,13 @@ func ReduceCoupon(eg *xorm.Engine, req md.CouponCheckRequest) error { | |||
var count = len(*list) | |||
for k, v := range *list { | |||
LeaveCouponAmountValue := v.LeaveCouponAmountValue | |||
if leaveAmount == 0 || (utils.StrToFloat64(v.LeaveCouponAmountValue) == 0 && req.IsMustReduce != "1") { //没有了 | |||
if leaveAmount == 0 || (zhios_tool_utils.StrToFloat64(v.LeaveCouponAmountValue) == 0 && req.IsMustReduce != "1") { //没有了 | |||
continue | |||
} | |||
//减去负数的金额 | |||
var amount = utils.StrToFloat64(v.LeaveCouponAmountValue) | |||
var amount = zhios_tool_utils.StrToFloat64(v.LeaveCouponAmountValue) | |||
//当前能使用的额度 | |||
var canUseAmount = utils.StrToFloat64(v.LeaveCouponAmountValue) + negative | |||
var canUseAmount = zhios_tool_utils.StrToFloat64(v.LeaveCouponAmountValue) + negative | |||
var basicAmount = amount - canUseAmount | |||
if canUseAmount > 0 { | |||
amount = canUseAmount | |||
@@ -81,13 +81,13 @@ func ReduceCoupon(eg *xorm.Engine, req md.CouponCheckRequest) error { | |||
leaveAmount = leaveAmount - amount | |||
} | |||
leave = leave + basicAmount | |||
v.LeaveCouponAmountValue = utils.Float64ToStr(leave) | |||
v.LeaveCouponAmountValue = zhios_tool_utils.Float64ToStr(leave) | |||
fmt.Println(v.LeaveCouponAmountValue) | |||
has, err := db.CouponUserTotalByInsertWithUpdate(session, &v) | |||
if (has == 0 && leave != amount) || err != nil { | |||
fmt.Println(err) | |||
session.Rollback() | |||
return e.NewErr(400, "抵扣失败,暂不能购买!") | |||
return zhios_tool_e.NewErr(400, "抵扣失败,暂不能购买!") | |||
} | |||
if reduceAmount != 0 { | |||
@@ -98,7 +98,7 @@ func ReduceCoupon(eg *xorm.Engine, req md.CouponCheckRequest) error { | |||
OrdType: 3, | |||
UsePvd: v.UsePvd, | |||
PackageId: 0, | |||
CouponAmountValue: utils.Float64ToStr(reduceAmount), | |||
CouponAmountValue: zhios_tool_utils.Float64ToStr(reduceAmount), | |||
PayAmountValue: "", | |||
Discount: "", | |||
PayAmount: req.CouponAmount, | |||
@@ -109,19 +109,19 @@ func ReduceCoupon(eg *xorm.Engine, req md.CouponCheckRequest) error { | |||
Type: 1, | |||
Gid: req.Gid, | |||
Batch: num, | |||
ExpectedRefundTime: int(time.Now().Unix()) + utils.StrToInt(r["base_coupon_time"])*60, | |||
ExpectedRefundTime: int(time.Now().Unix()) + zhios_tool_utils.StrToInt(r["base_coupon_time"])*60, | |||
GoodsTitle: req.GoodsTitle, | |||
Oid: req.Oid, | |||
BeforeAmout: LeaveCouponAmountValue, | |||
AfterAmout: v.LeaveCouponAmountValue, | |||
} | |||
if req.CreateAt != "" { | |||
tmp.CreateAt = utils.TimeParseStd(req.CreateAt) | |||
tmp.CreateAt = zhios_tool_utils.TimeParseStd(req.CreateAt) | |||
} | |||
bools := db.CouponUserOrdByInsert(session, tmp) | |||
if bools == false { | |||
session.Rollback() | |||
return e.NewErr(400, "抵扣失败,暂不能购买!") | |||
return zhios_tool_e.NewErr(400, "抵扣失败,暂不能购买!") | |||
} | |||
} | |||
@@ -1,34 +0,0 @@ | |||
package e | |||
import ( | |||
"fmt" | |||
"path" | |||
"runtime" | |||
) | |||
type E struct { | |||
Code int // 错误码 | |||
msg string // 报错代码 | |||
st string // 堆栈信息 | |||
} | |||
func NewErr(code int, msg string) error { | |||
return E{code, msg, stack(3)} | |||
} | |||
func (e E) Error() string { | |||
return e.msg | |||
} | |||
func stack(skip int) string { | |||
stk := make([]uintptr, 32) | |||
str := "" | |||
l := runtime.Callers(skip, stk[:]) | |||
for i := 0; i < l; i++ { | |||
f := runtime.FuncForPC(stk[i]) | |||
name := f.Name() | |||
file, line := f.FileLine(stk[i]) | |||
str += fmt.Sprintf("\n%-30s[%s:%d]", name, path.Base(file), line) | |||
} | |||
return str | |||
} |
@@ -5,4 +5,5 @@ go 1.16 | |||
require ( | |||
github.com/go-sql-driver/mysql v1.6.0 | |||
xorm.io/xorm v1.3.0 | |||
code.fnuoos.com/go_rely_warehouse/zyos_go_tools.git v1.0.7 // indirect | |||
) |
@@ -1,359 +0,0 @@ | |||
package utils | |||
import ( | |||
"encoding/binary" | |||
"encoding/json" | |||
"fmt" | |||
"math" | |||
"reflect" | |||
"strconv" | |||
"strings" | |||
) | |||
func ToString(raw interface{}, e error) (res string) { | |||
if e != nil { | |||
return "" | |||
} | |||
return AnyToString(raw) | |||
} | |||
func ToInt64(raw interface{}, e error) int64 { | |||
if e != nil { | |||
return 0 | |||
} | |||
return AnyToInt64(raw) | |||
} | |||
func AnyToBool(raw interface{}) bool { | |||
switch i := raw.(type) { | |||
case float32, float64, int, int64, uint, uint8, uint16, uint32, uint64, int8, int16, int32: | |||
return i != 0 | |||
case []byte: | |||
return i != nil | |||
case string: | |||
if i == "false" { | |||
return false | |||
} | |||
return i != "" | |||
case error: | |||
return false | |||
case nil: | |||
return true | |||
} | |||
val := fmt.Sprint(raw) | |||
val = strings.TrimLeft(val, "&") | |||
if strings.TrimLeft(val, "{}") == "" { | |||
return false | |||
} | |||
if strings.TrimLeft(val, "[]") == "" { | |||
return false | |||
} | |||
// ptr type | |||
b, err := json.Marshal(raw) | |||
if err != nil { | |||
return false | |||
} | |||
if strings.TrimLeft(string(b), "\"\"") == "" { | |||
return false | |||
} | |||
if strings.TrimLeft(string(b), "{}") == "" { | |||
return false | |||
} | |||
return true | |||
} | |||
func AnyToInt64(raw interface{}) int64 { | |||
switch i := raw.(type) { | |||
case string: | |||
res, _ := strconv.ParseInt(i, 10, 64) | |||
return res | |||
case []byte: | |||
return BytesToInt64(i) | |||
case int: | |||
return int64(i) | |||
case int64: | |||
return i | |||
case uint: | |||
return int64(i) | |||
case uint8: | |||
return int64(i) | |||
case uint16: | |||
return int64(i) | |||
case uint32: | |||
return int64(i) | |||
case uint64: | |||
return int64(i) | |||
case int8: | |||
return int64(i) | |||
case int16: | |||
return int64(i) | |||
case int32: | |||
return int64(i) | |||
case float32: | |||
return int64(i) | |||
case float64: | |||
return int64(i) | |||
case error: | |||
return 0 | |||
case bool: | |||
if i { | |||
return 1 | |||
} | |||
return 0 | |||
} | |||
return 0 | |||
} | |||
func AnyToString(raw interface{}) string { | |||
switch i := raw.(type) { | |||
case []byte: | |||
return string(i) | |||
case int: | |||
return strconv.FormatInt(int64(i), 10) | |||
case int64: | |||
return strconv.FormatInt(i, 10) | |||
case float32: | |||
return Float64ToStr(float64(i)) | |||
case float64: | |||
return Float64ToStr(i) | |||
case uint: | |||
return strconv.FormatInt(int64(i), 10) | |||
case uint8: | |||
return strconv.FormatInt(int64(i), 10) | |||
case uint16: | |||
return strconv.FormatInt(int64(i), 10) | |||
case uint32: | |||
return strconv.FormatInt(int64(i), 10) | |||
case uint64: | |||
return strconv.FormatInt(int64(i), 10) | |||
case int8: | |||
return strconv.FormatInt(int64(i), 10) | |||
case int16: | |||
return strconv.FormatInt(int64(i), 10) | |||
case int32: | |||
return strconv.FormatInt(int64(i), 10) | |||
case string: | |||
return i | |||
case error: | |||
return i.Error() | |||
case bool: | |||
return strconv.FormatBool(i) | |||
} | |||
return fmt.Sprintf("%#v", raw) | |||
} | |||
func AnyToFloat64(raw interface{}) float64 { | |||
switch i := raw.(type) { | |||
case []byte: | |||
f, _ := strconv.ParseFloat(string(i), 64) | |||
return f | |||
case int: | |||
return float64(i) | |||
case int64: | |||
return float64(i) | |||
case float32: | |||
return float64(i) | |||
case float64: | |||
return i | |||
case uint: | |||
return float64(i) | |||
case uint8: | |||
return float64(i) | |||
case uint16: | |||
return float64(i) | |||
case uint32: | |||
return float64(i) | |||
case uint64: | |||
return float64(i) | |||
case int8: | |||
return float64(i) | |||
case int16: | |||
return float64(i) | |||
case int32: | |||
return float64(i) | |||
case string: | |||
f, _ := strconv.ParseFloat(i, 64) | |||
return f | |||
case bool: | |||
if i { | |||
return 1 | |||
} | |||
} | |||
return 0 | |||
} | |||
func ToByte(raw interface{}, e error) []byte { | |||
if e != nil { | |||
return []byte{} | |||
} | |||
switch i := raw.(type) { | |||
case string: | |||
return []byte(i) | |||
case int: | |||
return Int64ToBytes(int64(i)) | |||
case int64: | |||
return Int64ToBytes(i) | |||
case float32: | |||
return Float32ToByte(i) | |||
case float64: | |||
return Float64ToByte(i) | |||
case uint: | |||
return Int64ToBytes(int64(i)) | |||
case uint8: | |||
return Int64ToBytes(int64(i)) | |||
case uint16: | |||
return Int64ToBytes(int64(i)) | |||
case uint32: | |||
return Int64ToBytes(int64(i)) | |||
case uint64: | |||
return Int64ToBytes(int64(i)) | |||
case int8: | |||
return Int64ToBytes(int64(i)) | |||
case int16: | |||
return Int64ToBytes(int64(i)) | |||
case int32: | |||
return Int64ToBytes(int64(i)) | |||
case []byte: | |||
return i | |||
case error: | |||
return []byte(i.Error()) | |||
case bool: | |||
if i { | |||
return []byte("true") | |||
} | |||
return []byte("false") | |||
} | |||
return []byte(fmt.Sprintf("%#v", raw)) | |||
} | |||
func Int64ToBytes(i int64) []byte { | |||
var buf = make([]byte, 8) | |||
binary.BigEndian.PutUint64(buf, uint64(i)) | |||
return buf | |||
} | |||
func BytesToInt64(buf []byte) int64 { | |||
return int64(binary.BigEndian.Uint64(buf)) | |||
} | |||
func StrToInt(s string) int { | |||
res, _ := strconv.Atoi(s) | |||
return res | |||
} | |||
func StrToInt64(s string) int64 { | |||
res, _ := strconv.ParseInt(s, 10, 64) | |||
return res | |||
} | |||
func Float32ToByte(float float32) []byte { | |||
bits := math.Float32bits(float) | |||
bytes := make([]byte, 4) | |||
binary.LittleEndian.PutUint32(bytes, bits) | |||
return bytes | |||
} | |||
func ByteToFloat32(bytes []byte) float32 { | |||
bits := binary.LittleEndian.Uint32(bytes) | |||
return math.Float32frombits(bits) | |||
} | |||
func Float64ToByte(float float64) []byte { | |||
bits := math.Float64bits(float) | |||
bytes := make([]byte, 8) | |||
binary.LittleEndian.PutUint64(bytes, bits) | |||
return bytes | |||
} | |||
func ByteToFloat64(bytes []byte) float64 { | |||
bits := binary.LittleEndian.Uint64(bytes) | |||
return math.Float64frombits(bits) | |||
} | |||
func Float64ToStr(f float64) string { | |||
return strconv.FormatFloat(f, 'f', 2, 64) | |||
} | |||
func Float64ToStrWithPoint(f float64, point int) string { | |||
return strconv.FormatFloat(f, 'f', point, 64) | |||
} | |||
func Float64ToStrByPrec(f float64, prec int) string { | |||
return strconv.FormatFloat(f, 'f', prec, 64) | |||
} | |||
func Float64ToStrPrec1(f float64) string { | |||
return strconv.FormatFloat(f, 'f', 1, 64) | |||
} | |||
func Float32ToStr(f float32) string { | |||
return Float64ToStr(float64(f)) | |||
} | |||
func StrToFloat64(s string) float64 { | |||
res, err := strconv.ParseFloat(s, 64) | |||
if err != nil { | |||
return 0 | |||
} | |||
return res | |||
} | |||
func StrToFloat32(s string) float32 { | |||
res, err := strconv.ParseFloat(s, 32) | |||
if err != nil { | |||
return 0 | |||
} | |||
return float32(res) | |||
} | |||
func StrToBool(s string) bool { | |||
b, _ := strconv.ParseBool(s) | |||
return b | |||
} | |||
func BoolToStr(b bool) string { | |||
if b { | |||
return "true" | |||
} | |||
return "false" | |||
} | |||
func FloatToInt64(f float64) int64 { | |||
return int64(f) | |||
} | |||
func IntToStr(i int) string { | |||
return strconv.Itoa(i) | |||
} | |||
func Int64ToStr(i int64) string { | |||
return strconv.FormatInt(i, 10) | |||
} | |||
func IsNil(i interface{}) bool { | |||
vi := reflect.ValueOf(i) | |||
if vi.Kind() == reflect.Ptr { | |||
return vi.IsNil() | |||
} | |||
return false | |||
} | |||
func StrToFormat(s string, prec int) string { | |||
ex := strings.Split(s, ".") | |||
if len(ex) == 2 { | |||
if StrToFloat64(ex[1]) == 0 { //小数点后面为空就是不要小数点了 | |||
return ex[0] | |||
} | |||
//看取多少位 | |||
str := ex[1] | |||
str1 := str | |||
if prec < len(str) { | |||
str1 = str[0:prec] | |||
} else { | |||
for i := 0; i < prec-len(str); i++ { | |||
str1 += "0" | |||
} | |||
} | |||
return ex[0] + "." + str1 | |||
} | |||
return s | |||
} |
@@ -1,66 +0,0 @@ | |||
package utils | |||
import ( | |||
"math" | |||
) | |||
func CouponFormat(data string) string { | |||
switch data { | |||
case "0.00", "0", "": | |||
return "" | |||
default: | |||
return Int64ToStr(FloatToInt64(StrToFloat64(data))) | |||
} | |||
} | |||
func CommissionFormat(data string) string { | |||
if StrToFloat64(data) > 0 { | |||
return data | |||
} | |||
return "" | |||
} | |||
func HideString(src string, hLen int) string { | |||
str := []rune(src) | |||
if hLen == 0 { | |||
hLen = 4 | |||
} | |||
hideStr := "" | |||
for i := 0; i < hLen; i++ { | |||
hideStr += "*" | |||
} | |||
hideLen := len(str) / 2 | |||
showLen := len(str) - hideLen | |||
if hideLen == 0 || showLen == 0 { | |||
return hideStr | |||
} | |||
subLen := showLen / 2 | |||
if subLen == 0 { | |||
return string(str[:showLen]) + hideStr | |||
} | |||
s := string(str[:subLen]) | |||
s += hideStr | |||
s += string(str[len(str)-subLen:]) | |||
return s | |||
} | |||
//SaleCountFormat is 格式化销量 | |||
func SaleCountFormat(s string) string { | |||
if StrToInt(s) > 0 { | |||
if StrToInt(s) >= 10000 { | |||
num := FloatFormat(StrToFloat64(s)/10000, 2) | |||
s = Float64ToStr(num) + "w" | |||
} | |||
return s + "已售" | |||
} | |||
return "" | |||
} | |||
// 小数格式化 | |||
func FloatFormat(f float64, i int) float64 { | |||
if i > 14 { | |||
return f | |||
} | |||
p := math.Pow10(i) | |||
return float64(int64((f+0.000000000000009)*p)) / p | |||
} |
@@ -1,295 +0,0 @@ | |||
package utils | |||
import ( | |||
"errors" | |||
"fmt" | |||
"strconv" | |||
"strings" | |||
"time" | |||
) | |||
func StrToTime(s string) (int64, error) { | |||
// delete all not int characters | |||
if s == "" { | |||
return time.Now().Unix(), nil | |||
} | |||
r := make([]rune, 14) | |||
l := 0 | |||
// 过滤除数字以外的字符 | |||
for _, v := range s { | |||
if '0' <= v && v <= '9' { | |||
r[l] = v | |||
l++ | |||
if l == 14 { | |||
break | |||
} | |||
} | |||
} | |||
for l < 14 { | |||
r[l] = '0' // 补0 | |||
l++ | |||
} | |||
t, err := time.Parse("20060102150405", string(r)) | |||
if err != nil { | |||
return 0, err | |||
} | |||
return t.Unix(), nil | |||
} | |||
func Time2String(date time.Time, format string) string { | |||
if format == "" { | |||
format = "2006-01-02 15:04:05" | |||
} | |||
timeS := date.Format(format) | |||
if timeS == "0001-01-01 00:00:00" { | |||
return "" | |||
} | |||
return timeS | |||
} | |||
func TimeToStr(unixSecTime interface{}, layout ...string) string { | |||
i := AnyToInt64(unixSecTime) | |||
if i == 0 { | |||
return "" | |||
} | |||
f := "2006-01-02 15:04:05" | |||
if len(layout) > 0 { | |||
f = layout[0] | |||
} | |||
return time.Unix(i, 0).Format(f) | |||
} | |||
func FormatNanoUnix() string { | |||
return strings.Replace(time.Now().Format("20060102150405.0000000"), ".", "", 1) | |||
} | |||
func TimeParse(format, src string) (time.Time, error) { | |||
return time.ParseInLocation(format, src, time.Local) | |||
} | |||
func TimeParseStd(src string) time.Time { | |||
t, _ := TimeParse("2006-01-02 15:04:05", src) | |||
return t | |||
} | |||
func TimeStdParseUnix(src string) int64 { | |||
t, err := TimeParse("2006-01-02 15:04:05", src) | |||
if err != nil { | |||
return 0 | |||
} | |||
return t.Unix() | |||
} | |||
// 获取一个当前时间 时间间隔 时间戳 | |||
func GetTimeInterval(unit string, amount int) (startTime, endTime int64) { | |||
t := time.Now() | |||
nowTime := t.Unix() | |||
tmpTime := int64(0) | |||
switch unit { | |||
case "years": | |||
tmpTime = time.Date(t.Year()+amount, t.Month(), t.Day(), t.Hour(), 0, 0, 0, t.Location()).Unix() | |||
case "months": | |||
tmpTime = time.Date(t.Year(), t.Month()+time.Month(amount), t.Day(), t.Hour(), 0, 0, 0, t.Location()).Unix() | |||
case "days": | |||
tmpTime = time.Date(t.Year(), t.Month(), t.Day()+amount, t.Hour(), 0, 0, 0, t.Location()).Unix() | |||
case "hours": | |||
tmpTime = time.Date(t.Year(), t.Month(), t.Day(), t.Hour()+amount, 0, 0, 0, t.Location()).Unix() | |||
} | |||
if amount > 0 { | |||
startTime = nowTime | |||
endTime = tmpTime | |||
} else { | |||
startTime = tmpTime | |||
endTime = nowTime | |||
} | |||
return | |||
} | |||
// 几天前 | |||
func TimeInterval(newTime int) string { | |||
now := time.Now().Unix() | |||
newTime64 := AnyToInt64(newTime) | |||
if newTime64 >= now { | |||
return "刚刚" | |||
} | |||
interval := now - newTime64 | |||
switch { | |||
case interval < 60: | |||
return AnyToString(interval) + "秒前" | |||
case interval < 60*60: | |||
return AnyToString(interval/60) + "分前" | |||
case interval < 60*60*24: | |||
return AnyToString(interval/60/60) + "小时前" | |||
case interval < 60*60*24*30: | |||
return AnyToString(interval/60/60/24) + "天前" | |||
case interval < 60*60*24*30*12: | |||
return AnyToString(interval/60/60/24/30) + "月前" | |||
default: | |||
return AnyToString(interval/60/60/24/30/12) + "年前" | |||
} | |||
} | |||
// 时分秒字符串转时间戳,传入示例:8:40 or 8:40:10 | |||
func HmsToUnix(str string) (int64, error) { | |||
t := time.Now() | |||
arr := strings.Split(str, ":") | |||
if len(arr) < 3 { | |||
return 0, errors.New("Time format error") | |||
} | |||
h, _ := strconv.Atoi(arr[0]) | |||
m, _ := strconv.Atoi(arr[1]) | |||
s := 0 | |||
if len(arr) == 3 { | |||
s, _ = strconv.Atoi(arr[2]) | |||
} | |||
formatted1 := fmt.Sprintf("%d%02d%02d%02d%02d%02d", t.Year(), t.Month(), t.Day(), h, m, s) | |||
res, err := time.ParseInLocation("20060102150405", formatted1, time.Local) | |||
if err != nil { | |||
return 0, err | |||
} else { | |||
return res.Unix(), nil | |||
} | |||
} | |||
// 获取特定时间范围 | |||
func GetTimeRange(s string) map[string]int64 { | |||
t := time.Now() | |||
var stime, etime time.Time | |||
switch s { | |||
case "today": | |||
stime = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()) | |||
etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location()) | |||
case "yesterday": | |||
stime = time.Date(t.Year(), t.Month(), t.Day()-1, 0, 0, 0, 0, t.Location()) | |||
etime = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()) | |||
case "within_seven_days": | |||
// 前6天0点 | |||
stime = time.Date(t.Year(), t.Month(), t.Day()-6, 0, 0, 0, 0, t.Location()) | |||
// 明天 0点 | |||
etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location()) | |||
case "current_month": | |||
stime = time.Date(t.Year(), t.Month(), 0, 0, 0, 0, 0, t.Location()) | |||
etime = time.Date(t.Year(), t.Month()+1, 0, 0, 0, 0, 0, t.Location()) | |||
case "last_month": | |||
stime = time.Date(t.Year(), t.Month()-1, 0, 0, 0, 0, 0, t.Location()) | |||
etime = time.Date(t.Year(), t.Month(), 0, 0, 0, 0, 0, t.Location()) | |||
} | |||
return map[string]int64{ | |||
"start": stime.Unix(), | |||
"end": etime.Unix(), | |||
} | |||
} | |||
// 获取特定时间范围 | |||
func GetTimes(s string) map[string]string { | |||
t := time.Now() | |||
var stime, etime time.Time | |||
switch s { | |||
case "today": | |||
stime = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()) | |||
etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location()) | |||
case "yesterday": | |||
stime = time.Date(t.Year(), t.Month(), t.Day()-1, 0, 0, 0, 0, t.Location()) | |||
etime = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()) | |||
case "within_seven_days": | |||
// 前6天0点 | |||
stime = time.Date(t.Year(), t.Month(), t.Day()-6, 0, 0, 0, 0, t.Location()) | |||
// 明天 0点 | |||
etime = time.Date(t.Year(), t.Month(), t.Day()+1, 0, 0, 0, 0, t.Location()) | |||
case "current_month": | |||
stime = time.Date(t.Year(), t.Month(), 0, 0, 0, 0, 0, t.Location()) | |||
etime = time.Date(t.Year(), t.Month()+1, 0, 0, 0, 0, 0, t.Location()) | |||
case "last_month": | |||
stime = time.Date(t.Year(), t.Month()-1, 0, 0, 0, 0, 0, t.Location()) | |||
etime = time.Date(t.Year(), t.Month(), 0, 0, 0, 0, 0, t.Location()) | |||
} | |||
return map[string]string{ | |||
"start": stime.Format("2006-01-02 15:04:05"), | |||
"end": etime.Format("2006-01-02 15:04:05"), | |||
} | |||
} | |||
//获取传入的时间所在月份的第一天,即某月第一天的0点。如传入time.Now(), 返回当前月份的第一天0点时间。 | |||
func GetFirstDateOfMonth(d time.Time) time.Time { | |||
d = d.AddDate(0, 0, -d.Day()+1) | |||
return GetZeroTime(d) | |||
} | |||
//获取传入的时间所在月份的最后一天,即某月最后一天的0点。如传入time.Now(), 返回当前月份的最后一天0点时间。 | |||
func GetLastDateOfMonth(d time.Time) time.Time { | |||
return GetFirstDateOfMonth(d).AddDate(0, 1, -1) | |||
} | |||
func GetAnyFirstDateOfMonth(d time.Time, monthDiff int) time.Time { | |||
year, month, _ := d.Date() | |||
thisMonth := time.Date(year, month, 1, 0, 0, 0, 0, time.Local) | |||
monthOneDay := thisMonth.AddDate(0, monthDiff, 0) | |||
return monthOneDay | |||
} | |||
//当天时间戳 | |||
func GetDateTime(date string) (int64, int64) { | |||
//获取当前时区 | |||
loc, _ := time.LoadLocation("Local") | |||
//日期当天0点时间戳(拼接字符串) | |||
startDate := date + "_00:00:00" | |||
startTime, _ := time.ParseInLocation("2006-01-02_15:04:05", startDate, loc) | |||
//日期当天23时59分时间戳 | |||
endDate := date + "_23:59:59" | |||
end, _ := time.ParseInLocation("2006-01-02_15:04:05", endDate, loc) | |||
//返回当天0点和23点59分的时间戳 | |||
return startTime.Unix(), end.Unix() | |||
} | |||
//获取某一天的0点时间 | |||
func GetZeroTime(d time.Time) time.Time { | |||
return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location()) | |||
} | |||
// getYearMonthToDay 查询指定年份指定月份有多少天 | |||
// @params year int 指定年份 | |||
// @params month int 指定月份 | |||
func GetYearMonthToDay(year int, month int) int { | |||
// 有31天的月份 | |||
day31 := map[int]bool{ | |||
1: true, | |||
3: true, | |||
5: true, | |||
7: true, | |||
8: true, | |||
10: true, | |||
12: true, | |||
} | |||
if day31[month] == true { | |||
return 31 | |||
} | |||
// 有30天的月份 | |||
day30 := map[int]bool{ | |||
4: true, | |||
6: true, | |||
9: true, | |||
11: true, | |||
} | |||
if day30[month] == true { | |||
return 30 | |||
} | |||
// 计算是平年还是闰年 | |||
if (year%4 == 0 && year%100 != 0) || year%400 == 0 { | |||
// 得出2月的天数 | |||
return 29 | |||
} | |||
// 得出2月的天数 | |||
return 28 | |||
} | |||
// 获取两个时间相差的天数,0表同一天,正数表t1>t2,负数表t1<t2 | |||
func GetDiffDays(t1, t2 time.Time) int { | |||
t1 = time.Date(t1.Year(), t1.Month(), t1.Day(), 0, 0, 0, 0, time.Local) | |||
t2 = time.Date(t2.Year(), t2.Month(), t2.Day(), 0, 0, 0, 0, time.Local) | |||
return int(t1.Sub(t2).Hours() / 24) | |||
} |