package youngee_talent_service

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

	"github.com/gogf/gf/database/gdb"

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

// 获取达人收货地址
func GetTalentAddress(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(dao.YoungeeTalentDeliveryAddress.Table).All("talent_id", tid)
	if err != nil {
		return &TalentHttpResult{Code: -2, Msg: err.Error()}
	}

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

// 获取单个收货地址详情
func GetSingleAddress(r *ghttp.Request) *TalentHttpResult {
	tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
	if err != nil {
		return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
	}

	address_id := r.Get("address_id")
	res, err := g.DB().Model(dao.YoungeeTalentDeliveryAddress.Table).One("talent_id = ? and address_id = ?", tid, address_id)
	if err != nil {
		return &TalentHttpResult{Code: -2, Msg: err.Error()}
	}

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

// 添加收货地址
func OnAddTalentAddress(r *ghttp.Request) *TalentHttpResult {
	tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
	if err != nil {
		return &TalentHttpResult{Code: -1, Msg: "Add talent address failed"}
	}

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

	err = g.DB().Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
		var tErr error

		_, tErr = tx.Ctx(ctx).Model(dao.YoungeeTalentInfo.Table).Update("is_bind_location=1", "id", tid)
		if err != nil {
			return tErr
		}
		// 如果设置的是地址是默认地址，则把当前默认地址设为非默认
		if tAddr.DefaultTag > 0 {
			_, tErr = tx.Ctx(ctx).Model(dao.YoungeeTalentDeliveryAddress.Table).
				Update("default_tag=0", "talent_id=? and default_tag = 1", tid)
			if tErr != nil {
				return tErr
			}
		}
		// 将达人id存入结构体
		tAddr.TalentId = tid
		// 插入新的收货地址记录
		_, tErr = tx.Ctx(ctx).Model(dao.YoungeeTalentDeliveryAddress.Table).Insert(tAddr)
		if tErr != nil {
			return tErr
		}

		return nil
	})

	if err != nil {
		return &TalentHttpResult{Code: -3, Msg: "failed"}
	}

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

// 删除收货地址
func OnDeleteTalentAddress(r *ghttp.Request) *TalentHttpResult {
	tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
	if err != nil {
		return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
	}

	var deleteReq *youngee_talent_model.DeleteDeliveryAddress
	err = r.ParseForm(&deleteReq)
	if err != nil {
		return &TalentHttpResult{Code: -2, Msg: "params error"}
	}

	err = g.DB().Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
		if deleteReq.DefaultTag == 1 {
			_, err1 := tx.Ctx(ctx).Model(dao.YoungeeTalentDeliveryAddress.Table).Limit(1).Update("default_tag = 1", "talent_id = ? and address_id <> ?", tid, deleteReq.AddressId)
			if err1 != nil {
				return err1
			}
		}
		// 删除收货地址记录
		_, err1 := tx.Ctx(ctx).Model(dao.YoungeeTalentDeliveryAddress.Table).Delete("address_id", deleteReq.AddressId)
		if err1 != nil {
			return err1
		}
		c, err2 := tx.Ctx(ctx).Model(dao.YoungeeTalentDeliveryAddress.Table).Where("talent_id", tid).Count()
		if err2 != nil {
			return err2
		}
		if c == 0 {
			_, err3 := tx.Ctx(ctx).Model(dao.YoungeeTalentInfo.Table).Update("is_bind_location=0", "id", tid)
			if err3 != nil {
				return err3
			}
		}
		return nil
	})
	if err != nil {
		return &TalentHttpResult{Code: -3, Msg: "failed"}
	}

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

// 修改达人收货地址
func OnUpdateTalentAddress(r *ghttp.Request) *TalentHttpResult {
	tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
	if err != nil {
		return &TalentHttpResult{Code: -1, Msg: "Update talent address failed"}
	}

	var modifyReq *youngee_talent_model.DeliveryAddressModifyReq
	err = r.ParseForm(&modifyReq)
	if err != nil {
		return &TalentHttpResult{Code: -2, Msg: "params error"}
	}

	_, err = g.DB().Model(dao.YoungeeTalentDeliveryAddress.Table).Update(modifyReq, "address_id = ? and talent_id = ?", modifyReq.AddressId, tid)
	if err != nil {
		return &TalentHttpResult{Code: -3, Msg: "update failed"}
	}

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

// SetDefaultDeliveryAddr 设置默认收货地址
func SetDefaultDeliveryAddr(r *ghttp.Request) *TalentHttpResult {
	tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
	if err != nil {
		return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
	}

	aid := r.GetRequestInt32("address_id", 0)
	if aid == 0 {
		return &TalentHttpResult{Code: -1, Msg: "address id error"}
	}

	rec, err := g.DB().Model(dao.YoungeeTalentDeliveryAddress.Table).One("talent_id = ? and default_tag = 1", tid)
	if err != nil {
		return &TalentHttpResult{Code: -2, Msg: "found default delivery address error"}
	}

	if rec == nil {
		_, err = g.DB().Model(dao.YoungeeTalentDeliveryAddress.Table).Update("default_tag = 1", "address_id", aid)
	} else {
		err = g.DB().Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
			_, err1 := tx.Ctx(ctx).Model(dao.YoungeeTalentDeliveryAddress.Table).Update("default_tag = 0", "address_id", rec["address_id"])
			if err1 != nil {
				return err1
			}

			_, err1 = tx.Ctx(ctx).Model(dao.YoungeeTalentDeliveryAddress.Table).Update("default_tag = 1", "address_id", aid)
			if err1 != nil {
				return err1
			}

			return nil
		})
	}

	if err != nil {
		return &TalentHttpResult{Code: -3, Msg: "database error"}
	}

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