package user

import (
	"errors"
	logger "eta/eta_mini_ht_api/common/component/log"
	userDao "eta/eta_mini_ht_api/models/user"
	"gorm.io/gorm"
)

type UserDTO struct {
	Id           int
	Username     string
	Mobile       string
	OpenId       string
	UnionId      string
	FollowingGzh int
	GzhOpenId    string
}

type FeedbackDTO struct {
	Mobile  string `json:"mobile"`
	UserId  int    `json:"userId"`
	Message string `json:"message"`
}

func convertUserDTO(user userDao.TemplateUser) UserDTO {
	return UserDTO{
		Id:           user.Id,
		Username:     user.Username,
		Mobile:       user.Mobile,
		OpenId:       user.OpenId,
		FollowingGzh: user.FollowingGzh,
	}
}
func GetUserByMobile(mobile string) (UserDTO, error) {
	var dto UserDTO
	templateUser, err := userDao.GetUserByMobile(mobile)
	if err != nil {
		if !errors.Is(err, gorm.ErrRecordNotFound) {
			logger.Error("查询用户失败：%v", err)
		}
		return dto, err
	}
	dto = convertUserDTO(templateUser)
	return dto, nil
}

func GetUserAndCountReadTimes(userId int) (err error) {
	err = userDao.GetUserByIdAndCountReadTimes(userId)
	if err != nil {
		logger.Error("更新用户阅读次数失败:%v", err)
	}
	return
}

func RegisterTemplateUser(dto *UserDTO) (err error) {
	templateUser := convertToTemplateUser(dto)
	err = userDao.RegisterTemplateUser(&templateUser)
	if err != nil {
		logger.Error("创建临时用户失败:%v", err)
		return
	}
	convertToUserDTO(templateUser, dto)
	return
}

func GetTemplateUserByOpenId(openId string) (user UserDTO, err error) {
	templateUser, err := userDao.GetUserByOpenId(openId)
	if err != nil {
		logger.Error("获取用户信息失败:%v", err)
		return
	}
	user = convertUserDTO(templateUser)
	return
}
func FeedBack(dto FeedbackDTO) (err error) {
	feedBackEntity := userDao.FeedBack{
		Message: dto.Message,
		Mobile:  dto.Mobile,
		UserID:  dto.UserId,
	}
	return userDao.InsertFeedBack(feedBackEntity)
}

func convertToTemplateUser(dto *UserDTO) userDao.TemplateUser {
	return userDao.TemplateUser{
		Username:     dto.Username,
		Mobile:       dto.Mobile,
		OpenId:       dto.OpenId,
		UnionId:      dto.UnionId,
		GzhOpenId:    dto.GzhOpenId,
		FollowingGzh: dto.FollowingGzh,
	}
}

func convertToUserDTO(templateUser userDao.TemplateUser, dto *UserDTO) {
	dto.Id = templateUser.Id
	dto.Username = templateUser.Username
	dto.Mobile = templateUser.Mobile
	dto.OpenId = templateUser.OpenId
	dto.UnionId = templateUser.UnionId
	dto.FollowingGzh = templateUser.FollowingGzh
}

func BindUserMobile(userId int, mobile string) (err error) {
	err = userDao.BindMobile(userId, mobile)
	if err != nil {
		if !errors.Is(err, gorm.ErrRecordNotFound) {
			logger.Error("绑定手机号失败：%v", err)
		}
		return
	}
	return
}

func GetUserByOpenId(openId string) (userDto UserDTO, err error) {
	templateUser, err := userDao.GetUserByOpenId(openId)
	if err != nil {
		logger.Error("查询用户失败：%v", err)
		return
	}
	if templateUser.IsDeleted == 1 {
		logger.Error("用户已删除：%v", err)
		err = errors.New("用户已删除")
		return
	}
	userDto = convertUserDTO(templateUser)
	return
}

func GetTemplateUserByUnionId(unionId string) (user UserDTO, err error) {
	templateUser, err := userDao.GetUserByUnionId(unionId)
	if err != nil {
		logger.Error("获取用户信息失败:%v", err)
		return
	}
	user = convertUserDTO(templateUser)
	return
}

// 绑定小程序OpenId
func BindUserXcxOpenId(userId int, openId string) (err error) {
	err = userDao.BindXcxOpenId(userId, openId)
	if err != nil {
		if !errors.Is(err, gorm.ErrRecordNotFound) {
			logger.Error("绑定小程序openId失败：%v", err)
		}
		return
	}
	return
}

// 绑定公众号OpenId
func BindUserGzhOpenId(userId int, openId string, followingGzh int) (err error) {
	err = userDao.BindGzhOpenId(userId, openId, followingGzh)
	if err != nil {
		if !errors.Is(err, gorm.ErrRecordNotFound) {
			logger.Error("绑定公众号openId失败：%v", err)
		}
		return
	}
	return
}

func GetTemplateUserByGzhOpenId(openId string) (user UserDTO, err error) {
	templateUser, err := userDao.GetUserByGzhOpenId(openId)
	user = convertUserDTO(templateUser)
	return
}
