package services import ( "qteam/app/constants/common" "qteam/app/constants/errorcode" "qteam/app/http/entities/front" "qteam/app/models/ordermodel" "qteam/app/models/productmodel" "qteam/app/models/usersmodel" "qteam/app/utils" redis_util "qteam/app/utils/redis" "qteam/config" "strconv" "time" "xorm.io/builder" ) func CreateOrderService(userId int, productId int) (code int, data front.XinYeOrderResponse) { var err error { // redis保证用户当前抽奖结束才能开始下次抽奖 key := utils.GetRealKey("lottery_code:" + strconv.Itoa(userId) + strconv.Itoa(productId)) ok, err := redis_util.AcquireLock(key, time.Second*10) if ok { defer redis_util.Del(key) } else { if err != nil { utils.Log(nil, "CreateOrderService", err.Error()) } code = errorcode.OrderLottery return code, data } } session := ordermodel.GetInstance().GetDb().NewSession() defer func() { if err != nil { _ = session.Rollback() return } _ = session.Close() }() var product productmodel.Product has, err := productmodel.GetInstance().GetDb().Where("id =?", productId).Get(&product) if err != nil { return errorcode.SystemError, data } if !has { return errorcode.OrderProductNotExist, data } var user usersmodel.Users _, err = usersmodel.GetInstance().GetDb().Where("id =?", userId).Get(&user) if err != nil { return errorcode.SystemError, data } order := ordermodel.Order{ OrderNo: utils.GenerateOrderNumber(), UserId: userId, XinYeUserId: strconv.Itoa(user.XinYeUserId), ProductId: productId, ProductName: product.Name, UserName: user.Name, Price: product.Price, Mobile: user.Phone, ActivityId: config.GetConf().ActivityId, VoucherId: product.ThirdProductId, State: common.STATUSABLED, CreateTime: time.Now(), } // 创建订单,调收银台接口 code, data = CreateXyOrder(order) if code != errorcode.Success { return errorcode.SystemError, data } if _, err := session.Insert(order); err != nil { utils.Log(nil, "CreateOrderService", err.Error()) return errorcode.SystemError, data } if err = session.Begin(); err != nil { return errorcode.SystemError, data } return errorcode.Success, data } func OrderQueryService(userId int, OrderRequest *front.OrderListRequest) (code int, data []ordermodel.Order, count int64) { activityId := config.GetConf().ActivityId repo := ordermodel.GetInstance().GetDb() count, err := repo.Where("user_id = ? and activity_id = ?", userId, activityId). Desc("id").Limit(OrderRequest.PageSize, (OrderRequest.Page-1)*OrderRequest.PageSize).FindAndCount(&data) code = handErr(err) return } func OrderDetailService(order *ordermodel.Order) (has bool, err error) { repo := ordermodel.GetInstance().GetDb() conn := builder.NewCond() if order.Id != 0 { conn = conn.And(builder.Eq{"id": order.Id}) } if order.OrderNo != "" { conn = conn.And(builder.Eq{"order_no": order.OrderNo}) } return repo.Where(conn).Get(order) } func OrdersUpdateService(req front.OrdersUpdateRequest) (err error) { repo := ordermodel.GetInstance().GetDb() var order ordermodel.Order if req.Id != 0 { order.Id = req.Id } if req.Status != 0 { order.State = req.Status } if req.VoucherLink != "" { order.VoucherLink = req.VoucherLink } _, err = repo.Where("Id = ?", req.Id).Update(&order) return }