package youngee_talent_service

import (
	"youngmini_server/app/dao"
	"youngmini_server/app/model/youngee_talent_model"
	"youngmini_server/app/utils"

	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
)

func GetTalentAccount(r *ghttp.Request) *TalentHttpResult {
	tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
	if err != nil {
		return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
	}

	res, err := g.DB().Model("youngee_platform_account_info").All("talent_id", tid)
	if err != nil {
		return &TalentHttpResult{Code: -2, Msg: "query database error"}
	}

	if res == nil {
		return &TalentHttpResult{Code: -3, Msg: "未绑定任何平台"}
	}

	return &TalentHttpResult{Code: 0, Msg: "success", Data: res}
}

func OnAddTalentAccount(r *ghttp.Request) *TalentHttpResult {
	tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
	if err != nil {
		return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
	}

	account := youngee_talent_model.PlatformAccountInfo{}
	err = r.ParseForm(&account)
	if err != nil {
		return &TalentHttpResult{Code: -2, Msg: err.Error()}
	}

	// 将达人id存入结构体
	account.TalentId = tid
	// 插入达人账号信息
	_, err = g.Model(dao.YoungeePlatformAccountInfo.Table).Insert(account)
	if err != nil {
		return &TalentHttpResult{Code: -2, Msg: err.Error()}
	}

	return &TalentHttpResult{Code: 0, Msg: "success"}
}

func OnUpdateTalentAccount(r *ghttp.Request) *TalentHttpResult {
	tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
	if err != nil {
		return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
	}

	account := youngee_talent_model.PlatformAccountInfo{}
	err = r.ParseForm(&account)
	if err != nil {
		return &TalentHttpResult{Code: -2, Msg: err.Error()}
	}
	account.TalentId = tid
	_, err = g.DB().Model(dao.YoungeePlatformAccountInfo.Table).Update(account, "account_id = ? and talent_id = ?", account.AccountId, tid)
	if err != nil {
		return &TalentHttpResult{Code: -3, Msg: "update failed"}
	}

	return &TalentHttpResult{Code: 0, Msg: "success"}
}
