// 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"
)

/*
 * MallProductComment
 * Comment: 商品评论
 * Version: 10
 * Created: 2022-04-12 14:36:54 , Updated:0001-01-01 00:00:00
 */
type MallProductComment struct {
	gorm.Model
	types.Std_Owner
	types.Std_Identity
	ProductIdentity string                `gorm:"column:product_identity;type:varchar(36);" json:"product_identity"`            // 商品唯一码
	SpecIdentity    string                `gorm:"column:spec_identity;type:varchar(36);" json:"spec_identity"`                  // 商品规格唯一码
	StoreIdentity   string                `gorm:"column:store_identity;type:varchar(36);" json:"store_identity"`                // 店铺唯一标识
	CommentIdentity string                `gorm:"column:comment_identity;type:varchar(36);default:'';" json:"comment_identity"` // 被回复的评论唯一标识
	Nickname        string                `gorm:"column:nickname;type:varchar(255);default:'';" json:"nick_name"`               // 用户昵称
	Score           int8                  `gorm:"column:score;default:0;" json:"score"`                                         // 评分
	Comment         string                `gorm:"column:comment;type:varchar(500);" json:"comment"`                             // 评论内容
	Reply           string                `gorm:"column:reply;type:varchar(500);" json:"reply"`                                 // 评论回复
	Enabled         int8                  `gorm:"column:enabled;default:0;" json:"enabled"`                                     // 是否允许展示
	List            []*MallProductComment `gorm:"foreignKey:CommentIdentity;references:Identity;" json:"list"`

	ProductSpec MallProductSpec `gorm:"foreignKey:Identity;references:SpecIdentity;" json:"product_spec"`
	Product     MallProduct     `gorm:"foreignKey:Identity;references:ProductIdentity;" json:"product"`
}

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

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

// DelComment 删除评论
func DelComment(identity string) (int64, error) {
	result := impl.DBService.Where("identity = ?", identity).Delete(&MallProductComment{})
	return result.RowsAffected, result.Error
}

// CreateComment 添加评论
func CreateComment(comment *MallProductComment) (int64, error) {
	result := impl.DBService.Create(&comment)
	return result.RowsAffected, result.Error
}

// ReComment 回复评论
func ReComment(identity, reply string) (int64, error) {
	result := impl.DBService.Model(&MallProductComment{}).Select("reply").Where("identity = ?", identity).Updates(MallProductComment{Reply: reply})
	return result.RowsAffected, result.Error
}

// ModifyComment 修改评论
func ModifyComment(comment *MallProductComment) (int64, error) {
	result := impl.DBService.Where("identity = ?", comment.Identity).Updates(&comment)
	return result.RowsAffected, result.Error
}
