huangjiajun 1 month ago
parent
commit
7ca1f35e36
2 changed files with 439 additions and 2 deletions
  1. +79
    -2
      db/db_regional_agent_user_belong.go
  2. +360
    -0
      rule/regional_agent_sess.go

+ 79
- 2
db/db_regional_agent_user_belong.go View File

@@ -56,7 +56,6 @@ func (rab RegionalAgentUserBelongWithSession) UpdateRegionalAgentUserBelong(cols
return true, nil
}

//
func (rab RegionalAgentUserBelongWithSession) GetOneRecordByUidAndRegionId() (bool, error) {
isHas, err := rab.Session.Table("regional_agent_user_belong").
Where("uid = ? AND pvd = 'COMMON' AND is_use = 1", rab.RegionalAgentUserBelong.Uid).
@@ -185,6 +184,27 @@ func GetCountByRegionalAgentUserBelongById(Db *xorm.Engine, uid interface{}, pvd
return &m, nil
}
}
func GetCountByRegionalAgentUserBelongByIdSess(Db *xorm.Session, uid interface{}, pvd string, newPvd string) (*model.RegionalAgentUserBelong, error) {
var m model.RegionalAgentUserBelong
if newPvd == md.PVD_COMMON { //公共的
pvd = newPvd
}
if pvd == md.PVD_USER_LV_UP || pvd == md.PVD_CARD {
pvd = md.PVD_COMMON
}
fmt.Println(pvd)
if pvd == md.PVD_COMMON {
if has, err := Db.Where("uid = ? and (pvd='' or pvd=?)", uid, pvd).Desc("id").Get(&m); err != nil || has == false {
return nil, err
}
return &m, nil
} else {
if has, err := Db.Where("uid = ? and pvd=?", uid, pvd).Desc("id").Get(&m); err != nil || has == false {
return nil, err
}
return &m, nil
}
}

// RegionalAgentOrdBelongInsert 插入单条数据
func RegionalAgentOrdBelongInsert(Db *xorm.Engine, regionalAgentRegion *model.RegionalAgentOrdBelong) (int64, error) {
@@ -194,6 +214,13 @@ func RegionalAgentOrdBelongInsert(Db *xorm.Engine, regionalAgentRegion *model.Re
}
return regionalAgentRegion.Id, nil
}
func RegionalAgentOrdBelongInsertSess(Db *xorm.Session, regionalAgentRegion *model.RegionalAgentOrdBelong) (int64, error) {
_, err := Db.InsertOne(regionalAgentRegion)
if err != nil {
return 0, err
}
return regionalAgentRegion.Id, nil
}

// RegionalAgentRegionGetOneByParams 通过传入的参数查询数据(单条)
func RegionalAgentRegionGetOneByParams(Db *xorm.Engine, params map[string]interface{}) (*model.RegionalAgentRegion, error) {
@@ -204,7 +231,14 @@ func RegionalAgentRegionGetOneByParams(Db *xorm.Engine, params map[string]interf
}
return &m, nil
}

func RegionalAgentRegionGetOneByParamsSess(Db *xorm.Session, params map[string]interface{}) (*model.RegionalAgentRegion, error) {
var m model.RegionalAgentRegion
var query = fmt.Sprintf("%s =?", params["key"])
if has, err := Db.Where(query, params["value"]).Get(&m); err != nil || has == false {
return nil, err
}
return &m, nil
}
func RegionalAgentUserFindByValid(Db *xorm.Engine, regionId string) (*[]model.RegionalAgentUser, error) {
var m []model.RegionalAgentUser
err := Db.Where("region_id=? and deleted_time IS NULL and is_failure=0", regionId).Find(&m)
@@ -213,6 +247,14 @@ func RegionalAgentUserFindByValid(Db *xorm.Engine, regionId string) (*[]model.Re
}
return &m, nil
}
func RegionalAgentUserFindByValidSess(Db *xorm.Session, regionId string) (*[]model.RegionalAgentUser, error) {
var m []model.RegionalAgentUser
err := Db.Where("region_id=? and deleted_time IS NULL and is_failure=0", regionId).Find(&m)
if err != nil {
return nil, err
}
return &m, nil
}

// GetCountByRegionalAgentBase 通过传入的参数查询数据(单条)
func GetCountByRegionalAgentBase(Db *xorm.Engine) (*model.RegionalAgentBase, error) {
@@ -226,6 +268,17 @@ func GetCountByRegionalAgentBase(Db *xorm.Engine) (*model.RegionalAgentBase, err
}
return &m, nil
}
func GetCountByRegionalAgentBaseSess(Db *xorm.Session) (*model.RegionalAgentBase, error) {
var m model.RegionalAgentBase
get, err := Db.Get(&m)
if err != nil {
return &m, err
}
if !get {
return &m, errors.New("无")
}
return &m, nil
}

