diff --git a/app/db/db_coupon.go b/app/db/db_coupon.go index 3a49c63..7d7da59 100644 --- a/app/db/db_coupon.go +++ b/app/db/db_coupon.go @@ -1 +1,36 @@ package db + +import ( + "applet/app/db/model" + "applet/app/utils" + "xorm.io/xorm" +) + +func GetCoupon(eg *xorm.Engine, req map[string]string) *[]model.CommunityTeamCoupon { + var data []model.CommunityTeamCoupon + limit := 10 + start := (utils.StrToInt(req["p"]) - 1) * limit + sess := eg.Where("uid=0 and num>0 and is_use=1").OrderBy("sort desc,id desc").Limit(limit, start) + if req["name"] != "" { + sess.And("name like ?", "%"+req["name"]+"%") + } + if req["start_time"] != "" { + sess.And("created_time>=?", req["start_time"]) + } + if req["end_time"] != "" { + sess.And("created_time<=?", req["end_time"]) + } + err := sess.Find(&data) + if err != nil { + return nil + } + return &data +} +func GetCouponById(eg *xorm.Engine, id string) *model.CommunityTeamCoupon { + var data model.CommunityTeamCoupon + get, err := eg.Where("id=?", id).Get(&data) + if get == false || err != nil { + return nil + } + return &data +} diff --git a/app/db/model/community_team_coupon.go b/app/db/model/community_team_coupon.go index aeb2775..9680023 100644 --- a/app/db/model/community_team_coupon.go +++ b/app/db/model/community_team_coupon.go @@ -5,23 +5,24 @@ import ( ) type CommunityTeamCoupon struct { - Id int `json:"id" xorm:"not null pk autoincr INT(11)"` - Name string `json:"name" xorm:"comment('方案名称') VARCHAR(255)"` - Comment string `json:"comment" xorm:"comment('备注') VARCHAR(2048)"` - PublishTime time.Time `json:"publish_time" xorm:"comment('立即发布时间') DATETIME"` - Kind int `json:"kind" xorm:"not null comment('优惠券类型,1:立减,2:满减,3折扣') TINYINT(1)"` - IsReach int `json:"is_reach" xorm:"comment('是否有门槛,1:有,2:否;优惠券类型为3折扣时') TINYINT(1)"` - Cal string `json:"cal" xorm:"comment('满减及折扣有门槛时算法,{"reach": "20.12", "reduce": "2.2"}, reach:满X元,减/折reduce') VARCHAR(255)"` - State int `json:"state" xorm:"not null default 1 comment('状态,是否使用(1:使用;2:不使用)') TINYINT(1)"` - ActivityTimeStart time.Time `json:"activity_time_start" xorm:"not null comment('活动起止时间,开始时间') DATETIME"` - ActivityTimeEnd time.Time `json:"activity_time_end" xorm:"not null comment('活动起止时间,结束时间') DATETIME"` - ActivityStatement string `json:"activity_statement" xorm:"not null default '0' comment('活动规则说明') VARCHAR(5000)"` - Sort int `json:"sort" xorm:"comment('排序') INT(11)"` - Ext string `json:"ext" xorm:"comment('拓展字段') TEXT"` - IsPublishNow int `json:"is_publish_now" xorm:"not null default 0 comment('是否立即发布,0:否,1:是') TINYINT(1)"` - CreatedTime time.Time `json:"created_time" xorm:"not null default CURRENT_TIMESTAMP comment('创建时间') DATETIME"` - UpdatedTime time.Time `json:"updated_time" xorm:"not null default CURRENT_TIMESTAMP comment('更新时间') DATETIME"` - DeletedTime time.Time `json:"deleted_time" xorm:"comment('删除时间') DATETIME"` - StoreId int `json:"store_id" xorm:"default 0 INT(11)"` - StoreType int `json:"store_type" xorm:"default 0 comment('0官方自营店 1加盟店 2连锁店') INT(11)"` + Id int `json:"id" xorm:"not null pk autoincr INT(11)"` + Name string `json:"name" xorm:"comment('方案名称') VARCHAR(255)"` + Comment string `json:"comment" xorm:"comment('备注') VARCHAR(2048)"` + Kind int `json:"kind" xorm:"not null comment('优惠券类型,1:立减,2:满减,3折扣') TINYINT(1)"` + IsReach int `json:"is_reach" xorm:"comment('是否有门槛,1:有,2:否;优惠券类型为3折扣时') TINYINT(1)"` + Cal string `json:"cal" xorm:"comment('满减及折扣有门槛时算法,{"reach": "20.12", "reduce": "2.2"}, reach:满X元,减/折reduce') VARCHAR(255)"` + State int `json:"state" xorm:"not null default 1 comment('1开启') TINYINT(1)"` + Info string `json:"info" xorm:"not null default '0' comment('活动规则说明') VARCHAR(5000)"` + Sort int `json:"sort" xorm:"comment('排序') INT(11)"` + Ext string `json:"ext" xorm:"comment('拓展字段') TEXT"` + CreatedTime time.Time `json:"created_time" xorm:"not null default CURRENT_TIMESTAMP comment('创建时间') DATETIME"` + UpdatedTime time.Time `json:"updated_time" xorm:"not null default CURRENT_TIMESTAMP comment('更新时间') DATETIME"` + StoreId int `json:"store_id" xorm:"default 0 INT(11)"` + StoreType int `json:"store_type" xorm:"default 0 comment('0官方自营店 1加盟店 2连锁店') INT(11)"` + Img string `json:"img" xorm:"VARCHAR(255)"` + Num int `json:"num" xorm:"not null comment('剩余数量') INT(11)"` + ValidTimeType int `json:"valid_time_type" xorm:"comment('有效期类型:1固定日期2领取后x天有效') TINYINT(1)"` + ValidTimeStart time.Time `json:"valid_time_start" xorm:"comment('固定日期开始') DATETIME"` + ValidTimeEnd time.Time `json:"valid_time_end" xorm:"comment('固定日期结束') DATETIME"` + ValidDayNum int `json:"valid_day_num" xorm:"comment('领取后有效的天数') INT(11)"` } diff --git a/app/db/model/community_team_coupon_user.go b/app/db/model/community_team_coupon_user.go index a98b4d4..cf2f69f 100644 --- a/app/db/model/community_team_coupon_user.go +++ b/app/db/model/community_team_coupon_user.go @@ -13,8 +13,6 @@ type CommunityTeamCouponUser struct { Cal string `json:"cal" xorm:"not null default '0.00' comment('折扣算法json:形式:{"reach":"10.00", "reduce":"1.00"},无门槛reach为0') VARCHAR(255)"` ValidTimeStart time.Time `json:"valid_time_start" xorm:"comment('有效日期开始') DATETIME"` ValidTimeEnd time.Time `json:"valid_time_end" xorm:"comment('有效日期结束') DATETIME"` - UseRule int `json:"use_rule" xorm:"comment('1全部商品可用2指定商品3指定活动类型') TINYINT(1)"` - UseActivityType int `json:"use_activity_type" xorm:"comment('可用的活动类型: 1拼团活动 2秒杀活动 3砍价活动') TINYINT(1)"` CreateTime time.Time `json:"create_time" xorm:"default CURRENT_TIMESTAMP comment('创建时间') DATETIME"` UpdateTime time.Time `json:"update_time" xorm:"default CURRENT_TIMESTAMP comment('更新时间') DATETIME"` StoreId int `json:"store_id" xorm:"default 0 comment('0是官方') INT(11)"` diff --git a/app/hdl/hdl_coupon.go b/app/hdl/hdl_coupon.go new file mode 100644 index 0000000..1ed67c5 --- /dev/null +++ b/app/hdl/hdl_coupon.go @@ -0,0 +1,13 @@ +package hdl + +import ( + "applet/app/svc" + "github.com/gin-gonic/gin" +) + +func Coupon(c *gin.Context) { + svc.Coupon(c) +} +func CouponReceive(c *gin.Context) { + svc.CouponReceive(c) +} diff --git a/app/hdl/hdl_store.go b/app/hdl/hdl_store.go index 6085b9e..58b445d 100644 --- a/app/hdl/hdl_store.go +++ b/app/hdl/hdl_store.go @@ -50,6 +50,9 @@ func BankStore(c *gin.Context) { func Store(c *gin.Context) { svc.Store(c) } +func StoreLike(c *gin.Context) { + svc.StoreLike(c) +} func StoreAddLike(c *gin.Context) { svc.StoreAddLike(c) } diff --git a/app/router/router.go b/app/router/router.go index cc0df9e..c730b07 100644 --- a/app/router/router.go +++ b/app/router/router.go @@ -49,9 +49,12 @@ func routeCommunityTeam(r *gin.RouterGroup) { r.GET("/bank/store/cate", hdl.BankStoreCate) r.POST("/bank/store/list", hdl.BankStore) r.POST("/store", hdl.Store) + r.POST("/store/like/list", hdl.StoreLike) r.GET("/goods/cate", hdl.Cate) r.POST("/goods", hdl.Goods) r.POST("/goods/sku", hdl.GoodsSku) + r.POST("/coupon", hdl.Coupon) + r.POST("/coupon/receive", hdl.CouponReceive) r.GET("/user", hdl.User) // 用户授权后调用的接口 r.Use(mw.AuthJWT) diff --git a/app/svc/svc_coupon.go b/app/svc/svc_coupon.go new file mode 100644 index 0000000..3c9c80e --- /dev/null +++ b/app/svc/svc_coupon.go @@ -0,0 +1,82 @@ +package svc + +import ( + "applet/app/db" + "applet/app/db/model" + "applet/app/e" + "applet/app/utils" + "fmt" + "github.com/gin-gonic/gin" + "github.com/jinzhu/copier" + "time" +) + +func CouponReceive(c *gin.Context) { + var arg map[string]string + if err := c.ShouldBindJSON(&arg); err != nil { + e.OutErr(c, e.ERR_INVALID_ARGS, err) + return + } + coupon := db.GetCouponById(MasterDb(c), arg["id"]) + if coupon == nil { + e.OutErr(c, 400, e.NewErr(400, "优惠券不存在")) + return + } + user := GetUser(c) + count, _ := MasterDb(c).Where("uid=? and merchant_scheme_id=?", user.Info.Uid, arg["id"]).Count(&model.CommunityTeamCouponUser{}) + if count > 0 { + e.OutErr(c, 400, e.NewErr(400, "您已领取过")) + return + } + var data model.CommunityTeamCouponUser + err := copier.Copy(&data, coupon) + fmt.Println(err) + data.MerchantSchemeId = int(data.Id) + data.Id = 0 + data.IsUse = 1 + data.Uid = user.Info.Uid + now := time.Now() + data.CreateTime = time.Now() + data.UpdateTime = time.Now() + if coupon.ValidTimeType == 2 { + data.ValidTimeStart = now + data.ValidTimeEnd = time.Unix(now.Unix()+int64(coupon.ValidDayNum)*86400, 0) + } + MasterDb(c).Insert(&data) + e.OutSuc(c, "success", nil) + return +} +func Coupon(c *gin.Context) { + var arg map[string]string + if err := c.ShouldBindJSON(&arg); err != nil { + e.OutErr(c, e.ERR_INVALID_ARGS, err) + return + } + cate := db.GetCoupon(MasterDb(c), arg) + list := make([]map[string]string, 0) + if cate != nil { + stateMap := []string{"", "立减", "满减", "折扣"} + scheme, host := ImageBucket(c) + for _, v := range *cate { + tmp := map[string]string{ + "id": utils.IntToStr(v.Id), + "name": v.Name, + "info": v.Info, + "type": stateMap[v.Kind], + "num": utils.IntToStr(v.Num), + "valid_time": "", + "img_url": ImageFormatWithBucket(scheme, host, v.Img), + "create_at": v.CreatedTime.Format("2006-01-02 15:04:05"), + } + if v.ValidTimeType == 1 { + tmp["valid_time"] = v.ValidTimeStart.Format("2006.01.02") + "-" + v.ValidTimeEnd.Format("2006.01.02") + } else { + tmp["valid_time"] = "领取后" + utils.IntToStr(v.ValidDayNum) + "天有效" + } + list = append(list, tmp) + } + } + + e.OutSuc(c, list, nil) + return +} diff --git a/app/svc/svc_order.go b/app/svc/svc_order.go index 106ef44..ad07670 100644 --- a/app/svc/svc_order.go +++ b/app/svc/svc_order.go @@ -233,7 +233,7 @@ func CommCoupon(c *gin.Context, totalPrice string) map[string]interface{} { user := GetUser(c) now := time.Now().Format("2006-01-02 15:04:05") var ActCouponUserList []*model.CommunityTeamCouponUser - sess := engine.Table("act_coupon_user"). + sess := engine. Where("store_type=? and uid = ? AND is_use = ? AND (valid_time_start < ? AND valid_time_end > ?)", 0, user.Info.Uid, 0, now, now) err = sess.Find(&ActCouponUserList) diff --git a/app/svc/svc_store.go b/app/svc/svc_store.go index 0370d83..0b12a51 100644 --- a/app/svc/svc_store.go +++ b/app/svc/svc_store.go @@ -60,6 +60,54 @@ func BankStore(c *gin.Context) { e.OutSuc(c, storeList, nil) return } +func StoreLike(c *gin.Context) { + var arg map[string]string + if err := c.ShouldBindJSON(&arg); err != nil { + e.OutErr(c, e.ERR_INVALID_ARGS, err) + return + } + arg["store_type"] = "0" + user, _ := GetDefaultUser(c, c.GetHeader("Authorization")) + store := db.GetStoreLike(MasterDb(c), arg) + storeList := make([]map[string]interface{}, 0) + for _, v := range store { + km := v["km"] + if utils.StrToFloat64(v["km"]) < 1 { + v["km"] = utils.IntToStr(int(utils.StrToFloat64(v["km"])*1000)) + "m" + } else { + v["km"] = GetCommissionPrec(c, v["km"], "2", "1") + v["km"] += "km" + } + if utils.StrToFloat64(km) <= 0 || utils.StrToFloat64(v["lat"]) == 0 || utils.StrToFloat64(v["lng"]) == 0 { + v["km"] = "-" + } + label := make([]string, 0) + tmp := map[string]interface{}{ + "lat": v["lat"], + "lng": v["lng"], + "address": v["address"], + "work_state": v["work_state"], + "name": v["name"], + "id": v["id"], + "uid": v["uid"], + "km": v["km"], + "time_str": v["timer"], + "phone": v["phone"], + "label": label, + "is_like": "0", + } + if user != nil { + count, _ := MasterDb(c).Where("uid=? and store_id=?", user.Info.Uid, v["id"]).Count(&model.CommunityTeamStoreLike{}) + if count > 0 { + tmp["is_like"] = "1" + } + } + + storeList = append(storeList, tmp) + } + e.OutSuc(c, storeList, nil) + return +} func Store(c *gin.Context) { var arg map[string]string if err := c.ShouldBindJSON(&arg); err != nil {