diff --git a/db/db_niu_bei_coin_basic.go b/db/db_niu_bei_coin_basic.go new file mode 100644 index 0000000..77bf4bc --- /dev/null +++ b/db/db_niu_bei_coin_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" +) + +// BatchSelectNiuBeiCoinBasics 批量查询数据 TODO::和下面的方法重复了,建议采用下面的 `NiuBeiCoinBasicFindByParams` 方法 +func BatchSelectNiuBeiCoinBasics(session *xorm.Session, params map[string]interface{}) (*[]model.NiuBeiCoinBasic, error) { + var NiuBeiCoinBasicData []model.NiuBeiCoinBasic + if err := session.In(zhios_order_relate_utils.AnyToString(params["key"]), params["value"]). + Find(&NiuBeiCoinBasicData); err != nil { + return nil, zhios_order_relate_logx.Warn(err) + } + return &NiuBeiCoinBasicData, nil +} + +// NiuBeiCoinBasicInsert 插入单条数据 +func NiuBeiCoinBasicInsert(Db *xorm.Engine, NiuBeiCoinBasic *model.NiuBeiCoinBasic) (int, error) { + _, err := Db.InsertOne(NiuBeiCoinBasic) + if err != nil { + return 0, err + } + return NiuBeiCoinBasic.Id, nil +} + +// BatchAddNiuBeiCoinBasics 批量新增数据 +func BatchAddNiuBeiCoinBasics(Db *xorm.Engine, NiuBeiCoinBasicData []*model.NiuBeiCoinBasic) (int64, error) { + affected, err := Db.Insert(NiuBeiCoinBasicData) + if err != nil { + return 0, err + } + return affected, nil +} + +func GetNiuBeiCoinBasicCount(Db *xorm.Engine) int { + var NiuBeiCoinBasic model.NiuBeiCoinBasic + session := Db.Where("") + count, err := session.Count(&NiuBeiCoinBasic) + if err != nil { + return 0 + } + return int(count) +} + +// NiuBeiCoinBasicDelete 删除记录 +func NiuBeiCoinBasicDelete(Db *xorm.Engine, id interface{}) (int64, error) { + if reflect.TypeOf(id).Kind() == reflect.Slice { + return Db.In("id", id).Delete(model.NiuBeiCoinBasic{}) + } else { + return Db.Where("id = ?", id).Delete(model.NiuBeiCoinBasic{}) + } +} + +// NiuBeiCoinBasicUpdate 更新记录 +func NiuBeiCoinBasicUpdate(session *xorm.Session, id interface{}, NiuBeiCoinBasic *model.NiuBeiCoinBasic, forceColums ...string) (int64, error) { + var ( + affected int64 + err error + ) + if forceColums != nil { + affected, err = session.Where("id=?", id).Cols(forceColums...).Update(NiuBeiCoinBasic) + } else { + affected, err = session.Where("id=?", id).Update(NiuBeiCoinBasic) + } + if err != nil { + return 0, err + } + return affected, nil +} + +// NiuBeiCoinBasicGetOneByParams 通过传入的参数查询数据(单条) +func NiuBeiCoinBasicGetOneByParams(session *xorm.Session, params map[string]interface{}) (*model.NiuBeiCoinBasic, error) { + var m model.NiuBeiCoinBasic + 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 +} + +// NiuBeiCoinBasicFindByParams 通过传入的参数查询数据(多条) +func NiuBeiCoinBasicFindByParams(Db *xorm.Engine, params map[string]interface{}) (*[]model.NiuBeiCoinBasic, error) { + var m []model.NiuBeiCoinBasic + 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 NiuBeiCoinBasicFindByParamsByPage(Db *xorm.Engine, params map[string]interface{}, page, pageSize int) (*[]model.NiuBeiCoinBasic, error) { + var m []model.NiuBeiCoinBasic + 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_niu_bei_coin_user_with_ord.go b/db/db_niu_bei_coin_user_with_ord.go new file mode 100644 index 0000000..56f2d00 --- /dev/null +++ b/db/db_niu_bei_coin_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" +) + +// BatchSelectNiuBeiCoinUserWithOrds 批量查询数据 TODO::和下面的方法重复了,建议采用下面的 `NiuBeiCoinUserWithOrdFindByParams` 方法 +func BatchSelectNiuBeiCoinUserWithOrds(Db *xorm.Engine, params map[string]interface{}) (*[]model.NiuBeiCoinUserWithOrd, error) { + var NiuBeiCoinUserWithOrdData []model.NiuBeiCoinUserWithOrd + if err := Db.In(zhios_order_relate_utils.AnyToString(params["key"]), params["value"]). + Find(&NiuBeiCoinUserWithOrdData); err != nil { + return nil, zhios_order_relate_logx.Warn(err) + } + return &NiuBeiCoinUserWithOrdData, nil +} + +// NiuBeiCoinUserWithOrdInsert 插入单条数据 +func NiuBeiCoinUserWithOrdInsert(Db *xorm.Engine, NiuBeiCoinUserWithOrd *model.NiuBeiCoinUserWithOrd) (int, error) { + _, err := Db.InsertOne(NiuBeiCoinUserWithOrd) + if err != nil { + return 0, err + } + return NiuBeiCoinUserWithOrd.Id, nil +} + +// BatchAddNiuBeiCoinUserWithOrds 批量新增数据 +func BatchAddNiuBeiCoinUserWithOrds(Db *xorm.Engine, NiuBeiCoinUserWithOrdData []*model.NiuBeiCoinUserWithOrd) (int64, error) { + affected, err := Db.Insert(NiuBeiCoinUserWithOrdData) + if err != nil { + return 0, err + } + return affected, nil +} + +func GetNiuBeiCoinUserWithOrdCount(Db *xorm.Engine) int { + var NiuBeiCoinUserWithOrd model.NiuBeiCoinUserWithOrd + session := Db.Where("") + count, err := session.Count(&NiuBeiCoinUserWithOrd) + if err != nil { + return 0 + } + return int(count) +} + +// NiuBeiCoinUserWithOrdDelete 删除记录 +func NiuBeiCoinUserWithOrdDelete(Db *xorm.Engine, id interface{}) (int64, error) { + if reflect.TypeOf(id).Kind() == reflect.Slice { + return Db.In("id", id).Delete(model.NiuBeiCoinUserWithOrd{}) + } else { + return Db.Where("id = ?", id).Delete(model.NiuBeiCoinUserWithOrd{}) + } +} + +// NiuBeiCoinUserWithOrdUpdate 更新记录 +func NiuBeiCoinUserWithOrdUpdate(session *xorm.Session, id interface{}, NiuBeiCoinUserWithOrd *model.NiuBeiCoinUserWithOrd, forceColums ...string) (int64, error) { + var ( + affected int64 + err error + ) + if forceColums != nil { + affected, err = session.Where("id=?", id).Cols(forceColums...).Update(NiuBeiCoinUserWithOrd) + } else { + affected, err = session.Where("id=?", id).Update(NiuBeiCoinUserWithOrd) + } + if err != nil { + return 0, err + } + return affected, nil +} + +// NiuBeiCoinUserWithOrdGetOneByParams 通过传入的参数查询数据(单条) +func NiuBeiCoinUserWithOrdGetOneByParams(session *xorm.Session, params map[string]interface{}) (*model.NiuBeiCoinUserWithOrd, error) { + var m model.NiuBeiCoinUserWithOrd + 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 +} + +// NiuBeiCoinUserWithOrdFindByParams 通过传入的参数查询数据(多条) +func NiuBeiCoinUserWithOrdFindByParams(session *xorm.Session, params map[string]interface{}) (*[]model.NiuBeiCoinUserWithOrd, error) { + var m []model.NiuBeiCoinUserWithOrd + 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 NiuBeiCoinUserWithOrdFindByParamsByPage(Db *xorm.Engine, params map[string]interface{}, page, pageSize int) (*[]model.NiuBeiCoinUserWithOrd, error) { + var m []model.NiuBeiCoinUserWithOrd + 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_niu_bei_coin_user_with_ord_flow.go b/db/db_niu_bei_coin_user_with_ord_flow.go new file mode 100644 index 0000000..4d9f047 --- /dev/null +++ b/db/db_niu_bei_coin_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" +) + +// BatchSelectNiuBeiCoinUserWithOrdFlows 批量查询数据 TODO::和下面的方法重复了,建议采用下面的 `NiuBeiCoinUserWithOrdFlowFindByParams` 方法 +func BatchSelectNiuBeiCoinUserWithOrdFlows(Db *xorm.Engine, params map[string]interface{}) (*[]model.NiuBeiCoinUserWithOrdFlow, error) { + var NiuBeiCoinUserWithOrdFlowData []model.NiuBeiCoinUserWithOrdFlow + if err := Db.In(zhios_order_relate_utils.AnyToString(params["key"]), params["value"]). + Find(&NiuBeiCoinUserWithOrdFlowData); err != nil { + return nil, zhios_order_relate_logx.Warn(err) + } + return &NiuBeiCoinUserWithOrdFlowData, nil +} + +// NiuBeiCoinUserWithOrdFlowInsert 插入单条数据 +func NiuBeiCoinUserWithOrdFlowInsert(session *xorm.Session, NiuBeiCoinUserWithOrdFlow *model.NiuBeiCoinUserWithOrdFlow) (int, error) { + _, err := session.InsertOne(NiuBeiCoinUserWithOrdFlow) + if err != nil { + return 0, err + } + return NiuBeiCoinUserWithOrdFlow.Id, nil +} + +// BatchAddNiuBeiCoinUserWithOrdFlows 批量新增数据 +func BatchAddNiuBeiCoinUserWithOrdFlows(Db *xorm.Engine, NiuBeiCoinUserWithOrdFlowData []*model.NiuBeiCoinUserWithOrdFlow) (int64, error) { + affected, err := Db.Insert(NiuBeiCoinUserWithOrdFlowData) + if err != nil { + return 0, err + } + return affected, nil +} + +func GetNiuBeiCoinUserWithOrdFlowCount(Db *xorm.Engine) int { + var NiuBeiCoinUserWithOrdFlow model.NiuBeiCoinUserWithOrdFlow + session := Db.Where("") + count, err := session.Count(&NiuBeiCoinUserWithOrdFlow) + if err != nil { + return 0 + } + return int(count) +} + +// NiuBeiCoinUserWithOrdFlowDelete 删除记录 +func NiuBeiCoinUserWithOrdFlowDelete(Db *xorm.Engine, id interface{}) (int64, error) { + if reflect.TypeOf(id).Kind() == reflect.Slice { + return Db.In("id", id).Delete(model.NiuBeiCoinUserWithOrdFlow{}) + } else { + return Db.Where("id = ?", id).Delete(model.NiuBeiCoinUserWithOrdFlow{}) + } +} + +// NiuBeiCoinUserWithOrdFlowUpdate 更新记录 +func NiuBeiCoinUserWithOrdFlowUpdate(session *xorm.Session, id interface{}, NiuBeiCoinUserWithOrdFlow *model.NiuBeiCoinUserWithOrdFlow, forceColums ...string) (int64, error) { + var ( + affected int64 + err error + ) + if forceColums != nil { + affected, err = session.Where("id=?", id).Cols(forceColums...).Update(NiuBeiCoinUserWithOrdFlow) + } else { + affected, err = session.Where("id=?", id).Update(NiuBeiCoinUserWithOrdFlow) + } + if err != nil { + return 0, err + } + return affected, nil +} + +// NiuBeiCoinUserWithOrdFlowGetOneByParams 通过传入的参数查询数据(单条) +func NiuBeiCoinUserWithOrdFlowGetOneByParams(session *xorm.Session, params map[string]interface{}) (*model.NiuBeiCoinUserWithOrdFlow, error) { + var m model.NiuBeiCoinUserWithOrdFlow + 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 +} + +// NiuBeiCoinUserWithOrdFlowFindByParams 通过传入的参数查询数据(多条) +func NiuBeiCoinUserWithOrdFlowFindByParams(Db *xorm.Engine, params map[string]interface{}) (*[]model.NiuBeiCoinUserWithOrdFlow, error) { + var m []model.NiuBeiCoinUserWithOrdFlow + 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 NiuBeiCoinUserWithOrdFlowFindByParamsByPage(Db *xorm.Engine, params map[string]interface{}, page, pageSize int) (*[]model.NiuBeiCoinUserWithOrdFlow, error) { + var m []model.NiuBeiCoinUserWithOrdFlow + 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/niu_bei_coin_basic.go b/db/model/niu_bei_coin_basic.go new file mode 100644 index 0000000..f1ce9e9 --- /dev/null +++ b/db/model/niu_bei_coin_basic.go @@ -0,0 +1,23 @@ +package model + +import ( + "time" +) + +type NiuBeiCoinBasic 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)"` + Coin3 int `json:"coin_3" xorm:"not null default 0 comment('coinId_3(作用于抵扣劵)') INT(11)"` + Coin4 int `json:"coin_4" xorm:"not null default 0 comment('coinId_4(作用于期权激励)') INT(11)"` + TotalCoinReleaseRatio string `json:"total_coin_release_ratio" xorm:"not null default 0.0000 comment('总积分释放比例') DECIMAL(10,4)"` + DailyConsumeCoinReleaseRatio string `json:"daily_consume_coin_release_ratio" xorm:"not null default 0.0000 comment('每日消费积分释放比例') DECIMAL(10,4)"` + DailyNiubeiCoinReleaseRatio string `json:"daily_niubei_coin_release_ratio" xorm:"not null default 0.0000 comment('每日牛贝积分释放比例') DECIMAL(10,4)"` + DailyCouponCoinReleaseRatio string `json:"daily_coupon_coin_release_ratio" xorm:"not null default 0.0000 comment('每日抵扣劵释放比例') DECIMAL(10,4)"` + CouponConsumeDeductionRatio string `json:"coupon_consume_deduction_ratio" 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/niu_bei_coin_user_with_ord.go b/db/model/niu_bei_coin_user_with_ord.go new file mode 100644 index 0000000..ec23074 --- /dev/null +++ b/db/model/niu_bei_coin_user_with_ord.go @@ -0,0 +1,20 @@ +package model + +import ( + "time" +) + +type NiuBeiCoinUserWithOrd 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('平台') TINYINT(3)"` + TotalConsumeCoinNum string `json:"total_consume_coin_num" xorm:"not null default 0.0000 comment('总消费积分数量') DECIMAL(10,4)"` + ReleasedNiubeiCoinNum string `json:"released_niubei_coin_num" xorm:"not null default 0.0000 comment('已释放牛贝积分数量') DECIMAL(10,4)"` + ReleasedCouponCoinNum string `json:"released_coupon_coin_num" xorm:"not null default 0.0000 comment('已释放抵扣劵数量') DECIMAL(10,4)"` + ReleasedOptionCoinNum string `json:"released_option_coin_num" xorm:"not null default 0.0000 comment('已释放期权积分数量') DECIMAL(10,4)"` + ReleasedRatio string `json:"released_ratio" xorm:"not null default 0.0000 comment('已释放百分比') DECIMAL(10,4)"` + IsReleasedFinish int `json:"is_released_finish" xorm:"not null default 0 comment('是否释放完毕(0:未释放完 1:释放完毕)') 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/niu_bei_coin_user_with_ord_flow.go b/db/model/niu_bei_coin_user_with_ord_flow.go new file mode 100644 index 0000000..a98c627 --- /dev/null +++ b/db/model/niu_bei_coin_user_with_ord_flow.go @@ -0,0 +1,17 @@ +package model + +import ( + "time" +) + +type NiuBeiCoinUserWithOrdFlow 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)"` + ReleaseNiubeiCoinNum string `json:"release_niubei_coin_num" xorm:"not null default 0.0000 comment('释放牛贝积分数量') DECIMAL(10,4)"` + ReleaseCouponCoinNum string `json:"release_coupon_coin_num" xorm:"not null default 0.0000 comment('释放抵扣劵数量') DECIMAL(10,4)"` + ReleaseOptionCoinNum string `json:"release_option_coin_num" 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/block_star_chain.go b/md/block_star_chain.go index 75ca59c..3e1e16d 100644 --- a/md/block_star_chain.go +++ b/md/block_star_chain.go @@ -50,6 +50,13 @@ const ( IntegralReleaseGreenCoinGrantByRefundForUserVirtualCoinFlow = "订单退款-绿积分扣除" IntegralReleaseGreenCoinGrantByOrdTitleForUserVirtualCoinFlow = "积分释放-绿积分发放(订单)" IntegralReleaseGreenCoinGrantBySignInTitleForUserVirtualCoinFlow = "积分释放-绿积分发放(签到)" + + NiuBeiCoinByReleaseNiuBeiCoinTitleForUserVirtualCoinFlow = "牛贝积分-释放牛贝积分" + NiuBeiCoinByReleaseNiuBeiDestroyConsumeCoinNumTitleForUserVirtualCoinFlow = "释放牛贝积分-消耗消费积分数量" + NiuBeiCoinByReleaseCouponCoinNumTitleForUserVirtualCoinFlow = "牛贝积分-释放抵扣劵" + NiuBeiCoinByReleaseCouponDestroyConsumeCoinNumTitleForUserVirtualCoinFlow = "释放抵扣劵数量-消耗消费积分数量" + NiuBeiCoinByReleaseOptionCoinNumTitleForUserVirtualCoinFlow = "牛贝积分-释放期权积分" + NiuBeiCoinByReleaseOptionDestroyConsumeCoinNumTitleForUserVirtualCoinFlow = "释放期权积分数量-消耗消费积分数量" ) const ( @@ -93,6 +100,12 @@ const ( IntegralReleaseRedCoinGrantByOrdTransferTypeDeductForUserVirtualCoinFlow = 141 // 积分释放-红积分发放(订单) IntegralReleaseRedCoinGrantBySignInTransferTypeDeductForUserVirtualCoinFlow = 142 // 积分释放-红积分发放(签到) + NiuBeiCoinByReleaseNiuBeiCoinTransferTypeForUserVirtualCoinFlow = 145 //牛贝积分-释放牛贝积分 + NiuBeiCoinByReleaseNiuBeiDestroyConsumeCoinNumTransferTypeForUserVirtualCoinFlow = 146 //释放牛贝积分-消耗消费积分数量 + NiuBeiCoinByReleaseCouponCoinNumTransferTypeForUserVirtualCoinFlow = 147 //牛贝积分-释放抵扣劵 + NiuBeiCoinByReleaseCouponDestroyConsumeCoinNumTransferTypeForUserVirtualCoinFlow = 148 //释放抵扣劵数量-消耗消费积分数量 + NiuBeiCoinByReleaseOptionCoinNumTransferTypeForUserVirtualCoinFlow = 149 //牛贝积分-释放期权积分 + NiuBeiCoinByReleaseOptionDestroyConsumeCoinNumTransferTypeForUserVirtualCoinFlow = 150 //释放期权积分数量-消耗消费积分数量 ) const DealUserCoinRequestIdPrefix = "%s:block_star_chain_deal_user_coin:%d:uid:%d" diff --git a/rule/niu_bei_release_settlement.go b/rule/niu_bei_release_settlement.go new file mode 100644 index 0000000..d61b17e --- /dev/null +++ b/rule/niu_bei_release_settlement.go @@ -0,0 +1,376 @@ +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" + "errors" + "fmt" + "github.com/shopspring/decimal" + "strconv" + "time" + "xorm.io/xorm" +) + +func InitForNiuBeiRelease(redisAddr string) (err error) { + if redisAddr != "" { + cache.NewRedis(redisAddr) + } + _, err = cache.SelectDb(md.RedisDataBase) + return +} + +const PessimismLockKeyForNiuBeiRelease = "daily_settlement_niu_bei_release_pessimism_lock_key" +const PessimismLockValueForNiuBeiRelease = "running" +const DealUserCoinForNiuBeiReleaseRequestIdPrefix = "%s:integral_release_deal_user_coin:%d:uid:%d" + +// DailySettlementNiuBeiRelease 每日结算“牛贝牛贝积分释放” +func DailySettlementNiuBeiRelease(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` 基础设置 + niuBeiRelease, err := db.NiuBeiCoinBasicGetOneByParams(session, map[string]interface{}{ + "key": "is_use", + "value": 1, + }) + if err != nil { + _ = session.Rollback() + return err + } + if niuBeiRelease.ReleaseDate == today { + _ = session.Rollback() + return errors.New("今日“牛贝积分释放”已结算") + } + + //TODO::增加“悲观锁”防止串行 + getString, _ := cache.GetString(PessimismLockKeyForNiuBeiRelease) + if getString == PessimismLockValueForNiuBeiRelease { + fmt.Println("-----------------", "上一次结算未执行完") + return errors.New("上一次结算未执行完") + } + cache.SetEx(PessimismLockKeyForNiuBeiRelease, PessimismLockValueForNiuBeiRelease, 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、查询所有 `niu_bei_coin_user_with_ord` 记录 + niuBeiReleaseUserWithOrds, err := db.NiuBeiCoinUserWithOrdFindByParams(session, map[string]interface{}{ + "key": "is_released_finish", + "value": 0, + }) + for _, ord := range *niuBeiReleaseUserWithOrds { + //3、计算释放 + err, resp := CalcNiuBeiReleaseNums(session, niuBeiRelease, ord.TotalConsumeCoinNum, ord.ReleasedRatio) + if err != nil { + _ = session.Rollback() + return err + } + + //4、插入 `niu_bei_coin_user_with_ord_flow` 记录 + var niuBeiCoinUserWithOrdFlow = model.NiuBeiCoinUserWithOrdFlow{ + Uid: ord.Uid, + OrdId: ord.OrdId, + ReleaseNiubeiCoinNum: resp.ReleaseOptionCoinNum, + ReleaseCouponCoinNum: resp.ReleaseCouponCoinNum, + ReleaseOptionCoinNum: resp.ReleaseOptionCoinNum, + ReleaseDate: today, + CreateAt: now, + UpdateAt: now, + } + _, err = db.NiuBeiCoinUserWithOrdFlowInsert(session, &niuBeiCoinUserWithOrdFlow) + if err != nil { + _ = session.Rollback() + return err + } + + //5、处理积分释放 + err = ReleaseNiuBeiCoinForUser(session, mid, niuBeiRelease, &ord, resp) + 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(PessimismLockKeyForNiuBeiRelease) + + fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>牛贝积分释放结束<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<") + return nil +} + +type CalcNiuBeiReleaseResp struct { + ReleaseNiuBeiCoinNum string // 释放牛贝积分数量 + ReleaseNiuBeiDestroyConsumeCoinNum string // 释放牛贝积分-消耗消费积分数量 + ReleaseCouponCoinNum string // 释放抵扣劵数量 + ReleaseCouponDestroyConsumeCoinNum string // 释放抵扣劵数量-消耗消费积分数量 + ReleaseOptionCoinNum string // 释放期权积分数量 + ReleaseOptionDestroyConsumeCoinNum string // 释放期权积分数量-消耗消费积分数量 + IsReleasedFinish bool // 是否释放完毕 +} + +// CalcNiuBeiReleaseNums 计算释放释放数量 +func CalcNiuBeiReleaseNums(session *xorm.Session, niuBeiCoinBasic *model.NiuBeiCoinBasic, totalConsumeCoinNumValue, releasedRatioValue string) (err error, resp CalcNiuBeiReleaseResp) { + totalCoinReleaseRatio, _ := decimal.NewFromString(niuBeiCoinBasic.TotalCoinReleaseRatio) //总积分释放比例 + dailyConsumeCoinReleaseRatio, _ := decimal.NewFromString(niuBeiCoinBasic.DailyConsumeCoinReleaseRatio) //每日消费积分释放比例 + dailyNiubeiCoinReleaseRatio, _ := decimal.NewFromString(niuBeiCoinBasic.DailyNiubeiCoinReleaseRatio) //每日牛贝积分释放比例 + dailyCouponCoinReleaseRatio, _ := decimal.NewFromString(niuBeiCoinBasic.DailyCouponCoinReleaseRatio) //每日抵扣劵释放比例 + totalConsumeCoinNum, _ := decimal.NewFromString(totalConsumeCoinNumValue) //总消费积分数量 + releasedRatio, _ := decimal.NewFromString(releasedRatioValue) //已释放百分比 + percentageValue := decimal.NewFromFloat(100) + totalCoinRelease := dailyConsumeCoinReleaseRatio.Div(percentageValue).Mul(totalConsumeCoinNum) //总释放积分 + + //1、获取各积分的兑换比例 + var coinOne, coinTwo, coinThree, coinFour model.VirtualCoin + var coinOneValue, coinTwoValue, coinThreeValue, coinFourValue decimal.Decimal + has, err := session.Where("id = ?", niuBeiCoinBasic.Coin1).Get(&coinOne) + if err != nil { + return err, resp + } + if has == false { + return errors.New("未查询到相应的 消费积分 记录"), resp + } + coinOneValue, _ = decimal.NewFromString(coinOne.ExchangeRatio) + + has, err = session.Where("id = ?", niuBeiCoinBasic.Coin1).Get(&coinTwo) + if err != nil { + return err, resp + } + if has == false { + return errors.New("未查询到相应的 牛贝积分 记录"), resp + } + coinTwoValue, _ = decimal.NewFromString(coinTwo.ExchangeRatio) + + has, err = session.Where("id = ?", niuBeiCoinBasic.Coin1).Get(&coinThree) + if err != nil { + return err, resp + } + if has == false { + return errors.New("未查询到相应的 抵扣劵 记录"), resp + } + coinThreeValue, _ = decimal.NewFromString(coinThree.ExchangeRatio) + + has, err = session.Where("id = ?", niuBeiCoinBasic.Coin1).Get(&coinFour) + if err != nil { + return err, resp + } + if has == false { + return errors.New("未查询到相应的 期权 记录"), resp + } + coinFourValue, _ = decimal.NewFromString(coinFour.ExchangeRatio) + + //2、计算各个积分 (TODO:: 总释放积分 * 每日牛贝积分释放比例 * (消费积分兑换比例 / 牛贝积分兑换比例)) + resp.ReleaseNiuBeiCoinNum = totalCoinRelease.Mul(dailyNiubeiCoinReleaseRatio.Mul(percentageValue)).Mul(coinOneValue.Div(coinTwoValue)).String() + resp.ReleaseNiuBeiDestroyConsumeCoinNum = totalCoinRelease.Mul(dailyNiubeiCoinReleaseRatio.Mul(percentageValue)).Mul(coinOneValue).String() + + resp.ReleaseCouponCoinNum = totalCoinRelease.Mul(dailyCouponCoinReleaseRatio.Mul(percentageValue)).Mul(coinOneValue.Div(coinThreeValue)).String() + resp.ReleaseCouponDestroyConsumeCoinNum = totalCoinRelease.Mul(dailyCouponCoinReleaseRatio.Mul(percentageValue)).Mul(coinOneValue).String() + + resp.IsReleasedFinish = releasedRatio.Add(dailyConsumeCoinReleaseRatio).GreaterThanOrEqual(totalCoinReleaseRatio) + if resp.IsReleasedFinish { + //TODO:: (总消费积分数量 * (100 - 已释放百分比) / 100) *(消费积分兑换比例 / 牛贝积分兑换比例) + resp.ReleaseOptionCoinNum = totalConsumeCoinNum.Mul(decimal.NewFromFloat(100).Sub(releasedRatio.Add(dailyConsumeCoinReleaseRatio)).Div(percentageValue)).Mul(coinOneValue.Div(coinFourValue)).String() + resp.ReleaseOptionDestroyConsumeCoinNum = totalConsumeCoinNum.Mul(decimal.NewFromFloat(100).Sub(releasedRatio.Add(dailyConsumeCoinReleaseRatio)).Div(percentageValue)).Mul(coinOneValue).String() + } + return nil, resp +} + +// ReleaseNiuBeiCoinForUser 释放用户积分 +func ReleaseNiuBeiCoinForUser(session *xorm.Session, mid string, niuBeiRelease *model.NiuBeiCoinBasic, ord *model.NiuBeiCoinUserWithOrd, resp CalcNiuBeiReleaseResp) (err error) { + //1、牛贝积分-释放牛贝积分 + err = DealUserCoinForNiuBeiRelease(session, md.DealUserCoinReq{ + Kind: "add", + Mid: mid, + Title: md.NiuBeiCoinByReleaseNiuBeiCoinTitleForUserVirtualCoinFlow, + TransferType: md.NiuBeiCoinByReleaseNiuBeiCoinTransferTypeForUserVirtualCoinFlow, + OrdId: ord.OrdId, + CoinId: niuBeiRelease.Coin2, + Uid: ord.Uid, + Amount: zhios_order_relate_utils.StrToFloat64(resp.ReleaseNiuBeiCoinNum), + }) + if err != nil { + return err + } + + //2、释放牛贝积分-消耗消费积分数量 + err = DealUserCoinForNiuBeiRelease(session, md.DealUserCoinReq{ + Kind: "sub", + Mid: mid, + Title: md.NiuBeiCoinByReleaseNiuBeiDestroyConsumeCoinNumTitleForUserVirtualCoinFlow, + TransferType: md.NiuBeiCoinByReleaseNiuBeiDestroyConsumeCoinNumTransferTypeForUserVirtualCoinFlow, + OrdId: ord.OrdId, + CoinId: niuBeiRelease.Coin1, + Uid: ord.Uid, + Amount: zhios_order_relate_utils.StrToFloat64(resp.ReleaseNiuBeiDestroyConsumeCoinNum), + }) + if err != nil { + return err + } + + //3、牛贝积分-释放抵扣劵 + err = DealUserCoinForNiuBeiRelease(session, md.DealUserCoinReq{ + Kind: "add", + Mid: mid, + Title: md.NiuBeiCoinByReleaseCouponCoinNumTitleForUserVirtualCoinFlow, + TransferType: md.NiuBeiCoinByReleaseCouponCoinNumTransferTypeForUserVirtualCoinFlow, + OrdId: ord.OrdId, + CoinId: niuBeiRelease.Coin2, + Uid: ord.Uid, + Amount: zhios_order_relate_utils.StrToFloat64(resp.ReleaseCouponCoinNum), + }) + if err != nil { + return err + } + //4、释放抵扣劵数量-消耗消费积分数量 + err = DealUserCoinForNiuBeiRelease(session, md.DealUserCoinReq{ + Kind: "sub", + Mid: mid, + Title: md.NiuBeiCoinByReleaseCouponDestroyConsumeCoinNumTitleForUserVirtualCoinFlow, + TransferType: md.NiuBeiCoinByReleaseCouponDestroyConsumeCoinNumTransferTypeForUserVirtualCoinFlow, + OrdId: ord.OrdId, + CoinId: niuBeiRelease.Coin2, + Uid: ord.Uid, + Amount: zhios_order_relate_utils.StrToFloat64(resp.ReleaseCouponDestroyConsumeCoinNum), + }) + if err != nil { + return err + } + + //5、牛贝积分-释放期权积分 + err = DealUserCoinForNiuBeiRelease(session, md.DealUserCoinReq{ + Kind: "add", + Mid: mid, + Title: md.NiuBeiCoinByReleaseOptionCoinNumTitleForUserVirtualCoinFlow, + TransferType: md.NiuBeiCoinByReleaseOptionCoinNumTransferTypeForUserVirtualCoinFlow, + OrdId: ord.OrdId, + CoinId: niuBeiRelease.Coin2, + Uid: ord.Uid, + Amount: zhios_order_relate_utils.StrToFloat64(resp.ReleaseOptionCoinNum), + }) + if err != nil { + return err + } + + //6、释放期权积分数量-消耗消费积分数量 + err = DealUserCoinForNiuBeiRelease(session, md.DealUserCoinReq{ + Kind: "sub", + Mid: mid, + Title: md.NiuBeiCoinByReleaseOptionDestroyConsumeCoinNumTitleForUserVirtualCoinFlow, + TransferType: md.NiuBeiCoinByReleaseOptionDestroyConsumeCoinNumTransferTypeForUserVirtualCoinFlow, + OrdId: ord.OrdId, + CoinId: niuBeiRelease.Coin2, + Uid: ord.Uid, + Amount: zhios_order_relate_utils.StrToFloat64(resp.ReleaseOptionDestroyConsumeCoinNum), + }) + if err != nil { + return err + } + return nil +} + +// DealUserCoinForNiuBeiRelease 处理给用户虚拟币积分 +func DealUserCoinForNiuBeiRelease(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 +}