Browse Source

Merge branch 'yun-group-buy' of k.zhang/duoduo into yun-test

k.zhang 5 months ago
parent
commit
d5d2e1334b

+ 16 - 9
apis/shanghu/base.go

@@ -1,6 +1,8 @@
 package shanghu
 
-import "github.com/gin-gonic/gin"
+import (
+	"github.com/gin-gonic/gin"
+)
 
 func InitShangHuRouter(engine *gin.RouterGroup) {
 	v1 := engine.Group("v1")
@@ -72,13 +74,18 @@ func InitShangHuRouter(engine *gin.RouterGroup) {
 
 	activeV1 := engine.Group("activeV1")
 	{
-		activeV1.POST("/active/draw/product/create", DrawProductCreate) //抽奖奖品创建
-		activeV1.POST("/active/group/buy/create", GroupBuyCreate)       //拼团创建
-		activeV1.POST("/active/config/create", ActiveConfigCreate)      //创建活动
-		activeV1.POST("/active/config/list", ActiveConfigList)          //活动列表
-		activeV1.POST("/active/config/info", ActiveConfigInfo)          //活动详情
-		activeV1.POST("/active/draw/product/list", DrawProductList)     //奖品列表
-		activeV1.POST("/active/group/buy/list", GroupBuyList)           //拼团列表
-		// 中奖
+		activeV1.POST("/active/draw/product/create", DrawProductCreate)   //抽奖奖品创建
+		activeV1.POST("/active/group/buy/create", GroupBuyCreate)         //拼团创建
+		activeV1.POST("/active/config/create", ActiveConfigCreate)        //创建活动
+		activeV1.POST("/active/config/list", ActiveConfigList)            //活动列表
+		activeV1.POST("/active/config/info", ActiveConfigInfo)            //活动详情
+		activeV1.POST("/active/draw/product/list", DrawProductList)       //奖品列表
+		activeV1.POST("/active/group/buy/list", GroupBuyList)             //拼团列表
+		activeV1.POST("/active/draw", Draw)                               //抽奖
+		activeV1.POST("/active/draw/list", DrawLog)                       //中奖列表
+		activeV1.POST("/draw/verification/code", DrawLogVerificationCode) //奖品核销码                                           //中奖核销码
+		activeV1.POST("/draw/cancel", DrawCancel)                         //核销 奖品与拼团
+		//
+
 	}
 }

+ 141 - 0
apis/shanghu/client.active.draw.log.go

@@ -0,0 +1,141 @@
+package shanghu
+
+import (
+	"duoduo/apis/shanghu/models"
+	"duoduo/models/shanghu"
+	"duoduo/tools"
+	"duoduo/tools/app"
+	"encoding/base64"
+	"encoding/json"
+	"errors"
+	"github.com/gin-gonic/gin"
+	"github.com/skip2/go-qrcode"
+	"strconv"
+)
+
+func DrawLog(c *gin.Context) {
+	var inData models.ClientDrawLogRequest
+	var sqlData shanghu.ClientActiveDrawLog
+	var outData []models.ClientDrawLogReply
+
+	err := c.ShouldBindJSON(&inData)
+	if err != nil {
+		app.Error(c, 400, err, err.Error())
+		return
+	}
+
+	var pageSize = 10
+	var pageIndex = 1
+
+	if inData.PageSize != 0 {
+		pageSize = inData.PageSize
+	}
+	if inData.PageIndex != 0 {
+		pageIndex = inData.PageIndex
+	}
+
+	sqlData.ClientOpenID = inData.ClientOpenID
+	activeDrawLogList, count, err := sqlData.GetClientActiveDrawLogListByOpenId(pageSize, pageIndex)
+	if err != nil {
+		app.Error(c, 500, err, err.Error())
+		return
+	}
+
+	for _, v := range activeDrawLogList {
+		//查询商品名称
+		var drawProduct shanghu.MerchantActiveDrawProduct
+		var drawLog models.ClientDrawLogReply
+		drawProduct.ID = v.DrawProductID
+		drawProductInfo, err := drawProduct.GetDrawProductById()
+		if err != nil {
+			app.Error(c, 500, err, err.Error())
+			return
+		}
+		drawLog.DrawProductName = drawProductInfo.DrawProductName
+		drawLog.IsPrize = v.IsPrize
+		drawLog.Id = v.ID
+		outData = append(outData, drawLog)
+	}
+
+	app.PageOK(c, outData, count, pageIndex, pageSize, app.Success)
+
+}
+
+func DrawLogVerificationCode(c *gin.Context) {
+	var inData models.DrawLogVerificationCodeRequest
+	var outData models.DrawLogVerificationCodeReply
+	var sqlData shanghu.ClientActiveDrawLog
+
+	err := c.ShouldBindJSON(&inData)
+	if err != nil {
+		app.Error(c, 400, err, err.Error())
+		return
+	}
+
+	sqlData.DrawProductID = inData.DrawLogId
+	drawLog, err := sqlData.GetClientActiveDrawLogListById()
+	if err != nil {
+		app.Error(c, 500, err, err.Error())
+		return
+	}
+
+	if drawLog.IsPrize == models.NotWon {
+		app.Error(c, 500, errors.New("不需要兑奖"), "不需要兑奖")
+		return
+	}
+
+	outData.Key = "DRAW" + tools.MD5(strconv.FormatInt(inData.DrawLogId, 10)+"zhangkun429@")
+	outData.DrawLogId = inData.DrawLogId
+
+	str, err := json.Marshal(&outData)
+	if err != nil {
+		app.Error(c, 500, err, err.Error())
+		return
+	}
+	qr, err := qrcode.Encode(string(str), qrcode.High, 200)
+	if err != nil {
+		app.Error(c, 500, err, err.Error())
+		return
+	}
+	encodedData := base64.StdEncoding.EncodeToString(qr)
+
+	app.OK(c, encodedData, app.Success)
+}
+
+func DrawCancel(c *gin.Context) {
+	var inData models.DrawCancelRequest
+	var sqlData shanghu.ClientActiveDrawLog
+
+	err := c.ShouldBindJSON(&inData)
+	if err != nil {
+		app.Error(c, 400, err, err.Error())
+		return
+	}
+
+	key := "DRAW" + tools.MD5(strconv.FormatInt(inData.DrawLogId, 10)+"zhangkun429@")
+	if key != inData.Key {
+		app.Error(c, 500, errors.New("数据错误"), "数据错误")
+		return
+	}
+
+	sqlData.ID = inData.DrawLogId
+	drawLog, err := sqlData.GetClientActiveDrawLogListById()
+	if err != nil {
+		app.Error(c, 500, err, err.Error())
+		return
+	}
+
+	if drawLog.IsPrize != models.NotClaimed {
+		app.Error(c, 500, errors.New("不允许兑奖"), "不允许兑奖")
+		return
+	} else {
+		err = drawLog.UpdateDrawPrize()
+		if err != nil {
+			app.Error(c, 500, err, err.Error())
+			return
+		}
+	}
+
+	app.OK(c, nil, app.Success)
+
+}

+ 280 - 1
apis/shanghu/merchant.active.config.go

@@ -6,7 +6,9 @@ import (
 	"duoduo/tools"
 	"duoduo/tools/app"
 	"errors"
+	"fmt"
 	"github.com/gin-gonic/gin"
+	"math/rand"
 	"time"
 )
 
@@ -63,6 +65,22 @@ func ActiveConfigCreate(c *gin.Context) {
 		return
 	}
 
+	//必中校验
+	//if inData.DrawOneBiZhong > 0 {
+	//	var checkBZ shanghu.MerchantActiveDrawProduct
+	//	checkBZ.ID = inData.DrawOneBiZhong
+	//	bZInfo, err := checkBZ.GetDrawProductById()
+	//	if err != nil {
+	//		app.Error(c, 500, err, err.Error())
+	//		return
+	//	}
+	//
+	//	if !bZInfo.IsPrize {  //
+	//		app.Error(c, 500, err, err.Error())
+	//		return
+	//	}
+	//}
+
 	sqlData.ActiveName = inData.ActiveName
 	sqlData.MerchantOpenID = inData.MerchantOpenID
 	sqlData.CreatedAt = time.Now()
@@ -233,7 +251,7 @@ func ActiveConfigList(c *gin.Context) {
 
 	for _, v := range activeConfigList {
 		var activeConfig models.ActiveConfigListReply
-
+		activeConfig.ID = v.ID
 		activeConfig.ActivityStart = tools.TimeToStr(v.ActivityStartTime)
 		activeConfig.ActivityEnd = tools.TimeToStr(v.ActivityEndTime)
 		activeConfig.ActiveName = v.ActiveName
@@ -246,3 +264,264 @@ func ActiveConfigList(c *gin.Context) {
 	app.PageOK(c, outData, count, pageIndex, pageSize, app.Success)
 
 }
+
+// 抽奖
+func Draw(c *gin.Context) {
+	var inData models.DrawRequest
+	var sqlData shanghu.MerchantActiveConfig
+	var outData models.DrawReply
+
+	err := c.ShouldBindJSON(&inData)
+	if err != nil {
+		app.Error(c, 400, err, err.Error())
+		return
+	}
+	// 先校验是否有必中抽奖
+	sqlData.ID = inData.ActiveConfigID
+	config, err := sqlData.GetConfigInfoById()
+	if err != nil {
+		app.Error(c, 500, err, err.Error())
+		return
+	}
+
+	if config.DrawOneBiZhong > 0 {
+		// 必中抽奖需要产看中奖记录是否中过,没有中过直接中,中过了走正常的中奖概率
+		var drawConfig shanghu.ClientActiveDrawLog
+		drawConfig.ActiveConfigID = inData.ActiveConfigID
+		drawConfig.ClientOpenID = inData.ClientOpenID
+		drawConfig.DrawProductID = config.DrawOneBiZhong
+
+		count, err := drawConfig.GetClientActiveDrawLogByBiZHong()
+		if err != nil {
+			app.Error(c, 500, err, err.Error())
+			return
+		}
+
+		// 为0直接中奖 写中奖记录并且返回 查询中奖
+		if count == 0 {
+			//var draw shanghu.ClientActiveDrawLog
+			var drawProduct shanghu.MerchantActiveDrawProduct
+
+			drawProduct.ID = config.DrawOneBiZhong
+			drawProductInfo, err := drawProduct.GetDrawProductById()
+			if err != nil {
+				app.Error(c, 500, err, err.Error())
+				return
+			}
+
+			if drawProductInfo.ActiveConfigID != inData.ActiveConfigID {
+				app.Error(c, 500, errors.New("必中配置活动不一致"), "必中配置活动不一致")
+				return
+			}
+
+			if drawProductInfo.Stock <= 0 {
+				app.Error(c, 500, errors.New("库存不足"), "库存不足")
+				return
+			}
+
+			fmt.Println(drawProductInfo)
+
+			//新建log 减库存
+			err = subStockAddLog(drawProductInfo, inData.ClientOpenID)
+			if err != nil {
+				app.Error(c, 500, err, err.Error())
+				return
+			}
+
+			//draw.DrawProductID = config.DrawOneBiZhong
+			//draw.ActiveConfigID = inData.ActiveConfigID
+			//draw.ClientOpenID = inData.ClientOpenID
+			//
+			//if drawProductInfo.IsPrize {
+			//	draw.IsPrize = models.NotClaimed //未兑奖 必中一定是要能兑奖的。
+			//} else {
+			//	draw.IsPrize = models.NotWon //未中奖
+			//}
+			//
+			//draw.CreatedAt = time.Now()
+			//draw.UpdatedAt = time.Now()
+			//_, err = draw.Create() //创建完成
+			//if err != nil {
+			//	app.Error(c, 500, err, err.Error())
+			//	return
+			//}
+
+			outData.DrawProductName = drawProductInfo.DrawProductName
+			outData.DrawUrl = drawProductInfo.DrawUrl
+			outData.ID = config.DrawOneBiZhong
+
+			app.OK(c, outData, app.Success)
+			return
+		}
+
+	}
+
+	var drawProductList shanghu.MerchantActiveDrawProduct
+	var prizeList []Prize
+	drawProductList.ActiveConfigID = inData.ActiveConfigID
+	drawProductInfo, _, err := drawProductList.GetDrawProductListByActiveId()
+	if err != nil {
+		app.Error(c, 500, err, err.Error())
+		return
+	}
+
+	for _, v := range drawProductInfo {
+		var prize Prize
+		if v.Stock <= 0 {
+			continue
+		}
+		prize.Name = v.DrawProductName
+		prize.ID = v.ID
+		prize.Weight = v.DrawOdds
+		prize.InitStock = v.TotalStock
+		prize.Stock = v.Stock
+		prizeList = append(prizeList, prize)
+
+	}
+	//fmt.Println(prizeList)
+	// 创建抽奖实例
+	lottery := NewLottery(prizeList)
+
+	// 执行1次抽奖
+	results, err := lottery.Draw(1)
+	if err != nil {
+		app.Error(c, 500, err, err.Error())
+		return
+	}
+	fmt.Println(results)
+	var drawProduct shanghu.MerchantActiveDrawProduct
+	for _, v := range drawProductInfo {
+		if results[0].ID == v.ID {
+			outData.ID = v.ID
+			outData.DrawUrl = v.DrawUrl
+			outData.DrawProductName = v.DrawProductName
+			drawProduct = v
+			break
+		}
+	}
+
+	//库存减一 并且新增记录
+	err = subStockAddLog(drawProduct, inData.ClientOpenID)
+	if err != nil {
+		app.Error(c, 500, err, err.Error())
+		return
+	}
+
+	app.OK(c, outData, app.Success)
+
+}
+
+type Prize struct {
+	ID        int64  // 奖品ID
+	Name      string // 奖品名称
+	Weight    int    // 权重
+	Stock     int    // 库存数量
+	InitStock int    // 初始库存(用于展示)
+}
+
+// 带权重和库存的抽奖
+type Lottery struct {
+	Prizes      []Prize
+	TotalWeight int
+}
+
+func NewLottery(prizes []Prize) *Lottery {
+	totalWeight := 0
+	for _, prize := range prizes {
+		totalWeight += prize.Weight
+	}
+
+	// 深拷贝奖品列表并初始化初始库存
+	prizeCopy := make([]Prize, len(prizes))
+	for i, p := range prizes {
+		prizeCopy[i] = p
+		prizeCopy[i].InitStock = p.Stock
+	}
+
+	return &Lottery{
+		Prizes:      prizeCopy,
+		TotalWeight: totalWeight,
+	}
+}
+
+func (l *Lottery) Draw(times int) ([]Prize, error) {
+	// 检查总库存是否足够
+	totalStock := 0
+	for _, prize := range l.Prizes {
+		totalStock += prize.Stock
+	}
+	if totalStock < times {
+		return nil, fmt.Errorf("剩余总库存(%d)不足以支持%d次抽奖", totalStock, times)
+	}
+
+	rand.Seed(time.Now().UnixNano())
+	results := make([]Prize, 0, times)
+
+	for i := 0; i < times; i++ {
+		// 如果没有可用奖品了就停止
+		if l.TotalWeight <= 0 {
+			break
+		}
+
+		randNum := rand.Intn(l.TotalWeight)
+		currentWeight := 0
+
+		for j := range l.Prizes {
+			if l.Prizes[j].Stock <= 0 {
+				continue
+			}
+
+			currentWeight += l.Prizes[j].Weight
+			if randNum < currentWeight {
+				// 中奖
+				results = append(results, l.Prizes[j])
+				// 减少库存
+				l.Prizes[j].Stock--
+				// 如果库存耗尽,更新总权重
+				if l.Prizes[j].Stock == 0 {
+					l.TotalWeight -= l.Prizes[j].Weight
+				}
+				break
+			}
+		}
+	}
+
+	return results, nil
+}
+
+// 新建记录,减库存
+func subStockAddLog(drawProductInfo shanghu.MerchantActiveDrawProduct, clientOpenId string) error {
+	var draw shanghu.ClientActiveDrawLog
+	//var drawProduct shanghu.MerchantActiveDrawProduct
+
+	if drawProductInfo.Stock <= 0 {
+
+		return errors.New("库存不足")
+	}
+
+	//减库存
+	err := drawProductInfo.SubStock()
+	if err != nil {
+		return err
+	}
+
+	// 新建记录
+	draw.DrawProductID = drawProductInfo.ID
+	draw.ActiveConfigID = drawProductInfo.ActiveConfigID
+	draw.ClientOpenID = clientOpenId
+
+	if drawProductInfo.IsPrize {
+		draw.IsPrize = models.NotClaimed //未兑奖 必中一定是要能兑奖的。
+	} else {
+		draw.IsPrize = models.NotWon //未中奖
+	}
+
+	draw.CreatedAt = time.Now()
+	draw.UpdatedAt = time.Now()
+	_, err = draw.Create() //创建完成
+	if err != nil {
+		return err
+	}
+
+	return nil
+}

+ 4 - 3
apis/shanghu/merchant.active.draw.product.go

@@ -30,8 +30,9 @@ func DrawProductCreate(c *gin.Context) {
 		return
 	}
 
-	if inData.DrawOdds >= 100 {
+	if inData.DrawOdds > 100 {
 		app.Error(c, 500, errors.New("中奖率不能大于100"), "中奖率不能大于100")
+		return
 
 	}
 
@@ -46,7 +47,7 @@ func DrawProductCreate(c *gin.Context) {
 	sqlData.TotalStock = inData.TotalStock
 	sqlData.MerchantOpenID = inData.MerchantOpenID
 	sqlData.DrawProductName = inData.DrawProductName
-	sqlData.Stock = 0
+	sqlData.Stock = inData.TotalStock
 	sqlData.DrawUrl = inData.DrawUrl
 	sqlData.Version = 0
 	sqlData.IsPrize = inData.IsPrize
@@ -60,7 +61,7 @@ func DrawProductCreate(c *gin.Context) {
 	app.OK(c, nil, app.Success)
 }
 
-// 商品列表
+// 奖品商品列表
 func DrawProductList(c *gin.Context) {
 	var inData models.DrawProductListRequest
 	var sqlData shanghu.MerchantActiveDrawProduct

+ 21 - 0
apis/shanghu/models/active.config.go

@@ -74,8 +74,29 @@ type ActiveConfigListRequest struct {
 }
 
 type ActiveConfigListReply struct {
+	ID            int64  `json:"id"`             //id
 	ActiveName    string `json:"active_name"`    // 活动名称
 	ActivityEnd   string `json:"activity_end"`   // 活动结束时间
 	ActivityStart string `json:"activity_start"` //活动开始时间
 	CreatedAt     string `json:"created_at"`     //创建时间
 }
+
+// 抽奖
+type DrawRequest struct {
+	ClientOpenID   string `json:"client_open_id"`   //open-id
+	ActiveConfigID int64  `json:"active_config_id"` //活动ID
+}
+
+// 奖品返回
+type DrawReply struct {
+	ID              int64  `json:"id"`                //id
+	DrawUrl         string `json:"draw_url"`          //奖品url
+	DrawProductName string `json:"draw_product_name"` //奖品名称
+	Version         int    `json:"version"`
+}
+
+const (
+	NotWon     = 1 //未中奖
+	NotClaimed = 2 //未兑奖
+	Claimed    = 3 //已兑奖
+)

+ 27 - 0
apis/shanghu/models/client.active.draw.log.go

@@ -0,0 +1,27 @@
+package models
+
+type ClientDrawLogRequest struct {
+	ClientOpenID string `json:"client_open_id"` //
+	PageSize     int    `json:"page_size"`
+	PageIndex    int    `json:"page_index"`
+}
+
+type ClientDrawLogReply struct {
+	DrawProductName string `json:"draw_product_name"` //奖品名称
+	IsPrize         int    `json:"is_prize"`          //1-未中奖 2-未兑奖 3-已兑奖
+	Id              int64  `json:"id"`                //id
+}
+
+type DrawLogVerificationCodeRequest struct {
+	DrawLogId int64 `json:"draw_log_id"`
+}
+
+type DrawLogVerificationCodeReply struct {
+	DrawLogId int64  `json:"draw_log_id"`
+	Key       string `json:"key"`
+}
+
+type DrawCancelRequest struct {
+	DrawLogId int64  `json:"draw_log_id"`
+	Key       string `json:"key"`
+}

+ 53 - 0
models/shanghu/active.draw.config.go

@@ -0,0 +1,53 @@
+package shanghu
+
+//import (
+//	orm "duoduo/database"
+//	"time"
+//)
+//
+//type ClientActiveDrawLog struct {
+//	ID             int64     `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"`
+//	ActiveConfigID int64     `gorm:"column:active_config_id;type:bigint(20)" json:"active_config_id"` // 活动抽奖id
+//	ClientOpenID   string    `gorm:"column:client_open_id;type:varchar(255)" json:"client_open_id"`
+//	DrawProductID  int64     `gorm:"column:draw_product_id;type:bigint(20)" json:"draw_product_id"`     // 中奖商品
+//	CreateBy       int64     `gorm:"column:create_by;type:bigint(20)" json:"create_by"`                 // 创建者
+//	UpdateBy       int64     `gorm:"column:update_by;type:bigint(20)" json:"update_by"`                 // 更新者
+//	CreatedAt      time.Time `gorm:"column:created_at;type:datetime(3)" json:"created_at"`              // 创建时间
+//	UpdatedAt      time.Time `gorm:"column:updated_at;type:datetime(3)" json:"updated_at"`              // 最后更新时间
+//	DeletedAt      time.Time `gorm:"column:deleted_at;type:datetime(3);default:null" json:"deleted_at"` // 删除时间
+//	IsPrize        int       `gorm:"column:is_prize;type:int(11)" json:"is_prize"`                      // 1-未中奖 2-未兑奖 3-已兑奖
+//}
+//
+//func (m *ClientActiveDrawLog) TableName() string {
+//	return "client_active_draw_log"
+//}
+//
+//func (m *ClientActiveDrawLog) GetClientActiveDrawLogByBiZHong() (int64, error) {
+//	var count int64
+//
+//	table := orm.ShMysql.Table(m.TableName())
+//	table = table.Where("active_config_id = ? and client_open_id = ? and  draw_product_id = ? ",
+//		m.ActiveConfigID, m.ClientOpenID, m.DrawProductID)
+//	if err := table.Count(&count).Error; err != nil {
+//		return 0, nil
+//	}
+//	return count, nil
+//
+//}
+//
+//func (m *ClientActiveDrawLog) Create() (ClientActiveDrawLog, error) {
+//	var doc ClientActiveDrawLog
+//	var err error
+//
+//	doc = *m
+//	err = orm.ShMysql.Table(m.TableName()).Create(&doc).Error
+//	if err != nil {
+//		return doc, err
+//	}
+//
+//	return doc, nil
+//}
+
+//func (m *ClientActiveDrawLog) CreateClientActiveDraw()error  {
+//
+//}

+ 88 - 0
models/shanghu/client.active.draw.log.go

@@ -0,0 +1,88 @@
+package shanghu
+
+import (
+	orm "duoduo/database"
+	"duoduo/tools"
+	"time"
+)
+
+type ClientActiveDrawLog struct {
+	ID             int64     `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"`
+	ActiveConfigID int64     `gorm:"column:active_config_id;type:bigint(20)" json:"active_config_id"` // 活动抽奖id
+	ClientOpenID   string    `gorm:"column:client_open_id;type:varchar(255)" json:"client_open_id"`
+	DrawProductID  int64     `gorm:"column:draw_product_id;type:bigint(20)" json:"draw_product_id"` // 中奖商品
+	CreateBy       int64     `gorm:"column:create_by;type:bigint(20)" json:"create_by"`             // 创建者
+	UpdateBy       int64     `gorm:"column:update_by;type:bigint(20)" json:"update_by"`             // 更新者
+	CreatedAt      time.Time `gorm:"column:created_at;type:datetime(3)" json:"created_at"`          // 创建时间
+	UpdatedAt      time.Time `gorm:"column:updated_at;type:datetime(3)" json:"updated_at"`          // 最后更新时间
+	DeletedAt      time.Time `gorm:"column:deleted_at;type:datetime(3)" json:"deleted_at"`          // 删除时间
+	IsPrize        int       `gorm:"column:is_prize;type:int(11)" json:"is_prize"`                  // 1-未中奖 2-未兑奖 3-已兑奖
+}
+
+func (m *ClientActiveDrawLog) TableName() string {
+	return "client_active_draw_log"
+}
+
+func (m *ClientActiveDrawLog) GetClientActiveDrawLogByBiZHong() (int64, error) {
+	var count int64
+
+	table := orm.ShMysql.Table(m.TableName())
+	table = table.Where("active_config_id = ? and client_open_id = ? and  draw_product_id = ? ",
+		m.ActiveConfigID, m.ClientOpenID, m.DrawProductID)
+	if err := table.Count(&count).Error; err != nil {
+		return 0, nil
+	}
+	return count, nil
+
+}
+
+func (m *ClientActiveDrawLog) Create() (ClientActiveDrawLog, error) {
+	var doc ClientActiveDrawLog
+	var err error
+
+	doc = *m
+	err = orm.ShMysql.Table(m.TableName()).Create(&doc).Error
+	if err != nil {
+		return doc, err
+	}
+
+	return doc, nil
+}
+func (m *ClientActiveDrawLog) GetClientActiveDrawLogListByOpenId(pageSize int, pageIndex int) ([]ClientActiveDrawLog, int, error) {
+	var doc []ClientActiveDrawLog
+
+	table := orm.ShMysql.Table(m.TableName())
+
+	table = table.Where("client_open_id = ? ", m.ClientOpenID)
+	var count int
+	if err := table.Select("*").Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
+		return nil, 0, err
+	}
+	table.Count(&count)
+	return doc, count, nil
+}
+
+func (m *ClientActiveDrawLog) GetClientActiveDrawLogListById() (ClientActiveDrawLog, error) {
+	var doc ClientActiveDrawLog
+
+	table := orm.ShMysql.Table(m.TableName())
+
+	table = table.Where("id = ? ", m.ID)
+	if err := table.Select("*").Order("id desc").First(&doc).Error; err != nil {
+		return doc, err
+	}
+
+	return doc, nil
+}
+
+func (m *ClientActiveDrawLog) UpdateDrawPrize() error {
+
+	if err := orm.ShMysql.Table(m.TableName()).Model(&m).Where("id = ? ", m.ID).Updates(
+		map[string]interface{}{
+			"is_prize":   m.IsPrize,
+			"updated_at": tools.GetCurrntTimeStr()}).Error; err != nil {
+		return err
+	}
+
+	return nil
+}

+ 51 - 1
models/shanghu/merchant.active.draw.product.go

@@ -2,6 +2,7 @@ package shanghu
 
 import (
 	orm "duoduo/database"
+	"duoduo/tools"
 	"errors"
 	"time"
 )
@@ -52,7 +53,7 @@ func (u *MerchantActiveDrawProduct) UpdateConfigId(drawId int64) error {
 		return res.Error
 	}
 	if res.RowsAffected <= 0 {
-		return errors.New("未更新数据")
+		return errors.New("未更新抽奖数据")
 	}
 	return nil
 
@@ -101,3 +102,52 @@ func (m *MerchantActiveDrawProduct) GetDrawProductListById(idList []int64) ([]Me
 	table.Count(&count)
 	return doc, count, nil
 }
+
+// 中奖商品
+func (m *MerchantActiveDrawProduct) GetDrawProductById() (MerchantActiveDrawProduct, error) {
+	var doc MerchantActiveDrawProduct
+
+	table := orm.ShMysql.Table(m.TableName())
+
+	table = table.Where("id = ? ", m.ID)
+	var count int
+	if err := table.Select("*").First(&doc).Error; err != nil {
+		return doc, err
+	}
+	table.Count(&count)
+	return doc, nil
+}
+
+// 奖品列表
+func (m *MerchantActiveDrawProduct) GetDrawProductListByActiveId() ([]MerchantActiveDrawProduct, int, error) {
+	var doc []MerchantActiveDrawProduct
+
+	table := orm.ShMysql.Table(m.TableName())
+
+	table = table.Where("active_config_id = ? ", m.ActiveConfigID)
+	var count int
+	if err := table.Select("*").Order("id desc").Find(&doc).Error; err != nil {
+		return nil, 0, err
+	}
+	table.Count(&count)
+	return doc, count, nil
+}
+
+func (m *MerchantActiveDrawProduct) SubStock() error {
+
+	table := orm.ShMysql.Table(m.TableName())
+	result := table.Model(&MerchantActiveDrawProduct{}).Where("id = ? and version = ? ", m.ID, m.Version).Updates(
+		map[string]interface{}{
+			"version":    m.Version + 1,
+			"stock":      m.Stock - 1,
+			"updated_at": tools.GetCurrntTimeStr()})
+
+	if result.Error != nil {
+		return result.Error
+	}
+	if result.RowsAffected <= 0 {
+		err := errors.New("减库存失败,请重新再试。")
+		return err
+	}
+	return nil
+}

+ 1 - 1
models/shanghu/merchant.active.group.buy.go

@@ -58,7 +58,7 @@ func (u *MerchantActiveGroupBuy) UpdateConfigId(groupBuyId int64) error {
 		return res.Error
 	}
 	if res.RowsAffected <= 0 {
-		return errors.New("未更新数据")
+		return errors.New("未更新拼团数据")
 	}
 	return nil