From 40bd3a0ab696595fc8acfc4d1f5048b138cfc962 Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Fri, 14 Oct 2022 11:29:08 +0800 Subject: [PATCH] =?UTF-8?q?add=20reverse:for=20v3.5.3=20=E5=8C=BA=E5=9F=9F?= =?UTF-8?q?=E4=BB=A3=E7=90=86=E5=88=86=E4=BD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/db_regional_agent_user_belong.go | 274 ++++++++++++++ db/model/regional_agent_base.go | 39 ++ db/model/regional_agent_calc_system_relate.go | 19 + db/model/regional_agent_ord_belong.go | 23 ++ db/model/regional_agent_region.go | 35 ++ db/model/regional_agent_scheme.go | 35 ++ db/model/regional_agent_user_belong.go | 22 ++ md/regional_agent.go | 46 +++ rule/regional_agent.go | 354 ++++++++++++++++++ utils/convert.go | 9 +- 10 files changed, 855 insertions(+), 1 deletion(-) create mode 100644 db/db_regional_agent_user_belong.go create mode 100644 db/model/regional_agent_base.go create mode 100644 db/model/regional_agent_calc_system_relate.go create mode 100644 db/model/regional_agent_ord_belong.go create mode 100644 db/model/regional_agent_region.go create mode 100644 db/model/regional_agent_scheme.go create mode 100644 db/model/regional_agent_user_belong.go create mode 100644 md/regional_agent.go create mode 100644 rule/regional_agent.go diff --git a/db/db_regional_agent_user_belong.go b/db/db_regional_agent_user_belong.go new file mode 100644 index 0000000..2e3463e --- /dev/null +++ b/db/db_regional_agent_user_belong.go @@ -0,0 +1,274 @@ +package db + +import ( + "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" + "errors" + "fmt" + "reflect" + "strings" + "xorm.io/xorm" +) + +type RegionalAgentUserBelongWithSession struct { + Session *xorm.Session + RegionalAgentUserBelong *model.RegionalAgentUserBelong +} + +// 联表查询 +type RegionalAgentUserBelongJoinUser struct { + model.RegionalAgentUserBelong `xorm:"extends"` + model.User `xorm:"extends"` + model.UserLevel `xorm:"extends"` + model.UserProfile `xorm:"extends"` +} + +// 单条记录插入 +func (rab RegionalAgentUserBelongWithSession) InsertOne() error { + _, err := rab.Session.InsertOne(rab.RegionalAgentUserBelong) + if err != nil { + return err + } + return nil +} + +// 多条记录插入 +func (rab RegionalAgentUserBelongWithSession) InsertMulti(belong []model.RegionalAgentUserBelong) error { + _, err := rab.Session.Table("regional_agent_user_belong").InsertMulti(belong) + if err != nil { + return err + } + return nil +} + +// 跟pvd和uid更新 +func (rab RegionalAgentUserBelongWithSession) UpdateRegionalAgentUserBelong(cols ...string) (bool, error) { + _, err := rab.Session.Table("regional_agent_user_belong"). + Where("id = ? ", rab.RegionalAgentUserBelong.Id). + Cols(cols...). + Update(rab.RegionalAgentUserBelong) + /*Where("uid = ? AND pvd = '?' is_use = ?", rab.RegionalAgentUserBelong.Uid, rab.RegionalAgentUserBelong.Pvd). + Update(rab.RegionalAgentUserBelong)*/ + if err != nil { + return false, err + } + 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). + Get(rab.RegionalAgentUserBelong) + sql, _ := rab.Session.LastSQL() + fmt.Printf("123 %s\n uid: %s\n", sql, rab.RegionalAgentUserBelong.Uid) + if err != nil { + return false, err + } + return isHas, nil +} + +// 获取代理区域所有会员 +func (rab RegionalAgentUserBelongWithSession) GetAllRegionalAgentUserBelong(condition string, page, pageSize int, keyword, + sort, commissionSort, orderSort, userLevel string) (*[]RegionalAgentUserBelongJoinUser, error) { + if page == 0 && pageSize == 0 { + page = 1 + pageSize = 10 + } + var userBelongList []RegionalAgentUserBelongJoinUser + userJoinCondition := "" + orderBy := "" + //groupBy := "" + selectColumn := "*" + if keyword != "" { + userJoinCondition = " AND (phone like '%" + keyword + "%' OR nickname like '%" + keyword + "%')" + } + + if sort != "" { + selectColumn += ",count(user_relate.parent_uid) as sort" + orderBy += "sort " + sort + "," + } + if commissionSort != "" { + orderBy += "user_profile.fin_commission " + commissionSort + "," + } + if orderSort != "" { + selectColumn += ", count(regional_agent_user_ord.uid) as order_count," + orderBy += "order_count " + orderSort + "," + } + if userLevel != "" { + condition += " AND regional_agent_user_belong.user_level = " + userLevel + orderBy += "regional_agent_user_belong.create_time DESC" + } + orderBy = strings.TrimRight(orderBy, ",") + selectColumn = strings.TrimRight(selectColumn, ",") + //rab.Session.Join("INNER", "user_relate", "") + rab.Session.Join("INNER", "user", "regional_agent_user_belong.uid = user.uid"+userJoinCondition) + rab.Session.Join("INNER", "user_level", "user.level = user_level.id") + rab.Session.Join("INNER", "user_profile", "regional_agent_user_belong.uid = user_profile.uid") + rab.Session.Join("LEFT", "regional_agent_user_ord", "regional_agent_user_belong.uid = regional_agent_user_ord.uid") + rab.Session.Join("LEFT", "user_relate", "regional_agent_user_ord.uid = user_relate.parent_uid") + err := rab.Session.Select(selectColumn).Table("regional_agent_user_belong"). + Where(condition). + GroupBy("regional_agent_user_belong.uid"). + Limit(pageSize, (page-1)*pageSize). + OrderBy(orderBy). + Find(&userBelongList) + if err != nil { + return nil, err + } + return &userBelongList, nil +} + +// 根据pvd查询 +func (rab RegionalAgentUserBelongWithSession) GetRegionalAgentUserBelongByUidAndPvd() (bool, error) { + //var belong *model.RegionalAgentUserBelong + isHas, err := rab.Session.Table("regional_agent_user_belong"). + Get(rab.RegionalAgentUserBelong) + sql, _ := rab.Session.LastSQL() + fmt.Printf("%s\n", sql) + if err != nil { + return false, err + } + return isHas, nil +} + +// 根据用户id查找用户加入的所有网点 +func (rab RegionalAgentUserBelongWithSession) GetRegionalAgentUserBelongByUid() ([]*model.RegionalAgentUserBelong, error) { + var list []*model.RegionalAgentUserBelong + err := rab.Session.Table("regional_agent_user_belong").Where("uid = ? AND is_use = 1", rab.RegionalAgentUserBelong.Uid).Find(&list) + if err != nil { + return nil, err + } + return list, nil +} + +func GetCountByRegionalAgentUserBelong(Db *xorm.Engine, condition string) int { + var RegionalAgentUserBelong model.RegionalAgentUserBelong + session := Db.Where(condition) + count, err := session.Count(&RegionalAgentUserBelong) + if err != nil { + return 0 + } + return int(count) +} + +// GetCountByRegionalAgentUserBelongByParams 通过传入的参数查询数据(单条) +func GetCountByRegionalAgentUserBelongByParams(Db *xorm.Engine, uid interface{}, params map[string]interface{}) (*model.RegionalAgentUserBelong, error) { + var m model.RegionalAgentUserBelong + var query = fmt.Sprintf("%s =?", params["key"]) + if has, err := Db.Where("uid = ?", uid).Where(query, params["value"]).Get(&m); err != nil || has == false { + return nil, err + } + return &m, nil +} + +// GetCountByRegionalAgentUserBelong 通过传入的参数查询数据(单条) +func GetCountByRegionalAgentUserBelongById(Db *xorm.Engine, 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).Get(&m); err != nil || has == false { + return nil, err + } + return &m, nil + } else { + if has, err := Db.Where("uid = ? and pvd=?", uid, pvd).Get(&m); err != nil || has == false { + return nil, err + } + return &m, nil + } +} + +// RegionalAgentOrdBelongInsert 插入单条数据 +func RegionalAgentOrdBelongInsert(Db *xorm.Engine, 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) { + 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", regionId).Find(&m) + if err != nil { + return nil, err + } + return &m, nil +} + +// GetCountByRegionalAgentBase 通过传入的参数查询数据(单条) +func GetCountByRegionalAgentBase(Db *xorm.Engine) (*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) { + var m []model.VirtualCoin + if params["key"] == nil { + //查询全部数据 + err := Db.Find(&m) + if err != nil { + return nil, 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, 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, err + } + return &m, nil + } + + } +} +func RegionalAgentCalcSystemRelateInsert(Db *xorm.Engine, 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) { + 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 +} diff --git a/db/model/regional_agent_base.go b/db/model/regional_agent_base.go new file mode 100644 index 0000000..6977a80 --- /dev/null +++ b/db/model/regional_agent_base.go @@ -0,0 +1,39 @@ +package model + +import ( + "time" +) + +type RegionalAgentBase struct { + Id int `json:"id" xorm:"not null pk autoincr INT(11)"` + OpenProvinceAgent int `json:"open_province_agent" xorm:"not null comment('是否开启省级代理(否:0;是:1)') TINYINT(1)"` + ProvinceSchemeId int `json:"province_scheme_id" xorm:"comment('省级方案id') INT(11)"` + OpenCityAgent int `json:"open_city_agent" xorm:"not null default 0 comment('是否开启市级代理(否:0;是:1)') TINYINT(1)"` + CitySchemeId int `json:"city_scheme_id" xorm:"comment('市级方案id') INT(11)"` + OpenDistrictAgent int `json:"open_district_agent" xorm:"not null default 0 comment('是否开启县区级代理(否:0;是:1)') TINYINT(1)"` + DistrictSchemeId int `json:"district_scheme_id" xorm:"default 0 comment('县区级方案id') INT(11)"` + OpenBranchesAgent int `json:"open_branches_agent" xorm:"not null default 0 comment('是否开启网点代理(否:0;是:1)') TINYINT(1)"` + BranchesSchemeId int `json:"branches_scheme_id" xorm:"default 1 comment('网点方案id') INT(11)"` + UpgradeSequence int `json:"upgrade_sequence" xorm:"not null default 0 comment('升级顺序(1逐级升级2任意级别升级)') TINYINT(1)"` + AutoOrderBy int `json:"auto_order_by" xorm:"not null default 0 comment('订单归属,网点加入方式(1定位后自动加入,2用户手动选择加入)') TINYINT(1)"` + ScopeOfOrder int `json:"scope_of_order" xorm:"default 0 comment('当选择定位后自动加入,该字段必填;订单归属范围(km单位)用处:定位后自动绑定xxkm之内离用户最近的网点') INT(11)"` + RemindType int `json:"remind_type" xorm:"default 0 comment('当选择用户手动选择加入,该字段必填;应用内用户选择网点弹窗方式(0关闭,1正常开启,2强制开启)') TINYINT(1)"` + ScopeOfBranches int `json:"scope_of_branches" xorm:"not null comment('网点展示范围(单位:km)') INT(11)"` + PayChannel string `json:"pay_channel" xorm:"not null comment('支付方式设置([1,2,3])1余额支付2微信支付3支付宝支付') VARCHAR(255)"` + CreateTime time.Time `json:"create_time" xorm:"not null default 'CURRENT_TIMESTAMP' comment('创建时间') DATETIME"` + UpdateTime time.Time `json:"update_time" xorm:"not null default 'CURRENT_TIMESTAMP' comment('更新时间') DATETIME"` + MallAutoOrderBy int `json:"mall_auto_order_by" xorm:"default 0 comment('订单归属,网点加入方式(1定位后自动加入,2用户手动选择加入 3以订单收货地址自动归属)') TINYINT(1)"` + O2oAutoOrderBy int `json:"o2o_auto_order_by" xorm:"default 0 comment('订单归属,网点加入方式(1定位后自动加入,2用户手动选择加入 4以消费商家定位自动归属)') TINYINT(1)"` + MallScopeOfOrder int `json:"mall_scope_of_order" xorm:"default 0 comment('当选择定位后自动加入,该字段必填;订单归属范围(km单位)用处:定位后自动绑定xxkm之内离用户最近的网点') INT(11)"` + MallRemindType int `json:"mall_remind_type" xorm:"default 0 comment('当选择用户手动选择加入,该字段必填;应用内用户选择网点弹窗方式(0关闭,1正常开启,2强制开启)') TINYINT(1)"` + O2oScopeOfOrder int `json:"o2o_scope_of_order" xorm:"default 0 comment('当选择定位后自动加入,该字段必填;订单归属范围(km单位)用处:定位后自动绑定xxkm之内离用户最近的网点') INT(11)"` + O2oRemindType int `json:"o2o_remind_type" xorm:"default 0 comment('当选择用户手动选择加入,该字段必填;应用内用户选择网点弹窗方式(0关闭,1正常开启,2强制开启)') TINYINT(1)"` + IsJoin int `json:"is_join" xorm:"default 0 comment('是否加入,1:是,2:否, 3:关闭') TINYINT"` + CoinRewardOpen int `json:"coin_reward_open" xorm:"default 0 comment('1:启用,2:关闭') TINYINT(1)"` + CoinSelect string `json:"coin_select" xorm:"default 0 comment('选中的虚拟币') VARCHAR(255)"` + CoinSet string `json:"coin_set" xorm:"comment('虚拟币设置') TEXT"` + UserToBeRegionSet int `json:"user_to_be_region_set" xorm:"default 0 comment('用户成为区域代理设置:1、一个用户可以成为多个区域代理身份,2、一个用户只能有一个区域代理身份') TINYINT(4)"` + AutoAddRegion int `json:"auto_add_region" xorm:"default 0 comment('自动加入网点设置:1、自动获取位置并加入网点 2、手动选择网点加入') TINYINT(4)"` + CustomerSetRegion int `json:"customer_set_region" xorm:"default 0 comment('网点自定义设置:1、用户可以手动添加网点 2、只允许后台添加网点') TINYINT(4)"` + SiteOfMinimumSeparationDistance string `json:"site_of_minimum_separation_distance" xorm:"comment('网点之间最小间隔距离(单位:km)') VARCHAR(255)"` +} diff --git a/db/model/regional_agent_calc_system_relate.go b/db/model/regional_agent_calc_system_relate.go new file mode 100644 index 0000000..3e26a6a --- /dev/null +++ b/db/model/regional_agent_calc_system_relate.go @@ -0,0 +1,19 @@ +package model + +import ( + "time" +) + +type RegionalAgentCalcSystemRelate struct { + Id int64 `json:"id" xorm:"pk autoincr BIGINT(11)"` + Uid int `json:"uid" xorm:"not null comment('用户Id') INT(11)"` + DivideAmount float64 `json:"divide_amount" xorm:"not null comment('分佣所得金额') DOUBLE"` + DivideVirtualCurrency string `json:"divide_virtual_currency" xorm:"comment('分佣所得虚拟币详情') TEXT"` + Ext string `json:"ext" xorm:"comment('额外字段') TEXT"` + Status int `json:"status" xorm:"not null default 1 comment('状态(1:未结算,2:已结算)') TINYINT(3)"` + Type int `json:"type" xorm:"not null default 1 comment('1:分佣记录 2:返佣记录') TINYINT(1)"` + BelongOrdId string `json:"belong_ord_id" xorm:"not null comment('所属订单id') VARCHAR(255)"` + Pvd string `json:"pvd" xorm:"not null comment('渠道') VARCHAR(255)"` + CreatedAt time.Time `json:"created_at" xorm:"not null default 'CURRENT_TIMESTAMP' comment('创建时间') TIMESTAMP"` + UpdatedAt time.Time `json:"updated_at" xorm:"default 'CURRENT_TIMESTAMP' comment('更新时间') TIMESTAMP"` +} diff --git a/db/model/regional_agent_ord_belong.go b/db/model/regional_agent_ord_belong.go new file mode 100644 index 0000000..898d4c1 --- /dev/null +++ b/db/model/regional_agent_ord_belong.go @@ -0,0 +1,23 @@ +package model + +import ( + "time" +) + +type RegionalAgentOrdBelong struct { + Id int64 `json:"id" xorm:"pk autoincr BIGINT(20)"` + Uid int `json:"uid" xorm:"not null comment('用户id') index(mgbgu_uid_ord_id_group_id_index) INT(11)"` + Pvd string `json:"pvd" xorm:"not null comment('渠道:自营,导购,o2o。。。。') VARCHAR(255)"` + OrderId int64 `json:"order_id" xorm:"not null comment('订单id') BIGINT(20)"` + CurrencyType int `json:"currency_type" xorm:"not null default 1 comment('币种类型:(1:佣金,2:积分,3:区块币,4:...)') INT(3)"` + Commission string `json:"commission" xorm:"not null comment('订单区域代理总佣金') DECIMAL(12,4)"` + RegionId int `json:"region_id" xorm:"not null comment('区域id') INT(11)"` + RegionalName string `json:"regional_name" xorm:"not null comment('地区名:(例如:广东省;广东省-珠海市;广东省-珠海市-香洲区;广东省-珠海市-香洲区-港湾一号)') VARCHAR(255)"` + ProvinceId int64 `json:"province_id" xorm:"comment('省级区域id') BIGINT(20)"` + CityId int64 `json:"city_id" xorm:"comment('市级区域id') BIGINT(20)"` + DistrictId int64 `json:"district_id" xorm:"comment('区/县级id') BIGINT(20)"` + SiteId int64 `json:"site_id" xorm:"comment('网点id') BIGINT(20)"` + CreateTime time.Time `json:"create_time" xorm:"not null default 'CURRENT_TIMESTAMP' comment('创建时间') index(mgbgu_uid_ord_id_group_id_index) TIMESTAMP"` + UpdateTime time.Time `json:"update_time" xorm:"not null default 'CURRENT_TIMESTAMP' comment('更新时间') TIMESTAMP"` + Level int `json:"level" xorm:"not null default 0 comment('等级(1:省级;2:市级;3:区/县 级;4:网点)') TINYINT(1)"` +} diff --git a/db/model/regional_agent_region.go b/db/model/regional_agent_region.go new file mode 100644 index 0000000..f5fbbc8 --- /dev/null +++ b/db/model/regional_agent_region.go @@ -0,0 +1,35 @@ +package model + +import ( + "time" +) + +type RegionalAgentRegion struct { + Id int64 `json:"id" xorm:"pk autoincr comment('表的主键id(亦可以代表省级id)') BIGINT(20)"` + ProvinceId int64 `json:"province_id" xorm:"comment('设置市级代理数据时,省级的id') BIGINT(20)"` + CityId int64 `json:"city_id" xorm:"comment('设置县区级代理数据时,市级的id') BIGINT(20)"` + DistrictId int64 `json:"district_id" xorm:"comment('设置网点代理数据时,县,区id') BIGINT(20)"` + RegionName string `json:"region_name" xorm:"not null comment('地区名:(例如:广东省;广东省-珠海市;广东省-珠海市-香洲区;广东省-珠海市-香洲区-港湾一号)') VARCHAR(255)"` + Name string `json:"name" xorm:"not null comment('地区名)') VARCHAR(255)"` + Longitude string `json:"longitude" xorm:"comment('经度(只有设置网点代理才会有数据)') DECIMAL(10,7)"` + Latitude string `json:"latitude" xorm:"comment('纬度(只有设置网点代理才会有数据)') DECIMAL(10,7)"` + AgPrice string `json:"ag_price" xorm:"not null comment('永久代理价格') DECIMAL(12,2)"` + MonthAgPrice string `json:"month_ag_price" xorm:"not null comment('月代理价格') DECIMAL(12,2)"` + QuarterAgPrice string `json:"quarter_ag_price" xorm:"not null comment('季度代理价格') DECIMAL(12,2)"` + YearAgPrice string `json:"year_ag_price" xorm:"not null comment('年代理价格') DECIMAL(12,2)"` + RenewalAgPrice string `json:"renewal_ag_price" xorm:"not null comment('续费永久代理价格') DECIMAL(12,2)"` + RenewalMonthAgPrice string `json:"renewal_month_ag_price" xorm:"not null comment('续费月代理价格') DECIMAL(12,2)"` + RenewalQuarterAgPrice string `json:"renewal_quarter_ag_price" xorm:"not null comment('续费季度代理价格') DECIMAL(12,2)"` + RenewalYearAgPrice string `json:"renewal_year_ag_price" xorm:"not null comment('续费年代理价格') DECIMAL(12,2)"` + CommissionRate string `json:"commission_rate" xorm:"not null comment('分佣比例') DECIMAL(6,4)"` + ReturnedRate string `json:"returned_rate" xorm:"not null comment('返佣比例') DECIMAL(6,4)"` + VirtualCurrencyRate string `json:"virtual_currency_rate" xorm:"not null comment('虚拟币比例') DECIMAL(6,4)"` + LimitPerson int `json:"limit_person" xorm:"not null comment('限制人数(默认为0则不限制)') INT(11) "` + Remark string `json:"remark" xorm:"comment('备注') VARCHAR(255)"` + IsUse int `json:"is_use" xorm:"not null default 1 comment('是否使用(否:0;是:1)') TINYINT(1)"` + Level int `json:"level" xorm:"not null default 0 comment('等级(1:省级;2:市级;3:区/县 级;4:网点)') TINYINT(1)"` + CreateTime time.Time `json:"create_time" xorm:"not null default 'CURRENT_TIMESTAMP' comment('创建时间') TIMESTAMP"` + UpdateTime time.Time `json:"update_time" xorm:"default 'CURRENT_TIMESTAMP' comment('更新时间') TIMESTAMP"` + WebsiteRegionId string `json:"website_region_id" xorm:"default '' comment('关联总后台地区的id') TIMESTAMP"` + IsCustomer int `json:"is_customer" xorm:"not null default 1 comment('是否自定义新增的区域代理(否:0;是:1)') TINYINT(1)"` +} diff --git a/db/model/regional_agent_scheme.go b/db/model/regional_agent_scheme.go new file mode 100644 index 0000000..a03d52c --- /dev/null +++ b/db/model/regional_agent_scheme.go @@ -0,0 +1,35 @@ +package model + +import ( + "time" +) + +type RegionalAgentScheme struct { + Id int `json:"id" xorm:"not null pk autoincr INT(11)"` + Name string `json:"name" xorm:"comment('方案名称') VARCHAR(255)"` + Comment string `json:"comment" xorm:"comment('备注') VARCHAR(2048)"` + IsUse int `json:"is_use" xorm:"not null default 0 comment('是否使用') TINYINT(1)"` + AgentType int `json:"agent_type" xorm:"not null comment('(1省级代理2市级代理3县,区代理4网点代理)') TINYINT(1)"` + Label string `json:"label" xorm:"not null default '0' comment('别名,给前端使用') VARCHAR(255)"` + IsUnify int `json:"is_unify" xorm:"not null default 0 comment('是否统一佣金比例(否:0;是:1)') TINYINT(1)"` + UnifyCommissionRate string `json:"unify_commission_rate" xorm:"not null default 0.0000 comment('统一佣金比例') DECIMAL(6,4)"` + FistAutoAudit int `json:"fist_auto_audit" xorm:"not null default 1 comment('初次购买审核方式(1自动,2手动)') TINYINT(1)"` + RenewalAutoAudit int `json:"renewal_auto_audit" xorm:"not null default 0 comment('续费审核方式(1自动,2手动)') TINYINT(1)"` + IsUnifyAgentPrice int `json:"is_unify_agent_price" xorm:"default 0 comment('是否统一代理价格(1统一价格,2按区域设置不用价格)') TINYINT(1)"` + LevelUpdateCondition int `json:"level_update_condition" xorm:"default 0 comment('1是无条件升级,2是条件升级,3是组合条件升级') TINYINT(1)"` + FinishTaskNum int `json:"finish_task_num" xorm:"default 0 comment('完成任务数量(如果是组合条件,则该数指的是完成几组)') INT(11)"` + AgentDate int `json:"agent_date" xorm:"not null default 0 comment('代理有效期(0永久有效,单位月)') INT(11)"` + IsRenewal int `json:"is_renewal" xorm:"not null comment('是否开启续费功能(否:0;是:1)') TINYINT(1)"` + RenewalAgPrice string `json:"renewal_ag_price" xorm:"comment('续费永久代理价格') DECIMAL(12,2)"` + RenewalMonthAgPrice string `json:"renewal_month_ag_price" xorm:"default 0.00 comment('续费月代理价格') DECIMAL(12,2)"` + RenewalQuarterAgPrice string `json:"renewal_quarter_ag_price" xorm:"comment('续费季度代理价格') DECIMAL(12,2)"` + RenewalYearAgPrice string `json:"renewal_year_ag_price" xorm:"comment('续费年代理价格') DECIMAL(12,2)"` + RewardCoins string `json:"reward_coins" xorm:"comment('奖励虚拟币设置') TEXT"` + IsReward int `json:"is_reward" xorm:"not null comment('代理奖励(1:开启,2:关闭)') TINYINT(1)"` + CreateTime time.Time `json:"create_time" xorm:"not null default 'CURRENT_TIMESTAMP' comment('创建时间') DATETIME"` + UpdateTime time.Time `json:"update_time" xorm:"not null default 'CURRENT_TIMESTAMP' comment('更新时间') DATETIME"` + IsUnifyCommissionReturn int `json:"is_unify_commission_return" xorm:"not null default 0 comment('统一返佣金比例(是:0;否:1)') TINYINT(1)"` + UnifyCommissionReturnRate string `json:"unify_commission_return_rate" xorm:"not null default 0.0000 comment('统一返佣金比例') DECIMAL(6,4)"` + IsUnifyLimmitPersion int `json:"is_unify_limmit_persion" xorm:"not null default 0 comment('统一限制人数(是:0;否:1)') TINYINT(1)"` + UnifyLimmitPersion string `json:"unify_limmit_persion" xorm:"not null default 0 comment('统一限制人数') VARCHAR(50)"` +} diff --git a/db/model/regional_agent_user_belong.go b/db/model/regional_agent_user_belong.go new file mode 100644 index 0000000..9e7e125 --- /dev/null +++ b/db/model/regional_agent_user_belong.go @@ -0,0 +1,22 @@ +package model + +import ( + "time" +) + +type RegionalAgentUserBelong struct { + Id int64 `json:"id" xorm:"pk autoincr BIGINT(20)"` + Uid int `json:"uid" xorm:"not null comment('用户id') index(mgbgu_uid_ord_id_group_id_index) INT(11)"` + RegionId int `json:"region_id" xorm:"not null comment('区域id') INT(11)"` + ProvinceId int64 `json:"province_id" xorm:"comment('省级区域id') BIGINT(20)"` + CityId int64 `json:"city_id" xorm:"comment('市级区域id') BIGINT(20)"` + DistrictId int64 `json:"district_id" xorm:"comment('区/县级id') BIGINT(20)"` + SiteId int64 `json:"site_id" xorm:"comment('网点id') BIGINT(20)"` + RegionalName string `json:"regional_name" xorm:"not null comment('地区名:(例如:广东省;广东省-珠海市;广东省-珠海市-香洲区;广东省-珠海市-香洲区-港湾一号)') VARCHAR(255)"` + CreateTime time.Time `json:"create_time" xorm:"not null default 'CURRENT_TIMESTAMP' comment('创建时间') index(mgbgu_uid_ord_id_group_id_index) TIMESTAMP"` + UpdateTime time.Time `json:"update_time" xorm:"not null default 'CURRENT_TIMESTAMP' comment('更新时间') TIMESTAMP"` + Level int `json:"level" xorm:"not null default 0 comment('等级(1:省级;2:市级;3:区/县 级;4:网点)') TINYINT(1)"` + Pvd string `json:"pvd" xorm:"default '' comment('GUIDE:导购,SELF_MALL:自营,O2O:O2O') VARCHAR(20)"` + IsUse int `json:"is_use" xorm:"default 0 comment('是否在使用,1:是,2,否') TINYINT(1)"` + UserLevel int `json:"user_level" xorm:"default 0 comment('会员等级') INT"` +} diff --git a/md/regional_agent.go b/md/regional_agent.go new file mode 100644 index 0000000..3ae3d84 --- /dev/null +++ b/md/regional_agent.go @@ -0,0 +1,46 @@ +package md + +const ( + PROVINCE = "province" + CITY = "city" + COUNTRY = "country" + SITE = "site" +) + +const ( + ProvinceKey = 1 + CityKey = 2 + CountryKey = 3 + SiteKey = 4 +) + +type InsertRegionalAgentOrdBelongData struct { + Uid interface{} `json:"uid"` + Pvd string `json:"pvd"` + CommPvd string `json:"comm_pvd"` + Commission string `json:"commission"` + OrderId int64 `json:"order_id"` + Status string `json:"status"` +} + +type RegionalAgentSystemArgs struct { + Uid string `json:"uid"` + Amount string `json:"amount"` + OrdId string `json:"ord_id"` + Pvd string `json:"pvd"` + CommPvd string `json:"comm_pvd"` + Type string `json:"type"` + RegionId string `json:"regionId"` + Status string `json:"status"` +} +type CoinSetDetail struct { + CoinID int `json:"coin_id"` + CoinName string `json:"coin_name"` + Value string `json:"value"` +} +type CoinSet struct { + ProvinceAgent []CoinSetDetail `json:"province_agent"` + CityAgent []CoinSetDetail `json:"city_agent"` + DistrictAgent []CoinSetDetail `json:"district_agent"` + SiteAgent []CoinSetDetail `json:"site_agent"` +} diff --git a/rule/regional_agent.go b/rule/regional_agent.go new file mode 100644 index 0000000..942cd39 --- /dev/null +++ b/rule/regional_agent.go @@ -0,0 +1,354 @@ +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 BindUserOrdToAgent(eg *xorm.Engine, dbName string, data *md.InsertRegionalAgentOrdBelongData) { + CommRegionalAgentInsert(eg, data) +} + +//公共调用 +func CommRegionalAgentInsert(engine *xorm.Engine, args *md.InsertRegionalAgentOrdBelongData) int { + res, userBelong := InsertRegionalAgentOrdBelong(engine, args) + if !res { + return 0 + } + //todo::调用 “计算制度” 方法 + _, res = HandleCommissionDistributionSystem(engine, &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(userBelong.RegionId), + }) + if !res { + return 2 + } + return 1 +} + +func HandleCommissionDistributionSystem(engine *xorm.Engine, 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.GetCountByRegionalAgentUserBelongById(engine, 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.RegionalAgentRegionGetOneByParams(engine, map[string]interface{}{ + "key": "id", + "value": zhios_order_relate_utils.AnyToInt64(regionId), + }) + if err != nil { + return nil, false + } + //查询 regional_agent_base 表获取虚拟币相关配置信息 + agentBase, err := db.GetCountByRegionalAgentBase(engine) + 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.VirtualCoinFindByParams(engine, map[string]interface{}{}) + if err != nil { + return nil, false + } + var virtualCoinMap = map[int]string{} + for _, v := range *virtualCoin { + virtualCoinMap[v.Id] = v.ExchangeRatio + } + //查询代理网点的代理(网点、区、市、省)信息 + var parents, tempParents []map[string]interface{} + tempParents, amount, _, err = makeSystemData(engine, regionalAgentRegion, &coinSet, virtualCoinMap, parents, amount, md.SITE) + if err != nil { + return nil, false + } + if tempParents != nil { + parents = tempParents + } + + tempParents, amount, _, err = makeSystemData(engine, regionalAgentRegion, &coinSet, virtualCoinMap, parents, amount, md.COUNTRY) + if err != nil { + return nil, false + } + if tempParents != nil { + parents = tempParents + } + + tempParents, amount, _, err = makeSystemData(engine, regionalAgentRegion, &coinSet, virtualCoinMap, parents, amount, md.CITY) + if err != nil { + return nil, false + } + if tempParents != nil { + parents = tempParents + } + + tempParents, amount, _, err = makeSystemData(engine, 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.RegionalAgentCalcSystemRelateInsert(engine, &insertData) + if err != nil { + return nil, false + } + } + return parents, true +} + +func makeSystemData(engine *xorm.Engine, 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.RegionalAgentRegionGetOneByParams(engine, map[string]interface{}{ + "key": "id", + "value": value, + }) + //查询代理地区用户 + agentUser, err := db.RegionalAgentUserFindByValid(engine, value) + if err != nil { + return nil, amount, 0, err + } + var divideAmount float64 = 0 + //查询对应的等级方案 + scheme, err := db.GetCountByRegionalAgentSchemeInfoByParams(engine, 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 + +} + +//公共处理佣金 +func CommAmount(CommissionRate float64, amount float64, agentUser *[]model.RegionalAgentUser, coinSetDetail []md.CoinSetDetail, virtualCoin map[int]string, parents []map[string]interface{}, isTest bool) ([]map[string]interface{}, float64, float64, error) { + var divideAmount float64 = 0 + if amount == 0 { + for _, v := range *agentUser { + var temp = map[string]interface{}{} + temp["uid"] = v.Uid + temp["divide"] = 0 + parents = append(parents, temp) + } + } else { + rate := CommissionRate / 100 + fmt.Println("比例:", rate) + divideAmount = rate * amount + personNum := len(*agentUser) + fmt.Println("人数:", personNum) + if personNum == 0 && isTest == false { + return nil, amount, divideAmount, nil + } + everyPersonDivideAmount := divideAmount / zhios_order_relate_utils.AnyToFloat64(personNum) + for _, v := range *agentUser { + var temp = map[string]interface{}{} + temp["uid"] = v.Uid + if amount == 0 { + temp["divide_amount"] = 0 + } else { + temp["divide_amount"] = everyPersonDivideAmount + var coinSetDetailMap []map[string]interface{} + for _, v := range coinSetDetail { + var exchangeRatio float64 + if zhios_order_relate_utils.AnyToFloat64(virtualCoin[v.CoinID]) != 0 { + exchangeRatio = zhios_order_relate_utils.AnyToFloat64(virtualCoin[v.CoinID]) + } else { + exchangeRatio = 0 + } + coinSetDetailMap = append(coinSetDetailMap, map[string]interface{}{ + "coin_id": v.CoinID, + "coin_name": v.CoinName, + "divide_value": zhios_order_relate_utils.AnyToFloat64(temp["divide_amount"]) * (zhios_order_relate_utils.AnyToFloat64(v.Value) / 100) * exchangeRatio, + }) + } + temp["divide_virtual_currency"] = coinSetDetailMap + } + parents = append(parents, temp) + } + if amount-divideAmount <= 0 { + amount = 0 + } else { + amount = amount - divideAmount + } + } + return parents, amount, divideAmount, nil +} + +//InsertRegionalAgentOrdBelong 插入 regional_agent_ord_belong (用户订单归属表) 数据 +func InsertRegionalAgentOrdBelong(engine *xorm.Engine, data *md.InsertRegionalAgentOrdBelongData) (bool, *model.RegionalAgentUserBelong) { + // 查询 regional_agent_user_belong 得到用户归属的区域代理 + userBelong, err := db.GetCountByRegionalAgentUserBelongById(engine, data.Uid, data.Pvd, data.CommPvd) + + if err != nil || userBelong == nil { + return false, nil + } + date := time.Now() + _, err = db.RegionalAgentOrdBelongInsert(engine, &model.RegionalAgentOrdBelong{ + Uid: int(zhios_order_relate_utils.AnyToInt64(data.Uid)), + Pvd: data.Pvd, + OrderId: data.OrderId, + CurrencyType: 1, + Commission: data.Commission, + RegionId: userBelong.RegionId, + RegionalName: userBelong.RegionalName, + ProvinceId: userBelong.ProvinceId, + CityId: userBelong.CityId, + DistrictId: userBelong.DistrictId, + SiteId: userBelong.SiteId, + CreateTime: date, + UpdateTime: date, + Level: userBelong.Level, + }) + if err != nil { + return false, nil + } + + return true, userBelong +} diff --git a/utils/convert.go b/utils/convert.go index 62cb219..3645f12 100644 --- a/utils/convert.go +++ b/utils/convert.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "math" + "reflect" "strconv" "strings" ) @@ -22,7 +23,13 @@ func ToInt64(raw interface{}, e error) int64 { } return AnyToInt64(raw) } - +func IsNil(i interface{}) bool { + vi := reflect.ValueOf(i) + if vi.Kind() == reflect.Ptr { + return vi.IsNil() + } + return false +} func AnyToBool(raw interface{}) bool { switch i := raw.(type) { case float32, float64, int, int64, uint, uint8, uint16, uint32, uint64, int8, int16, int32: