// Models generated by mesh dev cli,@Author: David Yan(david.yan@qq.com).
package models

import (
	"bsm/full/module/ec/mall/internal/impl"
	"git.apinb.com/bsm-sdk/core/database"
	"git.apinb.com/bsm-sdk/core/types"
	"gorm.io/gorm"
)

/*
 * MallProductPhotos
 * Comment: 商品相册表
 * Version: 10
 * Created: 2022-04-12 15:36:17 , Updated:0001-01-01 00:00:00
 */
type MallProductPhotos struct {
	gorm.Model
	types.Std_Identity
	ProductID       uint   `gorm:"column:product_id;index;not null;" json:"product_id"`                           // 商品ID
	ProductIdentity string `gorm:"column:product_identity;type:varchar(255);default:'';" json:"product_identity"` // 商品唯一码
	Name            string `gorm:"column:name;type:varchar(255);default:'';" json:"name"`                         // 说明
	Url             string `gorm:"column:url;type:varchar(255);default:'';" json:"url"`                           // URL地址
	Sort            int32  `gorm:"column:sort;default:0;" json:"sort"`                                            // 排序

}

func init() {
	database.AppendMigrate(&MallProductPhotos{})
}

// TableName .
func (table *MallProductPhotos) TableName() string {
	return "mall_product_photos" //对应数据库表名
}

// PhotoList 产品图片列表
func PhotoList(identity string) ([]*MallProductPhotos, int32, error) {
	tx := impl.DBService.Where("product_identity = ?", identity)
	posts := make([]*MallProductPhotos, 0)
	var cnt int64 = 0
	tx.Order("created_at desc").Find(&posts).Count(&cnt)
	err := tx.Error
	return posts, int32(cnt), err
}

// DelPhoto 删除图片
func DelPhoto(identity string) (int64, error) {
	result := impl.DBService.Where("identity = ?", identity).Delete(&MallProductPhotos{})
	return result.RowsAffected, result.Error
}
