作者 yangfu

增加企业关注功能

... ... @@ -12,6 +12,8 @@ type UserBaseDto struct {
UserType int `json:"userType"`
// 用户信息
UserInfo *domain.UserInfo `json:"userInfo,omitempty"`
// 收藏
Favorite *domain.Favorite `json:"favorite"`
// 手机号码
//Account string `json:"phone,omitempty"`
// 密码
... ... @@ -31,6 +33,7 @@ type UserBaseDto struct {
func (u *UserBaseDto) LoadDto(ub *domain.UserBase) {
u.UserBaseId = ub.UserBaseId
u.UserInfo = ub.UserInfo
u.Favorite = ub.Favorite
u.UserType = domain.UserTypeVisitor
u.Im = ub.Im
}
... ...
package command
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"reflect"
"strings"
"github.com/beego/beego/v2/core/validation"
)
type UpdateFavoriteCommand struct {
OperateInfo *domain.OperateInfo `json:"-"`
UserBaseId int64 `json:"userBaseId" valid:"Required"`
// 菜单编码列表
Item string `json:"item" valid:"Required"`
// 项唯一标识
ItemId int64 `cname:"项唯一标识" json:"itemId" valid:"Required"`
// 用户Id 用户唯一标识
Action int64 `cname:"操作类型 1:关注 2:取消关注 " json:"action" valid:"Required"`
}
func (UpdateFavoriteCommand *UpdateFavoriteCommand) Valid(validation *validation.Validation) {
//validation.SetError("CustomValid", "未实现的自定义认证")
}
func (UpdateFavoriteCommand *UpdateFavoriteCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(UpdateFavoriteCommand)
if err != nil {
return err
}
if !b {
elem := reflect.TypeOf(UpdateFavoriteCommand).Elem()
for _, validErr := range valid.Errors {
field, isExist := elem.FieldByName(validErr.Field)
if isExist {
return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
} else {
return fmt.Errorf(validErr.Message)
}
}
}
return nil
}
... ...
... ... @@ -20,6 +20,8 @@ type UserDto struct {
UserRole []*domain.Role `json:"userRole"`
// 收藏的菜单(工作台)(菜单编码列表)
FavoriteMenus []string `json:"favoriteMenus"`
// 收藏
Favorite *domain.Favorite `json:"favorite"`
// 共创信息 (共创用户有效)
CooperationInfo *domain.CooperationInfo `json:"cooperationInfo,omitempty"`
// 状态(1:启用 2:禁用 3:注销)
... ... @@ -58,6 +60,7 @@ func (dto *UserDto) LoadDto(user *domain.User, company *domain.Company) error {
dto.CooperationInfo = user.CooperationInfo
dto.EnableStatus = user.EnableStatus
dto.UserInfo = user.UserInfo
dto.Favorite = user.Favorite
if company != nil {
dto.Company = &Company{
CompanyId: company.CompanyId,
... ...
... ... @@ -376,6 +376,7 @@ func (userService *UserService) GetUser(getUserQuery *query.GetUserQuery) (inter
}
if userBase != nil {
user.UserInfo = userBase.UserInfo
user.Favorite = userBase.Favorite
}
// TODO:后期可以移除有冗余roleType
for i := range user.UserRole {
... ... @@ -540,6 +541,7 @@ func (userService *UserService) ListUser(listUserQuery *query.ListUserQuery) (in
user.Organization = org.CloneSample()
user.Company = company.CloneSample()
user.UserInfo = userBase.UserInfo
user.Favorite = userBase.Favorite
}
}
if err := userDto.LoadDto(users[i], company); err != nil {
... ... @@ -812,6 +814,45 @@ func (userService *UserService) UpdateUsersBase(updateUsersBaseCommand *command.
return struct{}{}, nil
}
// 更新我喜欢菜单列表
func (userService *UserService) UpdateFavorite(updateFavoriteCommand *command.UpdateFavoriteCommand) (interface{}, error) {
if err := updateFavoriteCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
userRepository, user, err := factory.FastPgUserBase(transactionContext, updateFavoriteCommand.UserBaseId)
if err != nil {
return nil, err
}
switch updateFavoriteCommand.Action {
case domain.Follow:
err = user.AddFavorite(updateFavoriteCommand.Item, updateFavoriteCommand.ItemId)
case domain.Unfollow:
err = user.RemoveFavorite(updateFavoriteCommand.Item, updateFavoriteCommand.ItemId)
default:
err = fmt.Errorf("unkown action %v", updateFavoriteCommand.Action)
}
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
if _, err = userRepository.Save(user); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return struct{}{}, nil
}
func NewUserService(options map[string]interface{}) *UserService {
newUserService := &UserService{}
return newUserService
... ...
... ... @@ -4,7 +4,7 @@ import "os"
var (
// kafka 地址
KAFKA_HOST = "106.75.231.90:9092" //"192.168.0.250:9092,192.168.0.251:9092,192.168.0.252:9092"
KAFKA_HOST = "192.168.0.250:9092,192.168.0.251:9092,192.168.0.252:9092" //"106.75.231.90:9092"
// kafka topic log stash
TOPIC_LOG_STASH = "go_stash_dev" //"pushMessage"
// kafka topic up_block_chain
... ...
... ... @@ -66,6 +66,8 @@ type User struct {
Organization *Org `json:"org,omitempty"`
// 部门
Department *Department `json:"department,omitempty"`
// 收藏
Favorite *Favorite `json:"favorite"`
}
type UserRepository interface {
... ...
... ... @@ -7,6 +7,17 @@ import (
"time"
)
const (
FavoriteOrg = "org"
)
const (
// 关注
Follow = 1
// 取消关注
Unfollow = 2
)
// 用户基础
type UserBase struct {
// 用户基础数据id
... ... @@ -23,12 +34,18 @@ type UserBase struct {
RelatedUsers []int64 `json:"relatedUsers,omitempty"`
// 账号状态 1:正常 2.禁用 3:注销
Status int `json:"status,omitempty"`
// 关注的对象
Favorite *Favorite `json:"favorite,omitempty"`
// 创建时间
CreatedAt time.Time `json:"createdAt,omitempty"`
// 更新时间
UpdatedAt time.Time `json:"updatedAt,omitempty"`
}
type Favorite struct {
OrgItems []int64 `json:"orgItems"`
}
type UserBaseRepository interface {
Save(userBase *UserBase) (*UserBase, error)
Remove(userBase *UserBase) (*UserBase, error)
... ... @@ -178,6 +195,73 @@ func (userBase *UserBase) UpdateUserInfo(userInfo *UserInfo) error {
return nil
}
func (userBase *UserBase) AddFavorite(favoriteItem string, code int64) error {
if userBase.Favorite == nil {
userBase.Favorite = NewFavorite()
}
var err error
switch favoriteItem {
case FavoriteOrg:
userBase.Favorite.OrgItems, err = userBase.addFavorite(userBase.Favorite.OrgItems, code)
default:
return fmt.Errorf("unkown type " + favoriteItem)
}
return err
}
func (userBase *UserBase) RemoveFavorite(favoriteItem string, code int64) error {
if userBase.Favorite == nil {
userBase.Favorite = NewFavorite()
return nil
}
var err error
switch favoriteItem {
case FavoriteOrg:
userBase.Favorite.OrgItems, err = userBase.removeFavorite(userBase.Favorite.OrgItems, code)
default:
return fmt.Errorf("unkown type " + favoriteItem)
}
return err
}
func NewFavorite() *Favorite {
return &Favorite{
OrgItems: make([]int64, 0),
}
}
// addFavorite 收藏
//
// codes 菜单列表
func (userBase *UserBase) addFavorite(items []int64, code int64) ([]int64, error) {
var existed = false
for j := 0; j < len(items); j++ {
if items[j] == code {
existed = true
}
}
if !existed {
items = append(items, code)
}
return items, nil
}
// removeFavorite 收藏移除
//
// codes 菜单列表
func (userBase *UserBase) removeFavorite(items []int64, code int64) ([]int64, error) {
var newItems = make([]int64, 0)
for i := 0; i < len(items); i++ {
if items[i] == code {
continue
}
newItems = append(newItems, items[i])
}
return newItems, nil
}
/***** 2.缓存模块 *****/
func (m *UserBase) CacheKeyFunc() string {
... ...
... ... @@ -21,6 +21,8 @@ type UserBase struct {
RelatedUser []int64 `pg:",array" comment:"关联的用户 (冗余)"`
// 账号状态 1:正常 2.禁用 3:注销
Status int `comment:"账号状态 1:正常 2.禁用 3:注销"`
// 关注的数据
Favorite *domain.Favorite `comment:"关注的数据"`
// 创建时间
CreatedAt time.Time `comment:"创建时间"`
// 更新时间
... ...
... ... @@ -14,6 +14,7 @@ func TransformToUserBaseDomainModelFromPgModels(userBaseModel *models.UserBase)
Im: userBaseModel.Im,
RelatedUsers: userBaseModel.RelatedUser,
Status: userBaseModel.Status,
Favorite: userBaseModel.Favorite,
CreatedAt: userBaseModel.CreatedAt,
UpdatedAt: userBaseModel.UpdatedAt,
}, nil
... ...
... ... @@ -37,6 +37,7 @@ var (
"im",
"related_user",
"status",
"favorite",
"created_at",
"updated_at",
}
... ... @@ -66,6 +67,7 @@ func (repository *UserBaseRepository) Save(userBase *domain.UserBase) (*domain.U
&userBase.Im,
pg.Array(&userBase.RelatedUsers),
&userBase.Status,
&userBase.Favorite,
&userBase.CreatedAt,
&userBase.UpdatedAt,
),
... ... @@ -77,6 +79,7 @@ func (repository *UserBaseRepository) Save(userBase *domain.UserBase) (*domain.U
userBase.Im,
pg.Array(userBase.RelatedUsers),
userBase.Status,
userBase.Favorite,
userBase.CreatedAt,
userBase.UpdatedAt,
); err != nil {
... ... @@ -93,6 +96,7 @@ func (repository *UserBaseRepository) Save(userBase *domain.UserBase) (*domain.U
&userBase.Im,
pg.Array(&userBase.RelatedUsers),
&userBase.Status,
&userBase.Favorite,
&userBase.CreatedAt,
&userBase.UpdatedAt,
),
... ... @@ -103,6 +107,7 @@ func (repository *UserBaseRepository) Save(userBase *domain.UserBase) (*domain.U
userBase.Im,
pg.Array(userBase.RelatedUsers),
userBase.Status,
userBase.Favorite,
userBase.CreatedAt,
userBase.UpdatedAt,
userBase.Identify(),
... ...
... ... @@ -41,3 +41,11 @@ func (controller *UserFavoriteMenusController) DeleteFavoriteMenus() {
data, err := userService.DeleteFavoriteMenus(deleteFavoriteMenusCommand)
controller.Response(data, err)
}
func (controller *UserFavoriteMenusController) UpdateFavorite() {
userService := service.NewUserService(nil)
updateFavoriteMenusCommand := &command.UpdateFavoriteCommand{}
controller.Unmarshal(updateFavoriteMenusCommand)
data, err := userService.UpdateFavorite(updateFavoriteMenusCommand)
controller.Response(data, err)
}
... ...
... ... @@ -9,4 +9,5 @@ func init() {
web.Router("/user/:userId/favorite-menus", &controllers.UserFavoriteMenusController{}, "Put:UpdateFavoriteMenus")
web.Router("/user/:userId/favorite-menus", &controllers.UserFavoriteMenusController{}, "Get:GetFavoriteMenus")
web.Router("/user/:userId/favorite-menus/:code", &controllers.UserFavoriteMenusController{}, "Delete:DeleteFavoriteMenus")
web.Router("/user-base/favorite", &controllers.UserFavoriteMenusController{}, "Post:UpdateFavorite")
}
... ...