package talent_service

import (
	"context"
	"github.com/gogf/gf/database/gdb"
	"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"
)

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

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

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

	addrId := r.GetRequestInt("address_id", 0)
	if addrId == 0 {
		return &TalentHttpResult{Code: -2, Msg: "address id error"}
	}

	rec, err := g.DB().Model(dao.TalentDeliveryAddress.Table).One("address_id = ? and talent_id = ?", addrId, tid)
	if err != nil {
		return &TalentHttpResult{Code: -3, Msg: "Delivery address not found"}
	}

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

// OnAddDeliveryAddr 接受达人端上传的收货地址
func OnAddDeliveryAddr(r *ghttp.Request) *TalentHttpResult {
	tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
	if err != nil {
		return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
	}

	var tAddr *talent_model.DeliveryAddress
	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
		// 如果设置的是地址是默认地址，则把当前默认地址设为非默认
		if tAddr.DefaultTag > 0{
			_, tErr = tx.Ctx(ctx).Model(dao.TalentDeliveryAddress.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.TalentDeliveryAddress.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"}
}

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

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

	err = g.DB().Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
		_, err1 := tx.Ctx(ctx).Model(dao.TalentDeliveryAddress.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.TalentDeliveryAddress.Table).Delete("address_id", deleteReq.AddressId)
		return err1
	})

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

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

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

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

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

			_, err1 = tx.Ctx(ctx).Model(dao.TalentDeliveryAddress.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"}
}