base_resource_use.go 2.3 KB

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