285 lines
8.3 KiB
Go
285 lines
8.3 KiB
Go
package cms
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
|
|
"git.rosy.net.cn/baseapi/utils"
|
|
"git.rosy.net.cn/jx-callback/business/auth2"
|
|
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
|
|
"git.rosy.net.cn/jx-callback/business/model"
|
|
"git.rosy.net.cn/jx-callback/business/model/dao"
|
|
"git.rosy.net.cn/jx-callback/globals"
|
|
)
|
|
|
|
var (
|
|
ErrUserIDAndNameMustGiven = errors.New("用户ID2和用户名必须不为空")
|
|
)
|
|
|
|
var (
|
|
userProvider = &UserProvider{}
|
|
authTypeFieldMap = map[string]string{
|
|
auth2.UserIDID: "user_id",
|
|
auth2.UserIDID2: "user_id2",
|
|
auth2.UserIDMobile: "mobile",
|
|
auth2.UserIDEmail: "email",
|
|
}
|
|
jdUsersStruct GetJdUsersStruct
|
|
titleListJdUser = []string{
|
|
"用户名",
|
|
"关联门店",
|
|
"所属角色",
|
|
"状态",
|
|
}
|
|
)
|
|
|
|
type GetJdUsersStruct struct {
|
|
locker sync.RWMutex
|
|
userMap []JdUserStruct
|
|
}
|
|
|
|
type JdUserStruct struct {
|
|
UserName string `json:"用户名"`
|
|
StoreIDs string `json:"关联门店"`
|
|
RoleName string `json:"所属角色"`
|
|
Status string `json:"状态"`
|
|
}
|
|
|
|
type UserProvider struct {
|
|
}
|
|
|
|
func (*UserProvider) GetUser(authID, authIDType string) (user auth2.IUser) {
|
|
globals.SugarLogger.Debugf("GetUser, authID:%s, authIDType:%s", authID, authIDType)
|
|
fieldName := authTypeFieldMap[authIDType]
|
|
if fieldName != "" {
|
|
user2, err := dao.GetUserByID(dao.GetDB(), fieldName, authID)
|
|
if err == nil {
|
|
user = user2 // 这样写的原因是golang nil的比较问题
|
|
}
|
|
}
|
|
return user
|
|
}
|
|
|
|
func (*UserProvider) UpdateUserMobile(userID string, mobile string) (err error) {
|
|
_, err = dao.UpdateEntityLogically(dao.GetDB(), &model.User{}, map[string]interface{}{
|
|
"Mobile": mobile,
|
|
}, userID, map[string]interface{}{
|
|
"UserID": userID,
|
|
})
|
|
return err
|
|
}
|
|
|
|
func (*UserProvider) UpdateUserEmail(userID string, email string) (err error) {
|
|
_, err = dao.UpdateEntityLogically(dao.GetDB(), &model.User{}, map[string]interface{}{
|
|
"Email": email,
|
|
}, userID, map[string]interface{}{
|
|
"UserID": userID,
|
|
})
|
|
return err
|
|
}
|
|
|
|
func (*UserProvider) UpdateUserType(userID string, userTypeMask int8, updateType int) (err error) {
|
|
db := dao.GetDB()
|
|
user := &model.User{
|
|
UserID: userID,
|
|
}
|
|
if err = dao.GetEntity(db, user, "UserID"); err == nil {
|
|
if updateType == auth2.UpdateUserTypeAdd {
|
|
user.Type |= userTypeMask
|
|
} else if updateType == auth2.UpdateUserTypeDelete {
|
|
user.Type &= ^userTypeMask
|
|
} else {
|
|
user.Type = userTypeMask
|
|
}
|
|
dao.WrapUpdateULEntity(user, userID)
|
|
_, err = dao.UpdateEntity(db, user, "Type", model.FieldUpdatedAt, model.FieldLastOperator)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (*UserProvider) UpdateLastLogin(userID string, lastLoginType, fromIP string) (err error) {
|
|
_, err = dao.UpdateEntityLogically(dao.GetDB(), &model.User{}, map[string]interface{}{
|
|
"LastLoginAt": utils.Time2Pointer(time.Now()),
|
|
"LastLoginType": lastLoginType,
|
|
"LastLoginIP": fromIP,
|
|
}, userID, map[string]interface{}{
|
|
"UserID": userID,
|
|
})
|
|
return err
|
|
}
|
|
|
|
func init() {
|
|
auth2.Init(userProvider)
|
|
}
|
|
|
|
func GetUserBindAuthInfo(ctx *jxcontext.Context) (authList []*model.AuthBind, err error) {
|
|
authInfo, err := ctx.GetV2AuthInfo()
|
|
if err == nil {
|
|
return auth2.GetUserBindAuthInfo(authInfo.GetID())
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
func DisableUser(ctx *jxcontext.Context, userID string) (err error) {
|
|
userName := ctx.GetUserName()
|
|
if _, err = dao.UpdateEntityLogically(dao.GetDB(), &model.User{}, map[string]interface{}{
|
|
"Status": model.UserStatusDisabled,
|
|
}, userName, map[string]interface{}{
|
|
"UserID": userID,
|
|
}); err == nil {
|
|
auth2.DisableUser(userID, userName)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func GetUsers(ctx *jxcontext.Context, keyword string, userID string, pop int, mobile string, fromTime, toTime string, timeType int, cityCodes, consumeTypes []int, offset, pageSize int) (pagedInfo *model.PagedInfo, err error) {
|
|
return dao.GetUsers2(dao.GetDB(), keyword, userID, pop, mobile, utils.Str2Time(fromTime), utils.Str2Time(toTime), timeType, cityCodes, consumeTypes, offset, pageSize)
|
|
}
|
|
|
|
func GetUser(ctx *jxcontext.Context, userID string) (user *model.GetUserResult, err error) {
|
|
return dao.GetUser(dao.GetDB(), userID)
|
|
}
|
|
|
|
func checkUserType(userID string, userType int8) (err error) {
|
|
userList, _, err := dao.GetUsers(dao.GetDB(), 0, "", "", []string{userID}, nil, nil, 0, 0)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(userList) == 0 {
|
|
return fmt.Errorf("找不到用户:%s", userID)
|
|
}
|
|
if userList[0].Type&userType == 0 {
|
|
return fmt.Errorf("用户:%s不能用于当前操作", userID)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func DeleteUserDeliveryAddress(ctx *jxcontext.Context, userID string, addressID int) (err error) {
|
|
num, err := dao.DeleteEntityLogically(dao.GetDB(), &model.UserDeliveryAddress{}, nil, ctx.GetUserName(), map[string]interface{}{
|
|
model.FieldID: addressID,
|
|
"UserID": userID,
|
|
})
|
|
if err == nil {
|
|
if num == 0 {
|
|
err = fmt.Errorf("地址ID:%d不存在", addressID)
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
func DeleteMyDeliveryAddress(ctx *jxcontext.Context, addressID int) (err error) {
|
|
_, userID := ctx.GetMobileAndUserID()
|
|
return DeleteUserDeliveryAddress(ctx, userID, addressID)
|
|
}
|
|
|
|
func QueryUserDeliveryAddress(ctx *jxcontext.Context, userIDs []string, offset, pageSize int) (pagedInfo *model.PagedInfo, err error) {
|
|
addressList, totalCount, err := dao.QueryUserDeliveryAddress(dao.GetDB(), 0, userIDs, 0, offset, pageSize)
|
|
if err == nil {
|
|
pagedInfo = &model.PagedInfo{
|
|
TotalCount: totalCount,
|
|
Data: addressList,
|
|
}
|
|
}
|
|
return pagedInfo, err
|
|
}
|
|
|
|
func QueryMyDeliveryAddress(ctx *jxcontext.Context, addType int) (addressList []*dao.UserDeliveryAddressEx, err error) {
|
|
_, userID := ctx.GetMobileAndUserID()
|
|
addressList, _, err = dao.QueryUserDeliveryAddress(dao.GetDB(), 0, []string{userID}, addType, 0, model.UnlimitedPageSize)
|
|
return addressList, err
|
|
}
|
|
|
|
func SaveUserCart(ctx *jxcontext.Context, userID string, storeID int, cartItems []*model.UserCartItem) (err error) {
|
|
if userID == "" || storeID == 0 {
|
|
return fmt.Errorf("用户与门店必须要指定")
|
|
}
|
|
for _, v := range cartItems {
|
|
v.UserID = userID
|
|
v.StoreID = storeID
|
|
dao.WrapAddIDCULEntity(v, userID)
|
|
}
|
|
db := dao.GetDB()
|
|
txDB, _ := dao.Begin(db)
|
|
defer func() {
|
|
if r := recover(); r != nil || err != nil {
|
|
dao.Rollback(db, txDB)
|
|
if r != nil {
|
|
panic(r)
|
|
}
|
|
}
|
|
}()
|
|
_, err = dao.ExecuteSQL(db, `
|
|
DELETE t1
|
|
FROM user_cart_item t1
|
|
WHERE t1.user_id = ? AND t1.store_id = ?
|
|
`, userID, storeID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(cartItems) > 0 {
|
|
err = dao.CreateMultiEntities(db, cartItems)
|
|
}
|
|
if err == nil {
|
|
dao.Commit(db, txDB)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func LoadUserCart(ctx *jxcontext.Context, userID string, storeIDs []int) (cartItems []*model.UserCartItem, err error) {
|
|
if userID == "" || len(storeIDs) == 0 {
|
|
return nil, fmt.Errorf("用户与门店必须要指定")
|
|
}
|
|
sql := `
|
|
SELECT t1.*
|
|
FROM user_cart_item t1
|
|
WHERE t1.user_id = ? AND t1.store_id IN (` + dao.GenQuestionMarks(len(storeIDs)) + `)
|
|
ORDER BY t1.sku_id
|
|
`
|
|
err = dao.GetRows(dao.GetDB(), &cartItems, sql, userID, storeIDs)
|
|
return cartItems, err
|
|
}
|
|
|
|
func RefreshUserMemberStatus(ctx *jxcontext.Context) (err error) {
|
|
var (
|
|
db = dao.GetDB()
|
|
)
|
|
userMembers, err := dao.GetUserMember(db, "", 0)
|
|
for _, userMember := range userMembers {
|
|
if time.Now().Sub(userMember.EndAt) > 0 {
|
|
userMember.DeletedAt = time.Now()
|
|
dao.UpdateEntity(db, userMember, "DeletedAt")
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
type GetUserSerachKeywordResult struct {
|
|
AllSpan []*model.UserSearch `json:"allSpan"`
|
|
UserSpan []*model.UserSearch `json:"userSpan"`
|
|
}
|
|
|
|
func GetUserSerachKeyword(ctx *jxcontext.Context) (getUserSerachKeywordResult *GetUserSerachKeywordResult, err error) {
|
|
var (
|
|
db = dao.GetDB()
|
|
userID = ctx.GetUserID()
|
|
userSearchAll []*model.UserSearch
|
|
userSearch []*model.UserSearch
|
|
)
|
|
getUserSerachKeywordResult = &GetUserSerachKeywordResult{}
|
|
sql := `
|
|
SELECT keyword, SUM(count) count FROM user_search WHERE created_at > ? GROUP BY 1 ORDER BY SUM(count) DESC LIMIT 20
|
|
`
|
|
sqlParams := []interface{}{time.Now().AddDate(0, -1, 0)}
|
|
err = dao.GetRows(db, &userSearchAll, sql, sqlParams)
|
|
getUserSerachKeywordResult.AllSpan = userSearchAll
|
|
sql2 := `
|
|
SELECT keyword, SUM(count) count FROM user_search WHERE created_at > ? AND user_id = ? GROUP BY 1 ORDER BY SUM(count) DESC LIMIT 20
|
|
`
|
|
sqlParams2 := []interface{}{time.Now().AddDate(0, -1, 0), userID}
|
|
err = dao.GetRows(db, &userSearch, sql2, sqlParams2)
|
|
getUserSerachKeywordResult.UserSpan = userSearch
|
|
return getUserSerachKeywordResult, err
|
|
}
|