// 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 ArtshowArtistSupplementLogic struct{}

func (a ArtshowArtistSupplementLogic) BatchCreateSupplement(request *artistinfoArtshow.BatchCreateArtistSupplementRequest) error {
	var datas = []model.ArtshowArtistSupplement{}
	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,
			model.ArtshowArtistSupplement{LockTime: userInfo.LatestLockTime, ArtistUid: v, ArtistName: userInfo.RealName.Name, Status: 2, AuditStatus: 5},
		)
	}
	return dao.ArtistSupplement.BatchCreateData(datas)
}

func (a ArtshowArtistSupplementLogic) CreateSupplement(request *artistinfoArtshow.ArtistSupplementInfo) error {
	return dao.ArtistSupplement.CreateData(&model.ArtshowArtistSupplement{
		ArtistUid:       request.ArtistUid,
		Status:          2, //锁定
		AuditStatus:     5, //待补充
		LockTime:        request.LockTime,
		ArtistName:      request.ArtistName,
		ArtistProfile:   request.ArtistProfile,
		CountryArtLevel: request.CountryArtLevel,
		ArtistCertPic:   request.ArtistCertPic,
		BankNum:         request.BankNum,
		BankName:        request.BankName,
	})
}
func (a ArtshowArtistSupplementLogic) GetSupplementDetail(request *artistinfoArtshow.GetArtistSupplementDetailRequest) (rep *artistinfoArtshow.ArtistSupplementInfo, err error) {
	data, err := dao.ArtistSupplement.GetData(request.Id)
	if err != nil {
		if gorm.ErrRecordNotFound == err {
			err = errors.New("找不到数据")
		}
		return nil, err
	}
	rep = &artistinfoArtshow.ArtistSupplementInfo{
		ArtistUid:       data.ArtistUid,
		Status:          data.Status,
		LockTime:        data.LockTime,
		AuditStatus:     int64(data.AuditStatus),
		AuditMark1:      data.AuditMark1,
		AuditMark2:      data.AuditMark2,
		ArtistName:      data.ArtistName,
		ArtistProfile:   data.ArtistProfile,
		CountryArtLevel: data.CountryArtLevel,
		ArtistCertPic:   data.ArtistCertPic,
		BankNum:         data.BankNum,
		BankName:        data.BankName,
		Id:              data.ID,
		CreatedAt:       data.CreatedAt.Unix(),
		UpdatedAt:       data.UpdatedAt.Unix(),
		DeletedAt:       int64(data.DeletedAt),
		Editable:        data.Editable(),
	}
	return
}

func (a ArtshowArtistSupplementLogic) GetSupplementList(request *artistinfoArtshow.GetArtistSupplementListRequest) (res *artistinfoArtshow.GetArtistSupplementListResponse, err error) {
	res = &artistinfoArtshow.GetArtistSupplementListResponse{}
	datas, total, err := dao.ArtistSupplement.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.ArtistSupplementInfo{
			ArtistUid:       v.ArtistUid,
			ArtistName:      v.ArtistName,
			ArtistProfile:   v.ArtistProfile,
			CountryArtLevel: v.CountryArtLevel,
			ArtistCertPic:   v.ArtistCertPic,
			BankNum:         v.BankNum,
			BankName:        v.BankName,
			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 ArtshowArtistSupplementLogic) AuditSupplement(request *artistinfoArtshow.AuditArtistSupplementRequest) (*emptypb.Empty, error) {
	err := dao.ArtistSupplement.Audit(model.AuditStatus(request.AuditStatus), request.AuditMark1, request.AuditMark2, request.ArtistSupplementIds...)
	return nil, err
}

func (a ArtshowArtistSupplementLogic) UpdateSupplement(request *artistinfoArtshow.UpdateArtistSupplementRequest) (*emptypb.Empty, error) {
	err := dao.ArtistSupplement.UpdateData(&model.ArtshowArtistSupplement{
		Model:           model.Model{ID: request.Id},
		ArtistProfile:   request.ArtistProfile,
		CountryArtLevel: request.CountryArtLevel,
		ArtistCertPic:   request.ArtistCertPic,
		BankNum:         request.BankNum,
		BankName:        request.BankName,
		AuditStatus:     model.AuditType_Pending,
	})
	return nil, err
}

func (a ArtshowArtistSupplementLogic) DeletedSupplement(request *artistinfoArtshow.DeletedArtistSupplementRequest) (*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.ArtistSupplement.DeletedData(ids...)
	return nil, err
}
