service.go 11.2 KB
package service

import (
	"github.com/google/uuid"
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/mobile/user/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/mobile/user/dto"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/mobile/user/query"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/cache"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_basic"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/sms_serve"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/log"
)

type UserService struct {
}

//SendSmsCaptcha 发送验证码短信
func (srv UserService) SendSmsCaptcha(smsCodeCommand *command.SendSmsCodeCommand) error {
	if smsCodeCommand.Flag == 1 {
		creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
		userBase, err := creationUserGateway.AuthUserBaseInfo(allied_creation_user.ReqAuthUserBase{
			Account: smsCodeCommand.Phone,
		})
		if err != nil || userBase.UserInfo.Phone != smsCodeCommand.Phone {
			return application.ThrowError(application.TRANSACTION_ERROR, "输入的手机号不是平台用户,请重新输入")
		}
	}
	smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
	err := smsServeGateway.SendSms(smsCodeCommand.Phone)
	if err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return nil
}

//CheckSmsCode 验证手机短信验证码
func (srv UserService) CheckSmsCode(smsCodeCommand *command.CheckSmsCodeCommand) (interface{}, error) {
	smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
	err := smsServeGateway.CheckSmsCode(smsCodeCommand.Phone, smsCodeCommand.SmsCode)
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	uid := uuid.New()
	pcc := cache.PhoneCheckCache{}
	if err := pcc.Add(uid.String(), cache.PhoneCheckItem{
		Phone:           smsCodeCommand.Phone,
		SmsCodeIdentity: uid.String(),
		Action:          smsCodeCommand.Action,
	}); err != nil {
		log.Logger.Error(err.Error())
		return nil, application.ThrowError(application.BUSINESS_ERROR, "系统错误")
	}
	return map[string]interface{}{
		"smsCodeIdentity": uid.String(),
	}, nil
}

//ChangePassword 修改密码
func (srv UserService) ChangePassword(changePasswordCommand *command.ChangePasswordCommand) (interface{}, error) {
	smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
	if len(changePasswordCommand.OldPassword) > 0 {
		// 2.重置密码
		creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
		_, err := creationUserGateway.AuthChangePassword(allied_creation_user.ReqAuthChangePassword{
			OldPassword: changePasswordCommand.OldPassword,
			NewPassword: changePasswordCommand.NewPassword,
			UserId:      changePasswordCommand.Operator.UserId,
		})
		if err != nil {
			return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
		}
	} else {
		err := smsServeGateway.CheckSmsCode(changePasswordCommand.Phone, changePasswordCommand.SmsCode)
		if err != nil {
			return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
		}
		// 2.重置密码
		creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
		_, err = creationUserGateway.AuthResetPassword(allied_creation_user.ReqAuthResetPassword{
			Phone:    changePasswordCommand.Phone,
			Password: changePasswordCommand.NewPassword,
		})
		if err != nil {
			return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
		}
	}

	return struct{}{}, nil
}

