package activity

import (
	"errors"
	"gorm.io/gorm"
	company2 "hongze/hongze_yb/models/tables/company"
	"hongze/hongze_yb/models/tables/company_product"
	"hongze/hongze_yb/models/tables/yb_activity"
	"hongze/hongze_yb/models/tables/yb_activity_register"
	"hongze/hongze_yb/services/user"
	"log"
	"time"
)

type RegisterCheckInfo struct {
	Specific	int		`json:"specific" description:"是否指定返回"`
	Msg			string	`json:"msg" description:"提示信息"`
	Type   		string 	`json:"type" description:"校验失败，需要让前端处理的类型，枚举值：full"`
}

// CreateRegister 活动报名
func CreateRegister(activityId int, userInfo user.UserInfo) (err error, registerCheck RegisterCheckInfo) {
	userId := int(userInfo.UserID)
	ok, msg, registerCheck := checkRegisterOk(userId, activityId)
	if !ok {
		err = errors.New(msg)
		return
	}
	// 客户名称
	company, err := company2.GetByCompanyId(userInfo.CompanyID)
	if err != nil {
		log.Print(err.Error())
		err = errors.New("报名失败")
		return
	}
	companyName := ""
	if company != nil {
		companyName = company.CompanyName
	}
	// 销售名称
	companyProduct, err := company_product.GetByCompany2ProductId(int64(userInfo.CompanyID), 1)
	if err != nil {
		log.Print(err.Error())
		err = errors.New("报名失败")
		return
	}
	sellerName := ""
	if companyProduct != nil {
		sellerName = companyProduct.SellerName
	}
	// 新增报名信息
	registerInfo := &yb_activity_register.YbActivityRegister{
		ActivityID: 	uint32(activityId),
		UserID:     	uint64(userId),
		CompanyID:  	uint64(userInfo.CompanyID),
		CompanyName: 	companyName,
		SellerName: 	sellerName,
		RealName:   	userInfo.RealName,
		Mobile:     	userInfo.Mobile,
		RegisterState:	1,
		CreateTime:		time.Now(),
	}
	err = registerInfo.Create()
	if err != nil {
		log.Print(err.Error())
		err = errors.New("报名失败")
	}
	return
}

// checkRegisterOk 验证是否可以报名
func checkRegisterOk(userId, activityId int) (ok bool, msg string, registerCheck RegisterCheckInfo) {
	activityInfo, err := yb_activity.GetOneById(activityId)
	if err == gorm.ErrRecordNotFound {
		msg = "活动信息有误"
		return
	}
	registerInfo, err := yb_activity_register.RegisterExist(userId, activityId)
	if err != nil && err != gorm.ErrRecordNotFound {
		log.Print(err.Error())
		msg = "报名失败"
		return
	}
	if registerInfo != nil && registerInfo.RegisterState == 1 {
		msg = "请勿重复报名"
		return
	}
	if activityInfo.FirstActivityTypeID != 3 {
		msg = "仅线下沙龙可报名"
		return
	}
	if activityInfo.IsLimitPeople == 1 {
		limitNum := activityInfo.LimitPeopleNum
		registedNum, err := yb_activity_register.CountRegistedNumByActivityId(activityId)
		if err != nil {
			log.Print(err.Error())
			msg = "报名失败"
			return
		}
		if int(registedNum) >= limitNum {
			msg = "此活动报名人数已满，请留意下期活动"
			registerCheck = RegisterCheckInfo{
				Specific: 1,
				Msg: msg,
				Type: "full",
			}
			return
		}
	}
	ok = true
	return
}

// CancelRegister 取消报名
func CancelRegister(activityId int, userInfo user.UserInfo) (err error) {
	userId := int(userInfo.UserID)
	_, err = yb_activity.GetOneById(activityId)
	if err == gorm.ErrRecordNotFound {
		err = errors.New("活动信息有误")
		return
	}
	_, err = yb_activity_register.RegisterExist(userId, activityId)
	if err == gorm.ErrRecordNotFound {
		err = errors.New("报名信息有误")
		return
	}
	err = yb_activity_register.ModifyRegisterState(userId, activityId, 0)
	if err != nil {
		log.Print(err.Error())
		err = errors.New("取消报名失败")
		return
	}
	return
}
