|
|
@@ -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 |
|
|
|
} |