|
@@ -0,0 +1,81 @@
|
|
|
|
+package recharge
|
|
|
|
+
|
|
|
|
+import (
|
|
|
|
+ . "api"
|
|
|
|
+ "database/sql"
|
|
|
|
+ "errors"
|
|
|
|
+ "github.com/go-xweb/xweb"
|
|
|
|
+ "log"
|
|
|
|
+ "qfw/util"
|
|
|
|
+ "time"
|
|
|
|
+ util2 "util"
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+type EntRecharge struct {
|
|
|
|
+ *xweb.Action
|
|
|
|
+ recharge xweb.Mapper `xweb:"/back/Recharge"` //后台充值接口
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 充值用户额度
|
|
|
|
+func (this *EntRecharge) Recharge() {
|
|
|
|
+ if !R.CheckReqParam(this.ResponseWriter, this.Request, "phone", "entId", "rechargeNum") {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ // 获取用户id 企业id
|
|
|
|
+ form := this.GetForm()
|
|
|
|
+ phone := form.Get("phone")
|
|
|
|
+ entId := util.IntAll(form.Get("entId"))
|
|
|
|
+ rechargeNum := util.IntAll(form.Get("rechargeNum"))
|
|
|
|
+ remark := form.Get("remark")
|
|
|
|
+ // 充值
|
|
|
|
+ err := UserRecharge(phone, rechargeNum, entId, remark)
|
|
|
|
+ if err != nil {
|
|
|
|
+ this.ServeJson(Result{Error_code: 1006, Error_msg: "充值失败"})
|
|
|
|
+ } else {
|
|
|
|
+ this.ServeJson(Result{Error_code: 0})
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+// 用户额度充值
|
|
|
|
+func UserRecharge(phone string, rechargeNum int, entId int, remark string) error {
|
|
|
|
+ // 查询用户是否存在
|
|
|
|
+ tmpRs := util2.JyMysql.FindOne("entniche_user", map[string]interface{}{"phone": phone, "ent_id": entId}, "", "")
|
|
|
|
+ if tmpRs == nil {
|
|
|
|
+ log.Printf("用户手机号有误:phone-%s,\n", phone)
|
|
|
|
+ return errors.New("用户id错误")
|
|
|
|
+ }
|
|
|
|
+ userId := int((*tmpRs)["id"].(int64))
|
|
|
|
+ // 查询是否已经在账户余额表
|
|
|
|
+ queryMap := map[string]interface{}{"user_id": userId, "ent_id": entId}
|
|
|
|
+ rs := util2.JyMysql.FindOne("user_account", queryMap, "", "")
|
|
|
|
+ if rs == nil {
|
|
|
|
+ // 用户额度表还没有该用户的记录 1.初始化一条记录
|
|
|
|
+ nowStr := time.Now().Local().Format("2006-01-02 15:04:05")
|
|
|
|
+ addRs := util2.JyMysql.Insert("user_account", map[string]interface{}{"user_id": userId, "ent_id": entId, "left_num": 0, "create_at": nowStr})
|
|
|
|
+ println(addRs)
|
|
|
|
+ }
|
|
|
|
+ // 充值前剩余
|
|
|
|
+ result := util2.JyMysql.FindOne("user_account", queryMap, "", "")
|
|
|
|
+ if result == nil {
|
|
|
|
+ return errors.New("用户账户查询失败")
|
|
|
|
+ }
|
|
|
|
+ before := int((*result)["left_num"].(int64))
|
|
|
|
+ after := before + rechargeNum
|
|
|
|
+ // 充值
|
|
|
|
+ rechargeRs := util2.JyMysql.ExecTx("用户充值", func(tx *sql.Tx) bool {
|
|
|
|
+ // 更新剩余额度
|
|
|
|
+ updateNowStr := time.Now().Local().Format("2006-01-02 15:04:05")
|
|
|
|
+ ok1 := util2.JyMysql.UpdateOrDeleteBySqlByTx(tx, "update user_account set left_num=?,update_at=? where user_id=? and ent_id=?", after, updateNowStr, userId, entId) > 0
|
|
|
|
+ // 生充值记录
|
|
|
|
+ sqlStr := "insert into user_recharge_record (user_id,ent_id,`before`,`after`,trade,create_at,remark) values(?,?,?,?,?,?,?)"
|
|
|
|
+ ok2 := util2.JyMysql.InsertBySqlByTx(tx, sqlStr, userId, entId, before, after, rechargeNum, updateNowStr, remark) != -1
|
|
|
|
+ return ok1 && ok2
|
|
|
|
+ })
|
|
|
|
+ if rechargeRs != true {
|
|
|
|
+ log.Printf("用户额度充值失败:user_id-%dent_id:%d\n", userId, entId)
|
|
|
|
+ return errors.New("充值失败")
|
|
|
|
+ }
|
|
|
|
+ return nil
|
|
|
|
+}
|
|
|
|
+
|