package dao

import (
	"fonchain-artshow/cmd/model"
	"fonchain-artshow/pkg/db"
	uuid "github.com/satori/go.uuid"
	"go.uber.org/zap"
	"gorm.io/gorm"
)

func SaveArtworkPrice(tx *gorm.DB, artwork *model.ArtworkPrice) (err error) {
	uid, err := uuid.NewV4()
	if err != nil {
		return err
	}
	artwork.ArtworkPriceUID = uid.String()
	err = tx.Model(&model.ArtworkPrice{}).Create(&artwork).Error
	if err != nil {
		zap.L().Error("Artwork price save err", zap.Error(err))
		return
	}
	return
}

func UpdateArtworkPrice(tx *gorm.DB, artwork *model.ArtworkPrice) (err error) {
	err = tx.Model(&model.ArtworkPrice{}).Omit("artwork_price_uid").Where("artwork_price_uid = ?", artwork.ArtworkPriceUID).Updates(artwork).Error
	if err != nil {
		zap.L().Error("Artwork price save err", zap.Error(err))
		return
	}
	return
}

func ArtworkPriceList(showUID string) (err error, out []*model.ArtworkPrice) {
	out = make([]*model.ArtworkPrice, 0)
	err = db.DbArtShow.Model(&model.ArtworkPrice{}).Where("show_uid = ? ", showUID).Find(&out).Error
	if err != nil {
		zap.L().Error("Artwork list err", zap.Error(err))
		return
	}
	return
}

func QueryArtworkPrice_artworkUID(artworkUID string) (err error, out *model.ArtworkPrice) {
	out = new(model.ArtworkPrice)
	err = db.DbArtShow.Model(&model.ArtworkPrice{}).Where("artwork_uid = ? ", artworkUID).Find(&out).Error
	if err != nil {
		zap.L().Error("Artwork price  artwork_id query err", zap.Error(err))
		return
	}
	return
}

func QueryArtworkPrice_uid(uid string) (err error, out *model.ArtworkPrice) {
	out = new(model.ArtworkPrice)
	err = db.DbArtShow.Model(&model.ArtworkPrice{}).Where("artwork_price_uid = ? ", uid).Find(&out).Error
	if err != nil {
		zap.L().Error("Artwork price  id  query err", zap.Error(err))
		return
	}
	return
}

func DelArtworkPrice(tx *gorm.DB, artworkPriceUids []string) (err error) {
	err = tx.Where("artwork_price_uid in (?)", artworkPriceUids).Delete(&model.ArtworkPrice{}).Error
	if err != nil {
		zap.L().Error("Artwork delete  err", zap.Error(err))
		return
	}
	return
}

func DelArtworkPrice_showUID(tx *gorm.DB, show_uid string) (err error) {
	err = tx.Where("show_uid = ? ", show_uid).Delete(&model.ArtworkPrice{}).Error
	if err != nil {
		zap.L().Error("Artwork delete  err", zap.Error(err))
		return
	}
	return
}

func UpdateArtworkInfoWithMap(tx *gorm.DB, in map[string]interface{}, showUid, artworkPriceUid string) (err error) {
	if artworkPriceUid != "" {
		err = tx.Model(&model.ArtworkPrice{}).Omit("artwork_price_uid").Where("artwork_price_uid = ? and deleted_at is null ", artworkPriceUid).Updates(in).Error
		if err != nil {
			zap.L().Error("Artwork price update  err", zap.Error(err))
			return
		}
	} else if showUid != "" {
		err = tx.Model(&model.ArtworkPrice{}).Omit("artwork_price_uid,show_uid").Where("show_uid = ? and deleted_at is null ", showUid).Updates(in).Error
		if err != nil {
			zap.L().Error("Artwork price update  err", zap.Error(err))
			return
		}
	}
	return
}
