package user

import (
	"errors"
	userDao "eta/eta_mini_ht_api/models/user"
	"time"
)

type FollowDTO struct {
	UserId       int       `json:"userId"`
	AnalystId    int       `json:"analystId"`
	AnalystName  string    `json:"analystName"`
	FollowType   string    `json:"followType"`
	FollowedTime time.Time `json:"followedTime"`
}

func convertToCrmFollowingAnalyst(dto FollowDTO) userDao.UserAnalystFollowList {
	return userDao.UserAnalystFollowList{
		FinancialAnalystID:   dto.AnalystId,
		FinancialAnalystName: dto.AnalystName,
		FollowedTime:         time.Now(),
		Followed:             followType(dto.FollowType),
		UserID:               dto.UserId,
	}
}

func GetPostUser(authorName string, PublishTime string) (ids []int) {
	return userDao.GetPostUser(authorName, PublishTime)
}

func convertToFollowDTO(follow userDao.UserAnalystFollowList) (dto FollowDTO) {
	return FollowDTO{
		UserId:       follow.UserID,
		AnalystId:    follow.FinancialAnalystID,
		AnalystName:  follow.FinancialAnalystName,
		FollowType:   string(follow.Followed),
		FollowedTime: follow.FollowedTime,
	}
}
func convertFollowStatusToFollowDTO(follow userDao.FollowStatus) (dto FollowDTO) {
	return FollowDTO{
		AnalystName: follow.FinancialAnalystName,
		FollowType:  string(follow.Followed),
	}
}
func followType(followType string) userDao.FollowingType {
	switch followType {
	case "following":
		return userDao.Following
	case "unfollowed":
		return userDao.Unfollowed
	default:
		return ""
	}
}
func FollowAnalystsByName(userId int, analysts []FollowDTO, follow string) error {
	flType := followType(follow)
	var userFollows []userDao.UserAnalystFollowList
	if flType == "" {
		return errors.New("关注状态非法")
	}
	for _, analyst := range analysts {
		userFollow := convertToCrmFollowingAnalyst(analyst)
		userFollow.Followed = flType
		userFollows = append(userFollows, userFollow)
	}
	return userDao.FollowAnalystsByName(userId, userFollows, flType)
}
func CheckFollowStatusByNames(userId int, names []string) (follows []FollowDTO, err error) {
	list, err := userDao.CheckFollowStatusByNames(userId, names)
	if err != nil {
		return
	}
	for _, follow := range list {
		follows = append(follows, convertFollowStatusToFollowDTO(follow))
	}
	return
}
func GetFollowingAnalystList(userId int) (dtoList []FollowDTO, err error) {
	list, err := userDao.GetFollowingAnalystList(userId)
	if err != nil {
		return
	}
	for _, follow := range list {
		dtoList = append(dtoList, convertToFollowDTO(follow))
	}
	return
}

func GetFollowed(userId int, analystId int) string {
	return userDao.GetFollowed(userId, analystId)
}
func FollowAnalyst(dto FollowDTO) (err error) {
	follow := convertToCrmFollowingAnalyst(dto)
	return userDao.FollowAnalyst(follow)
}

func GetFollowing(userId int, analystId int) string {
	return userDao.GetFollowing(userId, analystId)
}
