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.

104 lines
3.1 KiB

package goods
import (
"image"
"image/jpeg"
"image/png"
"path/filepath"
"recook/internal/static_path"
"strings"
//"github.com/astaxie/beego/config"
"github.com/corona10/goimagehash"
"github.com/jinzhu/gorm"
"os"
"recook/internal/v2/lib/db"
)
const (
RecookGoodsMainPhotoIsMasterFalse = 0
RecookGoodsMainPhotoIsMasterTrue = 1
)
type RecookGoodsMainPhotoModel struct {
db.BaseModel
Id uint `gorm:"column:id;primary_key" json:"id"`
GoodsId uint `gorm:"column:goods_id" json:"goodsId"`
Url string `gorm:"column:url" json:"url"`
Name string `gorm:"column:name" json:"name"`
IsMaster int `gorm:"column:is_master" json:"isMaster"`
OrderNo int `gorm:"column:order_no" json:"orderNo"`
Width int `gorm:"column:width" json:"width"`
Height int `gorm:"column:height" json:"height"`
Hash string `gorm:"column:hash"`
}
// TableName sets the insert table name for this struct type
func (r *RecookGoodsMainPhotoModel) TableName() string {
return "recook_goods_main_photo"
}
func (r *RecookGoodsMainPhotoModel) BeforeCreate(tx *gorm.DB) (err error) {
if r.IsMaster != RecookGoodsMainPhotoIsMasterTrue {
return
}
uri := filepath.Join(static_path.Dir.Root, r.Url)
file, _ := os.Open(uri)
defer file.Close()
var img image.Image
if strings.HasSuffix(uri, "png") {
img, _ = png.Decode(file)
} else if strings.HasSuffix(uri, "jpg") || strings.HasSuffix(uri, "jpeg") {
img, _ = jpeg.Decode(file)
} else {
return
}
if img == nil {
r.Hash = ""
return
}
hash, _ := goimagehash.DifferenceHash(img)
r.Hash = hash.ToString()
return
}
func (r *RecookGoodsMainPhotoModel) CreateAll(datas *[]RecookGoodsMainPhotoModel) int64 {
valueStr := ""
var values []interface{}
for _, item := range *datas {
valueStr += ",(?,?,?,?,?,?,?)"
values = append(values, item.GoodsId, item.Url, item.Name, item.IsMaster, item.OrderNo, item.Width, item.Height)
}
if len(values) > 0 {
return r.GetDb().Exec("insert into recook_goods_main_photo(goods_id,url,name,is_master,order_no,width,height) values "+valueStr[1:], values...).RowsAffected
}
return 0
}
// @Style 批量插入
func (r *RecookGoodsMainPhotoModel) CreateSelect(subSql *gorm.SqlExpr) error {
return r.GetDb().Exec("insert into recook_goods_main_photo(goods_id,url,name,is_master,order_no,width,height) ?", subSql).Error
}
func (r *RecookGoodsMainPhotoModel) DeleteByGoodsId(goodsId uint) int64 {
r.GetDb().Delete(&RecookGoodsMainPhotoModel{}, "goods_id = ?", goodsId)
return 0
}
// @Style 批量获取商品主图
func (r *RecookGoodsMainPhotoModel) GetMainPhotoByGoodsIds(goodsIds []uint) (result []RecookGoodsMainPhotoModel) {
if len(goodsIds) == 0 {
return []RecookGoodsMainPhotoModel{}
}
r.GetDb().Model(&RecookGoodsMainPhotoModel{}).Find(&result, "goods_id in (?) and is_master = ?", goodsIds, RecookGoodsMainPhotoIsMasterTrue)
return
}
// @Style 获取商品图片
func (r *RecookGoodsMainPhotoModel) FindByGoodsId(goodsId uint) (result []RecookGoodsMainPhotoModel) {
r.GetDb().Model(&RecookGoodsMainPhotoModel{}).Order("order_no").Find(&result, "goods_id = ?", goodsId)
return
}