base_resource_use.go 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package entity
  2. import (
  3. "database/sql"
  4. "errors"
  5. "strings"
  6. . "app.yhyue.com/moapp/jybase/common"
  7. . "app.yhyue.com/moapp/jybase/date"
  8. . "bp.jydev.jianyu360.cn/BaseService/resourceCenter/public/db"
  9. )
  10. var Base_resource_use = base_resource_use{}
  11. //资源充值/消耗表
  12. type base_resource_use struct {
  13. Id int64
  14. Appid string
  15. Account_id int64 //账户id
  16. Function_code string //功能代码
  17. Add_count int64 //新增数量
  18. Surplus_count int64 //剩余数量
  19. Deduct_count int64 //扣除数量
  20. Create_time string //创建时间
  21. }
  22. //查找最新一条的权益使用记录
  23. func (b *base_resource_use) FindLastOne(appid, function_code string, account_id int64) (*base_resource_use, error) {
  24. list := Mysql_BaseService.SelectBySql(`select * from base_resource_use where appid=? and function_code=? and account_id=? order by create_time desc limit 1`, appid, function_code, account_id)
  25. if list == nil {
  26. return nil, errors.New("find error")
  27. }
  28. if len(*list) == 0 {
  29. return nil, nil
  30. }
  31. r, err := JsonUnmarshalByErr((*list)[0], &base_resource_use{})
  32. if err == nil {
  33. return r.(*base_resource_use), nil
  34. }
  35. return nil, err
  36. }
  37. //新增扣减记录
  38. func (b *base_resource_use) Deduction(use_values []interface{}, detail_values [][]interface{}, update [][]interface{}) bool {
  39. return Mysql_BaseService.ExecTx("新增扣减记录", func(tx *sql.Tx) bool {
  40. v1, v2 := Mysql_BaseService.InsertBatchByTx(tx, "base_resource_use", []string{"appid", "account_id", "function_code", "add_count", "surplus_count", "deduct_count", "business_id", "create_time"}, use_values)
  41. array := []interface{}{}
  42. for _, v := range detail_values {
  43. array = append(array, v...)
  44. array = append(array, v2)
  45. }
  46. v3, v4 := Mysql_BaseService.InsertBatchByTx(tx, "base_resource_use_detail", []string{"appid", "power_id", "surplus_count", "deduct_count", "create_time", "use_id"}, array)
  47. ok := true
  48. if len(update) > 0 {
  49. ok = Mysql_BaseService.UpdateBathByTx(tx, "base_power", []string{"id", "surplus_count", "update_time"}, update) == int64(len(update))
  50. }
  51. return v1 > 0 && v2 > 0 && v3 > 0 && v4 > 0 && ok
  52. })
  53. }
  54. //新增充值记录
  55. func (b *base_resource_use) Recharge(appid, function_code string, power_id, account_id int64, count int64, ids []string) int {
  56. status := 0
  57. Mysql_BaseService.ExecTx("新增充值记录", func(tx *sql.Tx) bool {
  58. power := Base_power.FindMyPowerForUpdate(power_id)
  59. if power == nil {
  60. return true
  61. }
  62. if power.Surplus_count+count > power.Use_count {
  63. status = -1
  64. return true
  65. }
  66. nowFormat := NowFormat(Date_Full_Layout)
  67. use_values := []interface{}{appid, account_id, function_code, count, power.Surplus_count + count, 0, strings.Join(ids, ","), nowFormat}
  68. v1, v2 := Mysql_BaseService.InsertBatchByTx(tx, "base_resource_use", []string{"appid", "account_id", "function_code", "add_count", "surplus_count", "deduct_count", "business_id", "create_time"}, use_values)
  69. use_detail_values := []interface{}{appid, v2, power_id, 0, 0, nowFormat}
  70. v4, v5 := Mysql_BaseService.InsertBatchByTx(tx, "base_resource_use_detail", []string{"appid", "use_id", "power_id", "surplus_count", "deduct_count", "create_time"}, use_detail_values)
  71. v3 := Mysql_BaseService.UpdateOrDeleteBySqlByTx(tx, `update base_power set surplus_count=surplus_count+?,update_time=? where id=?`, count, nowFormat, power_id)
  72. if v1 > 0 && v2 > 0 && v3 > 0 && v4 > 0 && v5 > 0 {
  73. status = 1
  74. return true
  75. }
  76. return false
  77. })
  78. return status
  79. }