123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- package customer
-
- import (
- "errors"
- "spaceofcheng/services/models"
- "spaceofcheng/services/models/model"
- "spaceofcheng/services/utils"
- "strconv"
- "time"
-
- "github.com/yl10/kit/guid"
- )
-
- // SaveAccount 保存账户信息
- func (m *CustomerDAO) SaveAccount(account *model.TaCustomerAccount) error {
- if account.CustomerId == "" {
- return errors.New("账户未关联人员")
- }
-
- account.Status = models.STATUS_NORMAL
- account.CreateDate = time.Now().Local()
- account.AccountId = guid.NewGUIDString()
-
- _, err := m.db.Insert(account)
- return err
- }
-
- // InsertAccountRecords 新增账户消费记录
- func (m *CustomerDAO) InsertAccountRecords(rec *model.TaAccountChange) error {
- if rec.AccountId == "" {
- return errors.New("无法确定消费账户")
- }
-
- if rec.CaseId == "" {
- return errors.New("无法确定消费案场")
- }
-
- acc, err := m.GetAccountByID(rec.AccountId)
- if err != nil {
- utils.LogError("获取人员账户失败: " + err.Error())
- return errors.New("找不到账户消费记录")
- }
-
- if acc.Status != models.STATUS_NORMAL {
- return errors.New("人员账户状态不正常")
- }
-
- // 补充部分字段
- rec.DetailId = guid.NewGUIDString()
- rec.CreateDate = time.Now().Local()
- rec.Status = models.STATUS_NORMAL
- rec.OrgId = acc.OrgId
-
- // 消费内容
- points := float64(0.0)
- moenyCheng := float64(0.0)
- amount, _ := strconv.ParseFloat(rec.Amount, 64)
- if rec.ChangeType == models.CONSUME_POINTS {
- points = amount
- } else if rec.ChangeType == models.CONSUME_COINCHG {
- moenyCheng = amount
- }
-
- // 反向更新账户表
- accAmount, _ := strconv.ParseFloat(acc.Amount, 64)
- accPoints, _ := strconv.ParseFloat(acc.Points, 64)
- accPayedAmount, _ := strconv.ParseFloat(acc.PayedAmount, 64)
- accPayedPoints, _ := strconv.ParseFloat(acc.PayedPoints, 64)
-
- if rec.FloatType == models.ACCOUNT_SPENT {
- accPayedAmount += moenyCheng
- accPayedPoints += points
-
- accAmount -= moenyCheng
- accPoints -= points
- } else {
- accAmount += moenyCheng
- accPoints += points
- }
-
- // 账户不能为负
- if accAmount < 0 || accPoints < 0 {
- return errors.New("账户不足, 不能消费")
- }
-
- acc.Amount = strconv.FormatFloat(accAmount, 'f', -1, 64)
- acc.Points = strconv.FormatFloat(accPoints, 'f', -1, 64)
- acc.PayedAmount = strconv.FormatFloat(accPayedAmount, 'f', -1, 64)
- acc.PayedPoints = strconv.FormatFloat(accPayedPoints, 'f', -1, 64)
-
- if _, err := m.db.Insert(rec); err != nil {
- utils.LogError("插入消费记录失败: " + err.Error())
- return errors.New("记录消费记录失败")
- }
-
- if _, err := m.db.Where("account_id=?", acc.AccountId).
- Cols([]string{"amount", "points", "payed_amount", "payed_points"}...).
- Update(acc); err != nil {
- utils.LogError("更新账户信息失败: " + err.Error())
- return errors.New("更新消费信息失败")
- }
-
- return nil
- }
-
- // GetAccountByID 获取账户信息
- func (m *CustomerDAO) GetAccountByID(id string) (*model.TaCustomerAccount, error) {
- acc := new(model.TaCustomerAccount)
-
- if _, err := m.db.Where("account_id=?", id).Get(acc); err != nil {
- return nil, err
- }
-
- return acc, nil
- }
-
- // GetAccountByCust 获取账户信息
- func (m *CustomerDAO) GetAccountByCust(id string) (*model.TaCustomerAccount, error) {
- acc := new(model.TaCustomerAccount)
-
- if _, err := m.db.Where("customer_id=?", id).Get(acc); err != nil {
- return nil, err
- }
-
- return acc, nil
- }
-
- // GetCustomerVipAmount 获取用户vip金额
- func (m *CustomerDAO) GetCustomerVipAmount(id string) (string, error) {
- type amount struct {
- Balance string
- }
- sql := `select sum(balance) as balance from ta_customer_vip where customer_id=?`
- var vip = amount{}
- _, err := m.db.Sql(sql, id).Get(&vip)
- if err != nil {
- return "0.00", err
- }
- if vip.Balance == "" {
- vip.Balance = "0.00"
- }
- return vip.Balance, nil
- }
|