You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
473 lines
14 KiB
473 lines
14 KiB
package activebit
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"recook/internal/dbc"
|
|
"recook/internal/v2/model/gys/enterprise"
|
|
"recook/internal/v2/model/recommendedlist"
|
|
goods2 "recook/internal/v2/model/recook/goods"
|
|
"recook/internal/v2/model/rotation"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
mysql2 "git.oa00.com/go/mysql"
|
|
"github.com/golangkit/formatime"
|
|
"github.com/shopspring/decimal"
|
|
)
|
|
|
|
type logicActiveBit struct {
|
|
}
|
|
|
|
var LogicActiveBit = &logicActiveBit{}
|
|
|
|
type CreatedReq struct {
|
|
Id uint `json:"id"`
|
|
URL string `json:"url"`
|
|
GoodsID uint `json:"goodsId" validate:"numeric"`
|
|
ActivityID uint `json:"activityId" validate:"numeric"`
|
|
ActivityOrGoods string `json:"activityOrGoods" validate:"required"` // 传递 activity or goods 大小写随意
|
|
Color string `json:"color"`
|
|
BackgroundColor string `json:"BackgroundColor" validate:"required"`
|
|
StartTime string `json:"start_time"`
|
|
EndTime string `json:"end_time"`
|
|
SortId uint `json:"sort_id"`
|
|
IsSale bool `json:"is_sale"`
|
|
}
|
|
|
|
//Add 新增轮播图
|
|
func (b logicActiveBit) Add(p CreatedReq) error {
|
|
//var ro rotation.RecookRotationModel
|
|
if p.GoodsID <= 0 && p.ActivityID <= 0 {
|
|
return errors.New("goodsId或activityId必传其一")
|
|
}
|
|
var activityOrGoods = strings.ToLower(p.ActivityOrGoods)
|
|
if activityOrGoods != "goods" && activityOrGoods != "activity" {
|
|
return errors.New("非法的参数")
|
|
}
|
|
// 检查个数
|
|
//var count uint
|
|
//dbc.DB.Table((ro).TableName()).Where("is_active=?", 1).Count(&count)
|
|
//if count > 50 {
|
|
// return errors.New("最多只能有50个钻展商品")
|
|
//}
|
|
// 验证颜色
|
|
if len(p.Color) > 0 && (len(p.Color) != 7 || !strings.HasPrefix(p.Color, "#")) {
|
|
return errors.New("非法的颜色参数")
|
|
}
|
|
if len(p.BackgroundColor) > 0 && (len(p.BackgroundColor) != 7 || !strings.HasPrefix(p.BackgroundColor, "#")) {
|
|
return errors.New("非法的颜色参数")
|
|
}
|
|
//判断sort——id
|
|
//var sort int64
|
|
//dbc.DB.Table(ro.TableName()).Where("sort_id=?", p.SortId).Where("is_active=?", 1).Count(&sort)
|
|
//if sort > 0 {
|
|
// return errors.New("该序号已被使用")
|
|
//}
|
|
|
|
//判断活动开始和结束时间
|
|
if p.StartTime == "" || p.EndTime == "" {
|
|
return errors.New("必须传入展示时间")
|
|
}
|
|
t1, _ := time.ParseInLocation("2006-01-02 15:04:05", p.StartTime, time.Local)
|
|
t2, _ := time.ParseInLocation("2006-01-02 15:04:05", p.EndTime, time.Local)
|
|
if t2.Before(t1) {
|
|
return errors.New("请传入正确时间")
|
|
}
|
|
|
|
var one rotation.RecookRotationModel
|
|
|
|
if p.Id > 0 {
|
|
//有数据,编辑
|
|
if activityOrGoods == "goods" && p.GoodsID > 0 {
|
|
// 重复性检查
|
|
var show rotation.RecookRotationModel
|
|
dbc.DB.Where("is_active=1").First(&show, "good_id = ?", p.GoodsID)
|
|
if show.ID > 0 {
|
|
return errors.New("该商品已在钻展中")
|
|
}
|
|
one = rotation.RecookRotationModel{
|
|
GoodsID: p.GoodsID,
|
|
URL: p.URL,
|
|
IsActive: 1,
|
|
ActiveID: 0,
|
|
Color: p.Color,
|
|
CreatedAt: formatime.NewSecondNow(),
|
|
BackgroundColor: p.BackgroundColor,
|
|
StartTime: formatime.NewSecondFrom(t1),
|
|
EndTime: formatime.NewSecondFrom(t2),
|
|
SortId: p.SortId,
|
|
}
|
|
err := dbc.DB.Table(one.TableName()).Where("id=?", p.Id).Updates(&one).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
} else if activityOrGoods == "activity" && p.ActivityID > 0 {
|
|
// 重复性检查
|
|
var show rotation.RecookRotationModel
|
|
dbc.DB.Where("is_active=1").First(&show, "activity_id = ?", p.ActivityID)
|
|
if show.ID > 0 {
|
|
return errors.New("该活动已在钻展中")
|
|
}
|
|
// 检查活动是否存在
|
|
var activityData = rotation.RecookActivityInfoModel{}
|
|
dbc.DB.Where("id=?", p.ActivityID).Find(&activityData)
|
|
if activityData.ID <= 0 {
|
|
return errors.New("活动不存在,无法添加")
|
|
}
|
|
|
|
one = rotation.RecookRotationModel{
|
|
GoodsID: 0,
|
|
ActiveID: p.ActivityID,
|
|
URL: p.URL,
|
|
IsActive: 1,
|
|
Color: p.Color,
|
|
CreatedAt: formatime.NewSecondNow(),
|
|
BackgroundColor: p.BackgroundColor,
|
|
StartTime: formatime.NewSecondFrom(t1),
|
|
EndTime: formatime.NewSecondFrom(t2),
|
|
SortId: p.SortId,
|
|
}
|
|
if err := dbc.DB.Table(one.TableName()).Where("id=?", p.Id).Updates(&one).Error; err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
return errors.New("非法的参数")
|
|
}
|
|
|
|
} else {
|
|
if activityOrGoods == "goods" && p.GoodsID > 0 {
|
|
// 重复性检查
|
|
var show rotation.RecookRotationModel
|
|
dbc.DB.Where("is_active=1").Where("is_sale=?", p.IsSale).First(&show, "good_id = ?", p.GoodsID)
|
|
if show.ID > 0 {
|
|
return errors.New("该商品已在钻展中")
|
|
}
|
|
one = rotation.RecookRotationModel{
|
|
GoodsID: p.GoodsID,
|
|
URL: p.URL,
|
|
IsActive: 1,
|
|
ActiveID: 0,
|
|
Color: p.Color,
|
|
CreatedAt: formatime.NewSecondNow(),
|
|
BackgroundColor: p.BackgroundColor,
|
|
StartTime: formatime.NewSecondFrom(t1),
|
|
EndTime: formatime.NewSecondFrom(t2),
|
|
SortId: p.SortId,
|
|
IsSale: p.IsSale,
|
|
}
|
|
err := dbc.DB.Table(one.TableName()).Create(&one).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
} else if activityOrGoods == "activity" && p.ActivityID > 0 {
|
|
// 重复性检查
|
|
var show rotation.RecookRotationModel
|
|
dbc.DB.Where("is_active=1").First(&show, "activity_id = ?", p.ActivityID)
|
|
if show.ID > 0 {
|
|
return errors.New("该活动已在钻展中")
|
|
}
|
|
// 检查活动是否存在
|
|
var activityData = rotation.RecookActivityInfoModel{}
|
|
dbc.DB.Where("id=?", p.ActivityID).Find(&activityData)
|
|
if activityData.ID <= 0 {
|
|
return errors.New("活动不存在,无法添加")
|
|
}
|
|
|
|
one = rotation.RecookRotationModel{
|
|
GoodsID: 0,
|
|
ActiveID: p.ActivityID,
|
|
URL: p.URL,
|
|
IsActive: 1,
|
|
Color: p.Color,
|
|
CreatedAt: formatime.NewSecondNow(),
|
|
BackgroundColor: p.BackgroundColor,
|
|
StartTime: formatime.NewSecondFrom(t1),
|
|
EndTime: formatime.NewSecondFrom(t2),
|
|
SortId: p.SortId,
|
|
IsSale: p.IsSale,
|
|
}
|
|
if err := dbc.DB.Create(&one).Error; err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
return errors.New("非法的参数")
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type queryResp struct {
|
|
Base rotation.RecookRotationModel
|
|
GoodsName string `gorm:"column:goods_name" json:"goodsName"`
|
|
GoodOrActivity int8 `json:"goodOrActivity"` // goods activity--->1
|
|
Status uint `json:"status"` //1:待生效 2:生效中 3已失效
|
|
}
|
|
|
|
func (b logicActiveBit) ShowList(isSale bool) (list []queryResp) {
|
|
|
|
var of []rotation.RecookRotationModel
|
|
mysql2.Db.Table((&rotation.RecookRotationModel{}).TableName()).Where("is_sale=?", isSale).
|
|
Where("is_active=?", 1).Order("id desc").Find(&of)
|
|
fmt.Println(len(of))
|
|
|
|
for _, v := range of {
|
|
if v.GoodsID > 0 && v.ActiveID == 0 {
|
|
var info goods2.RecookGoodsInfoModel
|
|
mysql2.Db.Table(info.TableName()).Where("id=?", v.GoodsID).First(&info)
|
|
status := 0
|
|
if v.StartTime.Time.After(time.Now()) {
|
|
status = 1
|
|
}
|
|
if v.StartTime.Time.Before(time.Now()) && v.EndTime.Time.After(time.Now()) {
|
|
status = 2
|
|
}
|
|
if v.EndTime.Time.Before(time.Now()) {
|
|
status = 3
|
|
}
|
|
list = append(list, queryResp{
|
|
Base: v,
|
|
GoodsName: info.GoodsName,
|
|
GoodOrActivity: 0,
|
|
Status: uint(status),
|
|
})
|
|
|
|
} else if v.GoodsID == 0 && v.ActiveID > 0 {
|
|
|
|
var info rotation.RecookActivityInfoModel
|
|
mysql2.Db.Table(info.TableName()).Where("id=?", v.ActiveID).First(&info)
|
|
status := 0
|
|
if v.StartTime.Time.After(time.Now()) {
|
|
status = 1
|
|
}
|
|
if v.StartTime.Time.Before(time.Now()) && v.EndTime.Time.After(time.Now()) {
|
|
status = 2
|
|
}
|
|
if v.EndTime.Time.Before(time.Now()) {
|
|
status = 3
|
|
}
|
|
list = append(list, queryResp{
|
|
Base: v,
|
|
GoodsName: info.Name,
|
|
GoodOrActivity: 1,
|
|
Status: uint(status),
|
|
})
|
|
}
|
|
|
|
}
|
|
return
|
|
}
|
|
|
|
type ActivityAddReq struct {
|
|
ID uint `json:"id"`
|
|
URL string `json:"url"`
|
|
ActiveID uint `json:"activityId"`
|
|
BackgroundColor string `json:"background_color"`
|
|
StartTime string `json:"start_time"`
|
|
EndTime string `json:"end_time"`
|
|
Type uint `json:"type" validate:"oneof= 1 2 3 4"`
|
|
}
|
|
|
|
//ActivityAdd add or update
|
|
func (b logicActiveBit) ActivityAdd(p ActivityAddReq) error {
|
|
//判断时间
|
|
// 验证颜色
|
|
//if len(p.BackgroundColor) > 0 && (len(p.BackgroundColor) != 7 || !strings.HasPrefix(p.BackgroundColor, "#")) {
|
|
// return errors.New("非法的颜色参数")
|
|
//}
|
|
//判断活动开始和结束时间
|
|
if p.StartTime == "" || p.EndTime == "" {
|
|
return errors.New("必须传入展示时间")
|
|
}
|
|
t1, _ := time.ParseInLocation("2006-01-02 15:04:05", p.StartTime, time.Local)
|
|
t2, _ := time.ParseInLocation("2006-01-02 15:04:05", p.EndTime, time.Local)
|
|
if t2.Before(t1) {
|
|
return errors.New("请传入正确时间")
|
|
}
|
|
if p.ActiveID == 0 {
|
|
return errors.New("请传入活动多品")
|
|
}
|
|
var one rotation.RecookActivityTypeInfoModel
|
|
if p.ID > 0 {
|
|
//update
|
|
one = rotation.RecookActivityTypeInfoModel{
|
|
URL: p.URL,
|
|
ActiveID: p.ActiveID,
|
|
BackgroundColor: p.BackgroundColor,
|
|
IsActive: 1,
|
|
StartTime: formatime.NewSecondFrom(t1),
|
|
EndTime: formatime.NewSecondFrom(t2),
|
|
Type: p.Type,
|
|
}
|
|
err := mysql2.Db.Table(one.TableName()).Where("id=?", p.ID).Updates(&one).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
} else {
|
|
one = rotation.RecookActivityTypeInfoModel{
|
|
CreatedAt: formatime.NewSecondNow(),
|
|
URL: p.URL,
|
|
ActiveID: p.ActiveID,
|
|
BackgroundColor: p.BackgroundColor,
|
|
IsActive: 1,
|
|
StartTime: formatime.NewSecondFrom(t1),
|
|
EndTime: formatime.NewSecondFrom(t2),
|
|
Type: p.Type,
|
|
}
|
|
err := mysql2.Db.Table(one.TableName()).Create(&one).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type showActivityListRes struct {
|
|
rotation.RecookActivityTypeInfoModel
|
|
Title string `json:"title"`
|
|
Status uint `json:"status"` //1:已过期 2:生效中 待生效 4未开始
|
|
}
|
|
|
|
func (b logicActiveBit) ShowActivityList(id uint) ([]showActivityListRes, int64) {
|
|
var list []showActivityListRes
|
|
//var infos []rotation.RecookActivityTypeInfoModel
|
|
var info rotation.RecookActivityTypeInfoModel
|
|
var act rotation.RecookActivityInfoModel
|
|
mysql2.Db.Table(fmt.Sprintf("%s as a", info.TableName())).Joins(fmt.Sprintf("left join %s as b on b.id = a.activity_id", act.TableName())).Select("a.*,b.name as title").Where("a.type=?", id).Where("a.is_active=?", 1).Find(&list)
|
|
fmt.Println(list)
|
|
fmt.Println(id)
|
|
//已失效
|
|
var rs []uint
|
|
mysql2.Db.Table((&rotation.RecookActivityTypeInfoModel{}).TableName()).Where("end_time<?", time.Now()).Where("is_active=?", 1).Where("type=?", id).Pluck("id", &rs)
|
|
mp := map[uint]string{}
|
|
for _, v := range rs {
|
|
mp[v] = "ok"
|
|
}
|
|
|
|
//待生效
|
|
var ds []uint
|
|
mysql2.Db.Table((&rotation.RecookActivityTypeInfoModel{}).TableName()).Where("start_time>?", time.Now()).Where("type=?", id).Where("is_active=?", 1).Pluck("id", &ds)
|
|
mp2 := map[uint]string{}
|
|
for _, v := range ds {
|
|
mp2[v] = "ok"
|
|
}
|
|
|
|
//生效中
|
|
var ss []rotation.RecookActivityTypeInfoModel
|
|
mysql2.Db.Table((&rotation.RecookActivityTypeInfoModel{}).TableName()).Where("start_time<=?", time.Now()).Where("type=?", id).Where("end_time>=?", time.Now()).Where("is_active=?", 1).Find(&ss)
|
|
|
|
var sid uint
|
|
now := time.Now()
|
|
var temp int64
|
|
if len(ss) > 0 {
|
|
fmt.Println(ss[0])
|
|
temp = now.Unix() - ss[0].StartTime.Time.Unix()
|
|
sid = ss[0].ID
|
|
}
|
|
for _, s := range ss {
|
|
num := now.Unix() - s.StartTime.Time.Unix()
|
|
if num < temp {
|
|
temp = num
|
|
sid = s.ID
|
|
fmt.Println(temp, "sid=", sid)
|
|
}
|
|
}
|
|
fmt.Println(temp, sid)
|
|
|
|
for i, v := range list {
|
|
_, ok := mp[v.ID]
|
|
_, okm := mp2[v.ID]
|
|
if v.ID == sid {
|
|
list[i].Status = 2
|
|
} else if ok {
|
|
list[i].Status = 1
|
|
} else if okm {
|
|
list[i].Status = 4
|
|
} else {
|
|
list[i].Status = 3
|
|
}
|
|
}
|
|
|
|
var total int64
|
|
mysql2.Db.Table((&rotation.RecookActivityTypeInfoModel{}).TableName()).Where("type=?", id).Where("is_active=?", 1).Count(&total)
|
|
return list, total
|
|
|
|
}
|
|
|
|
//遍历存数据库
|
|
|
|
func (b logicActiveBit) AddCommends(result []string, status string, isSale bool) error {
|
|
|
|
//判读status
|
|
st, _ := strconv.Atoi(status)
|
|
if st != 1 && st != 2 && st != 10 {
|
|
return errors.New("传入参数有误")
|
|
}
|
|
var gsList []uint
|
|
for _, v := range result {
|
|
n1, _ := strconv.Atoi(v)
|
|
if n1 > 0 {
|
|
gsList = append(gsList, uint(n1))
|
|
}
|
|
}
|
|
//遍历id——list
|
|
var list []recommendedlist.RecookCommendList
|
|
for _, v := range gsList {
|
|
var info goods2.RecookGoodsInfoModel
|
|
mysql2.Db.Table(info.TableName()).Where("id=?", v).First(&info)
|
|
if info.Id == 0 {
|
|
continue
|
|
}
|
|
var on recommendedlist.RecookCommendList
|
|
mysql2.Db.Table(on.TableName()).Where("goods_id=?", v).First(&on)
|
|
if on.Id > 0 {
|
|
continue
|
|
} else {
|
|
var sku []goods2.RecookGoodsSkuModel
|
|
mysql2.Db.Table((&goods2.RecookGoodsSkuModel{}).TableName()).Where("goods_id=?", v).Find(&sku)
|
|
var gys enterprise.GysEnterpriseStateModel
|
|
mysql2.Db.Table(gys.TableName()).Where("user_id=?", info.VendorID).First(&gys)
|
|
var photo goods2.RecookGoodsMainPhotoModel
|
|
mysql2.Db.Table(photo.TableName()).Where("is_master=? and goods_id=?", 1, v).First(&photo)
|
|
|
|
var comstr string
|
|
var discon string
|
|
for i, j := range sku {
|
|
if i == 0 {
|
|
comstr = comstr + fmt.Sprintf("%v", j.CommissionRate.Mul(decimal.NewFromInt(100)))
|
|
discon = discon + fmt.Sprintf("%v", j.DiscountPrice)
|
|
} else {
|
|
comstr = comstr + fmt.Sprintf(",%v", j.CommissionRate.Mul(decimal.NewFromInt(100)))
|
|
discon = discon + fmt.Sprintf(",%v", j.DiscountPrice)
|
|
}
|
|
}
|
|
var one = recommendedlist.RecookCommendList{
|
|
GoodsId: v,
|
|
MainPhoto: photo.Url,
|
|
GoodsName: info.GoodsName,
|
|
CommissionList: comstr,
|
|
DiscountPriceList: discon,
|
|
GysName: gys.EnterpriseName,
|
|
Status: uint(st),
|
|
IsSale: isSale,
|
|
}
|
|
list = append(list, one)
|
|
}
|
|
}
|
|
if len(list) == 0 {
|
|
return errors.New("商品全部已存在")
|
|
}
|
|
err := mysql2.Db.Table((&recommendedlist.RecookCommendList{}).TableName()).Create(&list).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|