| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- package balance_model
- import (
- "fmt"
- "strings"
- "time"
- "github.com/astaxie/beego"
- "github.com/astaxie/beego/orm"
- "github.com/uuid"
- // "fohow.com/apps/shop"
- // "fohow.com/cache"
- // "fohow.com/apps/models/user_model"
- // "fohow.com/apps/models/vas_model"
- "fohow.com/cache"
- // "fohow.com/libs/wx_mp"
- )
- const (
- balances_tablename = "balances"
- // balance_hitories_tablename = "d5c_balance_hitories"
- balance_orders_tablename = "balance_orders"
- // BALANCE_SERVICE_CHARGE = 100 //手续费
- BALANCE_UPGRADE = int64(420000) //升级代金券
- BALANCE_PAIED = int64(300000) //升级支付金额
- BALANCE_BL = int64(8) //群主推荐比例
- RECHARGE_BALANCE_BL = int64(85) //群主优惠比例
- AWARD_UPGRADE = int64(50000) //升级群主奖励
- ORDER_ID_PREFIX_CZ = "CZ" //充值
- BALANCE_SOURCE_PLATFORM_EXCHANGE = "platform_exchange" //企业大使兑换拉比代金券
- BALANCE_SOURCE_PLATFORM_EXCHANGE_NAME = "企业大使兑换"
- BALANCE_SOURCE_RECHARGE = "recharge" //充值
- BALANCE_SOURCE_RECHARGE_NAME = "充值"
- BALANCE_SOURCE_EXCHANGE_PRODUCT = "exchange_product" //拉比代金券兑换商品
- BALANCE_SOURCE_EXCHANGE_PRODUCT_NAME = "兑换商品"
- BALANCE_SOURCE_ALL_REFUNDED = "all_refunded"
- BALANCE_SOURCE_ALL_REFUNDED_NAME = "全额退款"
- BALANCE_SOURCE_PART_REFUNDED = "part_refunded"
- BALANCE_SOURCE_PART_REFUNDED_NAME = "部分退款"
- SHOP_APPYCATION = "shop_applycation"
- SHOP_APPYCATION_NAME = "店铺申请"
- BALANCE_FREND_BUY = "frend_buy"
- BALANCE_FREND_BUY_NAME = "群员购物"
- BALANCE_SOURCE_DEDUCT = "deduct"
- BALANCE_SOURCE_DEDUCT_NAME = "扣除"
- PAY_WAY_TYPE_SERVICE_WXPAY = "service_wxpay"
- )
- type Balance struct {
- Id int64 `orm:"column(id);pk" json:"id"` // int(11)
- UserId int64 `orm:"column(user_id);null" json:"user_id"` // int(11)
- WxUserId int64 `orm:"column(wx_user_id);null" json:"wx_user_id"` // int(11)
- Count int64 `orm:"column(count);null" json:"count"` // int(11)
- Source string `orm:"column(source);null" json:"source"` // varchar(20)
- SourceName string `orm:"-" json:"source_name"` // varchar(20)
- RelateId string `orm:"column(relate_id);null" json:"relate_id"` // varchar(255)
- Remark string `orm:"column(remark);null" json:"remark"` // varchar(255)
- CreatedAt time.Time `orm:"column(created_at);auto_now_add;type(datetime)" json:"-"` // datetime
- CTime int64 `orm:"-" json:"ctime"` // datetime
- UpdatedAt time.Time `orm:"column(updated_at);auto_now;type(datetime)" json:"-"` // datetime
- }
- func (self *Balance) TableName() string {
- return balances_tablename
- }
- type BalanceOrder struct {
- Id int64 `orm:"column(id);pk" json:"id"` // int(11)
- OrderId string `orm:"column(order_id)" json:"order_id"` // varchar(64)
- UserId int64 `orm:"column(user_id)" json:"user_id"` // int(11)
- WxUserId int64 `orm:"column(wx_user_id);null" json:"wx_user_id"` // int(11)
- PayWay string `orm:"column(pay_way);null" json:"pay_way"` // varchar(20)
- TradeNo string `orm:"column(trade_no);null" json:"-"` // varchar(64)
- PaiedAt int64 `orm:"column(paied_at);null" json:"paied_at"` // datetime
- TotalPrice int64 `orm:"column(total_price);null" json:"total_price"` // int(11)
- PaiedPrice int64 `orm:"column(paied_price);null" json:"paied_price"` // int(11)
- State int64 `orm:"column(state);null" json:"state"` // tinyint(1)
- Remark string `orm:"column(remark);null" json:"remark"` // varchar(64)
- CreatedAt time.Time `orm:"column(created_at);auto_now_add;type(datetime)" json:"-"` // datetime
- UpdatedAt time.Time `orm:"column(updated_at);auto_now;type(datetime)" json:"-"` // datetime
- BalanceBankCardNo string `orm:"column(balance_bank_card_no);null" json:"balance_bank_card_no"`
- // 该字段是给连连支付认证支付时,卡前置存银行卡号用的
- BankCard string `orm:"-" json:"bank_card"` // datetime
- }
- func (self *BalanceOrder) TableName() string {
- return balance_orders_tablename
- }
- func (self *Balance) Create(wxUId, uId, c int64, s, rId, remark string) (item *Balance) {
- item = &Balance{
- Count: c,
- WxUserId: wxUId,
- UserId: uId,
- Source: s,
- RelateId: rId,
- Remark: remark,
- }
- id, err := orm.NewOrm().Insert(item)
- if err != nil {
- beego.BeeLogger.Error("Create Balance err=[%s]", err)
- return nil
- }
- item.Id = id
- return item
- }
- //返回source名称
- func (self *Balance) GetSourceName() string {
- sourceName := ""
- switch self.Source {
- case BALANCE_SOURCE_RECHARGE:
- sourceName = BALANCE_SOURCE_RECHARGE_NAME
- case BALANCE_SOURCE_PLATFORM_EXCHANGE:
- sourceName = BALANCE_SOURCE_PLATFORM_EXCHANGE_NAME
- case BALANCE_SOURCE_EXCHANGE_PRODUCT:
- sourceName = BALANCE_SOURCE_EXCHANGE_PRODUCT_NAME
- case BALANCE_SOURCE_ALL_REFUNDED:
- sourceName = BALANCE_SOURCE_ALL_REFUNDED_NAME
- case BALANCE_SOURCE_PART_REFUNDED:
- sourceName = BALANCE_SOURCE_PART_REFUNDED_NAME
- case BALANCE_SOURCE_DEDUCT:
- sourceName = BALANCE_SOURCE_DEDUCT_NAME
- case BALANCE_FREND_BUY:
- sourceName = BALANCE_FREND_BUY_NAME
- case SHOP_APPYCATION:
- sourceName = SHOP_APPYCATION_NAME
- }
- return sourceName
- }
- func GetBalanceById(id int64) *Balance {
- item := &Balance{}
- if err := orm.NewOrm().QueryTable(item).
- Filter("id", id).Limit(1).
- One(item); err != nil {
- beego.BeeLogger.Debug("GetBalanceById(%s), err=%s",
- id, err)
- return nil
- }
- return item
- }
- //获取某人的现金流列表
- func GetBalanceListByWxUId(wxUId, page, perPage int64, useCache bool) (list []*Balance) {
- k := fmt.Sprintf("balance_model.GetBalanceList.wxUId(%d).page(%d).perPage(%d)", wxUId, page, perPage)
- if useCache {
- if s, ok := cache.Cache.Get(k).([]*Balance); ok {
- return s
- }
- }
- balance := new(Balance)
- qs := orm.NewOrm().QueryTable(balance)
- qs = qs.Filter("wx_user_id", wxUId)
- if _, err := qs.OrderBy("-created_at", "-id").
- Limit(perPage, (page-1)*perPage).All(&list); err != nil {
- beego.BeeLogger.Debug("get balance with wxUId=%d, err=[%s]", wxUId, err)
- return nil
- }
- cache.Cache.Put(k, list, 10*time.Minute)
- return list
- }
- //获取某人的现金流列表总条数
- func GetBalanceCountByWxUId(wxUId int64) int64 {
- item := new(Balance)
- o := orm.NewOrm()
- count, _ := o.QueryTable(item).Filter("wx_user_id", wxUId).Count()
- return count
- }
- //获取某个用户的余额
- func GetUserTotalBalance(wxUId int64) int64 {
- type Ret struct {
- Count int64
- }
- ret := &Ret{}
- o := orm.NewOrm()
- tbn := new(Balance).TableName()
- sql := fmt.Sprintf("SELECT sum(`count`) as count FROM `%s` WHERE wx_user_id=?;", tbn)
- err := o.Raw(sql, wxUId).QueryRow(ret)
- if err != nil {
- beego.BeeLogger.Error("get user=[%d] total balance err=[%s]", wxUId, err)
- return 0
- }
- if ret.Count < 0 {
- return 0
- }
- return ret.Count
- }
- //获取某个source累计余额
- func GetBalanceCountBySource(wxUId int64, source string) int64 {
- type Ret struct {
- Count int64
- }
- ret := &Ret{}
- o := orm.NewOrm()
- tbn := new(Balance).TableName()
- sql := fmt.Sprintf("SELECT sum(`count`) as count FROM `%s` WHERE wx_user_id=? and source=?", tbn)
- err := o.Raw(sql, wxUId, source).QueryRow(ret)
- if err != nil {
- beego.BeeLogger.Error("get user=[%d] total balance err=[%s]", wxUId, err)
- return 0
- }
- if ret.Count < 0 {
- return 0
- }
- return ret.Count
- }
- func GetBalanceBySourceAndRId(source, rId string) *Balance {
- item := &Balance{}
- if err := orm.NewOrm().QueryTable(item).
- Filter("source", source).
- Filter("relate_id", rId).Limit(1).
- One(item); err != nil {
- beego.BeeLogger.Info("GetBalanceBySourceAndRId(%s,%s), err=%s",
- source, rId, err)
- return nil
- }
- return item
- }
- func GetBalanceByUIdAndRIdAndSource(uId int64, rId, source string) *Balance {
- item := &Balance{}
- if err := orm.NewOrm().QueryTable(item).
- Filter("user_id", uId).
- Filter("source", source).
- Filter("relate_id", rId).OrderBy("-created_at").Limit(1).
- One(item); err != nil {
- beego.BeeLogger.Info("GetBalanceByUIdAndRIdAndSource(%d,%s,%s), err=%s",
- uId, rId, source, err)
- return nil
- }
- return item
- }
- func GetBalanceOrderByOId(oId string, useCache bool) *BalanceOrder {
- k := fmt.Sprintf("balance_model.GetBalanceOrderByOId[%s]", oId)
- if useCache {
- if order, ok := cache.Cache.Get(k).(*BalanceOrder); ok {
- return order
- }
- }
- item := &BalanceOrder{}
- if err := orm.NewOrm().QueryTable(item).Filter("order_id", oId).Limit(1).
- One(item); err != nil {
- beego.BeeLogger.Info("GetBalanceOrderByOId(%s), err=%s", oId, err)
- return nil
- }
- cache.Cache.Put(k, item, 5*time.Minute)
- return item
- }
- func GetBuyBalanceByRelateId(rId string, useCache bool) *Balance {
- k := fmt.Sprintf("balance_model.GetBuyBalanceByRelateId[%s]", rId)
- if useCache {
- if order, ok := cache.Cache.Get(k).(*Balance); ok {
- return order
- }
- }
- //lt < 0
- item := &Balance{}
- if err := orm.NewOrm().QueryTable(item).Filter("relate_id", rId).Filter("count__lt", 0).Limit(1).
- One(item); err != nil {
- beego.BeeLogger.Info("GetBuyBalanceByRelateId(%s), err=%s", rId, err)
- return nil
- }
- cache.Cache.Put(k, item, 5*time.Minute)
- return item
- }
- func GetRefundBalanceByRelateId(rId string, useCache bool) *Balance {
- k := fmt.Sprintf("balance_model.GetRefundBalanceByRelateId[%s]", rId)
- if useCache {
- if order, ok := cache.Cache.Get(k).(*Balance); ok {
- return order
- }
- }
- //gt > 0
- item := &Balance{}
- if err := orm.NewOrm().QueryTable(item).Filter("relate_id", rId).Filter("count__gt", 0).Limit(1).
- One(item); err != nil {
- beego.BeeLogger.Info("GetRefundBalanceByRelateId(%s), err=%s", rId, err)
- return nil
- }
- cache.Cache.Put(k, item, 5*time.Minute)
- return item
- }
- // 获取订单号前缀
- func (self *BalanceOrder) GetOrderIdPrefix() string {
- s := strings.Split(self.OrderId, "")
- prefix := fmt.Sprintf("%s%s", s[0], s[1])
- return prefix
- }
- //生成订单ID
- func createOrderId(prefix string) string {
- if prefix == "" {
- return ""
- }
- n := time.Now().Format("20060102")
- u := uuid.NewV4().String()
- c := strings.Split(u, "-")
- oId := strings.ToUpper(fmt.Sprintf("%s%s%s", prefix, n, c[0]))
- return oId
- }
- func (self *BalanceOrder) Create(wxUId, uId, tp, paidPrice int64, payway string) (item *BalanceOrder) {
- oId := createOrderId(ORDER_ID_PREFIX_CZ)
- item = &BalanceOrder{
- OrderId: oId,
- WxUserId: wxUId,
- UserId: uId,
- PayWay: payway,
- TotalPrice: tp,
- PaiedPrice: paidPrice,
- Remark: "充值",
- }
- id, err := orm.NewOrm().Insert(item)
- if err != nil {
- beego.BeeLogger.Error("Create BalanceOrder err=[%s]", err)
- return nil
- }
- item.Id = id
- return item
- }
- func (self *Balance) Save() error {
- _, err := orm.NewOrm().Update(self)
- if err != nil {
- beego.BeeLogger.Error("Save Balance id=[%d] .err=[%s]", self.Id, err)
- }
- return err
- }
- func (self *BalanceOrder) Save() error {
- _, err := orm.NewOrm().Update(self)
- if err != nil {
- beego.BeeLogger.Error("Save BalanceOrder id=[%d] .err=[%s]", self.Id, err)
- }
- return err
- }
|