|
|
package flashsale
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
mysql2 "git.oa00.com/go/mysql"
|
|
|
"github.com/golangkit/formatime"
|
|
|
"github.com/pkg/errors"
|
|
|
"github.com/shopspring/decimal"
|
|
|
"gorm.io/gorm"
|
|
|
"math/rand"
|
|
|
"recook/internal/cache"
|
|
|
"recook/internal/libs/bean"
|
|
|
"recook/internal/v2/logic/manage/message"
|
|
|
"recook/internal/v2/model/flashsale"
|
|
|
"recook/internal/v2/model/recook/goods"
|
|
|
"strconv"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
type sale struct {
|
|
|
}
|
|
|
|
|
|
var FlashSale = &sale{}
|
|
|
|
|
|
//提报新秒杀活动
|
|
|
|
|
|
func (s sale) Add(name string, body string, activityStart string, activityEnd string, enrollStart string, enrollEnd string) error {
|
|
|
|
|
|
//解析时间
|
|
|
if activityStart == "" || activityEnd == "" || enrollStart == "" || enrollEnd == "" {
|
|
|
return errors.New("时间必填全")
|
|
|
}
|
|
|
start1 := jxTime(activityStart)
|
|
|
end1 := jxTime(activityEnd)
|
|
|
start2 := jxTime(enrollStart)
|
|
|
end2 := jxTime(enrollEnd)
|
|
|
//step1:新增活动到数据库
|
|
|
var one = flashsale.RecookSecKillModel{
|
|
|
ActivityName: name,
|
|
|
ActivityBody: body,
|
|
|
ActivityStartTime: start1,
|
|
|
ActivityEndTime: end1,
|
|
|
EnrollStartTime: start2,
|
|
|
EnrollEndTime: end2,
|
|
|
Status: 0,
|
|
|
}
|
|
|
nms := message.MRequest{
|
|
|
Title: name,
|
|
|
SendingType: 1,
|
|
|
Timing: 2,
|
|
|
Text: body,
|
|
|
Type: 1,
|
|
|
}
|
|
|
|
|
|
msg := message.MessageLogic
|
|
|
//事务
|
|
|
err := mysql2.Db.Transaction(func(tx *gorm.DB) error {
|
|
|
//add
|
|
|
err := tx.Table(one.TableName()).Create(&one).Error
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
//message
|
|
|
err = msg.AddNewMessage(nms)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
return nil
|
|
|
|
|
|
})
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
type showListRes struct {
|
|
|
flashsale.RecookSecKillModel
|
|
|
GysNum uint `json:"gys_num"`
|
|
|
GoodsNum uint `json:"goods_num"`
|
|
|
SortList string `json:"sort_list"`
|
|
|
OrderNum uint `json:"order_num"`
|
|
|
Amount decimal.Decimal `json:"amount"`
|
|
|
}
|
|
|
type sorts struct {
|
|
|
ShowSortId uint `json:"show_sort_id"`
|
|
|
ShowTimeStart time.Time `json:"show_time_start"`
|
|
|
ShowTimeEnd time.Time `json:"show_time_end"`
|
|
|
}
|
|
|
|
|
|
//条件显示总表数据
|
|
|
func (s sale) ShowList(title string, status uint, activityStart string, activityEnd string, enrollStart string, enrollEnd string, limit int, offset int) ([]showListRes, int64) {
|
|
|
|
|
|
where := mysql2.Db.Where("1=1")
|
|
|
//解析时间
|
|
|
if activityStart != "" {
|
|
|
start1 := jxTime(activityStart)
|
|
|
where = where.Where("activity_start_time>=?", start1)
|
|
|
}
|
|
|
if activityEnd != "" {
|
|
|
end1 := jxTime(activityEnd)
|
|
|
where = where.Where("activity_end_time<=?", end1)
|
|
|
}
|
|
|
if enrollStart != "" {
|
|
|
start2 := jxTime(enrollStart)
|
|
|
where = where.Where("enroll_start_time>=?", start2)
|
|
|
}
|
|
|
if enrollEnd != "" {
|
|
|
end2 := jxTime(enrollEnd)
|
|
|
where = where.Where("enroll_end_time<=?", end2)
|
|
|
}
|
|
|
if title != "" {
|
|
|
where = where.Where("activity_name like (?)", fmt.Sprintf("%%%s%%", title))
|
|
|
}
|
|
|
|
|
|
switch status {
|
|
|
//1:报名中 2:配置商品(运营审核) 3:审核中(总监审核) 4:待开始 5:进行中 6:已结束
|
|
|
case 1:
|
|
|
//报名中 报名结束时间大于现在 报名开始时间小于当前
|
|
|
where = where.Where("enroll_end_time>? and enroll_start_time<?", formatime.NewSecondNow(), formatime.NewSecondNow())
|
|
|
case 2:
|
|
|
//待审核,运营总监还没审核
|
|
|
where = where.Where("status in(1,3)").Where("activity_start_time>?", formatime.NewSecondNow()).Where("enroll_end_time<?", formatime.NewSecondNow())
|
|
|
case 3:
|
|
|
//待审核,运营总监审核
|
|
|
where = where.Where("status=?", 4).Where("activity_start_time>?", formatime.NewSecondNow())
|
|
|
case 4:
|
|
|
//待开始,总监已结审核,但活动还没开始
|
|
|
where = where.Where("status=?", 2).Where("activity_start_time>?", formatime.NewSecondNow())
|
|
|
case 5:
|
|
|
//进行中,当前时间小于活动结束时间,大于开始时间
|
|
|
where = where.Where("activity_start_time<? and activity_end_time>?", formatime.NewSecondNow(), formatime.NewSecondNow()).Where("status=?", 2)
|
|
|
case 6:
|
|
|
//已结束 活动结束时间小于当前时间
|
|
|
where = where.Where("activity_end_time<?", formatime.NewSecondNow())
|
|
|
}
|
|
|
|
|
|
//获取list
|
|
|
var list []flashsale.RecookSecKillModel
|
|
|
q := mysql2.Db.Table((&flashsale.RecookSecKillModel{}).TableName()).Preload("SecGys").Preload("SecGoods").Where(where).Limit(limit).Offset(offset)
|
|
|
q.Find(&list)
|
|
|
//获取gys_num and goods_num
|
|
|
var result []showListRes
|
|
|
for _, v := range list {
|
|
|
one := showListRes{}
|
|
|
one.Id = v.Id
|
|
|
one.ActivityName = v.ActivityName
|
|
|
one.ActivityBody = v.ActivityBody
|
|
|
one.ActivityStartTime = v.ActivityStartTime
|
|
|
one.ActivityEndTime = v.ActivityEndTime
|
|
|
one.Status = v.Status
|
|
|
one.EnrollStartTime = v.EnrollStartTime
|
|
|
one.EnrollEndTime = v.EnrollEndTime
|
|
|
one.YunAuditTime = v.YunAuditTime
|
|
|
one.LastAuditTime = v.LastAuditTime
|
|
|
one.GysNum = uint(len(v.SecGys))
|
|
|
one.GoodsNum = uint(len(v.SecGoods))
|
|
|
showst := ""
|
|
|
var st []sorts
|
|
|
mysql2.Db.Table((&flashsale.RecookSecKillSortModel{}).TableName()).Where("sec_kill_activity_id=?", 12).Group("show_sort_id").Order("show_sort_id").Find(&st)
|
|
|
for _, j := range st {
|
|
|
showst += j.ShowTimeStart.Format("15:04:05") + "—" + j.ShowTimeEnd.Format("15:04:05") + "; "
|
|
|
}
|
|
|
one.SortList = showst
|
|
|
//if status == 6 {
|
|
|
// amount := decimal.Zero
|
|
|
// var orders []manage.RecookOrderGoodsDetailModel
|
|
|
// mysql2.Db.Table((&manage.RecookOrderGoodsDetailModel{}).TableName()).Where("created_at between ? and ?", v.ActivityStartTime, v.ActivityEndTime).Where("")
|
|
|
//}
|
|
|
result = append(result, one)
|
|
|
}
|
|
|
fmt.Println(result)
|
|
|
var total int64
|
|
|
mysql2.Db.Table((&flashsale.RecookSecKillModel{}).TableName()).Where(where).Count(&total)
|
|
|
return result, total
|
|
|
}
|
|
|
|
|
|
func jxTime(tim string) formatime.Second {
|
|
|
t1, _ := time.ParseInLocation("2006-01-02 15:04:05", tim, time.Local)
|
|
|
strat1 := formatime.NewSecondFrom(t1)
|
|
|
return strat1
|
|
|
}
|
|
|
|
|
|
type ShowGoodsRequest struct {
|
|
|
ActivityId uint `json:"activity_id" validate:"required" label:"活动id为必传项"`
|
|
|
GysId uint `json:"gys_id"`
|
|
|
GoodsName string `json:"goods_name"`
|
|
|
FirstCategoryID uint `json:"first_category_id"`
|
|
|
SecondCategoryID uint `json:"second_category_id"`
|
|
|
SkuCode string `json:"sku_code"`
|
|
|
GoodsNum string `json:"goods_num"`
|
|
|
StockStart decimal.Decimal `json:"stock_start"`
|
|
|
StockEnd decimal.Decimal `json:"stock_end"`
|
|
|
DiscountPriceStart decimal.Decimal `json:"discount_price_start"`
|
|
|
DiscountPriceEnd decimal.Decimal `json:"discount_price_end"`
|
|
|
PurchaseStart decimal.Decimal `json:"purchase_start"`
|
|
|
PurchaseEnd decimal.Decimal `json:"purchase_end"`
|
|
|
CommissionStart decimal.Decimal `json:"commission_start"`
|
|
|
CommissionEnd decimal.Decimal `json:"commission_end"`
|
|
|
Status int `json:"status"` //1,报名中(全部),2配置活动,待审核,待开始,进行中,已结束
|
|
|
bean.Page
|
|
|
}
|
|
|
type showGoodsListResponse struct {
|
|
|
SortId uint `json:"sort_id"`
|
|
|
ShowTimeStart time.Time `json:"show_time_start"`
|
|
|
ShowTimeEnd time.Time `json:"show_time_end"`
|
|
|
GoodsList []showList
|
|
|
Num int64 `json:"num"`
|
|
|
}
|
|
|
|
|
|
type showList struct {
|
|
|
GysName string `json:"gys_name"`
|
|
|
FirstCategory string `json:"first_category"`
|
|
|
SecondCategory string `json:"second_category"`
|
|
|
GoodsName string `json:"goods_name"`
|
|
|
GoodsId uint `json:"goods_id"`
|
|
|
SkuID uint `json:"sku_id"`
|
|
|
SkuName string `json:"sku_name"`
|
|
|
SkuCode string `json:"sku_code"`
|
|
|
GoodsNum string `json:"goods_num"`
|
|
|
MainPhoto string `json:"main_photo"`
|
|
|
DetailPhoto []string `json:"detail_photo"`
|
|
|
Purchase decimal.Decimal `json:"purchase"`
|
|
|
SecPurchase decimal.Decimal `json:"sec_purchase"`
|
|
|
SecStock decimal.Decimal `json:"sec_stock"`
|
|
|
CGPurchase decimal.Decimal `json:"cg_purchase"`
|
|
|
SecCGPurchase decimal.Decimal `json:"sec_cg_purchase"`
|
|
|
DiscountPrice decimal.Decimal `json:"discount_price"`
|
|
|
SecDiscountPrice decimal.Decimal `json:"sec_discount_price"`
|
|
|
SecCommissionRate decimal.Decimal `json:"sec_commission_rate"`
|
|
|
SecCommission decimal.Decimal `json:"sec_commission"`
|
|
|
SecMaoLiRate decimal.Decimal `json:"sec_mao_li_rate"`
|
|
|
SortNum uint `json:"sort_num"`
|
|
|
}
|
|
|
|
|
|
//显示供应商提报的商品
|
|
|
func (s sale) ShowGoodsList(p ShowGoodsRequest) (result []showGoodsListResponse, total int64) {
|
|
|
var idss []int
|
|
|
mysql2.Db.Table((&flashsale.RecookSecKillGoodsModel{}).TableName()).Where("sec_kill_activity_id=?", p.ActivityId).Pluck("goods_sku_id", &idss)
|
|
|
if len(idss) == 0 {
|
|
|
return result, total
|
|
|
}
|
|
|
where := mysql2.Db.Where("id in(?)", idss)
|
|
|
var info goods.RecookGoodsInfoModel
|
|
|
if p.GoodsName != "" {
|
|
|
var ids []int
|
|
|
mysql2.Db.Table(info.TableName()).Where("goods_name like (?)", fmt.Sprintf("%%%s%%", p.GoodsName)).Pluck("id", &ids)
|
|
|
where = where.Where("goods_id in(?)", ids)
|
|
|
}
|
|
|
if p.FirstCategoryID != 0 {
|
|
|
var ids []int
|
|
|
mysql2.Db.Table(info.TableName()).Where("first_category_id=?", p.FirstCategoryID).Pluck("id", &ids)
|
|
|
where = where.Where("goods_id in(?)", ids)
|
|
|
}
|
|
|
if p.SecondCategoryID != 0 {
|
|
|
var ids []int
|
|
|
mysql2.Db.Table(info.TableName()).Where("second_category_id=?", p.SecondCategoryID).Pluck("id", &ids)
|
|
|
where = where.Where("goods_id in(?)", ids)
|
|
|
}
|
|
|
if p.SkuCode != "" {
|
|
|
where = where.Where("code like (?)", fmt.Sprintf("%%%s%%", p.SkuCode))
|
|
|
}
|
|
|
if p.GoodsNum != "" {
|
|
|
where = where.Where("goods_num like (?)", fmt.Sprintf("%%%s%%", p.GoodsNum))
|
|
|
}
|
|
|
if p.GysId != 0 {
|
|
|
var ids []int
|
|
|
mysql2.Db.Table(info.TableName()).Where("b.vendor_id=?", p.GysId).Pluck("id", &ids)
|
|
|
where = where.Where("goods_id in(?)", ids)
|
|
|
}
|
|
|
num0 := decimal.NewFromInt(0)
|
|
|
var secinfo flashsale.RecookSecKillGoodsModel
|
|
|
if p.StockStart.GreaterThan(num0) && p.StockEnd.GreaterThan(num0) {
|
|
|
var ids []int
|
|
|
mysql2.Db.Table(secinfo.TableName()).Where("sec_kill_stock between ? and ?", p.StockStart, p.StockEnd).Pluck("goods_sku_id", &ids)
|
|
|
where = where.Where("id in (?)", ids)
|
|
|
}
|
|
|
if p.DiscountPriceStart.GreaterThan(num0) && p.DiscountPriceEnd.GreaterThanOrEqual(num0) {
|
|
|
where = where.Where("discount_price between ? and ?", p.DiscountPriceStart, p.DiscountPriceEnd)
|
|
|
}
|
|
|
if p.PurchaseStart.GreaterThan(num0) && p.PurchaseEnd.GreaterThan(num0) {
|
|
|
where = where.Where("purchase_price between ? and ?", p.PurchaseStart, p.PurchaseEnd)
|
|
|
}
|
|
|
if p.CommissionStart.GreaterThan(num0) && p.CommissionEnd.GreaterThan(num0) {
|
|
|
where = where.Where("commission between ? and ?", p.PurchaseStart, p.PurchaseEnd)
|
|
|
}
|
|
|
|
|
|
var goodsAll []goods.RecookGoodsSkuModel
|
|
|
//q := mysql2.Db.Table(fmt.Sprintf("%s as a", (&goods.RecookGoodsSkuModel{}).TableName())).
|
|
|
// Preload("Goods.Vendor").
|
|
|
// Preload("Goods.Vendor.CGSource").
|
|
|
// Preload("Goods.FirstCategory").
|
|
|
// Preload("Goods.SecondCategory").
|
|
|
// Preload("Goods.MainPhoto", "is_master=1").
|
|
|
// Joins(fmt.Sprintf("left join %s as b on b.id=a.goods_id", oneinfo.TableName())).
|
|
|
// Joins(fmt.Sprintf("left join %s as c on c.goods_sku_id=a.id", (&flashsale.RecookSecKillGoodsModel{}).TableName())).
|
|
|
// Where(where).Select(
|
|
|
// "a.id as sku_id," +
|
|
|
// "a.Goods.Vendor.enterprise_name as gys_name," +
|
|
|
// "a.Goods.FirstCategory.name as first_category," +
|
|
|
// "a.Goods.SecondCategory.name as second_category," +
|
|
|
// "b.goods_name as goods_name," +
|
|
|
// "b.id as goods_id," +
|
|
|
// "a.name as sku_name," +
|
|
|
// "a.code as sku_code," +
|
|
|
// "a.goods_num as goods_num," +
|
|
|
// "a.Goods.MainPhoto.url as main_photo," +
|
|
|
// "a.purchase_price as purchase," +
|
|
|
// "c.sec_kill_purchase as sec_purchase," +
|
|
|
// "c.sec_kill_stock as sec_stock," +
|
|
|
// "a.discount_price as discount_price," +
|
|
|
// "c.sec_kill_discount_price as sec_discount_price," +
|
|
|
// "c.sec_kill_commission_rate as sec_commission_rate," +
|
|
|
// "c.sec_kill_commission as sec_commission," +
|
|
|
// "a.Goods.Vendor.CGSource.coefficient as coefficient")
|
|
|
|
|
|
q := mysql2.Db.Table((&goods.RecookGoodsSkuModel{}).TableName()).
|
|
|
Preload("Goods.Vendor").
|
|
|
Preload("Goods.Vendor.CGSource").
|
|
|
Preload("Goods.FirstCategory").
|
|
|
Preload("Goods.SecondCategory").
|
|
|
Preload("Goods.MainPhoto", "is_master=1").
|
|
|
Preload("Goods").
|
|
|
Preload("SecKill").Preload("DetailPhoto").
|
|
|
Where(where)
|
|
|
mysql2.Db.Table((&goods.RecookGoodsSkuModel{}).TableName()).Where(where).Count(&total)
|
|
|
q.Find(&goodsAll)
|
|
|
fmt.Println(len(goodsAll))
|
|
|
|
|
|
mp := make(map[uint]showList)
|
|
|
list := []showList{}
|
|
|
for _, model := range goodsAll {
|
|
|
var dl []string
|
|
|
for _, photoModel := range model.DetailPhoto {
|
|
|
dl = append(dl, photoModel.Url)
|
|
|
}
|
|
|
//var sn flashsale.RecookSecKillSortModel
|
|
|
//mysql2.Db.Table((&flashsale.RecookSecKillSortModel{}).TableName()).Where("sku_id=?", model.Id).
|
|
|
// Where("sec_kill_activity_id=?", p.ActivityId).First(&sn)
|
|
|
var one = showList{
|
|
|
GysName: model.Goods.Vendor.EnterpriseName,
|
|
|
FirstCategory: model.Goods.FirstCategory.Name,
|
|
|
SecondCategory: model.Goods.SecondCategory.Name,
|
|
|
GoodsName: model.Goods.GoodsName,
|
|
|
GoodsId: model.GoodsId,
|
|
|
SkuID: model.Id,
|
|
|
SkuName: model.Name,
|
|
|
SkuCode: model.Code,
|
|
|
GoodsNum: model.GoodsNum,
|
|
|
MainPhoto: model.Goods.MainPhoto.Url,
|
|
|
DetailPhoto: dl,
|
|
|
Purchase: model.PurchasePrice,
|
|
|
SecPurchase: model.SecKill.SecKillPurchase,
|
|
|
SecStock: model.SecKill.SecKillStock,
|
|
|
SecCGPurchase: model.SecKill.SecKillPurchase.Mul(model.Goods.Vendor.CGSource.Coefficient),
|
|
|
CGPurchase: model.PurchasePrice.Mul(model.Goods.Vendor.CGSource.Coefficient),
|
|
|
DiscountPrice: model.DiscountPrice,
|
|
|
SecDiscountPrice: model.SecKill.SecKillDiscountPrice,
|
|
|
SecCommissionRate: model.SecKill.SecKillCommissionRate,
|
|
|
SecCommission: model.SecKill.SecKillCommission,
|
|
|
SecMaoLiRate: model.SecKill.SecMaoliRate,
|
|
|
}
|
|
|
//one.SortNum=sn
|
|
|
mp[model.Id] = one
|
|
|
list = append(list, one)
|
|
|
}
|
|
|
fmt.Println("mp:", mp)
|
|
|
fmt.Println("waaaaaa", len(list))
|
|
|
//排序展示
|
|
|
//get sortId list
|
|
|
var ids []uint
|
|
|
mysql2.Db.Table((&flashsale.RecookSecKillSortModel{}).TableName()).Where("sec_kill_activity_id=?", p.ActivityId).Group("show_sort_id").Pluck("show_sort_id", &ids)
|
|
|
if p.Status == 1 {
|
|
|
//全显
|
|
|
result = append(result, showGoodsListResponse{
|
|
|
GoodsList: list,
|
|
|
Num: int64(len(list)),
|
|
|
})
|
|
|
|
|
|
return result, total
|
|
|
} else {
|
|
|
var num int64
|
|
|
for _, v := range ids {
|
|
|
var onegoods []flashsale.RecookSecKillSortModel
|
|
|
mysql2.Db.Table((&flashsale.RecookSecKillSortModel{}).TableName()).Where("sec_kill_activity_id=?", p.ActivityId).Where("show_sort_id=?", v).Find(&onegoods)
|
|
|
var lp []showList
|
|
|
start := time.Time{}
|
|
|
end := time.Time{}
|
|
|
|
|
|
for _, j := range onegoods {
|
|
|
if _, ok := mp[j.SkuId]; ok {
|
|
|
var one = mp[j.SkuId]
|
|
|
one.SortNum = j.SortNum
|
|
|
lp = append(lp, one)
|
|
|
}
|
|
|
start = j.ShowTimeStart
|
|
|
end = j.ShowTimeEnd
|
|
|
}
|
|
|
result = append(result, showGoodsListResponse{
|
|
|
SortId: v,
|
|
|
ShowTimeStart: start,
|
|
|
ShowTimeEnd: end,
|
|
|
GoodsList: lp,
|
|
|
Num: int64(len(lp)),
|
|
|
})
|
|
|
num = num + int64(len(lp))
|
|
|
}
|
|
|
return result, num
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
type SecAddSortReq struct {
|
|
|
ActivityId uint `json:"activity_id" validate:"required" label:"活动id为必填字段"`
|
|
|
//SortId uint `json:"_"` //修改则传入sortId,新增传0
|
|
|
TimeStart string `json:"time_start"`
|
|
|
TimeEnd string `json:"time_end"`
|
|
|
GoodsList []goodsList `json:"goods_list"`
|
|
|
}
|
|
|
type goodsList struct {
|
|
|
GoodsId uint `json:"goods_id"`
|
|
|
SkuId uint `json:"sku_id"`
|
|
|
SecKillDiscountPrice decimal.Decimal `json:"sec_kill_discount_price"`
|
|
|
SecKillCommissionRate decimal.Decimal `json:"sec_kill_commission_rate"`
|
|
|
SecKillCommission decimal.Decimal `json:"sec_kill_commission"`
|
|
|
SecMaoliRate decimal.Decimal `json:"sec_maoli_rate"`
|
|
|
SortNum uint `json:"sort_num"`
|
|
|
}
|
|
|
|
|
|
//新增
|
|
|
|
|
|
func (s sale) AddSort(p SecAddSortReq) error {
|
|
|
//验证
|
|
|
var act flashsale.RecookSecKillModel
|
|
|
mysql2.Db.Table(act.TableName()).Where("id=?", p.ActivityId).First(&act)
|
|
|
if act.Id == 0 {
|
|
|
return errors.New("传入的活动id不正确")
|
|
|
}
|
|
|
if act.Status == 2 {
|
|
|
return errors.New("传入的活动已审核结束,不能修改或新增")
|
|
|
}
|
|
|
if len(p.GoodsList) == 0 {
|
|
|
return errors.New("请添加商品")
|
|
|
}
|
|
|
|
|
|
//if p.SortId > 0 {
|
|
|
// //验证sort_id
|
|
|
// var inlen int64
|
|
|
// var isSort flashsale.RecookSecKillSortModel
|
|
|
// mysql2.Db.Table(isSort.TableName()).Where("sec_kill_activity_id=?", p.ActivityId).Where("show_sort_id=?", p.SortId).Count(&inlen)
|
|
|
// if inlen == 0 {
|
|
|
// return errors.New("该sortId 不存在")
|
|
|
// }
|
|
|
//}
|
|
|
//解析时间
|
|
|
start, _ := time.ParseInLocation("15:04:05", p.TimeStart, time.Local)
|
|
|
end, _ := time.ParseInLocation("15:04:05", p.TimeEnd, time.Local)
|
|
|
var max uint
|
|
|
mysql2.Db.Table((&flashsale.RecookSecKillSortModel{}).TableName()).Where("sec_kill_activity_id=?", p.ActivityId).Select("max(show_sort_id)").Scan(&max)
|
|
|
//给sortId 赋值
|
|
|
sortId := max + 1
|
|
|
////验证时间
|
|
|
//var maxinfo flashsale.RecookSecKillSortModel
|
|
|
//mysql2.Db.Table((&flashsale.RecookSecKillSortModel{}).TableName()).Where("sec_kill_activity_id=?", p.ActivityId).Where("show_sort_id=?", max).First(&maxinfo)
|
|
|
//if start.Before(maxinfo.ShowTimeEnd) {
|
|
|
// return errors.New("传入的时间必须大于上次时间段结束时间")
|
|
|
//}
|
|
|
|
|
|
//var list []flashsale.RecookSecKillSortModel
|
|
|
|
|
|
err := mysql2.Db.Transaction(func(tx *gorm.DB) error {
|
|
|
//if p.SortId > 0 {
|
|
|
// //删去老数据
|
|
|
// var old flashsale.RecookSecKillSortModel
|
|
|
// err := tx.Table(old.TableName()).Where("sec_kill_activity_id=?", p.ActivityId).Where("show_sort_id=?", p.SortId).Delete(&old).Error
|
|
|
// if err != nil {
|
|
|
// return err
|
|
|
// }
|
|
|
//}
|
|
|
|
|
|
for _, v := range p.GoodsList {
|
|
|
one := flashsale.RecookSecKillSortModel{
|
|
|
GoodsId: v.GoodsId,
|
|
|
SkuId: v.SkuId,
|
|
|
SecKillActivityId: p.ActivityId,
|
|
|
ShowSortId: sortId,
|
|
|
ShowTimeStart: start,
|
|
|
ShowTimeEnd: end,
|
|
|
Created: formatime.NewSecondNow(),
|
|
|
}
|
|
|
err := tx.Create(&one).Error
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
secGoods := flashsale.RecookSecKillGoodsModel{
|
|
|
SecKillDiscountPrice: v.SecKillDiscountPrice,
|
|
|
SecKillCommissionRate: v.SecKillCommissionRate,
|
|
|
SecKillCommission: v.SecKillCommission,
|
|
|
SecMaoliRate: v.SecMaoliRate,
|
|
|
}
|
|
|
err = tx.Table(secGoods.TableName()).Where("sec_kill_activity_id=?", p.ActivityId).Where("goods_sku_id=?", v.SkuId).Updates(&secGoods).Error
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
}
|
|
|
return nil
|
|
|
})
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
type killGoods struct {
|
|
|
Status uint `json:"status"` //1,活动未开始 2活动中
|
|
|
GoodsList []appGoods `json:"goods_list"` //商品list
|
|
|
EndTime string `json:"end_time"` //活动结束时间
|
|
|
StartTime string `json:"start_time"` //活动未开始
|
|
|
ShoppingPeople uint `json:"shopping_people"` //购物人数
|
|
|
}
|
|
|
type appGoods struct {
|
|
|
GoodsId uint `json:"goods_id"`
|
|
|
GoodsName string `json:"goods_name"`
|
|
|
SubTitle string `json:"sub_title"`
|
|
|
MainPhoto string `json:"main_photo"`
|
|
|
CountryUrl string `json:"country_url"`
|
|
|
BrandName string `json:"brand_name"`
|
|
|
BrandUrl string `json:"brand_url"`
|
|
|
MinDiscountPrice decimal.Decimal `json:"min_discount_price"`
|
|
|
SaleNum decimal.Decimal `json:"sale_num"`
|
|
|
BrandId uint `json:"brand_id"`
|
|
|
Inventory decimal.Decimal `json:"inventory"`
|
|
|
Commission decimal.Decimal `json:"commission"`
|
|
|
}
|
|
|
|
|
|
func (s sale) ShowAppGoodsList(limit int, page int) (result killGoods) {
|
|
|
now := time.Now()
|
|
|
//显示现在时间存在的秒杀活动
|
|
|
var seckillNow flashsale.RecookSecKillModel
|
|
|
mysql2.Db.Table(seckillNow.TableName()).Where("activity_start_time<?", now).Where("activity_end_time>?", now).First(&seckillNow)
|
|
|
if seckillNow.Id == 0 {
|
|
|
//没有秒杀活动,找最新的活动
|
|
|
var sk flashsale.RecookSecKillModel
|
|
|
mysql2.Db.Table(sk.TableName()).Where("activity_start_time>?", now).Order("activity_start_time").First(&sk)
|
|
|
if sk.Id > 0 {
|
|
|
result = killGoods{
|
|
|
Status: 1,
|
|
|
GoodsList: nil,
|
|
|
StartTime: sk.ActivityStartTime.Time.Format("15:04:05"),
|
|
|
}
|
|
|
return
|
|
|
} else {
|
|
|
//近期没有活动
|
|
|
result = killGoods{
|
|
|
Status: 1,
|
|
|
GoodsList: nil,
|
|
|
}
|
|
|
return result
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
//显示当前时间的秒杀商品list
|
|
|
nowhour := time.Date(0, 1, 1, time.Now().Hour(), time.Now().Minute(), time.Now().Second(), 0, time.Local)
|
|
|
//商品
|
|
|
var gsl []uint
|
|
|
mysql2.Db.Table((&flashsale.RecookSecKillSortModel{}).TableName()).Where("sec_kill_activity_id=?", seckillNow.Id).Where("show_time_start<=?", nowhour).Where("show_time_end>=?", nowhour).Order("sort_num").Limit(limit).Offset(page).Pluck("goods_id", &gsl)
|
|
|
if len(gsl) == 0 {
|
|
|
//该时间段没有秒杀商品,则显示最近的活动的商品
|
|
|
var sort flashsale.RecookSecKillSortModel
|
|
|
mysql2.Db.Table(sort.TableName()).Where("show_time_start>?", nowhour).Order("show_time_start").First(&sort)
|
|
|
var newgsl []uint
|
|
|
mysql2.Db.Table(sort.TableName()).Where("show_sort_id=?", sort.ShowSortId).Limit(limit).Offset(page).Pluck("goods_id", &newgsl)
|
|
|
var gl []goods.RecookGoodsInfoModel
|
|
|
mysql2.Db.Table((&goods.RecookGoodsInfoModel{}).TableName()).Preload("GoodsSkuModel").Preload("Brand").Preload("CountryModel").Where("id in(?)", newgsl).Find(&gl)
|
|
|
var list []appGoods
|
|
|
for _, v := range gl {
|
|
|
minprice := v.GoodsSkuModel[0].DiscountPrice
|
|
|
var salenum decimal.Decimal
|
|
|
var stock decimal.Decimal
|
|
|
var com decimal.Decimal
|
|
|
for _, model := range v.GoodsSkuModel {
|
|
|
salenum = salenum.Add(decimal.NewFromInt(int64(model.SalesVolumeInc)))
|
|
|
if model.DiscountPrice.LessThan(minprice) {
|
|
|
minprice = model.DiscountPrice
|
|
|
}
|
|
|
if com.LessThan(model.DiscountPrice.Mul(model.CommissionRate)) {
|
|
|
com = model.DiscountPrice.Mul(model.CommissionRate).Truncate(2)
|
|
|
}
|
|
|
stock = stock.Add(decimal.NewFromInt(int64(model.Inventory)))
|
|
|
}
|
|
|
one := appGoods{
|
|
|
GoodsId: v.Id,
|
|
|
GoodsName: v.GoodsName,
|
|
|
MainPhoto: v.MainPhoto.Url,
|
|
|
SubTitle: v.Description,
|
|
|
CountryUrl: v.CountryModel.Icon,
|
|
|
BrandName: v.Brand.Name,
|
|
|
BrandUrl: v.Brand.LogoUrl,
|
|
|
MinDiscountPrice: minprice,
|
|
|
SaleNum: salenum,
|
|
|
BrandId: v.BrandID,
|
|
|
Inventory: stock,
|
|
|
Commission: com,
|
|
|
}
|
|
|
list = append(list, one)
|
|
|
}
|
|
|
|
|
|
result = killGoods{
|
|
|
Status: 1,
|
|
|
GoodsList: list,
|
|
|
StartTime: sort.ShowTimeStart.Format("15:04:05"),
|
|
|
}
|
|
|
return result
|
|
|
|
|
|
} else {
|
|
|
//获取商品全部资料
|
|
|
var gl []goods.RecookGoodsInfoModel
|
|
|
mysql2.Db.Table((&goods.RecookGoodsInfoModel{}).TableName()).Preload("GoodsSkuModel").Preload("GoodsSkuModel.SecKill", "sec_kill_activity_id=?", seckillNow.Id).Preload("Brand").Preload("CountryModel").Where("id in(?)", gsl).Find(&gl)
|
|
|
var list []appGoods
|
|
|
var sort flashsale.RecookSecKillSortModel
|
|
|
mysql2.Db.Table(sort.TableName()).Where("sec_kill_activity_id=?", seckillNow.Id).Where("show_time_start<=?", nowhour).Where("show_time_end>=?", nowhour).First(&sort)
|
|
|
for _, v := range gl {
|
|
|
minprice := v.GoodsSkuModel[0].SecKill.SecKillDiscountPrice
|
|
|
fmt.Println(minprice)
|
|
|
var salenum decimal.Decimal
|
|
|
fmt.Println(v.GoodsSkuModel)
|
|
|
var stock decimal.Decimal
|
|
|
var com decimal.Decimal
|
|
|
for _, model := range v.GoodsSkuModel {
|
|
|
if model.SecKill.Id > 0 {
|
|
|
salenum = salenum.Add(model.SecKill.SaleNum)
|
|
|
fmt.Println(":", minprice)
|
|
|
if model.SecKill.SecKillDiscountPrice.LessThan(minprice) {
|
|
|
minprice = model.SecKill.SecKillDiscountPrice
|
|
|
}
|
|
|
stock = stock.Add(model.SecKill.SecKillStock)
|
|
|
a := model.SecKill.SecKillDiscountPrice.Mul(model.SecKill.SecKillCommissionRate)
|
|
|
if com.LessThan(a) {
|
|
|
com = a.Truncate(2)
|
|
|
}
|
|
|
} else {
|
|
|
continue
|
|
|
}
|
|
|
}
|
|
|
fmt.Println(minprice, salenum)
|
|
|
one := appGoods{
|
|
|
GoodsId: v.Id,
|
|
|
GoodsName: v.GoodsName,
|
|
|
MainPhoto: v.MainPhoto.Url,
|
|
|
SubTitle: v.Description,
|
|
|
CountryUrl: v.CountryModel.Icon,
|
|
|
BrandName: v.Brand.Name,
|
|
|
BrandUrl: v.Brand.LogoUrl,
|
|
|
MinDiscountPrice: minprice,
|
|
|
SaleNum: salenum,
|
|
|
BrandId: v.BrandID,
|
|
|
Inventory: stock,
|
|
|
Commission: com,
|
|
|
}
|
|
|
list = append(list, one)
|
|
|
}
|
|
|
fmt.Println(list)
|
|
|
people := cache.GetSecKillPeople(seckillNow.Id)
|
|
|
num1, _ := strconv.Atoi(people)
|
|
|
if num1 == 0 {
|
|
|
rand.NewSource(time.Now().Unix())
|
|
|
uid := rand.Intn(700) + 800
|
|
|
cache.SetSecKillPeople(seckillNow.Id, uint(uid))
|
|
|
num2, _ := strconv.Atoi(cache.GetSecKillPeople(seckillNow.Id))
|
|
|
num1 = num2
|
|
|
}
|
|
|
|
|
|
result = killGoods{
|
|
|
Status: 2,
|
|
|
GoodsList: list,
|
|
|
EndTime: sort.ShowTimeEnd.Format("15:04:05"),
|
|
|
ShoppingPeople: uint(num1),
|
|
|
}
|
|
|
return result
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|