package user

import (
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/auth"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/partnerInfo/command"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/partnerInfo/query"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/partnerInfo/service"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
	"strings"
)

//用户信息
func UserInfo(header *protocol.RequestHeader, request *protocol.UserInfoRequest) (rsp *protocol.UserInfoResponse, err error) {
	var (
		PartnerInfoService = service.NewPartnerInfoService(nil)
		partnerInfo        *domain.PartnerInfo
	)
	rsp = &protocol.UserInfoResponse{}
	if partnerInfo, err = PartnerInfoService.GetPartnerInfo(&query.GetPartnerInfoQuery{Id: int(header.UserId)}); err != nil {
		err = protocol.NewErrWithMessage(502, err) //账号不存在
		return
	}
	rsp.User = protocol.User{
		Id:          partnerInfo.Id,
		PartnerName: partnerInfo.PartnerName,
		Phone:       partnerInfo.Account,
		CooperateCompany: protocol.Company{
			Id:   1,
			Name: "福州素天下有限公司",
		},
		JoinWay:       partnerInfo.PartnerCategoryInfo(),
		District:      partnerInfo.RegionInfo,
		SerialNo:      partnerInfo.Id,
		CooperateTime: partnerInfo.CooperateTime.Unix() * 1000,
	}
	if len(partnerInfo.Salesman) > 0 {
		rsp.User.Salesman = partnerInfo.Salesman[0]
	}
	return
}

//验证smscode
func CheckSmsCode(header *protocol.RequestHeader, request *protocol.CheckSmsCodeRequest) (rsp *protocol.CheckSmsCodeResponse, err error) {
	var (
		PartnerInfoService = service.NewPartnerInfoService(nil)
		partnerInfo        *domain.PartnerInfo
	)
	if partnerInfo, err = PartnerInfoService.GetPartnerInfo(&query.GetPartnerInfoQuery{Id: int(header.UserId)}); err != nil {
		err = protocol.NewErrWithMessage(502, err) //账号不存在
		return
	}
	if _, err = auth.CheckSmsCode(strings.TrimSpace(partnerInfo.Account), request.Captcha); err != nil {
		log.Error(err, partnerInfo.Account, request.Captcha)
		return
	}
	return
}

//修改手机号
func ChangePhone(header *protocol.RequestHeader, request *protocol.ChangePhoneRequest) (rsp *protocol.ChangePhoneResponse, err error) {
	var (
		PartnerInfoService = service.NewPartnerInfoService(nil)
	)
	if _, err = PartnerInfoService.GetPartnerInfo(&query.GetPartnerInfoQuery{Id: int(header.UserId)}); err != nil {
		err = protocol.NewErrWithMessage(502, err) //账号不存在
		return
	}
	if _, err = PartnerInfoService.GetPartnerInfo(&query.GetPartnerInfoQuery{Account: request.Phone}); err == nil {
		err = protocol.NewErrWithMessage(2029, err) //账号不存在
		return
	}
	if _, err = auth.CheckSmsCode(request.Phone, request.Captcha); err != nil {
		log.Error(err)
		return
	}
	if _, err = PartnerInfoService.UpdatePartnerInfo(&command.UpdatePartnerInfoCommand{Account: request.Phone}); err == nil {
		return
	}
	return
}

//重置密码
func ResetPassword(header *protocol.RequestHeader, request *protocol.ResetPasswordRequest) (rsp *protocol.ResetPasswordResponse, err error) {
	var (
		PartnerInfoService = service.NewPartnerInfoService(nil)
		partnerInfo        *domain.PartnerInfo
	)
	if len(request.NewPwd) < 6 {
		err = protocol.NewErrWithMessage(2027)
		return
	}
	if !strings.EqualFold(request.NewPwd, request.ConfirmPwd) {
		err = protocol.NewErrWithMessage(2026)
		return
	}
	if partnerInfo, err = PartnerInfoService.GetPartnerInfo(&query.GetPartnerInfoQuery{Id: int(header.UserId)}); err != nil {
		err = protocol.NewErrWithMessage(502, err) //账号不存在
		return
	}
	if _, err = auth.CheckSmsCode(partnerInfo.Account, request.Captcha); err != nil {
		log.Error(err)
		return
	}
	if _, err = PartnerInfoService.UpdatePartnerInfo(&command.UpdatePartnerInfoCommand{Password: request.ConfirmPwd}); err == nil {
		return
	}
	return
}

//修改密码
func ChangePassword(header *protocol.RequestHeader, request *protocol.ChangePasswordRequest) (rsp *protocol.ChangePasswordResponse, err error) {
	var (
		PartnerInfoService = service.NewPartnerInfoService(nil)
		partnerInfo        *domain.PartnerInfo
	)
	rsp = &protocol.ChangePasswordResponse{}
	if !strings.EqualFold(request.NewPwd, request.ConfirmPwd) {
		err = protocol.NewErrWithMessage(2026)
		return
	}
	if strings.EqualFold(request.NewPwd, request.OldPwd) {
		err = protocol.NewErrWithMessage(2030)
		return
	}
	if len(request.NewPwd) < 6 {
		err = protocol.NewErrWithMessage(2027)
		return
	}
	if partnerInfo, err = PartnerInfoService.GetPartnerInfo(&query.GetPartnerInfoQuery{Id: int(header.UserId)}); err != nil {
		err = protocol.NewErrWithMessage(502, err) //账号不存在
		return
	}
	if !strings.EqualFold(partnerInfo.Password, request.OldPwd) {
		//密码不一致
		err = protocol.NewErrWithMessage(2028, err) //账号不存在
		return
	}
	if _, err = PartnerInfoService.UpdatePartnerInfo(&command.UpdatePartnerInfoCommand{Password: request.ConfirmPwd}); err == nil {
		return
	}
	return
}