212 lines
6.7 KiB
Go
212 lines
6.7 KiB
Go
package cms
|
||
|
||
import (
|
||
"fmt"
|
||
|
||
"git.rosy.net.cn/baseapi/utils"
|
||
"git.rosy.net.cn/jx-callback/business/jxcallback/auth"
|
||
"git.rosy.net.cn/jx-callback/business/jxcallback/auth/mobile"
|
||
"git.rosy.net.cn/jx-callback/business/jxcallback/auth/weixin"
|
||
"git.rosy.net.cn/jx-callback/business/jxutils"
|
||
"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/business/model/legacymodel"
|
||
"git.rosy.net.cn/jx-callback/globals"
|
||
"github.com/astaxie/beego/orm"
|
||
)
|
||
|
||
var (
|
||
LoginTypeFieldMap = map[string]string{
|
||
mobile.LoginType: "tel",
|
||
weixin.LoginType: "openid",
|
||
weixin.LoginTypeMiniProgram: "openid_mini",
|
||
}
|
||
)
|
||
|
||
func GetStoreUsers(ctx *jxcontext.Context, storeID int) (storeUserInfos []*dao.StoreUserInfo, err error) {
|
||
sql := `
|
||
SELECT t1.id, t1.jxstoreid, t1.openid, t1.tel, t1.nickname, t1.parentid, t3.tel parent_mobile,
|
||
CONCAT("[", GROUP_CONCAT(CONCAT('{"id":', t2.id, ',"parentID":', t2.parentid, ',"tel":"', t2.tel, '","nickname":"', IF(t2.nickname IS NULL, "", t2.nickname), '"}')), "]") members_str
|
||
FROM weixins t1
|
||
LEFT JOIN weixins t2 ON t2.parentid = t1.id
|
||
LEFT JOIN weixins t3 ON t1.parentid = t3.id
|
||
WHERE t1.parentid = -1 AND t1.jxstoreid = ?
|
||
GROUP BY 1,2,3,4,5,6,7;
|
||
`
|
||
globals.SugarLogger.Debug(sql)
|
||
if err = dao.GetRows(nil, &storeUserInfos, sql, storeID); err == nil {
|
||
for _, storeUserInfo := range storeUserInfos {
|
||
if storeUserInfo.MembersStr != "" {
|
||
err = utils.UnmarshalUseNumber([]byte(storeUserInfo.MembersStr), &storeUserInfo.Members)
|
||
}
|
||
}
|
||
}
|
||
return storeUserInfos, err
|
||
}
|
||
|
||
func GetUserInfo(ctx *jxcontext.Context, mobile string) (storeUserInfo *dao.StoreUserInfo, err error) {
|
||
storeUserInfo, err = dao.GetUserStoreInfo(dao.GetDB(), "tel", mobile)
|
||
globals.SugarLogger.Debugf("GetUserInfo:%s, token:%s, mobile:%s, storeUserInfo:%s, err:%v", ctx.GetTrackInfo(), ctx.GetToken(), mobile, utils.Format4Output(storeUserInfo, true), err)
|
||
return storeUserInfo, err
|
||
}
|
||
|
||
func GetSelfInfo(ctx *jxcontext.Context) (storeUserInfo *dao.StoreUserInfo, err error) {
|
||
loginInfo := ctx.GetLoginInfo()
|
||
if loginInfo == nil {
|
||
return nil, auth.ErrAPINeedRealLogin
|
||
}
|
||
fieldName := LoginTypeFieldMap[loginInfo.GetAuthType()]
|
||
if fieldName == "" {
|
||
return nil, auth.ErrIllegalLoginType
|
||
}
|
||
storeUserInfo, err = dao.GetUserStoreInfo(dao.GetDB(), fieldName, loginInfo.GetAuthID())
|
||
globals.SugarLogger.Debugf("GetSelfInfo:%s, token:%s, storeUserInfo:%s, err:%v", ctx.GetTrackInfo(), ctx.GetToken(), utils.Format4Output(storeUserInfo, true), err)
|
||
return storeUserInfo, err
|
||
}
|
||
|
||
func GetMyStoreList(ctx *jxcontext.Context) (storeList []*dao.StoreWithCityName, err error) {
|
||
mobileNum, _ := ctx.GetMobileAndUserID()
|
||
if mobileNum == "" {
|
||
return nil, fmt.Errorf("不能得到用户手机号")
|
||
}
|
||
storeList, err = dao.GetStoreListByMobile(dao.GetDB(), mobileNum)
|
||
return storeList, err
|
||
}
|
||
|
||
func UnbindMobile(ctx *jxcontext.Context, mobile string) (num int64, err error) {
|
||
db := dao.GetDB()
|
||
num, err = dao.UpdateEntityByKV(db, &legacymodel.WeiXins{}, map[string]interface{}{
|
||
"JxStoreID": 0,
|
||
"ParentID": -1,
|
||
}, map[string]interface{}{
|
||
"Tel": mobile,
|
||
})
|
||
jxutils.HandleUserWXRemark(db, mobile)
|
||
return num, err
|
||
}
|
||
|
||
func BindMobile2Store(ctx *jxcontext.Context, mobile string, storeID int) (num int64, err error) {
|
||
db := dao.GetDB()
|
||
user, err2 := verifyMobileIsBlank(db, mobile)
|
||
if err = err2; err == nil || err == orm.ErrNoRows {
|
||
user.JxStoreID = storeID
|
||
if err == nil {
|
||
dao.Begin(db)
|
||
defer func() {
|
||
if r := recover(); r != nil {
|
||
dao.Rollback(db)
|
||
panic(r)
|
||
}
|
||
}()
|
||
if num, err = dao.UpdateEntity(db, user, "JxStoreID"); err == nil {
|
||
err = dao.SetWeiXinsEmpty2Null(db, user)
|
||
}
|
||
if err != nil {
|
||
dao.Rollback(db)
|
||
} else {
|
||
dao.Commit(db)
|
||
}
|
||
} else {
|
||
dao.WrapAddIDCULEntity(user, ctx.GetUserName())
|
||
user.ParentID = -1
|
||
if err = dao.CreateWeiXins(db, user); err == nil {
|
||
num = 1
|
||
}
|
||
}
|
||
}
|
||
jxutils.HandleUserWXRemark(db, mobile)
|
||
return num, err
|
||
}
|
||
|
||
func AddMobile2Mobile(ctx *jxcontext.Context, parentMobile, mobile string) (num int64, err error) {
|
||
db := dao.GetDB()
|
||
parentUser := &legacymodel.WeiXins{}
|
||
parentUser.Tel = parentMobile
|
||
if err = dao.GetEntity(db, parentUser, "Tel"); err == nil {
|
||
if parentUser.ParentID == -1 {
|
||
globals.SugarLogger.Debug(parentUser)
|
||
if err = verifyMobileHasNoMembers(db, mobile); err == nil {
|
||
user, err2 := verifyMobileIsBlank(db, mobile)
|
||
if err = err2; err == nil || err == orm.ErrNoRows {
|
||
user.ParentID = parentUser.ID
|
||
if err == nil {
|
||
// todo transaction
|
||
if num, err = dao.UpdateEntity(db, user, "ParentID"); err == nil {
|
||
err = dao.SetWeiXinsEmpty2Null(db, user)
|
||
}
|
||
} else {
|
||
dao.WrapAddIDCULEntity(user, ctx.GetUserName())
|
||
if err = dao.CreateWeiXins(db, user); err == nil {
|
||
num = 1
|
||
}
|
||
}
|
||
}
|
||
}
|
||
} else {
|
||
err = fmt.Errorf("%s本身是成员", parentMobile)
|
||
}
|
||
}
|
||
jxutils.HandleUserWXRemark(db, mobile)
|
||
return num, err
|
||
}
|
||
|
||
func ChangeMobile(ctx *jxcontext.Context, curMobile, expectedMobile string) (num int64, err error) {
|
||
return dao.UpdateEntityByKV(nil, &legacymodel.WeiXins{}, map[string]interface{}{
|
||
"Tel": expectedMobile,
|
||
}, map[string]interface{}{
|
||
"Tel": curMobile,
|
||
})
|
||
}
|
||
|
||
func verifyMobileIsBlank(db *dao.DaoDB, mobile string) (user *legacymodel.WeiXins, err error) {
|
||
if !jxutils.IsStringLikeMobile(mobile) {
|
||
return nil, fmt.Errorf("%s看起来不像是一个手机号", mobile)
|
||
}
|
||
user = &legacymodel.WeiXins{
|
||
Tel: mobile,
|
||
}
|
||
if err = dao.GetEntity(db, user, "Tel"); err == nil {
|
||
if user.ParentID != -1 && user.ParentID != 0 {
|
||
userParent := &legacymodel.WeiXins{
|
||
ID: user.ParentID,
|
||
}
|
||
if err = dao.GetEntity(db, userParent); err != nil && err != orm.ErrNoRows {
|
||
return nil, err
|
||
}
|
||
if err != orm.ErrNoRows {
|
||
err = fmt.Errorf("%s已经是组长:%s,门店:%d的小组成员", mobile, userParent.Tel, userParent.JxStoreID)
|
||
} else {
|
||
err = nil
|
||
}
|
||
} else if user.JxStoreID != 0 {
|
||
store := &model.Store{}
|
||
store.ID = user.JxStoreID
|
||
if err = dao.GetEntity(db, store); err == nil {
|
||
err = fmt.Errorf("%s本身已经是门店:%d的组长", mobile, user.JxStoreID)
|
||
} else if dao.IsNoRowsError(err) {
|
||
err = nil
|
||
}
|
||
}
|
||
}
|
||
return user, err
|
||
}
|
||
|
||
func verifyMobileHasNoMembers(db *dao.DaoDB, mobile string) (err error) {
|
||
countInfo := &struct{ Ct int }{}
|
||
if err = dao.GetRow(db, countInfo, `
|
||
SELECT COUNT(*) ct
|
||
FROM weixins t1
|
||
JOIN weixins t2 ON t1.parentid = t2.id AND t2.tel = ?
|
||
`, mobile); err == nil {
|
||
if countInfo.Ct > 0 {
|
||
user := &legacymodel.WeiXins{
|
||
Tel: mobile,
|
||
}
|
||
dao.GetEntity(db, user, "Tel")
|
||
err = fmt.Errorf("%s本身已经是门店:%d组长", mobile, user.JxStoreID)
|
||
}
|
||
}
|
||
return err
|
||
}
|