package user

import (
	"fmt"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/auth"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/factory"
	"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"
	"strconv"
	"strings"
)

//用户信息
func UserInfo(header *protocol.RequestHeader, request *protocol.UserInfoRequest) (rsp *protocol.UserInfoResponse, err error) {
	var (
		partnerInfo            *domain.PartnerInfo
		transactionContext, _  = factory.CreateTransactionContext(nil)
		CompanyResponsitory, _ = factory.CreateCompanyRepository(transactionContext)
		PartnerInfoService, _  = factory.CreatePartnerInfoRepositoryIn(transactionContext)
		ImInfoRepository, _    = factory.CreateImInfoRepository(transactionContext)
		company                *domain.Company
	)
	if err = transactionContext.StartTransaction(); err != nil {
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	rsp = &protocol.UserInfoResponse{}
	if partnerInfo, err = PartnerInfoService.FindOne(map[string]interface{}{"id": header.UserId}); err != nil {
		err = protocol.NewErrWithMessage(502, err) //账号不存在
		return
	}
	if company, err = CompanyResponsitory.FindOne(map[string]interface{}{"id": 1}); err != nil {
		return
	}
	rsp.User = protocol.User{
		Id:          partnerInfo.Id,
		PartnerName: partnerInfo.PartnerName,
		Phone:       partnerInfo.Account,
		CooperateCompany: protocol.Company{
			Id:    company.Id,
			Name:  company.Name,
			Phone: company.Phone,
		},
		JoinWay:       partnerInfo.PartnerCategoryInfo(),
		District:      map[string]interface{}{"id": partnerInfo.RegionInfo.RegionId, "name": partnerInfo.RegionInfo.RegionName},
		SerialNo:      partnerInfo.Id,
		CooperateTime: partnerInfo.CooperateTime.Unix() * 1000,
	}
	if len(partnerInfo.Salesman) > 0 {
		rsp.User.Salesman = map[string]interface{}{"uname": partnerInfo.Salesman[0].Name, "phone": partnerInfo.Salesman[0].Telephone}
	} else {
		rsp.User.Salesman = map[string]interface{}{}
	}
	if imInfo, e := ImInfoRepository.FindOne(map[string]interface{}{"user_id": partnerInfo.Id}); e == nil && imInfo != nil {
		rsp.User.ImToken = imInfo.ImToken
		rsp.User.AccountID, _ = strconv.ParseInt(imInfo.ImId, 10, 64)
		rsp.User.CsAccountID = fmt.Sprintf("%v", imInfo.CustomerImId)
	}
	err = transactionContext.CommitTransaction()
	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 (
		transactionContext, _ = factory.CreateTransactionContext(nil)
		PartnerInfoDao, _     = factory.CreatePartnerInfoDao(transactionContext)
		PartnerInfoService, _ = factory.CreatePartnerInfoRepositoryIn(transactionContext)
		partnerInfo           *domain.PartnerInfo
	)
	if err = transactionContext.StartTransaction(); err != nil {
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	if partnerInfo, err = PartnerInfoService.FindOne(map[string]interface{}{"id": header.UserId}); err != nil {
		err = protocol.NewErrWithMessage(502, err) //账号不存在
		return
	}
	//验证新手机是否已经存在
	if _, e := PartnerInfoService.FindOne(map[string]interface{}{"account": request.Phone}); e == nil {
		err = protocol.NewErrWithMessage(2029, err) //账号已存在
		return
	}
	if _, err = auth.CheckSmsCode(request.Phone, request.Captcha); err != nil {
		log.Error(err)
		return
	}
	if err = PartnerInfoDao.Update(map[string]interface{}{
		"Id":      partnerInfo.Id,
		"Account": request.Phone,
	}); err != nil {
		return
	}
	err = transactionContext.CommitTransaction()
	return
}

//重置密码
func ResetPassword(header *protocol.RequestHeader, request *protocol.ResetPasswordRequest) (rsp *protocol.ResetPasswordResponse, err error) {
	var (
		partnerInfo           *domain.PartnerInfo
		transactionContext, _ = factory.CreateTransactionContext(nil)
		PartnerInfoDao, _     = factory.CreatePartnerInfoDao(transactionContext)
		PartnerInfoService, _ = factory.CreatePartnerInfoRepositoryIn(transactionContext)
	)
	rsp = &protocol.ResetPasswordResponse{}
	if err = transactionContext.StartTransaction(); err != nil {
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	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.FindOne(map[string]interface{}{"id": header.UserId}); err != nil {
		err = protocol.NewErrWithMessage(502, err) //账号不存在
		return
	}
	if err = PartnerInfoDao.Update(map[string]interface{}{
		"Id":       partnerInfo.Id,
		"Password": request.ConfirmPwd,
	}); err != nil {
		return
	}
	//清除auth token
	//auth := userAuth.NewRedisUserAuth(userAuth.WithUserId(header.UserId))
	//if !auth.Exist() {
	//	return
	//}
	//if err = auth.RemoveAuth(); err != nil {
	//	log.Error(err)
	//	err = protocol.NewErrWithMessage(4140, err)
	//	return
	//}
	err = transactionContext.CommitTransaction()
	return
}

//修改密码
func ChangePassword(header *protocol.RequestHeader, request *protocol.ChangePasswordRequest) (rsp *protocol.ChangePasswordResponse, err error) {
	var (
		partnerInfo           *domain.PartnerInfo
		transactionContext, _ = factory.CreateTransactionContext(nil)
		PartnerInfoDao, _     = factory.CreatePartnerInfoDao(transactionContext)
		PartnerInfoService, _ = factory.CreatePartnerInfoRepositoryIn(transactionContext)
	)
	if err = transactionContext.StartTransaction(); err != nil {
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	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.FindOne(map[string]interface{}{"id": 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 = PartnerInfoDao.Update(map[string]interface{}{
		"Id":       partnerInfo.Id,
		"Password": request.ConfirmPwd,
	}); err != nil {
		return
	}
	//清除auth token
	//auth := userAuth.NewRedisUserAuth(userAuth.WithUserId(header.UserId))
	//if !auth.Exist() {
	//	return
	//}
	//if err = auth.RemoveAuth(); err != nil {
	//	log.Error(err)
	//	err = protocol.NewErrWithMessage(4140, err)
	//	return
	//}
	err = transactionContext.CommitTransaction()
	return
}