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

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

type ArtshowArtistIndexLogic struct{}

func (a ArtshowArtistIndexLogic) BatchCreateArtistIndex(request *artistinfoArtshow.BatchCreateArtistIndexRequest) error {
	var datas = []model.ArtshowArtistIndex{}
	for _, v := range request.ArtistUids {
		userInfo, err := NewArtistInfo().FindUser(&artistInfoUser.FindUserRequest{
			MgmtArtistUid: v,
			IsArtist:      true,
		})
		if err != nil {
			return err
		}
		if userInfo.Id == 0 {
			return errors.New(fmt.Sprintf("用户%s不存在", v))
		}
		datas = append(datas,
			//Status=2(锁定)   AuditStatus=5(待补充)
			model.ArtshowArtistIndex{LockTime: userInfo.LatestLockTime, ArtistUid: v, Class: "exhibition", Title: "艺术家-展览", Types: "2", Status: 2, AuditStatus: 5},
			model.ArtshowArtistIndex{LockTime: userInfo.LatestLockTime, ArtistUid: v, Class: "seniority", Title: "艺术家-资历", Types: "1", Status: 2, AuditStatus: 5},
			model.ArtshowArtistIndex{LockTime: userInfo.LatestLockTime, ArtistUid: v, Class: "specialized", Title: "艺术家-专业", Types: "1", Status: 2, AuditStatus: 5},
			model.ArtshowArtistIndex{LockTime: userInfo.LatestLockTime, ArtistUid: v, Class: "Influence", Title: "艺术家-影响力", Types: "1", Status: 2, AuditStatus: 5},
			model.ArtshowArtistIndex{LockTime: userInfo.LatestLockTime, ArtistUid: v, Class: "collect", Title: "艺术家-收藏", Types: "1", Status: 2, AuditStatus: 5},
		)
	}
	return dao.ArtistinfoArtshowArtistIndex.BatchCreateData(datas)
}

func (a ArtshowArtistIndexLogic) CreateArtistIndex(request *artistinfoArtshow.ArtistIndexInfo) error {
	return dao.ArtistinfoArtshowArtistIndex.CreateData(&model.ArtshowArtistIndex{
		LockTime:  request.LockTime,
		ArtistUid: request.ArtistUid,
		Class:     request.Class,
		Title:     request.Title,
		Types:     request.Types,
		Status:    2,
	})
}
func (a ArtshowArtistIndexLogic) GetArtistIndexDetail(request *artistinfoArtshow.GetArtistIndexDetailRequest) (rep *artistinfoArtshow.ArtistIndexInfo, err error) {
	data, err := dao.ArtistinfoArtshowArtistIndex.GetData(request.Id)
	if err != nil {
		if gorm.ErrRecordNotFound == err {
			err = errors.New("找不到数据")
		}
		return nil, err
	}
	rep = &artistinfoArtshow.ArtistIndexInfo{
		ArtistUid:   data.ArtistUid,
		Title:       data.Title,
		Class:       data.Class,
		TitleScore:  data.TitleScore,
		Score:       data.Score,
		Types:       data.Types,
		Status:      data.Status,
		LockTime:    data.LockTime,
		AuditMark1:  data.AuditMark1,
		AuditMark2:  data.AuditMark2,
		AuditStatus: int64(data.AuditStatus),
		Id:          data.ID,
		CreatedAt:   data.CreatedAt.Unix(),
		UpdatedAt:   data.UpdatedAt.Unix(),
		DeletedAt:   int64(data.DeletedAt),
		Editable:    data.Editable(),
	}
	return
}

func (a ArtshowArtistIndexLogic) GetArtistIndexList(request *artistinfoArtshow.GetArtistIndexListRequest) (res *artistinfoArtshow.GetArtistIndexListResponse, err error) {
	res = &artistinfoArtshow.GetArtistIndexListResponse{}
	datas, total, err := dao.ArtistinfoArtshowArtistIndex.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.ArtistIndexInfo{
			ArtistUid:   v.ArtistUid,
			Title:       v.Title,
			Class:       v.Class,
			TitleScore:  v.TitleScore,
			Score:       v.Score,
			Types:       v.Types,
			Status:      v.Status,
			LockTime:    v.LockTime,
			AuditMark1:  v.AuditMark1,
			AuditMark2:  v.AuditMark2,
			AuditStatus: int64(v.AuditStatus),
			Id:          v.ID,
			CreatedAt:   v.CreatedAt.Unix(),
			UpdatedAt:   v.UpdatedAt.Unix(),
			DeletedAt:   int64(v.DeletedAt),
		})
	}
	return
}

func (a ArtshowArtistIndexLogic) AuditArtistIndex(request *artistinfoArtshow.AuditArtistIndexRequest) (*emptypb.Empty, error) {
	err := dao.ArtistinfoArtshowArtistIndex.Audit(model.AuditStatus(request.AuditStatus), request.AuditMark1, request.AuditMark2, request.ArtistIndexIds...)
	return nil, err
}

func (a ArtshowArtistIndexLogic) UpdateArtistIndex(request *artistinfoArtshow.UpdateArtistIndexRequest) (*emptypb.Empty, error) {
	err := dao.ArtistinfoArtshowArtistIndex.UpdateData(&model.ArtshowArtistIndex{
		Model:       model.Model{ID: request.Id},
		TitleScore:  request.TitleScore,
		Score:       request.Score,
		AuditStatus: model.AuditType_Pending,
	})
	return nil, err
}

func (a ArtshowArtistIndexLogic) DeletedArtistIndex(request *artistinfoArtshow.DeletedArtistIndexRequest) (*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.ArtistinfoArtshowArtistIndex.DeletedData(ids...)
	return nil, err
}