//ChangePhone 修改手机号
func (srv UserService) ChangePhone(resetPhoneCommand *command.ResetPhoneCommand) (interface{}, error) {
	smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
	err := smsServeGateway.CheckSmsCode(resetPhoneCommand.NewPhone, resetPhoneCommand.SmsCode)
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	// 2.重置手机号
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
	_, err = creationUserGateway.AuthResetPhone(allied_creation_user.ReqAuthResetPhone{
		UserId:   resetPhoneCommand.Operator.UserId,
		OldPhone: resetPhoneCommand.Operator.Phone,
		NewPhone: resetPhoneCommand.NewPhone,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	return resetPhoneCommand, nil
}

//UpdateUserBaseInfo 更新用户信息
func (srv UserService) UpdateUserBaseInfo(updateUserInfoCommand *command.UpdateUserInfoCommand) (interface{}, error) {
	if err := updateUserInfoCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
	user, err := creationUserGateway.UserGet(allied_creation_user.ReqGetUser{
		UserId: int(updateUserInfoCommand.Operator.UserId),
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, "用户不存在")
	}
	if _, ok := updateUserInfoCommand.BodyKV["avatar"]; !ok {
		updateUserInfoCommand.Avatar = user.UserInfo.Avatar
	}
	if _, ok := updateUserInfoCommand.BodyKV["email"]; !ok {
		updateUserInfoCommand.Email = user.UserInfo.Email
	}
	if _, ok := updateUserInfoCommand.BodyKV["phone"]; !ok {
		updateUserInfoCommand.Phone = user.UserInfo.Phone
	}
	if _, ok := updateUserInfoCommand.BodyKV["userName"]; !ok {
		updateUserInfoCommand.UserName = user.UserInfo.UserName
	}
	if _, ok := updateUserInfoCommand.BodyKV["smsCode"]; ok && len(updateUserInfoCommand.SmsCode) > 0 {
		smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
		err := smsServeGateway.CheckSmsCode(updateUserInfoCommand.Phone, updateUserInfoCommand.SmsCode)
		if err != nil {
			return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
		}
	}
	_, err = creationUserGateway.UserUpdateBaseInfo(allied_creation_user.ReqUserUpdateBaseInfo{
		UserId:   int64(user.UserId),
		UserName: updateUserInfoCommand.UserName,
		Avatar:   updateUserInfoCommand.Avatar,
		Phone:    updateUserInfoCommand.Phone,
		Email:    updateUserInfoCommand.Email,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	return map[string]interface{}{
		"avatar":   updateUserInfoCommand.Avatar,
		"userName": updateUserInfoCommand.UserName,
	}, nil
}

// CompanySignUp 企业注册
func (srv UserService) DestroyAccount(destroyAccountCommand *command.DestroyAccountCommand) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
	result, err := creationUserGateway.AuthDestroyAccount(allied_creation_user.ReqAuthDestroyAccount{
		//UserId: destroyAccountCommand.Operator.UserId,
		Account: destroyAccountCommand.Operator.Phone,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	return result, err
}

//DepartmentsUsers 部门用户列表
func (srv UserService) DepartmentsUsers(departmentsUsersQuery *query.DepartmentsUsersQuery) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		departmentsUsersQuery.Operator)
	orgs, err := creationUserGateway.OrgGetSubDepartment(allied_creation_user.ReqOrgGetSubDepartment{
		OrgId: departmentsUsersQuery.Operator.OrgId,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}

	users, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
		//Offset:         0,
		//Limit:          999,
		CompanyId:      departmentsUsersQuery.Operator.CompanyId,
		OrganizationId: departmentsUsersQuery.Operator.OrgId,
		UserType:       domain.UserTypeEmployee, //TODO:是否要共创用户
		InEnableStatus: []int{domain.UserStatusEnable},
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	departmentUsersDto := &dto.DepartmentUsersDto{}
	if err := departmentUsersDto.LoadDto(departmentsUsersQuery.Type, orgs, users); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	return departmentUsersDto, nil
}

//MessagesList 消息列表
func (srv UserService) MessagesList(cmd *query.MessagesListQuery) (int64, interface{}, error) {
	gateway := allied_creation_basic.NewHttplibAlliedCreationBasic(
		cmd.Operator)
	messages, err := gateway.NoticePersonal(allied_creation_basic.ReqNoticePersonal{
		PageIndex:  cmd.PageNumber,
		PageSize:   cmd.PageSize,
		UserBaseId: cmd.Operator.UserBaseId,
	})
	if err != nil {
		return 0, nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	var results []*dto.MessageDto
	for i := 0; i < len(messages.List); i++ {
		mItem := messages.List[i]
		message := &dto.MessageDto{
			MsgId:      mItem.NoticePersonalID,
			MsgContent: mItem.Content,
			MsgTime:    mItem.CreatedAt.Unix() * 1000,
			Read:       mItem.IsRead,
		}
		message.MsgType = message.LoadMsgType(mItem.Module, mItem.ModuleAction)
		results = append(results, message)
	}

	return messages.Count, results, nil
}

//MessagesList 消息列表
func (srv UserService) MessagesMarkRead(cmd *command.MessageMarkReadCommand) (interface{}, error) {
	gateway := allied_creation_basic.NewHttplibAlliedCreationBasic(
		cmd.Operator)
	_, err := gateway.ReadNotice(allied_creation_basic.ReqReadNotice{
		MsgId:      cmd.MsgId,
		ReadAll:    cmd.ReadAll,
		UserBaseId: cmd.Operator.UserBaseId,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	return struct{}{}, nil
}

// 设置收藏菜单
func (srv UserService) UpdateMenuFavorite(menuFavoriteCommand *command.MenuFavoriteCommand) (interface{}, error) {
	if err := menuFavoriteCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		menuFavoriteCommand.Operator,
	)
	result, err := creationUserGateway.FavoriteMenusUpadate(allied_creation_user.ReqFavoriteMenusUpdate{
		UserId:        menuFavoriteCommand.Operator.UserId,
		FavoriteMenus: menuFavoriteCommand.FavoriteMenus,
		Action:        menuFavoriteCommand.Action,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return result, nil
}

// 共创组织列表
func (srv UserService) CooperationOrg(operator domain.Operator) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(operator)
	orgs, err := creationUserGateway.OrgSearch(allied_creation_user.ReqOrgSearch{
		IsOrg: domain.IsOrgFlag,
		Limit: 50,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	var ret = make([]interface{}, 0)
	for i := range orgs.Orgs {
		item := orgs.Orgs[i]
		ret = append(ret, map[string]interface{}{
			"orgId":   item.OrgID,
			"orgName": item.OrgName,
		})
	}
	return map[string]interface{}{
		"orgs": ret,
	}, nil
}