package talent_service

import (
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"youngmini_server/app/dao"
	"youngmini_server/app/model/talent_model"
	"youngmini_server/app/utils"
)

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

	isComplete := false
	rec, err := g.DB().Model(dao.TalentInfo.Table).One("id", tid)
	test := rec[dao.TalentInfo.Columns.TalentWxNumber].Val()
	if err != nil || rec == nil || test == nil {
		isComplete = false
	} else {
		isComplete = true
	}

	platCnt, err := g.DB().Model(dao.RTalentPlatformTable.Table).
		Where("tid = ? and (examine_state = 1 or examine_state = 2)", tid).Count()
	if err != nil {
		platCnt = 0
	}

	boboCoin, err := g.DB().Model(dao.BobocoinIncomeRecord.Table).
		Where("talent_id", tid).
		Sum(dao.BobocoinIncomeRecord.Columns.BobocoinValue)
	if err != nil {
		boboCoin = 0
	}

	drawMoney, err := g.DB().Model(dao.BobocoinWithdrawalRecord.Table).
		Where("talent_id = ? and (pay_state = 1 or pay_state = 2)", tid).
		Sum(dao.BobocoinWithdrawalRecord.Columns.DrawAmount)
	if err != nil {
		drawMoney = 0
	}

	hasAddr := false
	addrCnt, err := g.DB().Model(dao.TalentDeliveryAddress.Table).
		Where(dao.TalentDeliveryAddress.Columns.TalentId, tid).Count(dao.TalentDeliveryAddress.Columns.AddressId)
	if err != nil {
		hasAddr = false
	} else {
		hasAddr = addrCnt > 0
	}

	briefInfo := talent_model.TalentBriefInfo{
		IsInfoComplete: isComplete,
		PlatformCount: platCnt,
		BoBoCoinValue: int64(boboCoin - drawMoney),
		HasDeliveryAddr: hasAddr,
	}

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

// OnGetTalentDetailInfo 获取达人详细信息
func OnGetTalentDetailInfo(r *ghttp.Request) *TalentHttpResult {
	tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
	if err != nil {
		return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
	}

	rec, err := g.DB().Model(dao.TalentInfo.Table).Fields(dao.TalentInfo.Columns.TalentAgeBracket,
		dao.TalentInfo.Columns.TalentSkinType, dao.TalentInfo.Columns.TalentWxNumber,
		dao.TalentInfo.Columns.TalentNationality, dao.TalentInfo.Columns.VisitStoreRegion,
		dao.TalentInfo.Columns.TalentGender, dao.TalentInfo.Columns.TalentPhoneNumber).One("id", tid)

	if err != nil {
		return &TalentHttpResult{Code: -2, Msg: "can not found talent info"}
	}

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

// OnPostTalentDetailInfo 修改达人详细信息
func OnPostTalentDetailInfo(r *ghttp.Request) *TalentHttpResult {
	tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
	if err != nil {
		return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
	}

	var infos *talent_model.TalentSelfInputInfo
	err = r.ParseForm(&infos)
	if err != nil {
		return &TalentHttpResult{Code: -2, Msg: err.Error()}
	}

	_, err = g.DB().Model(dao.TalentInfo.Table).Update(infos, "id", tid)
	if err != nil {
		return &TalentHttpResult{Code: -4, Msg: "update failed"}
	}

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

