|
- package svc
-
- import (
- "applet/app/db/model"
- "applet/app/md"
- db2 "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db"
- model2 "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db/model"
- "strconv"
- "strings"
- "time"
-
- "xorm.io/xorm"
-
- "applet/app/db"
- "applet/app/utils"
- "applet/app/utils/logx"
- )
-
- func CheckNewOrderAuth(eg *xorm.Engine, uid int, thisPrice *model.NewcomersFreePriceType) (bool, *md.PricePerm) {
- // 测试开发阶段返回true
-
- user, _ := db.UserFindByID(eg, uid)
- userProfile, _ := db.UserProfileFindByID(eg, uid)
- perm := &md.PricePerm{}
- utils.Unserialize([]byte(thisPrice.Auth), perm)
- var permList []bool
- if perm.Auth.ValidUser.Open == "1" {
- permList = append(permList, checkValidUser(userProfile))
- }
- if perm.Auth.NewUser.Open == "1" {
- permList = append(permList, checkNewUser(userProfile))
- }
- if perm.Auth.UserLevel.Open == "1" {
- permList = append(permList, checkUserLevel(eg, user, perm.Auth.UserLevel.Value))
- }
- if perm.Auth.Onrecord.Open == "1" {
- permList = append(permList, checkOnRecord(userProfile))
- }
- if perm.Auth.InviteFriends.Open == "1" {
- permList = append(permList, checkInviteFriends(eg, user, perm.Auth.InviteFriends.Value))
- }
- if perm.Auth.Commission.Open == "1" {
- permList = append(permList, checkCommission(eg, perm.Auth.Commission.Days, perm.Auth.Commission.Money, user))
- }
- if perm.Auth.NewDays.Open == "1" {
- permList = append(permList, checkNewdaysOrder(eg, perm.Auth.NewDays.Days, user))
- }
- if perm.Auth.TeamMembers.Open == "1" {
- permList = append(permList, checkTeamMembers(eg, perm.Auth.TeamMembers.Amount, user))
- }
- if perm.Auth.Orders.Open == "1" {
- permList = append(permList, checkCompleteOrders(eg, perm.Auth.Orders.Days, perm.Auth.Orders.Amount, user))
- }
- var r bool
- if perm.AuthType == "all" {
- r = true
- for _, v := range permList {
- r = r && v
- }
- } else {
- r = false
- for _, v := range permList {
- r = r || v
- }
- }
- // 检查福利券是否符合
- needNewCoupon := thisPrice.NeedQuan
- //如果全部条件都需要满足 但是福利券不满足
- if perm.AuthType == "all" && userProfile.FreeRemainTime < needNewCoupon && thisPrice.NeedUseQuan == 1 {
- r = false
- }
- return r, perm
- }
-
- // 是否有效用户
- func checkValidUser(user *model.UserProfile) bool {
- if user.IsVerify == 1 {
- return true
- }
- return false
- }
-
- // 是否新用户
- func checkNewUser(user *model.UserProfile) bool {
- if user.IsNew == 1 {
- return true
- }
- return false
- }
-
- // 用户等级
- func checkUserLevel(Db *xorm.Engine, user *model.User, needUserLevelId string) bool {
- if user == nil {
- return false
- }
- needLevel, err := db2.UserLevelByID(Db, needUserLevelId)
- if err != nil {
- // 该等级已被删除的话就false
- return false
- }
- userLevel, err := db2.UserLevelByID(Db, user.Level)
- if err != nil {
- // 该等级已被删除的话就false
- return false
- }
- return userLevel.LevelWeight >= needLevel.LevelWeight
- }
-
- // 淘宝备案授权
- func checkOnRecord(user *model.UserProfile) bool {
- if user.AccTaobaoShareId > 0 {
- return true
- }
- return false
- }
-
- // 直推好友
- func checkInviteFriends(Db *xorm.Engine, user *model.User, need string) bool {
- if user == nil {
- return false
- }
- needPerson := utils.AnyToInt64(need)
- total, err := InviteCountlevelOne(Db, user)
- if err != nil {
- _ = logx.Warn(err)
- return false
- }
- if total >= needPerson {
- return true
- }
- return false
- }
- func InviteCountlevelOne(Db *xorm.Engine, user *model.User) (total int64, err error) {
- relate := new(model2.UserRelate)
- total, err = Db.Where("parent_uid=? and level=1", user.Uid).Count(relate)
- return
- }
-
- // 团队所有成员统计
- func InviteCountlevelAll(Db *xorm.Engine, user *model.User) (total int64, err error) {
- relate := new(model2.UserRelate)
- total, err = Db.Where("parent_uid=?", user.Uid).Count(relate)
- return
- }
-
- // 检查佣金
- func checkCommission(Db *xorm.Engine, leftValue, rightValue string, user *model.User) bool {
- if user == nil {
- return false
- }
- needDays, _ := strconv.Atoi(leftValue)
- needMoney, _ := strconv.Atoi(rightValue)
- t := time.Now()
- stime := time.Date(t.Year(), t.Month(), t.Day()-int(needDays), t.Hour(), 0, 0, 0, t.Location()).Unix()
- etime := t.Unix()
- ms, err := db.OrderListByTimeByState(Db, user.Uid, 3, stime, etime)
- if err != nil {
- _ = logx.Warn(err)
- return false
- } else {
- money := getSumForOrder(utils.IntToStr(user.Uid), ms)
- if money >= utils.AnyToFloat64(needMoney) {
- return true
- }
- }
- return false
- }
- func getSumForOrder(uid string, ol []*model.OrdList) float64 {
- var oa []float64
- var sum float64
-
- for _, item := range ol {
- for _, s := range strings.Split(item.BenefitList, "|") {
- if strings.Contains(s, uid) {
- uidbenfit := strings.Split(s, ":")
- if len(uidbenfit) != 2 {
- logx.Warn("OrderList BenefitList is format error")
- return 0
- }
- oa = append(oa, utils.StrToFloat64(uidbenfit[len(uidbenfit)-1]))
- break
- }
-
- }
- }
- for _, item := range oa {
- sum += item
- }
- return sum
- }
-
- func getSumForOrderRelate(oidMapAmount map[int64]float64, ol []*model.OrdList) float64 {
- var sum float64
-
- for _, item := range ol {
- if oidMapAmount[item.OrdId] > 0 {
- sum += oidMapAmount[item.OrdId]
- }
- }
- return sum
- }
-
- // 注册多少天之内没有订单
- func checkNewdaysOrder(Db *xorm.Engine, value string, user *model.User) bool {
- stime, etime := utils.GetTimeInterval("days", -utils.StrToInt(value))
- ordList, err := db.OrderListCountByUIDByTime(Db, user.Uid, stime, etime)
- if err != nil {
- _ = logx.Warn(err)
- return false
- }
- if len(*ordList) == 0 {
- return true
- }
- return false
- }
-
- // 团队数量达到多少个
- func checkTeamMembers(Db *xorm.Engine, value string, user *model.User) bool {
- needPerson := utils.AnyToInt64(value)
- total, err := InviteCountlevelAll(Db, user)
- if err != nil {
- _ = logx.Warn(err)
- return false
- }
- if total >= needPerson {
- return true
- }
- return false
- }
-
- // 几天内,已经结算订单达到几个
- func checkCompleteOrders(Db *xorm.Engine, leftValue, rightValue string, user *model.User) bool {
- needDays, _ := strconv.Atoi(leftValue)
- needAmount, _ := strconv.Atoi(rightValue)
- stime, etime := utils.GetTimeInterval("days", -needDays)
- ms, err := db.OrderListByTimeByState(Db, user.Uid, 3, stime, etime)
- if err != nil {
- _ = logx.Warn(err)
- return false
- }
- if len(ms) >= needAmount {
- return true
- }
- return false
- }
|