aa
This commit is contained in:
@@ -1482,31 +1482,57 @@ func GetPriceDefendOrder(db *DaoDB, vendorOrderID string, storeIDs, skuIDs, issu
|
||||
return priceDefendOrders, err
|
||||
}
|
||||
|
||||
func GetCoupons(db *DaoDB, couponType int, couponStatuss, storeIDs []int, keyword string) (coupons []*model.Coupons, err error) {
|
||||
func GetCoupons(db *DaoDB, couponType int, couponStatuss, storeIDs []int, userID, mobile, keyword string) (coupons []*model.Coupons, err error) {
|
||||
sqlParams := []interface{}{}
|
||||
sql := `
|
||||
SELECT *
|
||||
FROM coupons
|
||||
WHERE 1 = 1 AND is_all = ?
|
||||
SELECT DISTINCT a.*
|
||||
FROM coupons a
|
||||
`
|
||||
if userID != "" || mobile != "" {
|
||||
sql += " JOIN user_coupons b ON a.id = b.coupon_id"
|
||||
if userID != "" {
|
||||
sql += " AND b.user_id = ?"
|
||||
sqlParams = append(sqlParams, userID)
|
||||
}
|
||||
if mobile != "" {
|
||||
sql += " AND b.mobile = ?"
|
||||
sqlParams = append(sqlParams, mobile)
|
||||
}
|
||||
}
|
||||
sql += `
|
||||
WHERE a.is_all = ?
|
||||
`
|
||||
sqlParams = append(sqlParams, model.YES)
|
||||
if couponType != 0 {
|
||||
sql += " AND coupon_type = ?"
|
||||
sql += " AND a.coupon_type = ?"
|
||||
sqlParams = append(sqlParams, couponType)
|
||||
}
|
||||
if keyword != "" {
|
||||
sql += " AND (name LIKE ? OR desc LIKE ?)"
|
||||
sql += " AND (a.name LIKE ? OR a.desc LIKE ?)"
|
||||
sqlParams = append(sqlParams, "%"+keyword+"%", "%"+keyword+"%")
|
||||
}
|
||||
if len(couponStatuss) > 0 {
|
||||
sql += " AND coupon_status IN (" + GenQuestionMarks(len(couponStatuss)) + ")"
|
||||
sql += " AND a.coupon_status IN (" + GenQuestionMarks(len(couponStatuss)) + ")"
|
||||
sqlParams = append(sqlParams, couponStatuss)
|
||||
}
|
||||
sql +=
|
||||
`UNION
|
||||
sql += `
|
||||
UNION
|
||||
SELECT DISTINCT a.*
|
||||
FROM coupons a
|
||||
LEFT JOIN store_coupons b ON a.id = b.coupon_id AND b.deleted_at = ?
|
||||
`
|
||||
if userID != "" || mobile != "" {
|
||||
sql += " JOIN user_coupons c ON a.id = c.coupon_id"
|
||||
if userID != "" {
|
||||
sql += " AND c.user_id = ?"
|
||||
sqlParams = append(sqlParams, userID)
|
||||
}
|
||||
if mobile != "" {
|
||||
sql += " AND c.mobile = ?"
|
||||
sqlParams = append(sqlParams, mobile)
|
||||
}
|
||||
}
|
||||
sql += `
|
||||
WHERE 1 = 1
|
||||
`
|
||||
sqlParams = append(sqlParams, utils.DefaultTimeValue)
|
||||
@@ -1545,5 +1571,33 @@ func GetStoreCoupons(db *DaoDB, couponIDs, storeIDs []int) (storeCoupons []*mode
|
||||
sql += " AND store_id IN (" + GenQuestionMarks(len(storeIDs)) + ")"
|
||||
sqlParams = append(sqlParams, storeIDs)
|
||||
}
|
||||
err = GetRows(db, &storeCoupons, sql, sqlParams)
|
||||
return storeCoupons, err
|
||||
}
|
||||
|
||||
func GetUserCoupons(db *DaoDB, couponIDs []int, userIDs, mobiles []string, userStatus int) (userCoupons []*model.UserCoupons, err error) {
|
||||
sql := `
|
||||
SELECT *
|
||||
FROM user_coupons
|
||||
WHERE 1 = 1
|
||||
`
|
||||
sqlParams := []interface{}{}
|
||||
if len(couponIDs) > 0 {
|
||||
sql += " AND coupon_id IN (" + GenQuestionMarks(len(couponIDs)) + ")"
|
||||
sqlParams = append(sqlParams, couponIDs)
|
||||
}
|
||||
if len(userIDs) > 0 {
|
||||
sql += " AND user_id IN (" + GenQuestionMarks(len(userIDs)) + ")"
|
||||
sqlParams = append(sqlParams, userIDs)
|
||||
}
|
||||
if len(mobiles) > 0 {
|
||||
sql += " AND mobile IN (" + GenQuestionMarks(len(mobiles)) + ")"
|
||||
sqlParams = append(sqlParams, mobiles)
|
||||
}
|
||||
if userStatus != 0 {
|
||||
sql += " AND user_coupon_status = ?"
|
||||
sqlParams = append(sqlParams, userStatus)
|
||||
}
|
||||
err = GetRows(db, &userCoupons, sql, sqlParams)
|
||||
return userCoupons, err
|
||||
}
|
||||
|
||||
@@ -2153,7 +2153,7 @@ func GetCoupons(ctx *jxcontext.Context, couponType int, couponStatuss, storeIDs
|
||||
var (
|
||||
db = dao.GetDB()
|
||||
)
|
||||
coupons, err = dao.GetCoupons(db, couponType, couponStatuss, storeIDs, keyword)
|
||||
coupons, err = dao.GetCoupons(db, couponType, couponStatuss, storeIDs, "", "", keyword)
|
||||
return coupons, err
|
||||
}
|
||||
|
||||
@@ -2265,3 +2265,36 @@ func UpdateCoupons(ctx *jxcontext.Context, payload map[string]interface{}, store
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func ReceiveCoupons(ctx *jxcontext.Context, couponID int) (err error) {
|
||||
var (
|
||||
db = dao.GetDB()
|
||||
userID = ctx.GetUserID()
|
||||
coupons = &model.Coupons{}
|
||||
)
|
||||
coupons.ID = int64(couponID)
|
||||
dao.GetEntity(db, coupons)
|
||||
if coupons == nil {
|
||||
return fmt.Errorf("领取优惠券失败!")
|
||||
}
|
||||
if coupons.CouponStatus != model.CouponStatusNormal {
|
||||
return fmt.Errorf("优惠券状态有误,领取失败!")
|
||||
}
|
||||
if time.Now().Sub(coupons.EndAt) > 0 {
|
||||
return fmt.Errorf("优惠券已过期,领取失败!")
|
||||
}
|
||||
if time.Now().Sub(coupons.BeginAt) < 0 {
|
||||
return fmt.Errorf("优惠券未生效,领取失败!")
|
||||
}
|
||||
userCoupons, _ := dao.GetUserCoupons(db, []int{couponID}, []string{userID}, nil, model.CouponStatusNormal)
|
||||
if len(userCoupons) > 0 {
|
||||
return fmt.Errorf("您已经领取过该优惠券了,请使用后再领取!")
|
||||
}
|
||||
userCoupon := &model.UserCoupons{
|
||||
CouponID: couponID,
|
||||
UserID: userID,
|
||||
}
|
||||
dao.WrapAddIDCULEntity(userCoupon, ctx.GetUserName())
|
||||
err = dao.CreateEntity(db, userCoupon)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -349,3 +349,17 @@ func (c *JxOrderController) UpdateCoupons() {
|
||||
return retVal, "", err
|
||||
})
|
||||
}
|
||||
|
||||
// @Title 领取优惠券
|
||||
// @Description 领取优惠券
|
||||
// @Param token header string true "认证token"
|
||||
// @Param couponID formData int true "券ID"
|
||||
// @Success 200 {object} controllers.CallResult
|
||||
// @Failure 200 {object} controllers.CallResult
|
||||
// @router /ReceiveCoupons [post]
|
||||
func (c *JxOrderController) ReceiveCoupons() {
|
||||
c.callReceiveCoupons(func(params *tJxorderReceiveCouponsParams) (retVal interface{}, errCode string, err error) {
|
||||
err = localjx.ReceiveCoupons(params.Ctx, params.CouponID)
|
||||
return retVal, "", err
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user