|
|
@@ -2,11 +2,15 @@ package dao
|
|
|
|
|
|
import (
|
|
|
"demo/configs"
|
|
|
+ "demo/data/dao/manage"
|
|
|
"demo/data/domain"
|
|
|
"demo/data/domain/vo"
|
|
|
"fmt"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
+// SelectSkuBySkuId 根据id查询sku
|
|
|
func SelectSkuBySkuId(skuId int) (domain.GoodsSku, error) {
|
|
|
var sku domain.GoodsSku
|
|
|
_, err := configs.Engine.Table("goods_sku").Where("id = ?", skuId).Get(&sku)
|
|
|
@@ -16,8 +20,109 @@ func SelectSkuBySkuId(skuId int) (domain.GoodsSku, error) {
|
|
|
return sku, nil
|
|
|
}
|
|
|
|
|
|
+// GetSkuBelong 查询sku所属topic,goods,type
|
|
|
+func GetSkuBelong(skuId, couponUserId int64, tp float64) (bool, string, error) {
|
|
|
+ //时间内可用
|
|
|
+ availableTime := false
|
|
|
+ //范围内可用
|
|
|
+ availableField := false
|
|
|
+ //金额条件可用
|
|
|
+ availableMatch := false
|
|
|
+
|
|
|
+ var couponUser domain.GoodsCouponUser
|
|
|
+ _, err := configs.Engine.Table("goods_coupon_user").Where("id = ?", couponUserId).Get(&couponUser)
|
|
|
+ if err != nil {
|
|
|
+ return false, "", err
|
|
|
+ }
|
|
|
+ coupon, err := manage.GetGoodsCouponById(couponUser.CouponId)
|
|
|
+ if err != nil {
|
|
|
+ return false, "", err
|
|
|
+ }
|
|
|
+ //判断是否过期
|
|
|
+ //领取后开始计时
|
|
|
+ if coupon.Validity == "ClaimTiming" {
|
|
|
+ if couponUser.CollectionTime.UnixMilli()+coupon.ValidityPeriod.UnixMilli() < time.Now().UnixMilli() {
|
|
|
+ return false, "优惠券已过期", nil
|
|
|
+ }
|
|
|
+ //固定时间段内可用
|
|
|
+ } else if coupon.Validity == "FixedTime" {
|
|
|
+ if coupon.ValidityPeriod.UnixMilli() < time.Now().UnixMilli() {
|
|
|
+ return false, "优惠券不在可使用时间段内", nil
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ availableTime = true
|
|
|
+ }
|
|
|
+ if !availableTime {
|
|
|
+ return availableTime, "", nil
|
|
|
+ }
|
|
|
+
|
|
|
+ sku, err := manage.GetGoodsSkuById(skuId)
|
|
|
+ if err != nil {
|
|
|
+ return false, "", err
|
|
|
+ }
|
|
|
+ //goods
|
|
|
+ if coupon.ConditionByGoods != "" {
|
|
|
+ split := strings.Split(coupon.ConditionByGoods, ",")
|
|
|
+ for i := range split {
|
|
|
+ if fmt.Sprint(skuId) == split[i] {
|
|
|
+ availableField = true
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ m := make(map[string]interface{})
|
|
|
+ _, err = configs.Engine.Select("goods_type.id as tid, goods.id as gid").Table("goods_type").
|
|
|
+ Join("INNER ", "goods", "goods_type.id = goods.type_id").
|
|
|
+ Join("INNER ", "goods_sku", "goods.id = goods_sku.goods_id").
|
|
|
+ Where("goods_sku.id = ?", sku.GoodsId).Get(&m)
|
|
|
+ if err != nil {
|
|
|
+ return false, "type 查询错误", err
|
|
|
+ }
|
|
|
+ //type
|
|
|
+ if !availableField && coupon.ConditionByType != "" && m["tid"] != "" {
|
|
|
+ split := strings.Split(coupon.ConditionByType, ",")
|
|
|
+ for i := range split {
|
|
|
+ if fmt.Sprint(m["tid"]) == split[i] {
|
|
|
+ availableField = true
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //topic
|
|
|
+ var topic domain.ShopTopic
|
|
|
+ _, err = configs.Engine.Table("shop_topic").Where("find_in_set(?,type_ids)", m["tid"]).Get(&topic)
|
|
|
+ if err != nil {
|
|
|
+ return false, "topic 查询错误", err
|
|
|
+ }
|
|
|
+ if !availableField && topic.Id != 0 {
|
|
|
+ availableField = true
|
|
|
+ }
|
|
|
+
|
|
|
+ if tp > coupon.CashBackPoint {
|
|
|
+ availableMatch = true
|
|
|
+ }
|
|
|
+ if !availableTime || !availableField || !availableMatch {
|
|
|
+ return false, "不满足条件", nil
|
|
|
+ }
|
|
|
+ return availableTime && availableField && availableMatch, "", nil
|
|
|
+}
|
|
|
+
|
|
|
// CreateOrder 创建订单
|
|
|
func CreateOrder(order *domain.GoodsOrder) (*domain.GoodsOrder, error) {
|
|
|
+
|
|
|
+ var couponUser domain.GoodsCouponUser
|
|
|
+ _, err := configs.Engine.Table("goods_coupon_user").Where("id = ?", order.CouponUserId).Get(&couponUser)
|
|
|
+ if err != nil {
|
|
|
+ return order, err
|
|
|
+ }
|
|
|
+ coupon, err := manage.GetGoodsCouponById(couponUser.CouponId)
|
|
|
+ if err != nil {
|
|
|
+ return order, err
|
|
|
+ }
|
|
|
+ order.TotalPrice = order.TotalPrice - coupon.CashBackPrice
|
|
|
+ //优惠券使用
|
|
|
+ configs.Engine.ID(order.CouponUserId).Cols("state").Update(map[string]any{"state": 2})
|
|
|
+ //创建订单
|
|
|
id, err := configs.Engine.Table("goods_order").Insert(&order)
|
|
|
fmt.Println("is id? ", id)
|
|
|
if err != nil {
|
|
|
@@ -28,8 +133,29 @@ func CreateOrder(order *domain.GoodsOrder) (*domain.GoodsOrder, error) {
|
|
|
}
|
|
|
|
|
|
// OrderPaySuccess 订单支付成功
|
|
|
-func OrderPaySuccess(orderId int64) {
|
|
|
- configs.Engine.ID(orderId).Cols("state").Update(map[string]any{"state": 1})
|
|
|
+func OrderPaySuccess(orderId int64, userId int64) error {
|
|
|
+ //查询订单
|
|
|
+ order, err := manage.GetGoodsOrderById(orderId)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ configs.Engine.Table("goods_order").ID(orderId).Cols("state").Update(map[string]any{"state": 1})
|
|
|
+
|
|
|
+ //获取价格
|
|
|
+ var price = order.TotalPrice
|
|
|
+ //给推荐人分成
|
|
|
+ user, err := manage.GetUserById(userId)
|
|
|
+ wallet := domain.UserWallet{UserId: user.InviterBy}
|
|
|
+ list, err := manage.GetUserWalletList(wallet, 0, 1)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ userWallet, ok := list.List[0].(domain.UserWallet)
|
|
|
+ if ok {
|
|
|
+ userWallet.Balance = userWallet.Balance + (price * 0.1)
|
|
|
+ configs.Engine.Table("user_wallet").ID(userWallet.Id).Cols("balance").Update(map[string]any{"balance": userWallet.Balance})
|
|
|
+ }
|
|
|
+ return nil
|
|
|
}
|
|
|
|
|
|
// GetOrderByUserId 获取订单集合
|