account.go 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package customer
  2. import (
  3. "errors"
  4. "spaceofcheng/services/models"
  5. "spaceofcheng/services/models/model"
  6. "spaceofcheng/services/utils"
  7. "strconv"
  8. "time"
  9. "github.com/yl10/kit/guid"
  10. )
  11. // SaveAccount 保存账户信息
  12. func (m *CustomerDAO) SaveAccount(account *model.TaCustomerAccount) error {
  13. if account.CustomerId == "" {
  14. return errors.New("账户未关联人员")
  15. }
  16. account.Status = models.STATUS_NORMAL
  17. account.CreateDate = time.Now().Local()
  18. account.AccountId = guid.NewGUIDString()
  19. _, err := m.db.Insert(account)
  20. return err
  21. }
  22. // InsertAccountRecords 新增账户消费记录
  23. func (m *CustomerDAO) InsertAccountRecords(rec *model.TaAccountChange) error {
  24. if rec.AccountId == "" {
  25. return errors.New("无法确定消费账户")
  26. }
  27. if rec.CaseId == "" {
  28. return errors.New("无法确定消费案场")
  29. }
  30. acc, err := m.GetAccountByID(rec.AccountId)
  31. if err != nil {
  32. utils.LogError("获取人员账户失败: " + err.Error())
  33. return errors.New("找不到账户消费记录")
  34. }
  35. if acc.Status != models.STATUS_NORMAL {
  36. return errors.New("人员账户状态不正常")
  37. }
  38. // 补充部分字段
  39. rec.DetailId = guid.NewGUIDString()
  40. rec.CreateDate = time.Now().Local()
  41. rec.Status = models.STATUS_NORMAL
  42. rec.OrgId = acc.OrgId
  43. // 消费内容
  44. points := float64(0.0)
  45. moenyCheng := float64(0.0)
  46. amount, _ := strconv.ParseFloat(rec.Amount, 64)
  47. if rec.ChangeType == models.CONSUME_POINTS {
  48. points = amount
  49. } else if rec.ChangeType == models.CONSUME_COINCHG {
  50. moenyCheng = amount
  51. }
  52. // 反向更新账户表
  53. accAmount, _ := strconv.ParseFloat(acc.Amount, 64)
  54. accPoints, _ := strconv.ParseFloat(acc.Points, 64)
  55. accPayedAmount, _ := strconv.ParseFloat(acc.PayedAmount, 64)
  56. accPayedPoints, _ := strconv.ParseFloat(acc.PayedPoints, 64)
  57. if rec.FloatType == models.ACCOUNT_SPENT {
  58. accPayedAmount += moenyCheng
  59. accPayedPoints += points
  60. accAmount -= moenyCheng
  61. accPoints -= points
  62. } else {
  63. accAmount += moenyCheng
  64. accPoints += points
  65. }
  66. // 账户不能为负
  67. if accAmount < 0 || accPoints < 0 {
  68. return errors.New("账户不足, 不能消费")
  69. }
  70. acc.Amount = strconv.FormatFloat(accAmount, 'f', -1, 64)
  71. acc.Points = strconv.FormatFloat(accPoints, 'f', -1, 64)
  72. acc.PayedAmount = strconv.FormatFloat(accPayedAmount, 'f', -1, 64)
  73. acc.PayedPoints = strconv.FormatFloat(accPayedPoints, 'f', -1, 64)
  74. if _, err := m.db.Insert(rec); err != nil {
  75. utils.LogError("插入消费记录失败: " + err.Error())
  76. return errors.New("记录消费记录失败")
  77. }
  78. if _, err := m.db.Where("account_id=?", acc.AccountId).
  79. Cols([]string{"amount", "points", "payed_amount", "payed_points"}...).
  80. Update(acc); err != nil {
  81. utils.LogError("更新账户信息失败: " + err.Error())
  82. return errors.New("更新消费信息失败")
  83. }
  84. return nil
  85. }
  86. // GetAccountByID 获取账户信息
  87. func (m *CustomerDAO) GetAccountByID(id string) (*model.TaCustomerAccount, error) {
  88. acc := new(model.TaCustomerAccount)
  89. if _, err := m.db.Where("account_id=?", id).Get(acc); err != nil {
  90. return nil, err
  91. }
  92. return acc, nil
  93. }
  94. // GetAccountByCust 获取账户信息
  95. func (m *CustomerDAO) GetAccountByCust(id string) (*model.TaCustomerAccount, error) {
  96. acc := new(model.TaCustomerAccount)
  97. if _, err := m.db.Where("customer_id=?", id).Get(acc); err != nil {
  98. return nil, err
  99. }
  100. return acc, nil
  101. }
  102. // GetCustomerVipAmount 获取用户vip金额
  103. func (m *CustomerDAO) GetCustomerVipAmount(id string) (string, error) {
  104. type amount struct {
  105. Balance string
  106. }
  107. sql := `select sum(balance) as balance from ta_customer_vip where customer_id=?`
  108. var vip = amount{}
  109. _, err := m.db.Sql(sql, id).Get(&vip)
  110. if err != nil {
  111. return "0.00", err
  112. }
  113. if vip.Balance == "" {
  114. vip.Balance = "0.00"
  115. }
  116. return vip.Balance, nil
  117. }