// Package dao -----------------------------
// @file      : artistinfo_artshow.go
// @author    : JJXu
// @contact   : wavingbear@163.com
// @time      : 2023/3/2 12:06
// -------------------------------------------
package dao

import (
	"errors"
	"github.com/fonchain/fonchain-artistinfo/cmd/model"
	"github.com/fonchain/fonchain-artistinfo/pb/artistinfoArtshow"
	db "github.com/fonchain/fonchain-artistinfo/pkg/db"
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
)

var ArtistinfoArtshowVideo = new(artistinfoArtshowVideo)

type artistinfoArtshowVideo struct{}

func (a artistinfoArtshowVideo) BatchCreateData(datas []model.ArtshowVideoRecord) error {
	//return db.DB.Create(&datas).Error
	tx := db.DB.Begin()
	for _, v := range datas {
		err := a.CreateData(&v, tx)
		if err != nil {
			return err
		}
	}
	tx.Commit()
	return nil
}
func (a artistinfoArtshowVideo) CreateData(data *model.ArtshowVideoRecord, tx ...*gorm.DB) error {
	var txdb *gorm.DB
	if tx != nil {
		txdb = tx[0]
	} else {
		txdb = db.DB
	}
	var exist = model.ArtshowVideoRecord{}
	db.DB.Where("artist_uid = ? AND status = 2", data.ArtistUid).Find(&exist)
	if exist.ID != 0 {
		return errors.New("数据已存在")
	}
	return txdb.Create(&data).Error
}

func (a artistinfoArtshowVideo) UpdateData(data *model.ArtshowVideoRecord) error {
	return db.DB.Updates(&data).Error

}

func (a artistinfoArtshowVideo) DeletedData(id ...int64) (err error) {
	if len(id) == 1 {
		err = db.DB.Where("id = ?", id[0]).Delete(&model.ArtshowVideoRecord{}).Error
	} else if len(id) > 0 {
		err = db.DB.Where("id = ?", id).Delete(&model.ArtshowVideoRecord{}).Error
	}
	return err
}

func (a artistinfoArtshowVideo) GetData(id int64) (data *model.ArtshowVideoRecord, err error) {
	err = db.DB.Where("id = ?", id).First(data).Error
	return
}

func (a artistinfoArtshowVideo) GetDataList(req *artistinfoArtshow.GetArtshowVideoListRequst) (datas []model.ArtshowVideoRecord, total int64, err error) {
	datas = []model.ArtshowVideoRecord{}
	var tx = db.DB.Model(model.ArtshowVideoRecord{})
	if req.ArtistUid != "" {
		tx = tx.Where("artist_uid = ?", req.ArtistUid)
	}
	if req.LockTime != "" {
		tx = tx.Where("lock_time = ?", req.LockTime)
	}
	if req.ArtistName != "" {
		tx = tx.Clauses(clause.Like{
			Column: "artist_name",
			Value:  "%" + req.ArtistName + "%",
		})
	}
	if req.Status != 0 {
		tx = tx.Where("status = ?", req.Status)
	}
	//if req.VideoUrl != "" {
	//	tx = tx.Where("video_url = ?", req.VideoUrl)
	//}
	if req.AuditStatus != 0 {
		tx = tx.Where("audit_status = ?", req.AuditStatus)
	}
	//if req.AuditMark1 != "" {
	//	tx = tx.Where("audit_mark1 = ?", req.AuditMark1)
	//}
	//if req.AuditMark2 != "" {
	//	tx = tx.Where("audit_mark2 = ?", req.AuditMark2)
	//}
	err = tx.Count(&total).Scopes(db.Pagination(req.Page, req.PageSize)).Find(&datas).Error
	return
}
func (a artistinfoArtshowVideo) Audit(auditStatus model.AuditStatus, mark1, mark2 string, ids ...int64) (err error) {
	tx := db.DB.Begin()
	err = tx.Model(model.ArtshowVideoRecord{}).Where("id in ?", ids).Update("audit_status", auditStatus).Error
	if err != nil {
		return err
	}
	err = tx.Model(model.ArtshowVideoRecord{}).Where("id in ?", ids).Update("audit_mark1", mark1).Error
	if err != nil {
		return err
	}
	err = tx.Model(model.ArtshowVideoRecord{}).Where("id in ?", ids).Update("audit_mark2", mark2).Error
	if err != nil {
		return err
	}
	tx.Commit()
	return
}
func (a artistinfoArtshowVideo) GetArtshowVideoDetail(in *artistinfoArtshow.GetArtshowVideoDetailRequest) (rep model.ArtshowVideoRecord, err error) {
	var tx = db.DB.Model(model.ArtshowVideoRecord{})
	//if in.ArtistUid != "" {
	//	tx = tx.Where("artist_uid =?", in.ArtistUid)
	//}
	//if in.LockTime != "" {
	//	tx = tx.Where("lock_time = ?", in.LockTime)
	//}
	//if in.Status != 0 {
	//	tx = tx.Where("status = ?", in.Status)
	//}
	if in.Id != 0 {
		tx = tx.Where("id = ?", in.Id)
	}
	err = tx.First(&rep).Error
	return
}
