From a26580a0170c42903ab936087ee784d0d3fba42f Mon Sep 17 00:00:00 2001 From: DengBiao <2319963317@qq.com> Date: Thu, 17 Nov 2022 22:57:34 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E7=A7=AF=E5=88=86=E9=87=8A=E6=94=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/db_integral_release_basic.go | 157 ++++++++++ db/db_integral_release_user_with_ord.go | 157 ++++++++++ db/db_integral_release_user_with_ord_flow.go | 157 ++++++++++ db/model/integral_release_basic.go | 22 ++ db/model/integral_release_user_with_ord.go | 18 ++ .../integral_release_user_with_ord_flow.go | 20 ++ md/integral_release_settlement.go | 7 + rule/integral_release_settlement.go | 270 ++++++++++++++++++ 8 files changed, 808 insertions(+) create mode 100644 db/db_integral_release_basic.go create mode 100644 db/db_integral_release_user_with_ord.go create mode 100644 db/db_integral_release_user_with_ord_flow.go create mode 100644 db/model/integral_release_basic.go create mode 100644 db/model/integral_release_user_with_ord.go create mode 100644 db/model/integral_release_user_with_ord_flow.go create mode 100644 md/integral_release_settlement.go create mode 100644 rule/integral_release_settlement.go diff --git a/db/db_integral_release_basic.go b/db/db_integral_release_basic.go new file mode 100644 index 0000000..0198de7 --- /dev/null +++ b/db/db_integral_release_basic.go @@ -0,0 +1,157 @@ +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" +) + +// BatchSelectIntegralReleases 批量查询数据 TODO::和下面的方法重复了,建议采用下面的 `IntegralReleaseFindByParams` 方法 +func BatchSelectIntegralReleases(Db *xorm.Engine, params map[string]interface{}) (*[]model.IntegralReleaseBasic, error) { + var IntegralReleaseData []model.IntegralReleaseBasic + if err := Db.In(zhios_order_relate_utils.AnyToString(params["key"]), params["value"]). + Find(&IntegralReleaseData); err != nil { + return nil, zhios_order_relate_logx.Warn(err) + } + return &IntegralReleaseData, nil +} + +// IntegralReleaseInsert 插入单条数据 +func IntegralReleaseInsert(Db *xorm.Engine, IntegralRelease *model.IntegralReleaseBasic) (int, error) { + _, err := Db.InsertOne(IntegralRelease) + if err != nil { + return 0, err + } + return IntegralRelease.Id, nil +} + +// BatchAddIntegralReleases 批量新增数据 +func BatchAddIntegralReleases(Db *xorm.Engine, IntegralReleaseData []*model.IntegralReleaseBasic) (int64, error) { + affected, err := Db.Insert(IntegralReleaseData) + if err != nil { + return 0, err + } + return affected, nil +} + +func GetIntegralReleaseCount(Db *xorm.Engine) int { + var IntegralRelease model.IntegralReleaseBasic + session := Db.Where("") + count, err := session.Count(&IntegralRelease) + if err != nil { + return 0 + } + return int(count) +} + +// IntegralReleaseDelete 删除记录 +func IntegralReleaseDelete(Db *xorm.Engine, id interface{}) (int64, error) { + if reflect.TypeOf(id).Kind() == reflect.Slice { + return Db.In("id", id).Delete(model.IntegralReleaseBasic{}) + } else { + return Db.Where("id = ?", id).Delete(model.IntegralReleaseBasic{}) + } +} + +// IntegralReleaseUpdate 更新记录 +func IntegralReleaseUpdate(session *xorm.Session, id interface{}, IntegralRelease *model.IntegralReleaseBasic, forceColums ...string) (int64, error) { + var ( + affected int64 + err error + ) + if forceColums != nil { + affected, err = session.Where("id=?", id).Cols(forceColums...).Update(IntegralRelease) + } else { + affected, err = session.Where("id=?", id).Update(IntegralRelease) + } + if err != nil { + return 0, err + } + return affected, nil +} + +// IntegralReleaseGetOneByParams 通过传入的参数查询数据(单条) +func IntegralReleaseGetOneByParams(session *xorm.Session, params map[string]interface{}) (*model.IntegralReleaseBasic, error) { + var m model.IntegralReleaseBasic + var query = fmt.Sprintf("%s =?", params["key"]) + has, err := session.Where(query, params["value"]).Get(&m) + if err != nil { + return nil, zhios_order_relate_logx.Error(err) + } + if has == false { + return nil, errors.New("未查询到相应的 block_star_chain 记录") + } + return &m, nil +} + +// IntegralReleaseFindByParams 通过传入的参数查询数据(多条) +func IntegralReleaseFindByParams(Db *xorm.Engine, params map[string]interface{}) (*[]model.IntegralReleaseBasic, error) { + var m []model.IntegralReleaseBasic + 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 + } + + } +} + +func IntegralReleaseFindByParamsByPage(Db *xorm.Engine, params map[string]interface{}, page, pageSize int) (*[]model.IntegralReleaseBasic, error) { + var m []model.IntegralReleaseBasic + if params["value"] == nil { + return nil, errors.New("参数有误") + } + if page == 0 && pageSize == 0 { + page = 1 + pageSize = 10 + } + + if params["key"] == nil { + //查询全部数据 + err := Db.Limit(pageSize, (page-1)*pageSize).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"]).Limit(pageSize, (page-1)*pageSize).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"]).Limit(pageSize, (page-1)*pageSize).Find(&m) + if err != nil { + return nil, zhios_order_relate_logx.Error(err) + } + return &m, nil + } + + } +} diff --git a/db/db_integral_release_user_with_ord.go b/db/db_integral_release_user_with_ord.go new file mode 100644 index 0000000..767145d --- /dev/null +++ b/db/db_integral_release_user_with_ord.go @@ -0,0 +1,157 @@ +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" +) + +// BatchSelectIntegralReleaseUserWithOrds 批量查询数据 TODO::和下面的方法重复了,建议采用下面的 `IntegralReleaseUserWithOrdFindByParams` 方法 +func BatchSelectIntegralReleaseUserWithOrds(Db *xorm.Engine, params map[string]interface{}) (*[]model.IntegralReleaseUserWithOrd, error) { + var IntegralReleaseUserWithOrdData []model.IntegralReleaseUserWithOrd + if err := Db.In(zhios_order_relate_utils.AnyToString(params["key"]), params["value"]). + Find(&IntegralReleaseUserWithOrdData); err != nil { + return nil, zhios_order_relate_logx.Warn(err) + } + return &IntegralReleaseUserWithOrdData, nil +} + +// IntegralReleaseUserWithOrdInsert 插入单条数据 +func IntegralReleaseUserWithOrdInsert(Db *xorm.Engine, IntegralReleaseUserWithOrd *model.IntegralReleaseUserWithOrd) (int, error) { + _, err := Db.InsertOne(IntegralReleaseUserWithOrd) + if err != nil { + return 0, err + } + return IntegralReleaseUserWithOrd.Id, nil +} + +// BatchAddIntegralReleaseUserWithOrds 批量新增数据 +func BatchAddIntegralReleaseUserWithOrds(Db *xorm.Engine, IntegralReleaseUserWithOrdData []*model.IntegralReleaseUserWithOrd) (int64, error) { + affected, err := Db.Insert(IntegralReleaseUserWithOrdData) + if err != nil { + return 0, err + } + return affected, nil +} + +func GetIntegralReleaseUserWithOrdCount(Db *xorm.Engine) int { + var IntegralReleaseUserWithOrd model.IntegralReleaseUserWithOrd + session := Db.Where("") + count, err := session.Count(&IntegralReleaseUserWithOrd) + if err != nil { + return 0 + } + return int(count) +} + +// IntegralReleaseUserWithOrdDelete 删除记录 +func IntegralReleaseUserWithOrdDelete(Db *xorm.Engine, id interface{}) (int64, error) { + if reflect.TypeOf(id).Kind() == reflect.Slice { + return Db.In("id", id).Delete(model.IntegralReleaseUserWithOrd{}) + } else { + return Db.Where("id = ?", id).Delete(model.IntegralReleaseUserWithOrd{}) + } +} + +// IntegralReleaseUserWithOrdUpdate 更新记录 +func IntegralReleaseUserWithOrdUpdate(session *xorm.Session, id interface{}, IntegralReleaseUserWithOrd *model.IntegralReleaseUserWithOrd, forceColums ...string) (int64, error) { + var ( + affected int64 + err error + ) + if forceColums != nil { + affected, err = session.Where("id=?", id).Cols(forceColums...).Update(IntegralReleaseUserWithOrd) + } else { + affected, err = session.Where("id=?", id).Update(IntegralReleaseUserWithOrd) + } + if err != nil { + return 0, err + } + return affected, nil +} + +// IntegralReleaseUserWithOrdGetOneByParams 通过传入的参数查询数据(单条) +func IntegralReleaseUserWithOrdGetOneByParams(session *xorm.Session, params map[string]interface{}) (*model.IntegralReleaseUserWithOrd, error) { + var m model.IntegralReleaseUserWithOrd + var query = fmt.Sprintf("%s =?", params["key"]) + has, err := session.Where(query, params["value"]).Get(&m) + if err != nil { + return nil, zhios_order_relate_logx.Error(err) + } + if has == false { + return nil, errors.New("未查询到相应的 block_star_chain 记录") + } + return &m, nil +} + +// IntegralReleaseUserWithOrdFindByParams 通过传入的参数查询数据(多条) +func IntegralReleaseUserWithOrdFindByParams(session *xorm.Session, params map[string]interface{}) (*[]model.IntegralReleaseUserWithOrd, error) { + var m []model.IntegralReleaseUserWithOrd + if params["value"] == nil { + return nil, errors.New("参数有误") + } + if params["key"] == nil { + //查询全部数据 + err := session.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 := session.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 := session.Where(query, params["value"]).Find(&m) + if err != nil { + return nil, zhios_order_relate_logx.Error(err) + } + return &m, nil + } + + } +} + +func IntegralReleaseUserWithOrdFindByParamsByPage(Db *xorm.Engine, params map[string]interface{}, page, pageSize int) (*[]model.IntegralReleaseUserWithOrd, error) { + var m []model.IntegralReleaseUserWithOrd + if params["value"] == nil { + return nil, errors.New("参数有误") + } + if page == 0 && pageSize == 0 { + page = 1 + pageSize = 10 + } + + if params["key"] == nil { + //查询全部数据 + err := Db.Limit(pageSize, (page-1)*pageSize).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"]).Limit(pageSize, (page-1)*pageSize).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"]).Limit(pageSize, (page-1)*pageSize).Find(&m) + if err != nil { + return nil, zhios_order_relate_logx.Error(err) + } + return &m, nil + } + + } +} diff --git a/db/db_integral_release_user_with_ord_flow.go b/db/db_integral_release_user_with_ord_flow.go new file mode 100644 index 0000000..1e600dc --- /dev/null +++ b/db/db_integral_release_user_with_ord_flow.go @@ -0,0 +1,157 @@ +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" +) + +// BatchSelectIntegralReleaseUserWithOrdFlows 批量查询数据 TODO::和下面的方法重复了,建议采用下面的 `IntegralReleaseUserWithOrdFlowFindByParams` 方法 +func BatchSelectIntegralReleaseUserWithOrdFlows(Db *xorm.Engine, params map[string]interface{}) (*[]model.IntegralReleaseUserWithOrdFlow, error) { + var IntegralReleaseUserWithOrdFlowData []model.IntegralReleaseUserWithOrdFlow + if err := Db.In(zhios_order_relate_utils.AnyToString(params["key"]), params["value"]). + Find(&IntegralReleaseUserWithOrdFlowData); err != nil { + return nil, zhios_order_relate_logx.Warn(err) + } + return &IntegralReleaseUserWithOrdFlowData, nil +} + +// IntegralReleaseUserWithOrdFlowInsert 插入单条数据 +func IntegralReleaseUserWithOrdFlowInsert(session *xorm.Session, IntegralReleaseUserWithOrdFlow *model.IntegralReleaseUserWithOrdFlow) (int, error) { + _, err := session.InsertOne(IntegralReleaseUserWithOrdFlow) + if err != nil { + return 0, err + } + return IntegralReleaseUserWithOrdFlow.Id, nil +} + +// BatchAddIntegralReleaseUserWithOrdFlows 批量新增数据 +func BatchAddIntegralReleaseUserWithOrdFlows(Db *xorm.Engine, IntegralReleaseUserWithOrdFlowData []*model.IntegralReleaseUserWithOrdFlow) (int64, error) { + affected, err := Db.Insert(IntegralReleaseUserWithOrdFlowData) + if err != nil { + return 0, err + } + return affected, nil +} + +func GetIntegralReleaseUserWithOrdFlowCount(Db *xorm.Engine) int { + var IntegralReleaseUserWithOrdFlow model.IntegralReleaseUserWithOrdFlow + session := Db.Where("") + count, err := session.Count(&IntegralReleaseUserWithOrdFlow) + if err != nil { + return 0 + } + return int(count) +} + +// IntegralReleaseUserWithOrdFlowDelete 删除记录 +func IntegralReleaseUserWithOrdFlowDelete(Db *xorm.Engine, id interface{}) (int64, error) { + if reflect.TypeOf(id).Kind() == reflect.Slice { + return Db.In("id", id).Delete(model.IntegralReleaseUserWithOrdFlow{}) + } else { + return Db.Where("id = ?", id).Delete(model.IntegralReleaseUserWithOrdFlow{}) + } +} + +// IntegralReleaseUserWithOrdFlowUpdate 更新记录 +func IntegralReleaseUserWithOrdFlowUpdate(session *xorm.Session, id interface{}, IntegralReleaseUserWithOrdFlow *model.IntegralReleaseUserWithOrdFlow, forceColums ...string) (int64, error) { + var ( + affected int64 + err error + ) + if forceColums != nil { + affected, err = session.Where("id=?", id).Cols(forceColums...).Update(IntegralReleaseUserWithOrdFlow) + } else { + affected, err = session.Where("id=?", id).Update(IntegralReleaseUserWithOrdFlow) + } + if err != nil { + return 0, err + } + return affected, nil +} + +// IntegralReleaseUserWithOrdFlowGetOneByParams 通过传入的参数查询数据(单条) +func IntegralReleaseUserWithOrdFlowGetOneByParams(session *xorm.Session, params map[string]interface{}) (*model.IntegralReleaseUserWithOrdFlow, error) { + var m model.IntegralReleaseUserWithOrdFlow + var query = fmt.Sprintf("%s =?", params["key"]) + has, err := session.Where(query, params["value"]).Get(&m) + if err != nil { + return nil, zhios_order_relate_logx.Error(err) + } + if has == false { + return nil, errors.New("未查询到相应的 block_star_chain 记录") + } + return &m, nil +} + +// IntegralReleaseUserWithOrdFlowFindByParams 通过传入的参数查询数据(多条) +func IntegralReleaseUserWithOrdFlowFindByParams(Db *xorm.Engine, params map[string]interface{}) (*[]model.IntegralReleaseUserWithOrdFlow, error) { + var m []model.IntegralReleaseUserWithOrdFlow + 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 + } + + } +} + +func IntegralReleaseUserWithOrdFlowFindByParamsByPage(Db *xorm.Engine, params map[string]interface{}, page, pageSize int) (*[]model.IntegralReleaseUserWithOrdFlow, error) { + var m []model.IntegralReleaseUserWithOrdFlow + if params["value"] == nil { + return nil, errors.New("参数有误") + } + if page == 0 && pageSize == 0 { + page = 1 + pageSize = 10 + } + + if params["key"] == nil { + //查询全部数据 + err := Db.Limit(pageSize, (page-1)*pageSize).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"]).Limit(pageSize, (page-1)*pageSize).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"]).Limit(pageSize, (page-1)*pageSize).Find(&m) + if err != nil { + return nil, zhios_order_relate_logx.Error(err) + } + return &m, nil + } + + } +} diff --git a/db/model/integral_release_basic.go b/db/model/integral_release_basic.go new file mode 100644 index 0000000..0ec24b3 --- /dev/null +++ b/db/model/integral_release_basic.go @@ -0,0 +1,22 @@ +package model + +import ( + "time" +) + +type IntegralReleaseBasic struct { + Id int `json:"id" xorm:"not null pk autoincr comment('主键id') INT(11)"` + IsUse int `json:"is_use" xorm:"not null default 0 comment('是否开启(否:0;是:1)') TINYINT(1)"` + Coin1 int `json:"coin_1" xorm:"not null default 0 comment('coinId_1(类似于红积分)') INT(11)"` + Coin2 int `json:"coin_2" xorm:"not null default 0 comment('coinId_2(类似于绿积分)') INT(11)"` + RegisterGiveCoinNum string `json:"register_give_coin_num" xorm:"not null default 0.0000 comment('注册赠送积分数量') DECIMAL(10,4)"` + SignInReleaseRatio string `json:"sign_in_release_ratio" xorm:"not null default 0.0000 comment('签到释放比例') DECIMAL(10,4)"` + BusinessDiscountSet string `json:"business_discount_set" xorm:"not null comment('商家让利释放速度设置') TEXT"` + AlipayServiceChargeRate string `json:"alipay_service_charge_rate" xorm:"not null default 0.0000 comment('支付宝手续费比例') DECIMAL(10,4)"` + TaxRevenueRate string `json:"tax_revenue_rate" xorm:"not null default 0.0000 comment('税收比例') DECIMAL(10,4)"` + OperatingExpenseRate string `json:"operating_expense_rate" xorm:"not null default 0.0000 comment('服务费比例') DECIMAL(10,4)"` + ReleaseDate string `json:"release_date" xorm:"not null default '0000-00-00' comment('释放日期') VARCHAR(50)"` + Ext string `json:"ext" xorm:"TEXT"` + CreateAt time.Time `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' comment('创建时间') TIMESTAMP"` + UpdateAt time.Time `json:"update_at" xorm:"default 'CURRENT_TIMESTAMP' comment('更新时间') TIMESTAMP"` +} diff --git a/db/model/integral_release_user_with_ord.go b/db/model/integral_release_user_with_ord.go new file mode 100644 index 0000000..523c7ee --- /dev/null +++ b/db/model/integral_release_user_with_ord.go @@ -0,0 +1,18 @@ +package model + +import ( + "time" +) + +type IntegralReleaseUserWithOrd struct { + Id int `json:"id" xorm:"not null pk autoincr comment('主键id') INT(11)"` + Uid int `json:"uid" xorm:"not null default 0 comment('uid') INT(11)"` + OrdId string `json:"ord_id" xorm:"not null default '0' comment('订单号') VARCHAR(100)"` + Pvd int `json:"pvd" xorm:"not null default 0 comment('平台(0:注册)') TINYINT(3)"` + TotalRedCoinNum string `json:"total_red_coin_num" xorm:"not null default 0.0000 comment('总红积分数量') DECIMAL(10,4)"` + ReleasedGreenCoinNum string `json:"released_green_coin_num" xorm:"not null default 0.0000 comment('已释放绿积分数量') DECIMAL(10,4)"` + OrdDiscountRatio string `json:"ord_discount_ratio" xorm:"not null default 0.0000 comment('让利百分比') DECIMAL(10,4)"` + State int `json:"state" xorm:"not null default '1' comment('状态(1:正常 2:异常)') TINYINT(1)"` + CreateAt time.Time `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' comment('创建时间') TIMESTAMP"` + UpdateAt time.Time `json:"update_at" xorm:"default 'CURRENT_TIMESTAMP' comment('更新时间') TIMESTAMP"` +} diff --git a/db/model/integral_release_user_with_ord_flow.go b/db/model/integral_release_user_with_ord_flow.go new file mode 100644 index 0000000..df0aecc --- /dev/null +++ b/db/model/integral_release_user_with_ord_flow.go @@ -0,0 +1,20 @@ +package model + +import ( + "time" +) + +type IntegralReleaseUserWithOrdFlow struct { + Id int `json:"id" xorm:"not null pk autoincr comment('主键id') INT(11)"` + Uid int `json:"uid" xorm:"not null default 0 comment('uid') INT(11)"` + OrdId string `json:"ord_id" xorm:"not null default '0' comment('订单号') VARCHAR(100)"` + Pvd int `json:"pvd" xorm:"not null default 0 comment('平台(0:注册)') TINYINT(3)"` + TotalRedCoinNum string `json:"total_red_coin_num" xorm:"not null default 0.0000 comment('总红积分数量') DECIMAL(10,4)"` + ReleasedGreenCoinNumBefore string `json:"released_green_coin_num_before" xorm:"not null default 0.0000 comment('已释放绿积分数量-之前') DECIMAL(10,4)"` + ReleasedGreenCoinNumAfter string `json:"released_green_coin_num_after" xorm:"not null default 0.0000 comment('已释放绿积分数量-之后') DECIMAL(10,4)"` + ReleaseGreenCoinNum string `json:"release_green_coin_num" xorm:"not null default 0.0000 comment('释放绿积分数量') DECIMAL(10,4)"` + ReleaseRateDay string `json:"release_rate_day" xorm:"not null default 0.0000 comment('日释放系数') DECIMAL(10,4)"` + ReleaseDate string `json:"release_date" xorm:"not null default '0000-00-00' comment('释放日期') VARCHAR(50)"` + CreateAt time.Time `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' comment('创建时间') TIMESTAMP"` + UpdateAt time.Time `json:"update_at" xorm:"default 'CURRENT_TIMESTAMP' comment('更新时间') TIMESTAMP"` +} diff --git a/md/integral_release_settlement.go b/md/integral_release_settlement.go new file mode 100644 index 0000000..42be659 --- /dev/null +++ b/md/integral_release_settlement.go @@ -0,0 +1,7 @@ +package md + +// BusinessDiscountSet 商家让利释放速度设置 +type BusinessDiscountSet struct { + Range []float64 `json:"range"` // 范围 + ReleaseSpeed string `json:"release_speed"` // 释放速度 +} diff --git a/rule/integral_release_settlement.go b/rule/integral_release_settlement.go new file mode 100644 index 0000000..f248534 --- /dev/null +++ b/rule/integral_release_settlement.go @@ -0,0 +1,270 @@ +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" + "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/svc" + zhios_order_relate_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils" + "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/cache" + zhios_order_relate_logx "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/logx" + "encoding/json" + "errors" + "fmt" + "github.com/shopspring/decimal" + "strconv" + "time" + "xorm.io/xorm" +) + +func InitForIntegralRelease(redisAddr string) (err error) { + if redisAddr != "" { + cache.NewRedis(redisAddr) + } + _, err = cache.SelectDb(md.RedisDataBase) + return +} + +const PessimismLockKeyForIntegralRelease = "daily_settlement_integral_release_pessimism_lock_key" +const PessimismLockValueForIntegralRelease = "running" +const DealUserCoinForIntegralReleaseRequestIdPrefix = "%s:integral_release_deal_user_coin:%d:uid:%d" + +// DailySettlementIntegralRelease 每日结算“积分释放” +func DailySettlementIntegralRelease(engine *xorm.Engine, mid string, isTask bool) (err error) { + session := engine.NewSession() + defer func() { + session.Close() + if err := recover(); err != nil { + _ = zhios_order_relate_logx.Error(err) + } + }() + session.Begin() + now := time.Now() + today := now.Format("2006-01-02") + + fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", now.Hour()) + if isTask && (now.Hour() > 8) { + //TODO::只在凌晨一点 ~ 凌晨 8 点运行 + return errors.New("非运行时间") + } + + //1、查找 `integral_release` 基础设置 + integralRelease, err := db.IntegralReleaseGetOneByParams(session, map[string]interface{}{ + "key": "is_use", + "value": 1, + }) + if err != nil { + _ = session.Rollback() + return err + } + if integralRelease.ReleaseDate == today { + _ = session.Rollback() + return errors.New("今日“积分释放”已结算") + } + + //TODO::增加“悲观锁”防止串行 + getString, _ := cache.GetString(PessimismLockKeyForIntegralRelease) + if getString == PessimismLockValueForIntegralRelease { + fmt.Println("-----------------", "上一次结算未执行完") + return errors.New("上一次结算未执行完") + } + cache.SetEx(PessimismLockKeyForIntegralRelease, PessimismLockValueForIntegralRelease, 3600*8) //8小时 + + //TODO::关闭系统 + zhios_order_relate_utils.CurlPost("http://zhios-app:5000/api/v1/check_app_over_time", map[string]string{ + "mid": mid, + "is_close": "1", + }, map[string]string{}) + + //2、查询所有 `integral_release_user_with_ord` 记录 + integralReleaseUserWithOrds, err := db.IntegralReleaseUserWithOrdFindByParams(session, map[string]interface{}{ + "state": "is_use", + "value": 1, + }) + for _, ord := range *integralReleaseUserWithOrds { + //计算释放系数 + err, releaseSpeed := calcReleaseRate(integralRelease, zhios_order_relate_utils.StrToFloat64(ord.OrdDiscountRatio)) + if err != nil { + _ = session.Rollback() + return err + } + releaseRate, _ := decimal.NewFromString(releaseSpeed) + + //释放绿积分 + err = ReleaseGreenCoinForUser(session, mid, integralRelease, &ord, releaseRate) + if err != nil { + _ = session.Rollback() + return err + } + } + + //3、提交事务 + err = session.Commit() + if err != nil { + _ = session.Rollback() + return errors.New("事务提交失败") + } + + zhios_order_relate_utils.CurlPost("http://zhios-app:5000/api/v1/check_app_over_time", map[string]string{ + "mid": mid, + "is_close": "0", + }, map[string]string{}) + + //cache.Del(PessimismLockKeyForIntegralRelease) + + fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>绿色积分结束<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") + return nil +} + +// calcReleaseRate 计算释放系数 +func calcReleaseRate(integralReleaseBasic *model.IntegralReleaseBasic, ordDiscountRatio float64) (err error, releaseSpeed string) { + var businessDiscountSet []*md.BusinessDiscountSet + if err := json.Unmarshal([]byte(integralReleaseBasic.BusinessDiscountSet), &businessDiscountSet); err != nil { + return zhios_order_relate_logx.Warn("解析【商家让利释放速度设置】出错"), releaseSpeed + } + + for _, value := range businessDiscountSet { + if value.Range[0] <= ordDiscountRatio && ordDiscountRatio <= value.Range[1] { + releaseSpeed = value.ReleaseSpeed + } + } + return nil, releaseSpeed +} + +// ReleaseGreenCoinForUser 释放绿积分 +func ReleaseGreenCoinForUser(session *xorm.Session, mid string, integralReleaseBasic *model.IntegralReleaseBasic, integralReleaseUserWithOrd *model.IntegralReleaseUserWithOrd, releaseSpeed decimal.Decimal) error { + //1、计算需要释放多少绿积分 + totalRedCoinNum, _ := decimal.NewFromString(integralReleaseUserWithOrd.TotalRedCoinNum) + releasedGreenCoinNum, _ := decimal.NewFromString(integralReleaseUserWithOrd.ReleasedGreenCoinNum) + releaseCoinNum := totalRedCoinNum.Sub(releasedGreenCoinNum).Mul(releaseSpeed) + + //2、释放绿积分 + var title = md.IntegralReleaseGreenCoinGrantByOrdTitleForUserVirtualCoinFlow + var transferType = md.IntegralReleaseGreenCoinGrantByOrdTransferTypeForUserVirtualCoinFlow + if integralReleaseUserWithOrd.Pvd == 0 { + title = md.IntegralReleaseGreenCoinGrantBySignInTitleForUserVirtualCoinFlow + transferType = md.IntegralReleaseGreenCoinGrantBySignInTransferTypeForUserVirtualCoinFlow + } + err := DealUserCoinForIntegralRelease(session, md.DealUserCoinReq{ + Kind: "add", + Mid: mid, + Title: title, + TransferType: transferType, + OrdId: integralReleaseUserWithOrd.OrdId, + CoinId: integralReleaseBasic.Coin2, + Uid: integralReleaseUserWithOrd.Uid, + Amount: zhios_order_relate_utils.StrToFloat64(releaseCoinNum.String()), + }) + if err != nil { + return err + } + releasedGreenCoinNumBefore, _ := decimal.NewFromString(integralReleaseUserWithOrd.ReleasedGreenCoinNum) + releasedGreenCoinNumAfter := releasedGreenCoinNumBefore.Add(releaseCoinNum) + + //3、更改`integral_release_user_with_ord`记录 + integralReleaseUserWithOrd.ReleasedGreenCoinNum = releasedGreenCoinNumAfter.String() + updateAffected, err := db.IntegralReleaseUserWithOrdUpdate(session, integralReleaseUserWithOrd.Id, integralReleaseUserWithOrd) + if err != nil { + return err + } + if updateAffected == 0 { + err = errors.New("更新 integral_release_user_with_ord 记录失败") + return err + } + + //4、插入`integral_release_user_with_ord_flow`记录 + now := time.Now() + var integralReleaseUserWithOrdFlow = model.IntegralReleaseUserWithOrdFlow{ + Uid: integralReleaseUserWithOrd.Uid, + OrdId: integralReleaseUserWithOrd.OrdId, + Pvd: integralReleaseUserWithOrd.Pvd, + TotalRedCoinNum: integralReleaseUserWithOrd.TotalRedCoinNum, + ReleasedGreenCoinNumBefore: releasedGreenCoinNumBefore.String(), + ReleasedGreenCoinNumAfter: releasedGreenCoinNumAfter.String(), + ReleaseGreenCoinNum: releaseCoinNum.String(), + ReleaseRateDay: releaseSpeed.String(), + ReleaseDate: now.Format("2006-01-02"), + CreateAt: now, + UpdateAt: now, + } + _, err = db.IntegralReleaseUserWithOrdFlowInsert(session, &integralReleaseUserWithOrdFlow) + if err != nil { + return err + } + return nil +} + +// DealUserCoinForIntegralRelease 处理给用户虚拟币积分 +func DealUserCoinForIntegralRelease(session *xorm.Session, req md.DealUserCoinReq) (err error) { + if req.Amount < 0 { + req.Amount = 0 + } + //1、分布式锁阻拦 + requestIdPrefix := fmt.Sprintf(DealUserCoinForIntegralReleaseRequestIdPrefix, req.Mid, req.CoinId, req.Uid) + cb, err := svc.HandleDistributedLock(req.Mid, strconv.Itoa(req.Uid), requestIdPrefix) + if err != nil { + return err + } + if cb != nil { + defer cb() // 释放锁 + } + + //2、计算&&组装数据 + now := time.Now() + coinAmount, err := svc.GetUserCoinAmount(session, req.Mid, req.CoinId, req.Uid) + if err != nil { + return err + } + coinAmountValue := decimal.NewFromFloat(zhios_order_relate_utils.StrToFloat64(coinAmount)) + amountValue := decimal.NewFromFloat(req.Amount).RoundFloor(4) + + var userVirtualCoinFlow model.UserVirtualCoinFlow + userVirtualCoinFlow.CoinId = req.CoinId + userVirtualCoinFlow.Title = req.Title + userVirtualCoinFlow.TransferType = req.TransferType + userVirtualCoinFlow.Uid = req.Uid + userVirtualCoinFlow.ToUid = req.ToUid + userVirtualCoinFlow.OrdId = req.OrdId + userVirtualCoinFlow.BeforeAmout = coinAmount + userVirtualCoinFlow.Amout = amountValue.String() + userVirtualCoinFlow.CreateTime = now + if req.Kind == "add" { + userVirtualCoinFlow.Direction = 1 + userVirtualCoinFlow.AfterAmout = coinAmountValue.Add(amountValue).RoundFloor(4).String() + } else if req.Kind == "sub" { + userVirtualCoinFlow.Direction = 2 + userVirtualCoinFlow.AfterAmout = coinAmountValue.Sub(amountValue).RoundFloor(4).String() + } else { + err = errors.New("错误的kind类型") + return err + } + if zhios_order_relate_utils.StrToFloat64(userVirtualCoinFlow.AfterAmout) < 0 { + var coin model.VirtualCoin + _, err = session.Where("id = ?", req.CoinId).Get(&coin) + if err != nil { + return err + } + zhios_order_relate_utils.FilePutContents("virtual_coin_not", zhios_order_relate_utils.SerializeStr(map[string]interface{}{ + "uid": userVirtualCoinFlow.Uid, + "amount": userVirtualCoinFlow.Amout, + "before_amount": userVirtualCoinFlow.BeforeAmout, + "after_amount": userVirtualCoinFlow.AfterAmout, + "coin_id": userVirtualCoinFlow.CoinId, + })) + return errors.New("用户" + zhios_order_relate_utils.IntToStr(userVirtualCoinFlow.Uid) + "的" + coin.Name + "不足") + //userVirtualCoinFlow.AfterAmout = "0" + } + //3、插入 `user_virtual_coin_flow` 记录 + _, err = db.UserVirtualCoinFlowInsert(session, &userVirtualCoinFlow) + if err != nil { + return err + } + + //4、修改 `user_virtual_amount`的amount值 && 及缓存 + err = svc.SetCacheUserVirtualAmount(session, req.Mid, userVirtualCoinFlow.AfterAmout, req.CoinId, req.Uid, true) + if err != nil { + return err + } + + return nil +}