// Package dao -----------------------------
// @file      : artistinfo_artshow_artistIndex.go
// @author    : JJXu
// @contact   : wavingbear@163.com
// @time      : 2023/3/3 0:20
// -------------------------------------------
package dao

import (
	"errors"
	"fmt"
	"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"
)

var ArtistinfoArtshowArtistIndex = new(artistinfoArtshowArtistIndex)

type artistinfoArtshowArtistIndex struct{}

func (a artistinfoArtshowArtistIndex) BatchCreateData(datas []model.ArtshowArtistIndex) 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 artistinfoArtshowArtistIndex) CreateData(data *model.ArtshowArtistIndex, tx ...*gorm.DB) error {
	var txdb *gorm.DB
	if tx != nil {
		txdb = tx[0]
	} else {
		txdb = db.DB
	}
	var exist = model.ArtshowArtistIndex{}
	db.DB.Where("artist_uid = ? AND status = 2 AND class= ?", data.ArtistUid, data.Class).Find(&exist)
	if exist.ID != 0 {
		return errors.New(fmt.Sprintf("主题[%s]已存在", data.Title))
	}
	return txdb.Create(&data).Error
}

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

}

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

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

func (a artistinfoArtshowArtistIndex) GetDataList(req *artistinfoArtshow.GetArtistIndexListRequest) (datas []model.ArtshowArtistIndex, total int64, err error) {
	datas = []model.ArtshowArtistIndex{}
	var tx = db.DB.Model(model.ArtshowArtistIndex{})
	if req.ArtistUid != "" {
		tx = tx.Where("artist_uid = ?", req.ArtistUid)
	}
	if req.LockTime != "" {
		tx = tx.Where("lock_time = ?", req.LockTime)
	}
	if req.Status != 0 {
		tx = tx.Where("status = ?", req.Status)
	}
	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 artistinfoArtshowArtistIndex) Audit(auditStatus model.AuditStatus, mark1, mark2 string, ids ...int64) (err error) {
	tx := db.DB.Begin()
	err = tx.Model(model.ArtshowArtistIndex{}).Where("id in ?", ids).Update("audit_status", auditStatus).Error
	if err != nil {
		return err
	}
	err = tx.Model(model.ArtshowArtistIndex{}).Where("id in ?", ids).Update("audit_mark1", mark1).Error
	if err != nil {
		return err
	}
	err = tx.Model(model.ArtshowArtistIndex{}).Where("id in ?", ids).Update("audit_mark2", mark2).Error
	if err != nil {
		return err
	}
	tx.Commit()
	return
}

func (a artistinfoArtshowArtistIndex) GetArtistIndexDetail(in *artistinfoArtshow.GetArtistIndexDetailRequest) (rep model.ArtshowArtistIndex, err error) {
	var tx = db.DB.Model(model.ArtshowArtistIndex{})
	if in.Id != 0 {
		tx = tx.Where("id = ?", in.Id)
	}
	//var data model.ArtshowArtistIndex
	err = tx.First(&rep).Error
	return
}