// VirtualCoinFindByParams 通过传入的参数查询数据(多条)
func VirtualCoinFindByParams(Db *xorm.Engine, params map[string]interface{}) (*[]model.VirtualCoin, error) {
@@ -264,6 +317,15 @@ func VirtualCoinFindByAll(Db *xorm.Engine) (*[]model.VirtualCoin, error) {
}
return &m, nil
}
func VirtualCoinFindByAllSess(Db *xorm.Session) (*[]model.VirtualCoin, error) {
var m []model.VirtualCoin
//查询全部数据
err := Db.Find(&m)
if err != nil {
return nil, err
}
return &m, nil
}
func RegionalAgentCalcSystemRelateInsert(Db *xorm.Engine, regionalAgentCalcSystemRelate *model.RegionalAgentCalcSystemRelate) (int64, error) {
_, err := Db.InsertOne(regionalAgentCalcSystemRelate)
if err != nil {
@@ -271,6 +333,13 @@ func RegionalAgentCalcSystemRelateInsert(Db *xorm.Engine, regionalAgentCalcSyste
}
return regionalAgentCalcSystemRelate.Id, nil
}
func RegionalAgentCalcSystemRelateInsertSess(Db *xorm.Session, regionalAgentCalcSystemRelate *model.RegionalAgentCalcSystemRelate) (int64, error) {
_, err := Db.InsertOne(regionalAgentCalcSystemRelate)
if err != nil {
return 0, err
}
return regionalAgentCalcSystemRelate.Id, nil
}

// GetCountByRegionalAgentSchemeInfoByParams 通过传入的参数查询数据(单条)
func GetCountByRegionalAgentSchemeInfoByParams(Db *xorm.Engine, params map[string]interface{}) (*model.RegionalAgentScheme, error) {
@@ -281,3 +350,11 @@ func GetCountByRegionalAgentSchemeInfoByParams(Db *xorm.Engine, params map[strin
}
return &m, nil
}
func GetCountByRegionalAgentSchemeInfoByParamsSess(Db *xorm.Session, params map[string]interface{}) (*model.RegionalAgentScheme, error) {
var m model.RegionalAgentScheme
var query = fmt.Sprintf("%s =?", params["key"])
if has, err := Db.Where(query, params["value"]).Get(&m); err != nil || has == false {
return nil, err
}
return &m, nil
}

+ 360
- 0
rule/regional_agent_sess.go View File

@@ -0,0 +1,360 @@
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/db/model"
"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"
"encoding/json"
"fmt"
"time"
"xorm.io/xorm"
)

func BindUserOrdToAgentSess(sess *xorm.Session, dbName string, data *md.InsertRegionalAgentOrdBelongData) {
CommRegionalAgentInsertSess(sess, data)
}

// 公共调用
func CommRegionalAgentInsertSess(sess *xorm.Session, args *md.InsertRegionalAgentOrdBelongData) int {
res, RegionId := InsertRegionalAgentOrdBelongSess(sess, args)
if !res {
return 0
}
//todo::调用 “计算制度” 方法
_, res = HandleCommissionDistributionSystemSess(sess, &md.RegionalAgentSystemArgs{
Uid: zhios_order_relate_utils.AnyToString(args.Uid),
Amount: args.Commission,
OrdId: zhios_order_relate_utils.AnyToString(args.OrderId),
Pvd: args.Pvd,
CommPvd: args.CommPvd,
Status: args.Status,
Type: "2",
RegionId: zhios_order_relate_utils.AnyToString(RegionId),
})
if !res {
return 2
}
return 1
}

func HandleCommissionDistributionSystemSess(sess *xorm.Session, args *md.RegionalAgentSystemArgs) ([]map[string]interface{}, bool) {
amount := zhios_order_relate_utils.AnyToFloat64(args.Amount)
var regionId string
if args.Type == "1" {
//获取当前用户的 归属
userBelong, err := db.GetCountByRegionalAgentUserBelongByIdSess(sess, args.Uid, args.Pvd, args.CommPvd)
fmt.Println(err)
fmt.Println(userBelong)
if err != nil || userBelong == nil {
return nil, false
}
regionId = zhios_order_relate_utils.AnyToString(userBelong.RegionId)
} else {
regionId = args.RegionId
}

//查询用户所属 “代理网点” 信息
regionalAgentRegion, err := db.RegionalAgentRegionGetOneByParamsSess(sess, map[string]interface{}{
"key": "id",
"value": zhios_order_relate_utils.AnyToInt64(regionId),
})
if err != nil {
return nil, false
}
if regionalAgentRegion == nil {
return nil, true
}
//查询 regional_agent_base 表获取虚拟币相关配置信息
agentBase, err := db.GetCountByRegionalAgentBaseSess(sess)
if err != nil {
return nil, false
}
var coinSet = md.CoinSet{}
err = json.Unmarshal([]byte(agentBase.CoinSet), &coinSet)
if err != nil {
return nil, false
}

//查询 virtual_coin 获取虚拟货币的 “兑换比例”
virtualCoin, err := db.VirtualCoinFindByAllSess(sess)
if err != nil {
return nil, false
}
var virtualCoinMap = map[int]string{}
if virtualCoin != nil {
for _, v := range *virtualCoin {
virtualCoinMap[v.Id] = v.ExchangeRatio
}
}
//查询代理网点的代理(网点、区、市、省)信息
var parents, tempParents []map[string]interface{}
tempParents, amount, _, err = makeSystemDataSess(sess, regionalAgentRegion, &coinSet, virtualCoinMap, parents, amount, md.SITE)
if err != nil {
return nil, false
}
if tempParents != nil {
parents = tempParents
}

tempParents, amount, _, err = makeSystemDataSess(sess, regionalAgentRegion, &coinSet, virtualCoinMap, parents, amount, md.COUNTRY)
if err != nil {
return nil, false
}
if tempParents != nil {
parents = tempParents
}

tempParents, amount, _, err = makeSystemDataSess(sess, regionalAgentRegion, &coinSet, virtualCoinMap, parents, amount, md.CITY)
if err != nil {
return nil, false
}
if tempParents != nil {
parents = tempParents
}

tempParents, amount, _, err = makeSystemDataSess(sess, regionalAgentRegion, &coinSet, virtualCoinMap, parents, amount, md.PROVINCE)
if err != nil {
return nil, false
}
if tempParents != nil {
parents = tempParents
}

fmt.Println("区域代理")
fmt.Println(parents)

for _, v := range parents {
var divideVirtualCurrency = ""
if !zhios_order_relate_utils.IsNil(v["divide_virtual_currency"]) {
jsonStr, err := json.Marshal(v["divide_virtual_currency"])
if err != nil {
fmt.Println("虚拟币查询")
fmt.Println(err)
return nil, false
}
divideVirtualCurrency = string(jsonStr)
}
status := 1
if args.Status == "0" { //导购的一开始不用返的 导购结算才返
status = 0
}
var insertData = model.RegionalAgentCalcSystemRelate{
Uid: v["uid"].(int),
DivideAmount: zhios_order_relate_utils.AnyToFloat64(v["divide_amount"]),
DivideVirtualCurrency: divideVirtualCurrency,
BelongOrdId: args.OrdId,
Pvd: args.Pvd,
Status: status,
Type: zhios_order_relate_utils.StrToInt(args.Type),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
_, err := db.RegionalAgentCalcSystemRelateInsertSess(sess, &insertData)
if err != nil {
return nil, false
}
}
return parents, true
}

func makeSystemDataSess(sess *xorm.Session, regionalAgentRegion *model.RegionalAgentRegion, coinSet *md.CoinSet, virtualCoin map[int]string, parents []map[string]interface{}, amount float64, level string) ([]map[string]interface{}, float64, float64, error) {
var value string
coinSetDetail := coinSet.SiteAgent
if regionalAgentRegion.Level == md.SiteKey {
switch level {
case md.PROVINCE:
value = zhios_order_relate_utils.AnyToString(regionalAgentRegion.ProvinceId)
break
case md.CITY:
value = zhios_order_relate_utils.AnyToString(regionalAgentRegion.CityId)
break
case md.COUNTRY:
value = zhios_order_relate_utils.AnyToString(regionalAgentRegion.DistrictId)
break
case md.SITE:
value = zhios_order_relate_utils.AnyToString(regionalAgentRegion.Id)
break
}
//获得虚拟币的比例
coinSetDetail = coinSet.SiteAgent
}
if regionalAgentRegion.Level == md.CountryKey {
switch level {
case md.PROVINCE:
value = zhios_order_relate_utils.AnyToString(regionalAgentRegion.ProvinceId)
break
case md.CITY:
value = zhios_order_relate_utils.AnyToString(regionalAgentRegion.CityId)
break
case md.COUNTRY:
value = zhios_order_relate_utils.AnyToString(regionalAgentRegion.Id)
break
case md.SITE:
value = zhios_order_relate_utils.AnyToString(0)
break
}
//获得虚拟币的比例
coinSetDetail = coinSet.DistrictAgent
}
if regionalAgentRegion.Level == md.CityKey {
switch level {
case md.PROVINCE:
value = zhios_order_relate_utils.AnyToString(regionalAgentRegion.ProvinceId)
break
case md.CITY:
value = zhios_order_relate_utils.AnyToString(regionalAgentRegion.Id)
break
case md.COUNTRY:
value = zhios_order_relate_utils.AnyToString(0)
break
case md.SITE:
value = zhios_order_relate_utils.AnyToString(0)
break
}
//获得虚拟币的比例
coinSetDetail = coinSet.CityAgent
}
if regionalAgentRegion.Level == md.ProvinceKey {
switch level {
case md.PROVINCE:
value = zhios_order_relate_utils.AnyToString(regionalAgentRegion.Id)
break
case md.CITY:
value = zhios_order_relate_utils.AnyToString(0)
break
case md.COUNTRY:
value = zhios_order_relate_utils.AnyToString(0)
break
case md.SITE:
value = zhios_order_relate_utils.AnyToString(0)
break
}
//获得虚拟币的比例
coinSetDetail = coinSet.ProvinceAgent
}
fmt.Println("地区ID:", value)
fmt.Println("余额:", amount)
if value == "0" {
return nil, amount, 0, nil
}

//查询代理地区
regionalAgentRegionData, err := db.RegionalAgentRegionGetOneByParamsSess(sess, map[string]interface{}{
"key": "id",
"value": value,
})
//查询代理地区用户
agentUser, err := db.RegionalAgentUserFindByValidSess(sess, value)
if err != nil {
return nil, amount, 0, err
}
if regionalAgentRegionData == nil {
return nil, amount, 0, err
}
var divideAmount float64 = 0
//查询对应的等级方案
scheme, err := db.GetCountByRegionalAgentSchemeInfoByParamsSess(sess, map[string]interface{}{
"key": "agent_type",
"value": regionalAgentRegionData.Level,
})
if scheme == nil || err != nil {
return nil, amount, 0, err
}
var commissionRate string
if scheme.IsUnify == 1 {
commissionRate = regionalAgentRegionData.CommissionRate
} else {
commissionRate = scheme.UnifyCommissionRate
}
parents, amount, divideAmount, err = CommAmount(zhios_order_relate_utils.AnyToFloat64(commissionRate), amount, agentUser, coinSetDetail, virtualCoin, parents, false)
if err != nil {
return nil, amount, 0, err
}
return parents, amount, divideAmount, nil

}

//公共处理佣金

// InsertRegionalAgentOrdBelong 插入 regional_agent_ord_belong (用户订单归属表) 数据
func InsertRegionalAgentOrdBelongSess(sess *xorm.Session, data *md.InsertRegionalAgentOrdBelongData) (bool, int) {
// 查询 regional_agent_user_belong 得到用户归属的区域代理
RegionId := 0
ProvinceId := 0
RegionalName := ""
CityId := 0
DistrictId := 0
SiteId := 0
Level := 0
if data.BelongType == "1" {
ProvinceId = zhios_order_relate_utils.StrToInt(data.ProvinceId)
if ProvinceId > 0 {
Level = 1
RegionId = ProvinceId
}
CityId = zhios_order_relate_utils.StrToInt(data.CityId)
if CityId > 0 {
Level = 2
RegionId = CityId
}
DistrictId = zhios_order_relate_utils.StrToInt(data.CountyId)
if DistrictId > 0 {
Level = 3
RegionId = DistrictId
}
SiteId = zhios_order_relate_utils.StrToInt(data.SiteId)
if SiteId > 0 {
Level = 4
RegionId = SiteId
}
if RegionId > 0 {
regionalAgentRegion, err := db.RegionalAgentRegionGetOneByParamsSess(sess, map[string]interface{}{
"key": "id",
"value": zhios_order_relate_utils.AnyToInt64(RegionId),
})
if err != nil {
return false, 0
}
if regionalAgentRegion == nil {
return false, 0
}
RegionalName = regionalAgentRegion.RegionName
}
} else {
userBelong, err := db.GetCountByRegionalAgentUserBelongByIdSess(sess, data.Uid, data.Pvd, data.CommPvd)
if err != nil || userBelong == nil {
return false, 0
}
RegionId = userBelong.RegionId
ProvinceId = int(userBelong.ProvinceId)
RegionalName = userBelong.RegionalName
CityId = int(userBelong.CityId)
DistrictId = int(userBelong.DistrictId)
SiteId = int(userBelong.SiteId)
Level = userBelong.Level
}

date := time.Now()
_, err := db.RegionalAgentOrdBelongInsertSess(sess, &model.RegionalAgentOrdBelong{
Uid: int(zhios_order_relate_utils.AnyToInt64(data.Uid)),
Pvd: data.Pvd,
OrderId: data.OrderId,
CurrencyType: 1,
Commission: data.Commission,
RegionId: RegionId,
RegionalName: RegionalName,
ProvinceId: int64(ProvinceId),
CityId: int64(CityId),
DistrictId: int64(DistrictId),
SiteId: int64(SiteId),
CreateTime: date,
UpdateTime: date,
Level: Level,
})
if err != nil {
return false, 0
}

return true, RegionId
}

Loading…
Cancel
Save