// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package accounting import ( "context" "gd_management/apis" "gd_management/common.in/storage" "gd_management/common.in/utils" "gd_management/errors" "encoding/json" "fmt" "github.com/astaxie/beego/orm" "go.uber.org/zap" "time" ) func MerchantCharge(ctx context.Context, req *apis.MerchantChargeReq, reply *apis.MerchantChargeReply) error { // update t_gd_merchants set balance=balance+1000,bill_balance=bill_balance+1000 where id=9 // insert into 插入账单 if req.MerchantId == 0 { return errors.ArgsError } task := func(o orm.Ormer) error { sql := fmt.Sprintf("update t_gd_merchants set balance=balance+%f,bill_balance=bill_balance+%f where id=%d", req.Amount, req.Amount, req.MerchantId) _, err := o.Raw(sql).Exec() if err != nil { l.Error("func", zap.String("call", "MerchantCharge"), zap.String("args", utils.MarshalJsonString(req)), zap.String("error", err.Error())) return errors.DataBaseError } balance := float64(0) err = o.Raw("select balance from t_gd_merchants where id=?", req.MerchantId).QueryRow(&balance) if err != nil { l.Error("func", zap.String("call", "MerchantCharge"), zap.String("args", utils.MarshalJsonString(req)), zap.String("error", err.Error())) return errors.DataBaseError } bill := &apis.GdBill{} bill.MerchantId = req.MerchantId bill.BillType = 1 bill.UserName = req.UserName bill.BillTime = time.Now().Format("2006-01") bill.BillCreateTime = time.Now().Format("2006-01-02") bill.Amount = req.Amount tmp := []string{} tmp = append(tmp, req.Remark) remarkByte, _ := json.Marshal(tmp) bill.Remark = string(remarkByte) bill.Balance = balance bill.CreatedAt = time.Now().Unix() _, err = o.Insert(bill) if err != nil { l.Error("func", zap.String("call", "MerchantCharge"), zap.String("args", utils.MarshalJsonString(req)), zap.String("error", err.Error())) return errors.DataBaseError } return nil } tasks := []storage.DbaTasker{} tasks = append(tasks, storage.GenerateDbaTask(task)) storage.ExecTrans(tasks...) l.Debug(utils.MarshalJsonString(req, reply)) return nil }