jiaojiao7 4 年之前
父節點
當前提交
7a5149df12
共有 1 個文件被更改,包括 102 次插入0 次删除
  1. 102 0
      service/user.go

+ 102 - 0
service/user.go

@@ -0,0 +1,102 @@
+package service
+
+import (
+	"github.com/gin-gonic/gin"
+	"gorm.io/gorm"
+	"log"
+	"sfis/db"
+	"sfis/model"
+	"sfis/model/response"
+	"strconv"
+	"strings"
+	"time"
+)
+
+func UserProject(projectIds, appId, startTime, endTime string, leftNum, costModel, interfaceStatus, callTimesLimitDay, dataNumOneTimes, tradeMoney, buyType, historyUnitPrice int, c *gin.Context) {
+	projectIdsArr := strings.Split(projectIds, ",")
+	if len(projectIdsArr) > 0 {
+		var errs error
+		for _, v := range projectIdsArr {
+			userProject := &model.UserProduct{}
+			userProject.AppID = appId
+			userProject.StartAt, _ = time.ParseInLocation("2006-01-02 15:04:05", startTime, time.Local)
+			userProject.EndAt, _ = time.ParseInLocation("2006-01-02 15:04:05", endTime, time.Local)
+			userProject.LeftNum = leftNum
+			userProject.CostModel = costModel
+			userProject.InterfaceStatus = interfaceStatus
+			userProject.CallTimesLimitDay = callTimesLimitDay
+			userProject.DataNumLimitOneTimes = dataNumOneTimes
+			id, _ := strconv.Atoi(v)
+			userProject.ProductID = id
+			userProjectInfo := []model.UserProduct{}
+			err := db.GetSFISDB().Where("product_id = ? and app_id = ?", id, appId).Find(&userProjectInfo).Error
+			if err != nil {
+				response.FailWithMessage("查询用户产品信息出错", c)
+				return
+			}
+			if len(userProjectInfo) > 0 {
+				before := userProjectInfo[0].LeftNum
+				after := userProjectInfo[0].LeftNum + leftNum
+				userProjectId := userProjectInfo[0].ID
+				errs = db.GetSFISDB().Transaction(func(tx *gorm.DB) error {
+					//更新用户产品信息
+					userProject.LeftNum = userProjectInfo[0].LeftNum + leftNum
+					err := tx.Exec("update user_product set left_num = left_num + ?, start_at = ?,end_at = ?,cost_model = ?,interface_status = ?,call_times_limit_day=?,data_num_limit_one_times=? where product_id = ? and app_id = ?", leftNum, startTime, endTime, costModel, interfaceStatus, callTimesLimitDay, dataNumOneTimes, id, appId).Error
+					if err != nil {
+						log.Printf("appID:[%s],projectId:[%d] execute cost user_account error:[%v]", appId, id, err)
+						tx.Rollback()
+						return err
+					}
+					//生成购买产品记录
+					err = tx.Exec("insert into user_buy_record (`app_id`,`product_id`,`user_product_id`,`before`,`after`,`trade_money`,`buy_type`,`history_unit_price`) values (?,?,?,?,?,?,?,?)", appId, id, userProjectId, before, after, tradeMoney, buyType, historyUnitPrice).Error
+					if err != nil {
+						log.Printf("appID:[%s],projectId[%d],trade_money:[%d] execute insert into user_buy_record error:[%v]", appId, id, tradeMoney, err)
+						tx.Rollback()
+						return err
+					}
+					return nil
+				})
+			} else {
+				errs = db.GetSFISDB().Transaction(func(tx *gorm.DB) error {
+					//生用户产品
+					err := tx.Create(userProject).Error
+					if err != nil {
+						log.Printf("appID:[%s],projectId:[%d] insert user_project error:[%v]", appId, id, err)
+						tx.Rollback()
+						return tx.Error
+					}
+					userProjectId := userProject.ID
+					//生成购买产品记录
+					err = tx.Exec("insert into user_buy_record (`app_id`,`product_id`,`user_product_id`,`before`,`after`,`trade_money`,`buy_type`,`history_unit_price`) values (?,?,?,?,?,?,?,?)", appId, id, userProjectId, 0, leftNum, tradeMoney, buyType, historyUnitPrice).Error
+					if err != nil {
+						log.Printf("appID:[%s],projectId[%d],trade_money:[%d] execute insert into user_buy_record error:[%v]", appId, id, tradeMoney, err)
+						tx.Rollback()
+						return err
+					}
+					return nil
+				})
+			}
+
+		}
+		if errs == nil {
+			response.Ok(c)
+			return
+		} else {
+			response.FailWithMessage("购买产品失败", c)
+			return
+		}
+
+	}
+	response.FailWithMessage("缺少参数", c)
+}
+
+func UserProjectList(appId string, c *gin.Context) {
+	userProject := []model.UserProduct{}
+	err := db.GetSFISDB().Where("app_id = ? and ", appId).Find(&userProject).Error
+	if err != nil {
+		log.Printf("appID:[%s] find into user_project error:[%v]", appId, err)
+		response.FailWithMessage("查询出错", c)
+	} else {
+		response.OkWithData(userProject, c)
+	}
+}