// Package logic -----------------------------
// @file      : artistinfo_artshowVideo.go
// @author    : JJXu
// @contact   : wavingbear@163.com
// @time      : 2023/3/2 11:51
// -------------------------------------------
package logic

import (
	"errors"
	"github.com/fonchain/fonchain-artistinfo/cmd/internal/dao"
	"github.com/fonchain/fonchain-artistinfo/cmd/model"
	"github.com/fonchain/fonchain-artistinfo/pb/artistinfoArtshow"
	"google.golang.org/protobuf/types/known/emptypb"
	"gorm.io/gorm"
)

type ArtshowVideoLogic struct{}

func (a ArtshowVideoLogic) BatchCreateArtshowVideo(request *artistinfoArtshow.BatchCreateArtshowVideoRequest) error {
	var datas = []model.ArtshowVideoRecord{}
	for _, v := range request.Data {
		datas = append(datas, model.ArtshowVideoRecord{
			ArtistUid:   v.ArtistUid,
			LockTime:    v.LockTime,
			ArtistName:  v.ArtistName,
			VideoUrl:    v.VideoUrl,
			AuditStatus: model.AuditStatus(v.AuditStatus),
			AuditMark1:  v.AuditMark1,
			AuditMark2:  v.AuditMark2,
			Status:      v.Status,
		})
	}
	return dao.ArtistinfoArtshowVideo.BatchCreateData(datas)
}
func (a ArtshowVideoLogic) CreateArtshowVideo(request *artistinfoArtshow.ArtshowVideoInfo) error {
	return dao.ArtistinfoArtshowVideo.CreateData(&model.ArtshowVideoRecord{
		ArtistUid:   request.ArtistUid,
		LockTime:    request.LockTime,
		ArtistName:  request.ArtistName,
		VideoUrl:    request.VideoUrl,
		AuditStatus: model.AuditStatus(request.AuditStatus),
		AuditMark1:  request.AuditMark1,
		AuditMark2:  request.AuditMark2,
		Status:      request.Status,
	})
}
func (a ArtshowVideoLogic) GetArtshowVideoDetail(request *artistinfoArtshow.GetArtshowVideoDetailRequest) (rep *artistinfoArtshow.ArtshowVideoInfo, err error) {
	data, err := dao.ArtistinfoArtshowVideo.GetArtshowVideoDetail(request)
	if err != nil {
		if gorm.ErrRecordNotFound == err {
			err = errors.New("找不到数据")
		}
		return nil, err
	}
	rep = &artistinfoArtshow.ArtshowVideoInfo{
		Id:          data.ID,
		ArtistUid:   data.ArtistUid,
		LockTime:    data.LockTime,
		VideoUrl:    data.VideoUrl,
		AuditStatus: int64(data.AuditStatus),
		AuditMark1:  data.AuditMark1,
		AuditMark2:  data.AuditMark2,
		CreatedAt:   data.CreatedAt.Unix(),
		UpdatedAt:   data.UpdatedAt.Unix(),
		DeletedAt:   int64(data.DeletedAt),
		ArtistName:  data.ArtistName,
		Status:      data.Status,
		Editable:    data.Editable(),
	}
	return
}

func (a ArtshowVideoLogic) GetArtshowVideoList(request *artistinfoArtshow.GetArtshowVideoListRequst) (res *artistinfoArtshow.GetArtshowVideoListResponse, err error) {
	res = &artistinfoArtshow.GetArtshowVideoListResponse{}
	datas, total, err := dao.ArtistinfoArtshowVideo.GetDataList(request)
	if err != nil {
		return nil, err
	}
	res.Page = &artistinfoArtshow.VideoPagination{
		Page:     request.Page,
		PageSize: request.PageSize,
		Total:    total,
	}
	for _, v := range datas {
		res.Data = append(res.Data, &artistinfoArtshow.ArtshowVideoInfo{
			Id:          v.ID,
			ArtistUid:   v.ArtistUid,
			LockTime:    v.LockTime,
			VideoUrl:    v.VideoUrl,
			AuditStatus: int64(v.AuditStatus),
			AuditMark1:  v.AuditMark1,
			AuditMark2:  v.AuditMark2,
			CreatedAt:   v.CreatedAt.Unix(),
			UpdatedAt:   v.UpdatedAt.Unix(),
			DeletedAt:   int64(v.DeletedAt),
			Status:      v.Status,
			ArtistName:  v.ArtistName,
		})
	}
	return
}

func (a ArtshowVideoLogic) AuditArtshowVideo(request *artistinfoArtshow.AuditArtshowVideoRequest) (*emptypb.Empty, error) {
	err := dao.ArtistinfoArtshowVideo.Audit(model.AuditStatus(request.AuditStatus), request.AuditMark1, request.AuditMark2, request.ArtshowVideoIds...)
	return nil, err
}

func (a ArtshowVideoLogic) UpdateArtshowVideo(request *artistinfoArtshow.UpdateArtshowVideoRequest) (*emptypb.Empty, error) {
	err := dao.ArtistinfoArtshowVideo.UpdateData(&model.ArtshowVideoRecord{
		Model:       model.Model{ID: request.Id},
		ArtistUid:   request.ArtistUid,
		LockTime:    request.LockTime,
		ArtistName:  request.ArtistName,
		VideoUrl:    request.VideoUrl,
		AuditStatus: model.AuditStatus(request.AuditStatus),
		AuditMark1:  request.AuditMark1,
		AuditMark2:  request.AuditMark2,
	})
	return nil, err
}

func (a ArtshowVideoLogic) DeletedArtshowVideo(request *artistinfoArtshow.DeletedArtshowVideoRequest) (*emptypb.Empty, error) {
	var ids = []int64{}
	if request.Id != 0 {
		ids = append(ids, request.Id)
	} else if len(request.Ids) > 0 {
		ids = append(ids, request.Ids...)
	}
	err := dao.ArtistinfoArtshowVideo.DeletedData(ids...)
	return nil, err
}
