@@ -0,0 +1,117 @@ | |||
package db | |||
import ( | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db/model" | |||
zhios_order_relate_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils" | |||
zhios_order_relate_logx "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/logx" | |||
"errors" | |||
"fmt" | |||
"reflect" | |||
"xorm.io/xorm" | |||
) | |||
// BatchSelectUserPublicPlatoonRelations 批量查询数据 TODO::和下面的方法重复了,建议采用下面的 `UserPublicPlatoonRelationFindByParams` 方法 | |||
func BatchSelectUserPublicPlatoonRelations(Db *xorm.Engine, params map[string]interface{}) (*[]model.UserPublicPlatoonRelation, error) { | |||
var UserPublicPlatoonRelationData []model.UserPublicPlatoonRelation | |||
if err := Db.In(zhios_order_relate_utils.AnyToString(params["key"]), params["value"]). | |||
Find(&UserPublicPlatoonRelationData); err != nil { | |||
return nil, zhios_order_relate_logx.Warn(err) | |||
} | |||
return &UserPublicPlatoonRelationData, nil | |||
} | |||
// UserPublicPlatoonRelationInsert 插入单条数据 | |||
func UserPublicPlatoonRelationInsert(Db *xorm.Engine, UserPublicPlatoonRelation *model.UserPublicPlatoonRelation) (int, error) { | |||
_, err := Db.InsertOne(UserPublicPlatoonRelation) | |||
if err != nil { | |||
return 0, err | |||
} | |||
return UserPublicPlatoonRelation.Id, nil | |||
} | |||
// BatchAddUserPublicPlatoonRelations 批量新增数据 | |||
func BatchAddUserPublicPlatoonRelations(Db *xorm.Engine, UserPublicPlatoonRelationData []*model.UserPublicPlatoonRelation) (int64, error) { | |||
affected, err := Db.Insert(UserPublicPlatoonRelationData) | |||
if err != nil { | |||
return 0, err | |||
} | |||
return affected, nil | |||
} | |||
func GetUserPublicPlatoonRelationCount(Db *xorm.Engine) int { | |||
var UserPublicPlatoonRelation model.UserPublicPlatoonRelation | |||
session := Db.Where("") | |||
count, err := session.Count(&UserPublicPlatoonRelation) | |||
if err != nil { | |||
return 0 | |||
} | |||
return int(count) | |||
} | |||
// UserPublicPlatoonRelationDelete 删除记录 | |||
func UserPublicPlatoonRelationDelete(Db *xorm.Engine, id interface{}) (int64, error) { | |||
if reflect.TypeOf(id).Kind() == reflect.Slice { | |||
return Db.In("id", id).Delete(model.UserPublicPlatoonRelation{}) | |||
} else { | |||
return Db.Where("id = ?", id).Delete(model.UserPublicPlatoonRelation{}) | |||
} | |||
} | |||
// UserPublicPlatoonRelationUpdate 更新记录 | |||
func UserPublicPlatoonRelationUpdate(Db *xorm.Engine, id interface{}, UserPublicPlatoonRelation *model.UserPublicPlatoonRelation, forceColums ...string) (int64, error) { | |||
var ( | |||
affected int64 | |||
err error | |||
) | |||
if forceColums != nil { | |||
affected, err = Db.Where("id=?", id).Cols(forceColums...).Update(UserPublicPlatoonRelation) | |||
} else { | |||
affected, err = Db.Where("id=?", id).Update(UserPublicPlatoonRelation) | |||
} | |||
if err != nil { | |||
return 0, err | |||
} | |||
return affected, nil | |||
} | |||
// UserPublicPlatoonRelationGetOneByParams 通过传入的参数查询数据(单条) | |||
func UserPublicPlatoonRelationGetOneByParams(Db *xorm.Engine, params map[string]interface{}) (*model.UserPublicPlatoonRelation, error) { | |||
var m model.UserPublicPlatoonRelation | |||
var query = fmt.Sprintf("%s =?", params["key"]) | |||
if has, err := Db.Where(query, params["value"]).Get(&m); err != nil || has == false { | |||
return nil, zhios_order_relate_logx.Error(err) | |||
} | |||
return &m, nil | |||
} | |||
// UserPublicPlatoonRelationFindByParams 通过传入的参数查询数据(多条) | |||
func UserPublicPlatoonRelationFindByParams(Db *xorm.Engine, params map[string]interface{}) (*[]model.UserPublicPlatoonRelation, error) { | |||
var m []model.UserPublicPlatoonRelation | |||
if params["value"] == nil { | |||
return nil, errors.New("参数有误") | |||
} | |||
if params["key"] == nil { | |||
//查询全部数据 | |||
err := Db.Find(&m) | |||
if err != nil { | |||
return nil, zhios_order_relate_logx.Error(err) | |||
} | |||
return &m, nil | |||
} else { | |||
if reflect.TypeOf(params["value"]).Kind() == reflect.Slice { | |||
//指定In查询 | |||
if err := Db.In(zhios_order_relate_utils.AnyToString(params["key"]), params["value"]).Find(&m); err != nil { | |||
return nil, zhios_order_relate_logx.Warn(err) | |||
} | |||
return &m, nil | |||
} else { | |||
var query = fmt.Sprintf("%s =?", params["key"]) | |||
err := Db.Where(query, params["value"]).Find(&m) | |||
if err != nil { | |||
return nil, zhios_order_relate_logx.Error(err) | |||
} | |||
return &m, nil | |||
} | |||
} | |||
} |
@@ -0,0 +1,117 @@ | |||
package db | |||
import ( | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db/model" | |||
zhios_order_relate_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils" | |||
zhios_order_relate_logx "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/logx" | |||
"errors" | |||
"fmt" | |||
"reflect" | |||
"xorm.io/xorm" | |||
) | |||
// BatchSelectUserPublicPlatoonSettings 批量查询数据 TODO::和下面的方法重复了,建议采用下面的 `UserPublicPlatoonSettingFindByParams` 方法 | |||
func BatchSelectUserPublicPlatoonSettings(Db *xorm.Engine, params map[string]interface{}) (*[]model.UserPublicPlatoonSetting, error) { | |||
var UserPublicPlatoonSettingData []model.UserPublicPlatoonSetting | |||
if err := Db.In(zhios_order_relate_utils.AnyToString(params["key"]), params["value"]). | |||
Find(&UserPublicPlatoonSettingData); err != nil { | |||
return nil, zhios_order_relate_logx.Warn(err) | |||
} | |||
return &UserPublicPlatoonSettingData, nil | |||
} | |||
// UserPublicPlatoonSettingInsert 插入单条数据 | |||
func UserPublicPlatoonSettingInsert(Db *xorm.Engine, UserPublicPlatoonSetting *model.UserPublicPlatoonSetting) (int, error) { | |||
_, err := Db.InsertOne(UserPublicPlatoonSetting) | |||
if err != nil { | |||
return 0, err | |||
} | |||
return UserPublicPlatoonSetting.Id, nil | |||
} | |||
// BatchAddUserPublicPlatoonSettings 批量新增数据 | |||
func BatchAddUserPublicPlatoonSettings(Db *xorm.Engine, UserPublicPlatoonSettingData []*model.UserPublicPlatoonSetting) (int64, error) { | |||
affected, err := Db.Insert(UserPublicPlatoonSettingData) | |||
if err != nil { | |||
return 0, err | |||
} | |||
return affected, nil | |||
} | |||
func GetUserPublicPlatoonSettingCount(Db *xorm.Engine) int { | |||
var UserPublicPlatoonSetting model.UserPublicPlatoonSetting | |||
session := Db.Where("") | |||
count, err := session.Count(&UserPublicPlatoonSetting) | |||
if err != nil { | |||
return 0 | |||
} | |||
return int(count) | |||
} | |||
// UserPublicPlatoonSettingDelete 删除记录 | |||
func UserPublicPlatoonSettingDelete(Db *xorm.Engine, id interface{}) (int64, error) { | |||
if reflect.TypeOf(id).Kind() == reflect.Slice { | |||
return Db.In("id", id).Delete(model.UserPublicPlatoonSetting{}) | |||
} else { | |||
return Db.Where("id = ?", id).Delete(model.UserPublicPlatoonSetting{}) | |||
} | |||
} | |||
// UserPublicPlatoonSettingUpdate 更新记录 | |||
func UserPublicPlatoonSettingUpdate(Db *xorm.Engine, id interface{}, UserPublicPlatoonSetting *model.UserPublicPlatoonSetting, forceColums ...string) (int64, error) { | |||
var ( | |||
affected int64 | |||
err error | |||
) | |||
if forceColums != nil { | |||
affected, err = Db.Where("id=?", id).Cols(forceColums...).Update(UserPublicPlatoonSetting) | |||
} else { | |||
affected, err = Db.Where("id=?", id).Update(UserPublicPlatoonSetting) | |||
} | |||
if err != nil { | |||
return 0, err | |||
} | |||
return affected, nil | |||
} | |||
// UserPublicPlatoonSettingGetOneByParams 通过传入的参数查询数据(单条) | |||
func UserPublicPlatoonSettingGetOneByParams(Db *xorm.Engine, params map[string]interface{}) (*model.UserPublicPlatoonSetting, error) { | |||
var m model.UserPublicPlatoonSetting | |||
var query = fmt.Sprintf("%s =?", params["key"]) | |||
if has, err := Db.Where(query, params["value"]).Get(&m); err != nil || has == false { | |||
return nil, zhios_order_relate_logx.Error(err) | |||
} | |||
return &m, nil | |||
} | |||
// UserPublicPlatoonSettingFindByParams 通过传入的参数查询数据(多条) | |||
func UserPublicPlatoonSettingFindByParams(Db *xorm.Engine, params map[string]interface{}) (*[]model.UserPublicPlatoonSetting, error) { | |||
var m []model.UserPublicPlatoonSetting | |||
if params["value"] == nil { | |||
return nil, errors.New("参数有误") | |||
} | |||
if params["key"] == nil { | |||
//查询全部数据 | |||
err := Db.Find(&m) | |||
if err != nil { | |||
return nil, zhios_order_relate_logx.Error(err) | |||
} | |||
return &m, nil | |||
} else { | |||
if reflect.TypeOf(params["value"]).Kind() == reflect.Slice { | |||
//指定In查询 | |||
if err := Db.In(zhios_order_relate_utils.AnyToString(params["key"]), params["value"]).Find(&m); err != nil { | |||
return nil, zhios_order_relate_logx.Warn(err) | |||
} | |||
return &m, nil | |||
} else { | |||
var query = fmt.Sprintf("%s =?", params["key"]) | |||
err := Db.Where(query, params["value"]).Find(&m) | |||
if err != nil { | |||
return nil, zhios_order_relate_logx.Error(err) | |||
} | |||
return &m, nil | |||
} | |||
} | |||
} |
@@ -0,0 +1,18 @@ | |||
package model | |||
import ( | |||
"time" | |||
) | |||
type UserPublicPlatoonRelation struct { | |||
Id int `json:"id" xorm:"not null pk autoincr INT(11)"` | |||
Uid int `json:"uid" xorm:"not null default 0 comment('用户id') unique INT(11)"` | |||
FatherUid string `json:"father_uid" xorm:"not null default '' comment('父级id(123456-563464-438384)') index VARCHAR(100)"` | |||
RecommendUid int `json:"recommend_uid" xorm:"not null default 0 comment('推荐人id') INT(11)"` | |||
Level int `json:"level" xorm:"not null default 1 comment('等级(整个系统)') INT(11)"` | |||
Position int `json:"position" xorm:"not null default 1 comment('当前等级上位置') INT(11)"` | |||
UniqueIdentifier string `json:"unique_identifier" xorm:"not null default '' comment('唯一标识符(uid-推荐人id-等级-位置)') unique CHAR(100)"` | |||
ReturnCommissionNum int `json:"return_commission_num" xorm:"not null default 0 comment('返佣次数') INT(11)"` | |||
CreateAt time.Time `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"` | |||
UpdateAt time.Time `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"` | |||
} |
@@ -0,0 +1,16 @@ | |||
package model | |||
import ( | |||
"time" | |||
) | |||
type UserPublicPlatoonSetting struct { | |||
Id int `json:"id" xorm:"not null pk autoincr INT(11)"` | |||
IsOpen int `json:"is_open" xorm:"not null default 0 comment('是否开启(0:关闭 1:开启)') TINYINT(1)"` | |||
SeveralTimes int `json:"several_times" xorm:"not null default 0 comment('几乘') TINYINT(3)"` | |||
SeveralRows int `json:"several_rows" xorm:"not null default 0 comment('几排') TINYINT(3)"` | |||
OriginatorUid int `json:"originator_uid" xorm:"not null default 0 comment('创始人uid') INT(11)"` | |||
Ext string `json:"ext" xorm:"comment('拓展字段(json存储)') TEXT"` | |||
CreateAt time.Time `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"` | |||
UpdateAt time.Time `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"` | |||
} |
@@ -0,0 +1,30 @@ | |||
package md | |||
// 公排结构 | |||
type LvGrade struct { | |||
Lv int `json:"lv"` // 会员级别 | |||
LvName string `json:"lv_name"` // 会员名称 | |||
ReturnType []string `json:"return_type"` //返利类型 | |||
CommonWealthSystem map[string]string `json:"common_wealth_system"` //共富制度比例 | |||
SelfRateList map[string]string `json:"self_rate_list"` // 自购比例 | |||
DirectPush map[string]string `json:"direct_push"` // 直推奖励比例 | |||
} | |||
type PublicPlatoonRelateCommissionReq struct { | |||
Pvd string `json:"pvd"` | |||
Uid string `json:"uid"` | |||
UserLevel string `json:"user_level"` | |||
PendingAmount string `json:"pending_amount"` //待处理金额 | |||
Oid string `json:"oid"` | |||
} | |||
type PublicPlatoonRelateCommissionResp struct { | |||
Uid string `json:"uid"` | |||
CommonWealthBenefitUid string `json:"common_wealth_benefit_uid"` //共富奖励给到的uid | |||
DirectPushBenefitUid string `json:"direct_push_benefit_uid"` //共富奖励给到的uid | |||
PendingAmount string `json:"pending_amount"` //直推奖励给到的uid | |||
Oid string `json:"oid"` | |||
SelfRateList map[string]string `json:"self_rate_list"` // 自购奖励 | |||
CommonWealthSystem map[string]string `json:"common_wealth_system"` // 共富制度奖励 | |||
DirectPush map[string]string `json:"direct_push"` // 直推奖励 | |||
} |
@@ -0,0 +1,105 @@ | |||
package rule | |||
import ( | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db" | |||
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/md" | |||
zhios_order_relate_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils" | |||
zhios_order_relate_logx "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/logx" | |||
"encoding/json" | |||
"errors" | |||
"fmt" | |||
"strconv" | |||
"strings" | |||
"xorm.io/xorm" | |||
) | |||
// BatchGetPublicPlatoonRelateCommission 批量分佣 订单显示 | |||
func BatchGetPublicPlatoonRelateCommissionByOrder(engine *xorm.Engine, masterId string, PublicPlatoonRelateCommissionReqList []*md.PublicPlatoonRelateCommissionReq) (map[string]*md.PublicPlatoonRelateCommissionResp, error) { | |||
var resp map[string]*md.PublicPlatoonRelateCommissionResp | |||
userPublicPlatoonSetting, err := db.UserPublicPlatoonSettingGetOneByParams(engine, map[string]interface{}{ | |||
"key": "is_open", | |||
"value": 1, | |||
}) | |||
if err != nil { | |||
return nil, err | |||
} | |||
for _, param := range PublicPlatoonRelateCommissionReqList { | |||
// 根据供应商 | |||
rewardOpt, err := db.DbsPlanRewardByPvd(engine, param.Pvd) | |||
if err != nil { | |||
return nil, err | |||
} | |||
if rewardOpt == nil { | |||
return nil, zhios_order_relate_logx.Warn("找不到方案记录") | |||
} | |||
if rewardOpt.State == 0 { | |||
return nil, zhios_order_relate_logx.Warn("抽成方案未开启") | |||
} | |||
if rewardOpt.PlanCommissionId == 0 { | |||
return nil, zhios_order_relate_logx.Warn("抽成方案未设置佣金方案id") | |||
} | |||
fmt.Println("抽成设置:", zhios_order_relate_utils.SerializeStr(rewardOpt)) | |||
fmt.Println("commission id:", rewardOpt.PlanCommissionId) | |||
//TODO::计算共富收益 | |||
commissionOpt, err := db.DbsPlanCommissionById(engine, rewardOpt.PlanCommissionId) | |||
if err != nil || commissionOpt == nil || commissionOpt.Id == 0 { | |||
return nil, err | |||
} | |||
if err != nil || commissionOpt == nil || commissionOpt.Id == 0 { | |||
return nil, err | |||
} | |||
var subsidyTmp map[string]*md.LvGrade | |||
if err := json.Unmarshal([]byte(commissionOpt.Data), &subsidyTmp); err != nil { | |||
return nil, zhios_order_relate_logx.Warn(fmt.Sprintf("%s:分佣方案数据设置错误", masterId)) | |||
} | |||
lvGrade := subsidyTmp[param.UserLevel] | |||
var selfRateList, commonWealthSystem, directPush map[string]string | |||
for _, coinId := range lvGrade.ReturnType { | |||
selfRateList[coinId] = zhios_order_relate_utils.Float64ToStr(zhios_order_relate_utils.StrToFloat64(lvGrade.SelfRateList[coinId]) * zhios_order_relate_utils.StrToFloat64(param.PendingAmount)) | |||
commonWealthSystem[coinId] = zhios_order_relate_utils.Float64ToStr(zhios_order_relate_utils.StrToFloat64(lvGrade.CommonWealthSystem[coinId]) * zhios_order_relate_utils.StrToFloat64(param.PendingAmount)) | |||
directPush[coinId] = zhios_order_relate_utils.Float64ToStr(zhios_order_relate_utils.StrToFloat64(lvGrade.DirectPush[coinId]) * zhios_order_relate_utils.StrToFloat64(param.PendingAmount)) | |||
} | |||
//TODO::本次消费产生的共富收益给到谁 | |||
var nowBenefitUid string | |||
userPublicPlatoonRelation, err := db.UserPublicPlatoonRelationGetOneByParams(engine, map[string]interface{}{ | |||
"key": "uid", | |||
"value": param.Uid, | |||
}) | |||
if err != nil { | |||
return nil, err | |||
} | |||
if userPublicPlatoonRelation == nil { | |||
return nil, errors.New("未查询到公排关系记录") | |||
} | |||
fatherUids := strings.Split(userPublicPlatoonRelation.FatherUid, "-") | |||
userPublicPlatoonRelation.ReturnCommissionNum++ | |||
remainder := (userPublicPlatoonRelation.ReturnCommissionNum) % userPublicPlatoonSetting.SeveralRows | |||
if remainder == 0 { | |||
nowBenefitUid = fatherUids[userPublicPlatoonSetting.SeveralRows-1] | |||
} else { | |||
nowBenefitUid = fatherUids[remainder-1] | |||
} | |||
//TODO::更新 公排关系记录 中 `return_commission_num` | |||
affected, err := db.UserPublicPlatoonRelationUpdate(engine, userPublicPlatoonRelation.Id, userPublicPlatoonRelation, "return_commission_num") | |||
if err != nil { | |||
return nil, err | |||
} | |||
if affected == 0 { | |||
return nil, errors.New("更新公排关系记录失败") | |||
} | |||
resp[param.Oid] = &md.PublicPlatoonRelateCommissionResp{ | |||
Uid: param.Uid, | |||
CommonWealthBenefitUid: nowBenefitUid, | |||
DirectPushBenefitUid: strconv.Itoa(userPublicPlatoonRelation.RecommendUid), | |||
PendingAmount: param.PendingAmount, | |||
Oid: param.Oid, | |||
SelfRateList: selfRateList, | |||
CommonWealthSystem: commonWealthSystem, | |||
DirectPush: directPush, | |||
} | |||
} | |||
return resp, nil | |||
} |