@@ -0,0 +1,71 @@ | |||
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" | |||
"fmt" | |||
"time" | |||
"xorm.io/xorm" | |||
) | |||
//额度添加 | |||
func AddCoupon(session *xorm.Session, req *md.CouponCheckRequest) error { | |||
if req.PvdType == "" { | |||
req.PvdType = "ordinary" | |||
} | |||
//查出该用户有的券额 | |||
list, err := db.GetCouponUserTotalByOne(session, utils.StrToInt(req.Uid), req.PvdType) | |||
if err != nil { | |||
return err | |||
} else if list == nil { | |||
list = &model.CommCouponUserTotal{ | |||
Uid: 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)) | |||
ordType := 4 | |||
if req.Type == "" { | |||
list.CouponAmountValue = utils.Float64ToStr(utils.FloatFormat(utils.StrToFloat64(list.CouponAmountValue)+utils.StrToFloat64(req.CouponAmount), 2)) | |||
} | |||
if req.Type == "refund" { | |||
ordType = 5 | |||
} | |||
has, err := db.CouponUserTotalByInsertWithUpdate(session, list) | |||
if has == 0 || err != nil { | |||
fmt.Println(err) | |||
return err | |||
} | |||
//写入明细 | |||
var tmp = &model.CommCouponUserOrd{ | |||
Uid: list.Uid, | |||
PayMethod: 0, | |||
OrdType: ordType, | |||
UsePvd: list.UsePvd, | |||
PackageId: 0, | |||
CouponAmountValue: req.CouponAmount, | |||
PayAmountValue: "", | |||
Discount: "", | |||
PayAmount: "", | |||
CreateAt: time.Now(), | |||
UpdateAt: time.Now(), | |||
PvdType: list.PvdType, | |||
GoodsType: req.Pvd, | |||
Gid: req.Gid, | |||
GoodsTitle: req.GoodsTitle, | |||
BeforeAmout: LeaveCouponAmountValue, | |||
AfterAmout: list.LeaveCouponAmountValue, | |||
} | |||
bools := db.CouponUserOrdByInsert(session, tmp) | |||
if bools == false { | |||
return e.NewErr(400, "失败") | |||
} | |||
return nil | |||
} |
@@ -8,6 +8,7 @@ import ( | |||
"xorm.io/xorm" | |||
) | |||
//检测额度是否足够 | |||
func CommCheck(eg *xorm.Engine, req md.CouponCheckRequest) map[string]string { | |||
var r = map[string]string{ | |||
"user_coupon_amount": "0", | |||
@@ -0,0 +1,146 @@ | |||
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" | |||
"fmt" | |||
"time" | |||
"xorm.io/xorm" | |||
) | |||
//优惠券扣钱 | |||
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, "暂不能购买") | |||
} | |||
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") | |||
fmt.Println(sum) | |||
if sum < utils.StrToFloat64(req.CouponAmount) && req.IsMustReduce != "1" { | |||
return e.NewErr(400, "额度不足") | |||
} | |||
session := eg.NewSession() | |||
defer session.Close() | |||
//查出该用户有的券额 | |||
list, err := db.GetCouponUserTotalByLeaveSumWithTypeSession(session, 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") | |||
//循环出来判断扣钱 | |||
var leaveAmount = utils.StrToFloat64(req.CouponAmount) | |||
num := 0 | |||
one, err := db.CouponUserOrdByGid(session, utils.StrToInt(req.Uid), "1", req.Gid, req.Pvd) | |||
if err != nil { | |||
session.Rollback() | |||
return err | |||
} | |||
if one != nil { | |||
if req.Pvd == "oil" { //加油判断如果有了就不加了 | |||
return err | |||
} | |||
num = one.Batch + 1 | |||
} | |||
var count = len(*list) | |||
for k, v := range *list { | |||
LeaveCouponAmountValue := v.LeaveCouponAmountValue | |||
if leaveAmount == 0 || (utils.StrToFloat64(v.LeaveCouponAmountValue) == 0 && req.IsMustReduce != "1") { //没有了 | |||
continue | |||
} | |||
//减去负数的金额 | |||
var amount = utils.StrToFloat64(v.LeaveCouponAmountValue) | |||
//当前能使用的额度 | |||
var canUseAmount = utils.StrToFloat64(v.LeaveCouponAmountValue) + negative | |||
var basicAmount = amount - canUseAmount | |||
if canUseAmount > 0 { | |||
amount = canUseAmount | |||
negative = 0 | |||
} else { | |||
continue | |||
} | |||
var leave = amount - leaveAmount | |||
var reduceAmount = leaveAmount | |||
//如果大于等于0 | |||
if leave >= 0 { | |||
leaveAmount = 0 | |||
} | |||
//如果小于0 | |||
if leave < 0 && k+1 != count { //如果不是最后一个 | |||
reduceAmount = amount | |||
leave = 0 //被扣完了 | |||
leaveAmount = leaveAmount - amount | |||
} | |||
leave = leave + basicAmount | |||
v.LeaveCouponAmountValue = 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, "抵扣失败,暂不能购买!") | |||
} | |||
if reduceAmount != 0 { | |||
//写入明细 | |||
var tmp = &model.CommCouponUserOrd{ | |||
Uid: v.Uid, | |||
PayMethod: 0, | |||
OrdType: 3, | |||
UsePvd: v.UsePvd, | |||
PackageId: 0, | |||
CouponAmountValue: utils.Float64ToStr(reduceAmount), | |||
PayAmountValue: "", | |||
Discount: "", | |||
PayAmount: req.CouponAmount, | |||
CreateAt: time.Now(), | |||
UpdateAt: time.Now(), | |||
PvdType: v.PvdType, | |||
GoodsType: req.Pvd, | |||
Type: 1, | |||
Gid: req.Gid, | |||
Batch: num, | |||
ExpectedRefundTime: int(time.Now().Unix()) + 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) | |||
} | |||
bools := db.CouponUserOrdByInsert(session, tmp) | |||
if bools == false { | |||
session.Rollback() | |||
return e.NewErr(400, "抵扣失败,暂不能购买!") | |||
} | |||
} | |||
} | |||
if req.IsChangeState == "1" { | |||
//查询优惠券使用记录 | |||
couponList, _ := db.CouponUserOrdByGidAscBatch(session, req.Uid, req.Gid, 0, req.Pvd) | |||
if couponList != nil { | |||
couponAll, _ := db.CouponUserOrdByGidAscBatchALL(session, req.Uid, req.Gid, 0, couponList.Batch, req.Pvd) | |||
if couponAll != nil { | |||
for _, cv := range *couponAll { | |||
//更新使用状态 | |||
cv.RefundState = 1 | |||
db.CouponUserOrdByUpdate(session, &cv) | |||
} | |||
} | |||
} | |||
} | |||
session.Commit() | |||
return nil | |||
} |
@@ -0,0 +1,39 @@ | |||
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" | |||
"xorm.io/xorm" | |||
) | |||
//统计额度 | |||
func CouponTotal(eg *xorm.Engine, uid int) (userCouponAmount float64, couponSum float64, buySum float64) { | |||
userCouponAmount = GetCouponAmount(eg, uid, "", "leave_coupon_amount_value") //剩余额度 | |||
couponSum = GetCouponAmount(eg, uid, "", "coupon_amount_value") //总额度 | |||
buySum = GetCouponOrdAmount(eg, uid, "coupon_amount_value", 2, 4, 6) //购买总额度 | |||
return userCouponAmount, couponSum, buySum | |||
} | |||
//购买统计 | |||
func GetCouponOrdAmount(eg *xorm.Engine, uid int, cols string, ordType ...int) float64 { | |||
sum, err := db.GetCouponUserOrdBySumWithOrdType(eg, uid, cols, ordType...) | |||
if err != nil { | |||
return 0 | |||
} | |||
return sum | |||
} | |||
//额度统计 | |||
func GetCouponAmount(eg *xorm.Engine, uid int, pvd string, cols string) float64 { | |||
var data = &model.CommCouponUserTotal{ | |||
Uid: uid, | |||
} | |||
if pvd != "" { | |||
data.PvdType = pvd | |||
} | |||
sum, err := db.GetCouponUserTotalByLeaveSum(eg, data, cols) | |||
if err != nil { | |||
return 0 | |||
} | |||
return sum | |||
} |
@@ -0,0 +1,222 @@ | |||
package db | |||
import ( | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/db/model" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/utils" | |||
"fmt" | |||
"xorm.io/xorm" | |||
) | |||
//统计金额 | |||
func GetCouponUserOrdBySum(Db *xorm.Engine, condition *model.CommCouponUserOrd, cols string) (float64, error) { | |||
total, err := Db.Sum(condition, cols) | |||
if err != nil { | |||
return 0, err | |||
} | |||
return total, err | |||
} | |||
//统计金额 | |||
func GetCouponUserOrdBySumWithOrdType(Db *xorm.Engine, uid int, cols string, ordType ...int) (float64, error) { | |||
session := Db.Where("1=1") | |||
if uid > 0 { | |||
session = session.And("uid=?", uid) | |||
} | |||
if len(ordType) > 0 { | |||
session = session.In("ord_type", ordType) | |||
} | |||
total, err := session.Sum(&model.CommCouponUserOrd{}, cols) | |||
if err != nil { | |||
return 0, err | |||
} | |||
return total, err | |||
} | |||
//统计数量 | |||
func GetCouponUserOrdByCount(Db *xorm.Engine, condition *model.CommCouponUserOrd, createAt string) (total int64, err error) { | |||
if createAt != "" { | |||
total, err = Db.Where("create_at >= ? ", createAt).Count(condition) | |||
} else { | |||
total, err = Db.Count(condition) | |||
} | |||
fmt.Println(createAt) | |||
fmt.Println(total) | |||
if err != nil { | |||
return 0, err | |||
} | |||
return total, err | |||
} | |||
func CouponUserOrdByInsert(session *xorm.Session, m *model.CommCouponUserOrd) bool { | |||
_, err := session.Insert(m) | |||
if err != nil { | |||
return false | |||
} | |||
return true | |||
} | |||
func CouponUserOrdByGidAscBatch(session *xorm.Session, uid string, gid string, refundState int, goodsType string) (*model.CommCouponUserOrd, error) { | |||
var m model.CommCouponUserOrd | |||
session = session.Where("uid=? AND type= ? and gid=? and refund_state=?", uid, 1, gid, refundState) | |||
if goodsType != "" { | |||
session = session.And("goods_type=?", goodsType) | |||
} | |||
has, err := session.Asc("batch").Get(&m) | |||
if has == false || err != nil { | |||
return nil, err | |||
} | |||
return &m, nil | |||
} | |||
func CouponUserOrdById(eg *xorm.Engine, id string) (*model.CommCouponUserOrd, error) { | |||
var m model.CommCouponUserOrd | |||
has, err := eg.Where("id=?", id).Get(&m) | |||
if has == false || err != nil { | |||
return nil, err | |||
} | |||
return &m, nil | |||
} | |||
func CouponUserOrdByGidAscBatchALL(session *xorm.Session, uid string, gid string, refundState int, batch int, goodsType string) (*[]model.CommCouponUserOrd, error) { | |||
var m []model.CommCouponUserOrd | |||
session = session.Where("uid=? AND type= ? and gid=? and refund_state=? and batch=?", uid, 1, gid, refundState, batch) | |||
if goodsType != "" { | |||
session = session.And("goods_type=?", goodsType) | |||
} | |||
err := session.Find(&m) | |||
if err != nil { | |||
return nil, err | |||
} | |||
return &m, nil | |||
} | |||
func CouponUserOrdByGidAscBatchWithEg(eg *xorm.Engine, uid string, gid string, refundState int, goodsType string) (*model.CommCouponUserOrd, error) { | |||
var m model.CommCouponUserOrd | |||
session := eg.Where("uid=? AND type= ? and gid=? and refund_state=?", uid, 1, gid, refundState) | |||
if goodsType != "" { | |||
session = session.And("goods_type=?", goodsType) | |||
} | |||
has, err := session.Asc("batch").Get(&m) | |||
if has == false || err != nil { | |||
return nil, err | |||
} | |||
return &m, nil | |||
} | |||
func CouponUserOrdByGidAscBatchALLWithEg(eg *xorm.Engine, uid string, gid string, refundState int, batch int, goodsType string) (*[]model.CommCouponUserOrd, error) { | |||
var m []model.CommCouponUserOrd | |||
session := eg.Where("uid=? AND type= ? and gid=? and refund_state=? and batch=?", uid, 1, gid, refundState, batch) | |||
if goodsType != "" { | |||
session = session.And("goods_type=?", goodsType) | |||
} | |||
err := session.Find(&m) | |||
if err != nil { | |||
return nil, err | |||
} | |||
return &m, nil | |||
} | |||
func CouponUserOrdByInsertOne(eg *xorm.Engine, m *model.CommCouponUserOrd) bool { | |||
_, err := eg.InsertOne(m) | |||
if err != nil { | |||
return false | |||
} | |||
return true | |||
} | |||
func CouponUserOrdByInsertOneWithSession(session *xorm.Session, m *model.CommCouponUserOrd) bool { | |||
_, err := session.InsertOne(m) | |||
if err != nil { | |||
return false | |||
} | |||
return true | |||
} | |||
func commWhere(Db *xorm.Engine, uid int, types, pvdType, times string, ordType []string, keyword string) *xorm.Session { | |||
dbWhere := Db.Where("1=1") | |||
if uid > 0 { | |||
dbWhere = dbWhere.And("uid=?", uid) | |||
} | |||
if pvdType != "" { | |||
dbWhere = dbWhere.And("pvd_type=?", pvdType) | |||
} | |||
if times != "" { | |||
dbWhere = dbWhere.And("create_at like ?", times+"%") | |||
} | |||
if len(ordType) > 0 { | |||
dbWhere = dbWhere.In("ord_type", ordType) | |||
} | |||
if types == "output" { | |||
dbWhere = dbWhere.And("type=?", 1) | |||
} else if types == "input" { | |||
dbWhere = dbWhere.And("type=?", 0) | |||
} | |||
if keyword != "" { | |||
userOne, _ := UserFindByPhoneOrNickname(Db, keyword) | |||
uids := "0" | |||
if userOne != nil { | |||
uids = utils.IntToStr(userOne.Uid) | |||
} | |||
dbWhere = dbWhere.And("to_uid = ? and to_uid>0", uids) | |||
} | |||
return dbWhere | |||
} | |||
func CouponUserOrdList(Db *xorm.Engine, uid int, types, pvdType, times string, limit, start int, keyword string) ([]*model.CommCouponUserOrd, error) { | |||
var m []*model.CommCouponUserOrd | |||
dbWhere := commWhere(Db, uid, types, pvdType, times, []string{}, keyword) | |||
err := dbWhere.Desc("create_at").Limit(limit, start).Find(&m) | |||
if err != nil { | |||
return nil, err | |||
} | |||
return m, nil | |||
} | |||
func CouponUserOrdListWithOrdType(Db *xorm.Engine, uid int, types, pvdType string, limit, start int, ordType []string, keyword string) ([]*model.CommCouponUserOrd, error) { | |||
var m []*model.CommCouponUserOrd | |||
dbWhere := commWhere(Db, uid, types, pvdType, "", ordType, keyword) | |||
err := dbWhere.Desc("create_at").Limit(limit, start).Desc("create_at").Find(&m) | |||
if err != nil { | |||
return nil, err | |||
} | |||
return m, nil | |||
} | |||
func CouponUserOrdByGid(session *xorm.Session, uid int, types, gid, goodsType string) (*model.CommCouponUserOrd, error) { | |||
var m model.CommCouponUserOrd | |||
has, err := session.Where("uid=? AND type= ? and gid=? and goods_type=?", uid, types, gid, goodsType).Desc("batch").Get(&m) | |||
if has == false || err != nil { | |||
return nil, err | |||
} | |||
return &m, nil | |||
} | |||
func CouponUserOrdByGidAndTime(session *xorm.Session, expectedRefundTime int64) (*[]model.CommCouponUserOrd, error) { | |||
var m []model.CommCouponUserOrd | |||
err := session.Where("ord_type= ? and type= ? and refund_state=? and expected_refund_time<?", 3, 1, 0, expectedRefundTime).Find(&m) | |||
if err != nil { | |||
return nil, err | |||
} | |||
return &m, nil | |||
} | |||
func CouponUserOrdSum(Db *xorm.Engine, uid int, types, pvdType, times, cols string, keyword string) (float64, error) { | |||
dbWhere := commWhere(Db, uid, types, pvdType, times, []string{}, keyword) | |||
m := new(model.CommCouponUserOrd) | |||
total, err := dbWhere.Sum(m, cols) | |||
if err != nil { | |||
return 0, err | |||
} | |||
return total, err | |||
} | |||
func CouponUserOrdSumWithToUid(Db *xorm.Engine, uid, types, toUid int, pvdType, ordType string) (float64, error) { | |||
m := new(model.CommCouponUserOrd) | |||
total, err := Db.Where("uid=? and type=? and to_uid=? and pvd_type=? and ord_type=?", uid, types, toUid, pvdType, ordType).Sum(m, "coupon_amount_value") | |||
if err != nil { | |||
return 0, err | |||
} | |||
return total, err | |||
} | |||
func CouponUserOrdByUpdate(session *xorm.Session, m *model.CommCouponUserOrd) bool { | |||
_, err := session.Where("id=?", m.Id).Cols("refund_state").Update(m) | |||
if err != nil { | |||
return false | |||
} | |||
return true | |||
} | |||
func CouponUserOrdByUpdateWithEg(eg *xorm.Engine, m *model.CommCouponUserOrd) bool { | |||
_, err := eg.Where("id=?", m.Id).Cols("refund_state").Update(m) | |||
if err != nil { | |||
return false | |||
} | |||
return true | |||
} |
@@ -0,0 +1,91 @@ | |||
package db | |||
import ( | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/db/model" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/utils" | |||
"xorm.io/xorm" | |||
) | |||
//统计数量 | |||
func GetCouponUserTotalByLeaveSum(Db *xorm.Engine, condition *model.CommCouponUserTotal, cols string) (float64, error) { | |||
total, err := Db.Sum(condition, cols) | |||
if err != nil { | |||
return 0, err | |||
} | |||
return total, err | |||
} | |||
func GetCouponUserByUid(Db *xorm.Engine, uid string) (*[]model.CommCouponUserTotal, error) { | |||
var m []model.CommCouponUserTotal | |||
if err := Db.Where("uid = " + uid + " and leave_coupon_amount_value>=0 ").Asc("leave_coupon_amount_value").Find(&m); err != nil { | |||
return nil, err | |||
} | |||
return &m, nil | |||
} | |||
func GetCouponUserTotalByLeaveSumWithTypeSession(session *xorm.Session, uid int, pvdType string) (*[]model.CommCouponUserTotal, error) { | |||
var m []model.CommCouponUserTotal | |||
if err := session.Where("uid = " + utils.IntToStr(uid) + " and leave_coupon_amount_value>=0 and (pvd_type LIKE '%" + pvdType + "%' or pvd_type='ordinary' )").Asc("leave_coupon_amount_value").Find(&m); err != nil { | |||
return nil, err | |||
} | |||
return &m, nil | |||
} | |||
func GetCouponUserTotalByNegative(Db *xorm.Engine, uid int, pvdType, cols string) (float64, error) { | |||
var m model.CommCouponUserTotal | |||
total, err := Db.Where("uid = "+utils.IntToStr(uid)+" and leave_coupon_amount_value<0 and (pvd_type LIKE '%"+pvdType+"%' or pvd_type='ordinary' )").Sum(m, cols) | |||
if err != nil { | |||
return 0, err | |||
} | |||
return total, err | |||
} | |||
//查询对应记录 | |||
func GetCouponUserTotalByOne(session *xorm.Session, uid int, pvdType string) (*model.CommCouponUserTotal, error) { | |||
var m model.CommCouponUserTotal | |||
if has, err := session.Where("uid = ? and pvd_type=? ", uid, pvdType).Asc("leave_coupon_amount_value").Get(&m); err != nil || !has { | |||
return nil, err | |||
} | |||
return &m, nil | |||
} | |||
func GetCouponUserTotalById(eg *xorm.Engine, uid int, id string) (*model.CommCouponUserTotal, error) { | |||
var m model.CommCouponUserTotal | |||
if has, err := eg.Where("uid = ? and id=? ", uid, id).Asc("leave_coupon_amount_value").Get(&m); err != nil || !has { | |||
return nil, err | |||
} | |||
return &m, nil | |||
} | |||
//查询对应记录 | |||
func GetCouponUserTotalByOneEg(eg *xorm.Engine, uid int, pvdType string) (*model.CommCouponUserTotal, error) { | |||
var m model.CommCouponUserTotal | |||
if has, err := eg.Where("uid = ? and pvd_type=? ", uid, pvdType).Asc("leave_coupon_amount_value").Get(&m); err != nil || !has { | |||
return nil, err | |||
} | |||
return &m, nil | |||
} | |||
func GetCouponUserTotalByOneSession(session *xorm.Session, uid int, pvdType string) (*model.CommCouponUserTotal, error) { | |||
var m model.CommCouponUserTotal | |||
if has, err := session.Where("uid = ? and pvd_type=? ", uid, pvdType).Asc("leave_coupon_amount_value").Get(&m); err != nil || !has { | |||
return nil, err | |||
} | |||
return &m, nil | |||
} | |||
//写入或保存 | |||
func CouponUserTotalByInsertWithUpdate(session *xorm.Session, m *model.CommCouponUserTotal) (has int64, err error) { | |||
if m.Id > 0 { | |||
has, err = session.ID(m.Id).Cols("coupon_amount_value,leave_coupon_amount_value,user_pvd").Update(m) | |||
} else { | |||
has, err = session.InsertOne(m) | |||
} | |||
return has, err | |||
} | |||
//写入或保存 | |||
func CouponUserTotalByInsertWithUpdateEg(eg *xorm.Engine, m *model.CommCouponUserTotal) (has int64, err error) { | |||
if m.Id > 0 { | |||
has, err = eg.ID(m.Id).Cols("coupon_amount_value,leave_coupon_amount_value").Update(m) | |||
} else { | |||
has, err = eg.InsertOne(m) | |||
} | |||
return has, err | |||
} |
@@ -0,0 +1,15 @@ | |||
package db | |||
import ( | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/db/model" | |||
"xorm.io/xorm" | |||
) | |||
func UserFindByPhoneOrNickname(Db *xorm.Engine, mobile string) (*model.User, error) { | |||
var m model.User | |||
if has, err := Db.Where("(phone = ? or nickname=?) AND delete_at = 0", mobile, mobile). | |||
Get(&m); err != nil || has == false { | |||
return nil, err | |||
} | |||
return &m, nil | |||
} |
@@ -0,0 +1,35 @@ | |||
package model | |||
import ( | |||
"time" | |||
) | |||
type CommCouponUserOrd struct { | |||
Id int `json:"id" xorm:"not null pk autoincr comment('主键id') INT(11)"` | |||
Uid int `json:"uid" xorm:"not null comment('用户id') INT(11)"` | |||
IsRevoke int `json:"is_revoke" xorm:"not null comment('') INT(11)"` | |||
PayMethod int `json:"pay_method" xorm:"default 0 comment('支付方式:1balance 2alipay 3wx_pay(类型为1时,此值必填)') TINYINT(1)"` | |||
OrdType int `json:"ord_type" xorm:"not null default 0 comment('订单类型(1购买,2领取)') TINYINT(1)"` | |||
UsePvd string `json:"use_pvd" xorm:"comment('可使用渠道(json([]string))') VARCHAR(255)"` | |||
PackageId int `json:"package_id" xorm:"default 0 comment('套餐id(类型为2时,此值必填)') INT(11)"` | |||
CouponAmountValue string `json:"coupon_amount_value" xorm:"not null default 0.0000 comment('优惠劵额度') DECIMAL(12,4)"` | |||
RevokeValue string `json:"revoke_value" xorm:"not null default 0.0000 comment('') DECIMAL(12,4)"` | |||
PayAmountValue string `json:"pay_amount_value" xorm:"comment('付款金额(类型为1时,此值必填)') DECIMAL(12,4)"` | |||
Discount string `json:"discount" xorm:"comment('折扣(类型为1时,此值必填)') DECIMAL(6,4)"` | |||
PayAmount string `json:"pay_amount" xorm:"comment('实际付款金额(类型为1时,此值必填)') DECIMAL(12,4)"` | |||
CreateAt time.Time `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' comment('创建时间') TIMESTAMP"` | |||
UpdateAt time.Time `json:"update_at" xorm:"default 'CURRENT_TIMESTAMP' comment('更新时间') TIMESTAMP"` | |||
PvdType string `json:"pvd_type" xorm:"default '' comment('渠道类型 ordinary:通用 GUIDE:导购 SELF_MALL:自营 O2O:小店 GUIDE_SELF_MALL:导购+自营 SELF_MALL_O2O:自营+小店 ') VARCHAR(255)"` | |||
GoodsType string `json:"goods_type" xorm:"default '' comment('商品渠道') VARCHAR(255)"` | |||
Type int `json:"type" xorm:"default '0' comment('收入0 支出1 ') VARCHAR(255)"` | |||
Batch int `json:"batch" xorm:"default '0' comment('批次(用于购买商品记录) ') VARCHAR(255)"` | |||
ExpectedRefundTime int `json:"expected_refund_time" xorm:"default '0' comment('预计退回时间 如果购买过不会退回 ') VARCHAR(255)"` | |||
RefundState int `json:"refund_state" xorm:"default '0' comment('批次(用于购买商品记录) ') VARCHAR(255)"` | |||
Gid string `json:"gid" xorm:"default '' comment('商品id') VARCHAR(255)"` | |||
GoodsTitle string `json:"goods_title" xorm:"default '' comment('商品标题') VARCHAR(255)"` | |||
Oid string `json:"oid" xorm:"default '' comment('') VARCHAR(255)"` | |||
TransformInfo string `json:"transform_info" xorm:"TEXT""` | |||
ToUid int `json:"to_uid" xorm:"default 0 comment('') INT(11)"` | |||
BeforeAmout string `json:"before_amout" xorm:"not null comment('变更前数量') DECIMAL(16,6)"` | |||
AfterAmout string `json:"after_amout" xorm:"not null comment('变更后数量') DECIMAL(16,6)"` | |||
} |
@@ -0,0 +1,29 @@ | |||
package model | |||
import ( | |||
"time" | |||
) | |||
type User struct { | |||
Uid int `json:"uid" xorm:"not null pk autoincr comment('主键ID') INT(10)"` | |||
Username string `json:"username" xorm:"not null default '' comment('用户名') index VARCHAR(50)"` | |||
Password string `json:"password" xorm:"not null default '' comment('密码') CHAR(32)"` | |||
Email string `json:"email" xorm:"not null default '' comment('邮箱') VARCHAR(128)"` | |||
Phone string `json:"phone" xorm:"not null default '' comment('联系电话') VARCHAR(20)"` | |||
Nickname string `json:"nickname" xorm:"not null default '' comment('昵称') VARCHAR(20)"` | |||
Level int `json:"level" xorm:"not null default 0 comment('用户等级id') INT(11)"` | |||
InviteTotal int `json:"invite_total" xorm:"not null default 0 comment('直推邀请总人数') INT(11)"` | |||
LevelArriveAt time.Time `json:"level_arrive_at" xorm:"not null default CURRENT_TIMESTAMP comment('到达该等级的时间') TIMESTAMP"` | |||
LevelExpireAt time.Time `json:"level_expire_at" xorm:"not null default CURRENT_TIMESTAMP comment('该等级过期时间') TIMESTAMP"` | |||
CreateAt time.Time `json:"create_at" xorm:"created not null default CURRENT_TIMESTAMP comment('创建时间') TIMESTAMP"` | |||
UpdateAt time.Time `json:"update_at" xorm:"updated default CURRENT_TIMESTAMP comment('最后修改资料时间') TIMESTAMP"` | |||
LastLoginAt time.Time `json:"last_login_at" xorm:"default CURRENT_TIMESTAMP comment('最近登录时间') TIMESTAMP"` | |||
DeleteAt int `json:"delete_at" xorm:"not null default 0 comment('是否删除;0未删除;1已删除') TINYINT(1)"` | |||
State int `json:"state" xorm:"not null default 1 comment('0未激活,1正常,2冻结,3删除') TINYINT(1)"` | |||
LastLoginIp string `json:"last_login_ip" xorm:"not null default '' comment('最后登录IP') VARCHAR(64)"` | |||
RegisterIp string `json:"register_ip" xorm:"not null default '' comment('注册IP') VARCHAR(64)"` | |||
Zone string `json:"zone" xorm:"not null default '86' comment('区号') VARCHAR(100)"` | |||
SalePhone string `json:"sale_phone" xorm:"not null default '' comment('') VARCHAR(100)"` | |||
IsFake int `json:"is_fake" xorm:"not null default 0 comment('0真实 1虚拟') TINYINT(1)"` | |||
IsMarketer int `json:"is_marketer" xorm:"not null default 0 comment('是否市商 0否 1是') TINYINT(1)"` | |||
} |
@@ -0,0 +1,34 @@ | |||
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 | |||
} |
@@ -0,0 +1,40 @@ | |||
package md | |||
const ( | |||
/*********** DEVICE ***********/ | |||
PLATFORM_WX_APPLET = "wx_applet" // 小程序 | |||
PLATFORM_TOUTIAO_APPLET = "toutiao_applet" | |||
PLATFORM_TIKTOK_APPLET = "tiktok_applet" | |||
PLATFORM_BAIDU_APPLET = "baidu_applet" | |||
PLATFORM_ALIPAY_APPLET = "alipay_applet" | |||
PLATFORM_WAP = "wap" //h5 | |||
PLATFORM_ANDROID = "android" | |||
PLATFORM_IOS = "ios" | |||
PLATFORM_PC = "pc" | |||
PLATFORM_JSAPI = "jsapi" // 公众号 | |||
) | |||
const WX_PAY_BROWSER = "wx_pay_browser" // 用于判断显示支付方式 | |||
var PlatformList = map[string]struct{}{ | |||
PLATFORM_WX_APPLET: {}, | |||
PLATFORM_TOUTIAO_APPLET: {}, | |||
PLATFORM_TIKTOK_APPLET: {}, | |||
PLATFORM_BAIDU_APPLET: {}, | |||
PLATFORM_ALIPAY_APPLET: {}, | |||
PLATFORM_WAP: {}, | |||
PLATFORM_ANDROID: {}, | |||
PLATFORM_IOS: {}, | |||
PLATFORM_PC: {}, | |||
} | |||
var PlatformMap = map[string]string{ | |||
"android": "2", | |||
"ios": "2", | |||
"wap": "4", // 和小程序公用模板 | |||
"wx_applet": "4", //微信小程序 | |||
"tiktok_applet": "4", | |||
"baidu_applet": "4", | |||
"alipay_applet": "4", | |||
"toutiao_applet": "4", | |||
} |
@@ -0,0 +1,66 @@ | |||
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 | |||
} |
@@ -0,0 +1,295 @@ | |||
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) | |||
} |