package svc

import (
	"applet/app/db"
	"applet/app/e"
	"applet/app/md"
	"applet/app/svc"
	"applet/app/utils"
	"github.com/gin-gonic/gin"
)

func Login(c *gin.Context) {
	r := new(md.LoginResponse)
	requestBody := new(md.FastLoginRequestBody)
	if err := c.ShouldBindJSON(&requestBody); err != nil {
		e.OutErr(c, e.ERR_INVALID_ARGS, err)
		return
	}
	user, _ := db.UserFindByMobileAll(svc.MasterDb(c), requestBody.Mobile)
	if user == nil {
		e.OutErr(c, 400, e.NewErr(400, "账号不存在"))
		return
	}
	if user.Password != utils.Md5(requestBody.Password) {
		e.OutErr(c, 400, e.NewErr(400, "密码不正确"))
		return
	}
	userParty, _ := db.UserThirdPartyFindByID(svc.MasterDb(c), user.Uid)
	if userParty == nil || userParty.IsAgent == 0 {
		e.OutErr(c, 400, e.NewErr(400, "请联系平台成为代理"))
		return
	}
	if err := svc.FastLoginUserExist(c, r, requestBody); err != nil {
		if e.ErrorIsAccountBan(err) {
			e.OutErr(c, e.ERR_USER_IS_BAN, err)
			return
		}
		e.OutErr(c, 400, err)
		return
	}
	e.OutSuc(c, r, nil)
	return
}
