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.

492 lines
15 KiB

package goods
import (
"errors"
"fmt"
"recook/internal/dbc"
"recook/internal/libs/bean"
"recook/internal/v2/model/gys/enterprise"
"recook/internal/v2/model/recook/goods"
"time"
mysql2 "git.oa00.com/go/mysql"
gorm2 "gorm.io/gorm"
"github.com/golangkit/formatime"
)
var NoticeLogic = &noticeLogic{}
type noticeLogic struct {
}
type NoticeItem struct {
Id uint `json:"id"`
Title string `json:"title"`
VendorName string `json:"vendorName"`
Img []string `json:"img"`
StartTime int64 `json:"startTime"`
EndTime int64 `json:"endTime"`
CreatedAt int64 `json:"createdAt"`
Type int `json:"type"`
}
// @Style 商详通知列表
func (n *noticeLogic) List(vendorId uint, title string, startTimeBegin, startTimeEnd, endTimeBegin, endTimeEnd string, page bean.Page) (list []NoticeItem, total int) {
list = []NoticeItem{}
recookGoodsNoticeModel := &goods.RecookGoodsNoticeModel{}
query := ""
var queryArgs []interface{}
if vendorId != 0 {
query += " and vendor_id = ?"
queryArgs = append(queryArgs, vendorId)
}
if title != "" {
query += " and title like ?"
queryArgs = append(queryArgs, fmt.Sprintf("%%%s%%", title))
}
if startTimeBegin != "" && startTimeEnd != "" {
query += " and start_time between ? and ?"
startDate, _ := time.ParseInLocation("2006-01-02", startTimeBegin, time.Local)
endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", startTimeEnd+" 23:59:59", time.Local)
queryArgs = append(queryArgs, startDate, endDate)
}
if endTimeBegin != "" && endTimeEnd != "" {
query += " and end_time between ? and ?"
startDate, _ := time.ParseInLocation("2006-01-02", endTimeBegin, time.Local)
endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", endTimeEnd+" 23:59:59", time.Local)
queryArgs = append(queryArgs, startDate, endDate)
}
if query != "" {
query = query[4:]
}
total = recookGoodsNoticeModel.GetListsTotal(query, queryArgs...)
start := page.GetStart()
if total > start {
notices := recookGoodsNoticeModel.GetLists(start, page.GetLimit(), "id desc", query, queryArgs...)
var vendorIds []uint
for _, notice := range notices {
vendorIds = append(vendorIds, notice.VendorId)
}
gysEnterpriseStateModel := &enterprise.GysEnterpriseStateModel{}
enterprises := gysEnterpriseStateModel.FindByUserIds(vendorIds)
enterpriseMap := map[uint]enterprise.GysEnterpriseStateModel{}
for _, item := range enterprises {
enterpriseMap[item.UserId] = item
}
for _, notice := range notices {
//imgList
var imgs []string
mysql2.Db.Table((&goods.RecookGoodsNoticeImageModel{}).TableName()).Where("noticeId=?", notice.Id).Pluck("image", &imgs)
list = append(list, NoticeItem{
Id: notice.Id,
Title: notice.Title,
VendorName: enterpriseMap[notice.VendorId].EnterpriseName,
Img: imgs,
StartTime: notice.StartTime.Time.Unix(),
EndTime: notice.EndTime.Time.Unix(),
CreatedAt: notice.CreatedAt.Time.Unix(),
Type: notice.Type,
})
}
}
return
}
type NoticeInfo struct {
Id uint `json:"id"`
Title string `json:"title"`
VendorId uint `json:"vendorId"`
Img []string `json:"img"`
StartTime int64 `json:"startTime"`
EndTime int64 `json:"endTime"`
Type int `json:"type"`
Goods []GoodsItem `json:"goods"`
All bool `json:"all"`
}
type GoodsItem struct {
GoodsId uint `json:"goodsId"`
GoodsName string `json:"goodsName"`
}
// @Style 获取详情
func (n *noticeLogic) Info(noticeId uint) (result NoticeInfo) {
recookGoodsNoticeModel := &goods.RecookGoodsNoticeModel{}
info := recookGoodsNoticeModel.FindById(noticeId)
//获取图片
var imgs []string
mysql2.Db.Table((&goods.RecookGoodsNoticeImageModel{}).TableName()).Where("noticeId=?", info.Id).Pluck("image", &imgs)
if info.Id > 0 {
result = NoticeInfo{
Id: info.Id,
Title: info.Title,
VendorId: info.VendorId,
Img: imgs,
StartTime: info.StartTime.Time.Unix(),
EndTime: info.EndTime.Time.Unix(),
Type: info.Type,
}
recookGoodsNoticeGoodsModel := &goods.RecookGoodsNoticeGoodsModel{}
noticeGoods := recookGoodsNoticeGoodsModel.FindByNoticeId(info.Id)
var goodsIds []uint
for _, item := range noticeGoods {
goodsIds = append(goodsIds, item.GoodsId)
}
var total int
var gs goods.RecookGoodsInfoModel
dbc.DB.Table(gs.TableName()).
Where("vendor_id = ?", info.VendorId).
Where("publish_status = 1").Count(&total)
if len(goodsIds) == total {
result.All = true
}
recookGoodsInfoModel := &goods.RecookGoodsInfoModel{}
goodsInfos := recookGoodsInfoModel.FindByGoodsIds(goodsIds)
for _, goodsInfo := range goodsInfos {
result.Goods = append(result.Goods, GoodsItem{
GoodsId: goodsInfo.Id,
GoodsName: goodsInfo.GoodsName,
})
}
}
return
}
// @Style 添加
func (n *noticeLogic) Add(title string, img []string, noticeType int, vendorId uint, startTime, endTime formatime.Second, goodsIds []uint, all bool) error {
//recookGoodsNoticeModel := &goods.RecookGoodsNoticeModel{}
data := goods.RecookGoodsNoticeModel{
Title: title,
Img: img[0],
Type: noticeType,
StartTime: startTime,
EndTime: endTime,
VendorId: vendorId,
}
err := mysql2.Db.Transaction(func(tx *gorm2.DB) error {
//recookGoodsNoticeModel.SetDb(tx)
tx.Create(&data)
if data.Id == 0 {
return errors.New("新的网络错误")
}
//新增图片关联表
var imageList []goods.RecookGoodsNoticeImageModel
for _, v := range img {
images := goods.RecookGoodsNoticeImageModel{
NoticeId: data.Id,
Image: v,
}
imageList = append(imageList, images)
}
if err := tx.Create(&imageList).Error; err != nil {
return err
}
var noticeGoods []goods.RecookGoodsNoticeGoodsModel
if !all {
for _, goodsId := range goodsIds {
noticeGoods = append(noticeGoods, goods.RecookGoodsNoticeGoodsModel{
NoticeId: data.Id,
GoodsId: goodsId,
})
}
} else {
var gs goods.RecookGoodsInfoModel
subQ := tx.Table((&goods.RecookGoodsInfoModel{}).TableName()).
Select("id").Where("vendor_id = ?", vendorId)
if err := tx.Where("goods_id in (?)", subQ).Delete(&goods.RecookGoodsNoticeGoodsModel{}).Error; err != nil {
return err
}
var gsID []uint
tx.Table(gs.TableName()).
Where("vendor_id = ?", vendorId).
Where("publish_status = 1").Pluck("id", &gsID)
for _, goodsId := range gsID {
noticeGoods = append(noticeGoods, goods.RecookGoodsNoticeGoodsModel{
NoticeId: data.Id,
GoodsId: goodsId,
})
}
}
if len(noticeGoods) == 0 {
return errors.New("没有上架的商品")
}
step := 1000
start := 0
total := len(noticeGoods)
temp := make([][]goods.RecookGoodsNoticeGoodsModel, 0)
for {
if start > total {
break
}
if total < step {
temp = append(temp, noticeGoods[start:])
break
} else {
temp = append(temp, noticeGoods[start:step])
start += step
}
}
for _, v := range temp {
if err := tx.Create(&v).Error; err != nil {
return err
}
}
//recookGoodsNoticeGoodsModel := &goods.RecookGoodsNoticeGoodsModel{}
////recookGoodsNoticeGoodsModel.SetDb(tx)
//row := recookGoodsNoticeGoodsModel.CreateAll(&noticeGoods)
//if int64(len(noticeGoods)) != row {
// return errors.New("网络错误")
//}
return nil
})
if err != nil {
return err
}
return nil
}
type goodsInfo struct {
goods.RecookGoodsInfoModel
NoticeId uint `json:"noticeId"`
}
type SelectGoodsItem struct {
GoodsId uint `json:"goodsId"`
GoodsName string `json:"goodsName"`
BrandName string `json:"brandName"`
CategoryName string `json:"categoryName"`
NoticeFlag int `json:"noticeFlag"`
PublishStatus uint `json:"publishStatus"`
}
// @Style 商品筛选
func (n *noticeLogic) Select(goodsName string, vendorId, status, link, noticeId uint, brandIds, firstCateIds, secondCateIds []uint, page bean.Page) (result []SelectGoodsItem, total int) {
result = []SelectGoodsItem{}
recookGoodsInfoModel := &goods.RecookGoodsInfoModel{}
query := "vendor_id = ?"
queryArgs := []interface{}{vendorId}
if goodsName != "" {
query += " and goods_name like ?"
queryArgs = append(queryArgs, fmt.Sprintf("%%%s%%", goodsName))
}
if len(brandIds) > 0 {
query += " and brand_id in (?)"
queryArgs = append(queryArgs, brandIds)
}
if len(firstCateIds) > 0 {
query += " and first_category_id in (?)"
queryArgs = append(queryArgs, firstCateIds)
}
if len(secondCateIds) > 0 {
query += " and second_category_id in (?)"
queryArgs = append(queryArgs, secondCateIds)
}
if status > 0 {
if status == 1 {
// 上架
query += " and publish_status = ?"
queryArgs = append(queryArgs, goods.RecookGoodsInfoPublishStatusTrue)
} else {
query += " and publish_status = ?"
queryArgs = append(queryArgs, goods.RecookGoodsInfoPublishStatusFalse)
}
}
if link > 0 {
if link == 1 {
// 关联
query += " and noticeGoods.notice_id in (?)"
queryArgs = append(queryArgs, []uint{noticeId})
} else {
query += " and (noticeGoods.notice_id is null or noticeGoods.notice_id not in (?))"
queryArgs = append(queryArgs, noticeId)
}
}
recookGoodsNoticeGoodsModel := &goods.RecookGoodsNoticeGoodsModel{}
recookGoodsInfoModel.GetDb().Table(recookGoodsInfoModel.TableName()+" goodsInfo").
Joins(fmt.Sprintf("left join %s as noticeGoods on goodsInfo.id = noticeGoods.goods_id", recookGoodsNoticeGoodsModel.TableName())).
Where(query, queryArgs...).
Count(&total)
start := page.GetStart()
if total > start {
goodsList := []goodsInfo{}
recookGoodsInfoModel.GetDb().Table(recookGoodsInfoModel.TableName()+" goodsInfo").
Select("goodsInfo.*, noticeGoods.notice_id").
Joins(fmt.Sprintf("left join %s as noticeGoods on goodsInfo.id = noticeGoods.goods_id", recookGoodsNoticeGoodsModel.TableName())).
Where(query, queryArgs...).
Where("goodsInfo.publish_status = 1").
Order("goodsInfo.id desc").
Offset(start).
Limit(page.GetLimit()).
Find(&goodsList)
brandIds := []uint{}
categoryIds := []uint{}
for _, item := range goodsList {
brandIds = append(brandIds, item.BrandID)
categoryIds = append(categoryIds, item.FirstCategoryID, item.SecondCategoryID)
}
// 获取品牌
recookGoodsBrandModel := &goods.RecookGoodsBrandModel{}
brands := recookGoodsBrandModel.FindByIds(brandIds)
brandMap := map[uint]goods.RecookGoodsBrandModel{}
for _, brand := range brands {
brandMap[brand.Id] = brand
}
// 商品类目
recookGoodsCategoryModel := &goods.RecookGoodsCategoryModel{}
categorys := recookGoodsCategoryModel.FindByIds(categoryIds)
categoryMap := map[uint]goods.RecookGoodsCategoryModel{}
for _, category := range categorys {
categoryMap[category.Id] = category
}
for _, item := range goodsList {
flag := 0
if item.NoticeId > 0 {
flag = 1
}
result = append(result, SelectGoodsItem{
GoodsId: item.Id,
PublishStatus: item.PublishStatus,
GoodsName: item.GoodsName,
BrandName: brandMap[item.BrandID].Name,
CategoryName: fmt.Sprintf("%s/%s", categoryMap[item.FirstCategoryID].Name, categoryMap[item.SecondCategoryID].Name),
NoticeFlag: flag,
})
}
}
return
}
// @Style 解除关联
func (n *noticeLogic) UnLink(goodsId uint) error {
recookGoodsNoticeGoodsModel := &goods.RecookGoodsNoticeGoodsModel{}
return recookGoodsNoticeGoodsModel.DeleteByGoodsId(goodsId)
}
// @Style 删除
func (n *noticeLogic) Delete(noticeId uint) error {
//recookGoodsNoticeModel := &goods.RecookGoodsNoticeModel{}
err := mysql2.Db.Transaction(func(tx *gorm2.DB) error {
//recookGoodsNoticeModel.SetDb(tx)
if err := tx.Where("id=?", noticeId).Delete(&goods.RecookGoodsNoticeModel{}).Error; err != nil {
return err
}
if err := tx.Table((&goods.RecookGoodsNoticeImageModel{}).TableName()).Where("noticeId=?", noticeId).Delete(&goods.RecookGoodsNoticeImageModel{}).Error; err != nil {
return err
}
//recookGoodsNoticeGoodsModel := &goods.RecookGoodsNoticeGoodsModel{}
//recookGoodsNoticeGoodsModel.SetDb(tx)
//return recookGoodsNoticeGoodsModel.DeleteByNoticeId(noticeId)
if err := tx.Where("notice_id=?", noticeId).Delete(&goods.RecookGoodsNoticeGoodsModel{}).Error; err != nil {
return err
}
return nil
})
if err != nil {
return err
}
return nil
}
// @Style 添加
func (n *noticeLogic) Update(noticeId uint, title string, img []string, noticeType int, vendorId uint, startTime, endTime formatime.Second, goodsIds []uint, all bool) error {
recookGoodsNoticeModel := &goods.RecookGoodsNoticeModel{}
data := goods.RecookGoodsNoticeModel{
Title: title,
Img: img[0],
Type: noticeType,
StartTime: startTime,
EndTime: endTime,
VendorId: vendorId,
}
err := mysql2.Db.Transaction(func(tx *gorm2.DB) error {
//recookGoodsNoticeModel.SetDb(tx)
//if err := recookGoodsNoticeModel.UpdateById(noticeId, &data); err != nil {
// return err
//}
if err := tx.Table(recookGoodsNoticeModel.TableName()).Where("id=?", noticeId).Updates(&data).Error; err != nil {
return err
}
//删除旧图
//if err := tx.Where("noticeId=?", noticeId).Delete(&goods.RecookGoodsNoticeImageModel{}).Error; err != nil {
// return err
//}
//新增图片
var imgList []goods.RecookGoodsNoticeImageModel
for _, v := range img {
imgList = append(imgList, goods.RecookGoodsNoticeImageModel{
NoticeId: noticeId,
Image: v,
})
}
if err := tx.Where("noticeId=?", noticeId).Delete(&goods.RecookGoodsNoticeImageModel{}).Error; err != nil {
return err
}
if err := tx.Create(&imgList).Error; err != nil {
return err
}
var noticeGoods []goods.RecookGoodsNoticeGoodsModel
if !all {
for _, goodsId := range goodsIds {
noticeGoods = append(noticeGoods, goods.RecookGoodsNoticeGoodsModel{
NoticeId: data.Id,
GoodsId: goodsId,
})
}
if err := tx.Where("notice_id=?", noticeId).Delete(&goods.RecookGoodsNoticeGoodsModel{}).Error; err != nil {
return err
}
} else {
var gs goods.RecookGoodsInfoModel
subQ := tx.Table((&goods.RecookGoodsInfoModel{}).TableName()).
Select("id").
Where("vendor_id = ?", vendorId)
if err := tx.Delete(&goods.RecookGoodsNoticeGoodsModel{}, "goods_id in (?)", subQ).Error; err != nil {
return err
}
var gsID []uint
tx.Table(gs.TableName()).
Where("vendor_id = ?", vendorId).
Where("publish_status = 1").Pluck("id", &gsID)
for _, goodsId := range gsID {
noticeGoods = append(noticeGoods, goods.RecookGoodsNoticeGoodsModel{
NoticeId: noticeId,
GoodsId: goodsId,
})
}
}
//recookGoodsNoticeGoodsModel := &goods.RecookGoodsNoticeGoodsModel{}
if err := tx.Create(&noticeGoods).Error; err != nil {
return err
}
//recookGoodsNoticeGoodsModel.SetDb(tx)
//if err := recookGoodsNoticeGoodsModel.DeleteByNoticeId(noticeId); err != nil {
// return err
//}
//row := recookGoodsNoticeGoodsModel.CreateAll(&noticeGoods)
//if int64(len(noticeGoods)) != row {
// return errors.New("网络错误")
//}
return nil
})
if err != nil {
return err
}
return nil
}