package model import ( "errors" "fmt" "github.com/codinl/go-logger" "github.com/jinzhu/gorm" "mh-server/lib/utils" "time" ) //go:generate goqueryset -in cooperative_business.go // gen:qs type CooperativeBusiness struct { Model Name string `json:"name" binding:"required"` // AddTime time.Time `json:"add_time"` // 加入时间 Avatar string `json:"avatar" binding:"required"` Address string `json:"address" binding:"required"` Tel string `json:"tel" binding:"required"` CooperativeMemberDeduct *CooperativeMemberDeduct `json:"cooperative_member_deduct" gorm:"-"` CooperativeAssistantMemberDeduct *CooperativeAssistantMemberDeduct `json:"cooperative_assistant_member_deduct" gorm:"-"` //Identification string `json:"identification"` // 标识 // cooperative_business } /* 门店 游戏 卡 库存 用户 绑定门店 店员 借卡 订单 */ // gen:qs type CooperativeMemberDeduct struct { Model CooperativeBusinessId uint32 `json:"cooperative_business_id" gorm:"index"` GoldDeduct uint32 `json:"gold_deduct"` // 黄金会员提成 PlatinumDeduct uint32 `json:"platinum_deduct"` // 白金会员提成 BlackGoldDeduct uint32 `json:"black_gold_deduct"` // 黑金金会员提成 // cooperative_member_deduct } // gen:qs type CooperativeAssistantMemberDeduct struct { Model CooperativeBusinessId uint32 `json:"cooperative_business_id" gorm:"index"` StoreId uint32 `json:"store_id" gorm:"index"` // GoldDeduct uint32 `json:"gold_deduct"` // 黄金会员提成 PlatinumDeduct uint32 `json:"platinum_deduct"` // 白金会员提成 BlackGoldDeduct uint32 `json:"black_gold_deduct"` // 黑金金会员提成 // cooperative_assistant_member_deduct } const ( PromotionStateUnSettlement = "un_settlement" PromotionStateFinancePay = "finance_pay" PromotionStateSettled = "settled" ) // 合作商推广会员 // gen:qs type CooperativeMemberPromotion struct { Model CooperativeBusinessId uint32 `json:"cooperative_business_id" gorm:"index"` CooperativeName string `json:"cooperative_name"` // 合作商名称 GoldCount uint32 `json:"gold_count"` // 黄金会员数量 PlatinumCount uint32 `json:"platinum_count"` // 白金会员数量 BlackGoldCount uint32 `json:"black_gold_count"` // 黑金会员数量 Date string `json:"date" gorm:"index"` // State string `json:"state"` // 待发起结算 待财务打款 已结算 // cooperative_member_promotion } // 合作商推广会员门店 饼图 // gen:qs type CooperativeMemberPromotionStore struct { Model CooperativeBusinessId uint32 `json:"cooperative_business_id" gorm:"index"` CooperativeName string `json:"cooperative_name"` // 合作商名称 StoreId uint32 `json:"store_id" gorm:"index"` // 门店id GoldCount uint32 `json:"gold_count"` // 黄金会员数量 PlatinumCount uint32 `json:"platinum_count"` // 白金会员数量 BlackGoldCount uint32 `json:"black_gold_count"` // 黑金会员数量 Date string `json:"date" gorm:"index"` // // cooperative_member_promotion_store } // 合作商推广会员每天 // gen:qs type CooperativeMemberPromotionDay struct { Model CooperativeBusinessId uint32 `json:"cooperative_business_id" gorm:"index"` CooperativeName string `json:"cooperative_name"` // 合作商名称 GoldCount uint32 `json:"gold_count"` // 黄金会员数量 PlatinumCount uint32 `json:"platinum_count"` // 白金会员数量 BlackGoldCount uint32 `json:"black_gold_count"` // 黑金会员数量 DayTime string `json:"day_time" gorm:"index"` // // cooperative_member_promotion_day } // 合作商推广会员门店每天 柱状图 // gen:qs type CooperativeMemberPromotionStoreDay struct { Model CooperativeBusinessId uint32 `json:"cooperative_business_id" gorm:"index"` CooperativeName string `json:"cooperative_name"` // 合作商名称 StoreId uint32 `json:"store_id" gorm:"index"` // 门店id GoldCount uint32 `json:"gold_count"` // 黄金会员数量 PlatinumCount uint32 `json:"platinum_count"` // 白金会员数量 BlackGoldCount uint32 `json:"black_gold_count"` // 黑金会员数量 DayTime string `json:"day_time" gorm:"index"` // // cooperative_member_promotion_store_day } // gen:qs type CooperativeDeductSettle struct { Model CooperativeMemberPromotionId uint32 `json:"cooperative_member_promotion_id" gorm:"index"` CooperativeBusinessId uint32 `json:"cooperative_business_id" gorm:"index"` CooperativeName string `json:"cooperative_name"` // 合作商名称 DeductAmount uint32 `json:"deduct_amount"` // 提成金额 Date string `json:"date" gorm:"index"` // State string `json:"state"` // 待财务打款 已结算 GoldCount uint32 `json:"gold_count"` // 黄金会员数量 PlatinumCount uint32 `json:"platinum_count"` // 白金会员数量 BlackGoldCount uint32 `json:"black_gold_count"` // 黑金会员数量 GoldDeduct uint32 `json:"gold_deduct"` // 黄金会员提成配置 PlatinumDeduct uint32 `json:"platinum_deduct"` // 白金会员提成配置 BlackGoldDeduct uint32 `json:"black_gold_deduct"` // 黑金会员提成配置 } func AddCooperativeMemberPromotion(cooperativeId, storeId, assistantUid uint32, memberLevel int) { defer func() { if err := recover(); err != nil { logger.Error("err:", err) } }() var cooperativeBusiness CooperativeBusiness err := NewCooperativeBusinessQuerySet(DB).IDEq(cooperativeId).One(&cooperativeBusiness) if err != nil { logger.Error("cooperative business err:", err) return } //fmt.Println("cooperativeId:", cooperativeId) //fmt.Println("storeId:", storeId) //fmt.Println("assistantUid:", assistantUid) //fmt.Println("memberLevel:", memberLevel) go func() { promotion := &CooperativeMemberPromotion{CooperativeBusinessId: cooperativeId, CooperativeName: cooperativeBusiness.Name} promotionStore := &CooperativeMemberPromotionStore{CooperativeBusinessId: cooperativeId, CooperativeName: cooperativeBusiness.Name} promotionDay := &CooperativeMemberPromotionDay{CooperativeBusinessId: cooperativeId, CooperativeName: cooperativeBusiness.Name} promotionStoreDay := &CooperativeMemberPromotionStoreDay{CooperativeBusinessId: cooperativeId, CooperativeName: cooperativeBusiness.Name} inviteReport := &InviteMemberReport{CooperativeBusinessId: cooperativeId, CooperativeName: cooperativeBusiness.Name, Uid: assistantUid, StoreId: storeId, } begin := DB.Begin() err = promotion.AddPromotion(begin, memberLevel) if err != nil { begin.Rollback() logger.Error("promotion add promotion err:", err) return } err = promotionStore.AddPromotion(begin, memberLevel, storeId) if err != nil { begin.Rollback() logger.Error("promotion add promotion err:", err) return } err = promotionDay.AddPromotion(begin, memberLevel) if err != nil { begin.Rollback() logger.Error("promotion add promotion err:", err) return } err = promotionStoreDay.AddPromotion(begin, memberLevel, storeId) if err != nil { begin.Rollback() logger.Error("promotion add promotion err:", err) return } err = inviteReport.AddPromotion(begin, memberLevel) if err != nil { begin.Rollback() logger.Error("promotion add promotion err:", err) return } err = begin.Commit().Error if err != nil { begin.Rollback() logger.Error("commit err:", err) return } }() } func (m *CooperativeMemberPromotion) AddPromotion(gdb *gorm.DB, memberLevel int) error { dateString := utils.MonthDate() if memberLevel < 0 { return errors.New("member level err") } m.Date = dateString sql := "" switch memberLevel { case 2: sql = "UPDATE cooperative_member_promotion SET gold_count=gold_count+1 " + fmt.Sprintf("WHERE date='%s' AND cooperative_business_id=%d", dateString, m.CooperativeBusinessId) m.GoldCount = 1 case 4: sql = "UPDATE cooperative_member_promotion SET platinum_count=platinum_count+1 " + fmt.Sprintf("WHERE date='%s' AND cooperative_business_id=%d", dateString, m.CooperativeBusinessId) m.PlatinumCount = 1 case 5: sql = "UPDATE cooperative_member_promotion SET black_gold_count=black_gold_count+1 " + fmt.Sprintf("WHERE date='%s' AND cooperative_business_id=%d", dateString, m.CooperativeBusinessId) m.BlackGoldCount = 1 } fmt.Println("邀请会员sql:", sql) exist, err := QueryRecordExist(fmt.Sprintf( "SELECT * FROM cooperative_member_promotion WHERE cooperative_business_id=%d AND date='%s' ", m.CooperativeBusinessId, dateString)) if err != nil { logger.Error("cooperative member promotion record exist err:", err) return err } //fmt.Println("是否存在数据:", fmt.Sprintf( // "SELECT * FROM cooperative_member_promotion WHERE cooperative_business_id=%d AND date='%s'", // m.CooperativeBusinessId, dateString)) if exist { err = gdb.Exec(sql).Error if err != nil { logger.Error("update cooperative member promotion err:", err) return err } } else { m.State = PromotionStateUnSettlement err = gdb.Create(m).Error if err != nil { logger.Error("create cooperative member promotion err:", err) return err } } return nil } func (m *CooperativeMemberPromotionStore) AddPromotion(gdb *gorm.DB, memberLevel int, storeId uint32) error { dateString := utils.MonthDate() if memberLevel < 0 { return errors.New("member level err") } m.Date = dateString m.StoreId = storeId sql := "" switch memberLevel { case 2: sql = "UPDATE cooperative_member_promotion_store SET gold_count=gold_count+1 " + fmt.Sprintf("WHERE date='%s' AND cooperative_business_id=%d AND store_id=%d", dateString, m.CooperativeBusinessId, storeId) m.GoldCount = 1 case 4: sql = "UPDATE cooperative_member_promotion_store SET platinum_count=platinum_count+1 " + fmt.Sprintf("WHERE date='%s' AND cooperative_business_id=%d AND store_id=%d", dateString, m.CooperativeBusinessId, storeId) m.PlatinumCount = 1 case 5: sql = "UPDATE cooperative_member_promotion_store SET black_gold_count=black_gold_count+1 " + fmt.Sprintf("WHERE date='%s' AND cooperative_business_id=%d AND store_id=%d", dateString, m.CooperativeBusinessId, storeId) m.BlackGoldCount = 1 } exist, err := QueryRecordExist(fmt.Sprintf( "SELECT * FROM cooperative_member_promotion_store WHERE cooperative_business_id=%d AND date='%s' AND store_id=%d", m.CooperativeBusinessId, dateString, storeId)) if err != nil { logger.Error("cooperative member promotion record exist err:", err) return err } if exist { err = gdb.Exec(sql).Error if err != nil { logger.Error("update cooperative member promotion err:", err) return err } } else { err = gdb.Create(m).Error if err != nil { logger.Error("create cooperative member promotion err:", err) return err } } return nil } func (m *CooperativeMemberPromotionDay) AddPromotion(gdb *gorm.DB, memberLevel int) error { dayString := utils.TodayZeroDateFormat() if memberLevel < 0 { return errors.New("member level err") } m.DayTime = dayString sql := "" switch memberLevel { case 2: sql = "UPDATE cooperative_member_promotion_day SET gold_count=gold_count+1 " + fmt.Sprintf("WHERE day_time='%s' AND cooperative_business_id=%d", dayString, m.CooperativeBusinessId) m.GoldCount = 1 case 4: sql = "UPDATE cooperative_member_promotion_day SET platinum_count=platinum_count+1 " + fmt.Sprintf("WHERE day_time='%s' AND cooperative_business_id=%d", dayString, m.CooperativeBusinessId) m.PlatinumCount = 1 case 5: sql = "UPDATE cooperative_member_promotion_day SET black_gold_count=black_gold_count+1 " + fmt.Sprintf("WHERE day_time='%s' AND cooperative_business_id=%d", dayString, m.CooperativeBusinessId) m.BlackGoldCount = 1 } exist, err := QueryRecordExist(fmt.Sprintf( "SELECT * FROM cooperative_member_promotion_day WHERE cooperative_business_id=%d AND day_time='%s'", m.CooperativeBusinessId, dayString)) if err != nil { logger.Error("cooperative member promotion record exist err:", err) return err } if exist { err = gdb.Exec(sql).Error if err != nil { logger.Error("update cooperative member promotion day err:", err) return err } } else { err = gdb.Create(m).Error if err != nil { logger.Error("create cooperative member promotion day err:", err) return err } } return nil } func (m *CooperativeMemberPromotionStoreDay) AddPromotion(gdb *gorm.DB, memberLevel int, storeId uint32) error { dateString := utils.TodayZeroDateFormat() if memberLevel < 0 { return errors.New("member level err") } m.DayTime = dateString m.StoreId = storeId sql := "" switch memberLevel { case 2: sql = "UPDATE cooperative_member_promotion_store_day SET gold_count=gold_count+1 " + fmt.Sprintf("WHERE day_time='%s' AND cooperative_business_id=%d AND store_id=%d", dateString, m.CooperativeBusinessId, storeId) m.GoldCount = 1 case 4: sql = "UPDATE cooperative_member_promotion_store_day SET platinum_count=platinum_count+1 " + fmt.Sprintf("WHERE day_time='%s' AND cooperative_business_id=%d AND store_id=%d", dateString, m.CooperativeBusinessId, storeId) m.PlatinumCount = 1 case 5: sql = "UPDATE cooperative_member_promotion_store_day SET black_gold_count=black_gold_count+1 " + fmt.Sprintf("WHERE day_time='%s' AND cooperative_business_id=%d AND store_id=%d", dateString, m.CooperativeBusinessId, storeId) m.BlackGoldCount = 1 } exist, err := QueryRecordExist("SELECT * FROM cooperative_member_promotion_store_day " + fmt.Sprintf("WHERE cooperative_business_id=%d AND day_time='%s' AND store_id=%d", m.CooperativeBusinessId, dateString, storeId)) if err != nil { logger.Error("cooperative member promotion store day record exist err:", err) return err } if exist { err = gdb.Exec(sql).Error if err != nil { logger.Error("update cooperative member promotion store day err:", err) return err } } else { err = gdb.Create(m).Error if err != nil { logger.Error("create cooperative member promotion store day err:", err) return err } } return nil } func (m *InviteMemberReport) AddPromotion(gdb *gorm.DB, memberLevel int) error { dateString := utils.MonthDate() if memberLevel < 0 { return errors.New("member level err") } if gdb == nil { gdb = DB } if m.StoreId == 0 { return errors.New("store id is null") } if m.Uid == 0 { return errors.New("uid is null") } m.Date = dateString sql := "" switch memberLevel { case 2: sql = "UPDATE invite_member_report SET gold_count=gold_count+1 " + fmt.Sprintf("WHERE date='%s' AND cooperative_business_id=%d AND store_id=%d AND uid=%d", dateString, m.CooperativeBusinessId, m.StoreId, m.Uid) m.GoldCount = 1 case 4: sql = "UPDATE invite_member_report SET platinum_count=platinum_count+1 " + fmt.Sprintf("WHERE date='%s' AND cooperative_business_id=%d AND store_id=%d AND uid=%d", dateString, m.CooperativeBusinessId, m.StoreId, m.Uid) m.PlatinumCount = 1 case 5: sql = "UPDATE invite_member_report SET black_gold_count=black_gold_count+1 " + fmt.Sprintf("WHERE date='%s' AND cooperative_business_id=%d AND store_id=%d AND uid=%d", dateString, m.CooperativeBusinessId, m.StoreId, m.Uid) m.BlackGoldCount = 1 } exist, err := QueryRecordExist(fmt.Sprintf( "SELECT * FROM invite_member_report WHERE cooperative_business_id=%d AND date='%s' AND store_id=%d AND uid=%d", m.CooperativeBusinessId, dateString, m.StoreId, m.Uid)) if err != nil { logger.Error("cooperative member promotion record exist err:", err) return err } if exist { err = gdb.Exec(sql).Error if err != nil { logger.Error("update cooperative member promotion err:", err) return err } } else { //m.State = PromotionStateUnSettlement err = gdb.Create(m).Error if err != nil { logger.Error("create cooperative member promotion err:", err) return err } } return nil } func (m *CooperativeBusiness) SetAssistantMemberDeductConfig(storeId uint32) { var assistantDeductConfig CooperativeAssistantMemberDeduct err := DB.Table("cooperative_assistant_member_deduct").Where("cooperative_business_id=?", m.ID). Where("store_id=?", storeId).Order("id DESC").Limit(1).Find(&assistantDeductConfig).Error if err != nil && err != RecordNotFound { logger.Error("cooperative err:", err) return } m.CooperativeAssistantMemberDeduct = &assistantDeductConfig }