210 lines
5.7 KiB
Go
210 lines
5.7 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"
|
|
)
|
|
|
|
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) 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 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 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 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
|
|
}
|
|
|
|
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
|
|
}
|