diff --git a/business/jxcallback/orderman/order.go b/business/jxcallback/orderman/order.go index b6cab1841..4d8da0cde 100644 --- a/business/jxcallback/orderman/order.go +++ b/business/jxcallback/orderman/order.go @@ -177,6 +177,8 @@ func (c *OrderManager) SaveOrder(order *model.GoodsOrder, isAdjust bool, db *dao order.WaybillVendorID = model.VendorIDUnknown order.OrderFinishedAt = utils.DefaultTimeValue + // cms.HandleOrder4Consignee(order) + dao.Begin(db) defer func() { if r := recover(); r != nil || err != nil { diff --git a/business/jxstore/cms/user2.go b/business/jxstore/cms/user2.go index f955a3cbe..227807131 100644 --- a/business/jxstore/cms/user2.go +++ b/business/jxstore/cms/user2.go @@ -23,7 +23,7 @@ import ( ) var ( - ErrUserIDAndNameMustGiven = errors.New("用户ID2,用户名及手机号必须不为空") + ErrUserIDAndNameMustGiven = errors.New("用户ID2和用户名必须不为空") ) var ( @@ -128,7 +128,7 @@ func HandleOrder4Consignee(order *model.GoodsOrder) (err error) { var user *model.User authType := jxutils.GetAuthType4Vendor(order.VendorID) if authType == "" { - msg := fmt.Sprintf("平台ID:%d当前不支持", order.VendorID) + msg := fmt.Sprintf("平台ID:%d当前不被支持,请联系开发", order.VendorID) globals.SugarLogger.Warn(msg) return fmt.Errorf(msg) } @@ -147,42 +147,85 @@ func HandleOrder4Consignee(order *model.GoodsOrder) (err error) { } vendorUserID := order.VendorUserID - if err == nil { - authList, err2 := dao.GetUserBindAuthInfo(db, "", model.AuthBindTypeID, "", []string{authType}) - if err = err2; err != nil { + if err == nil && vendorUserID != "" { + authInfo, err2 := dao.GetAuthBind(db, "", model.AuthBindTypeID, authType, vendorUserID) + if err = err2; err != nil && !dao.IsNoRowsError(err) { return err } - if len(authList) > 0 { - if user, err = dao.GetUserByID(db, "UserID", authList[0].UserID); err != nil { - return err + if err == nil { + vendorUserID = "" + if user == nil { + if user, err = dao.GetUserByID(db, "UserID", authInfo.UserID); err != nil { + return err + } } + } else { + err = nil } } + if user == nil { user = &model.User{ UserID2: mobileNumber, Name: order.ConsigneeName, Mobile: &mobileNumber, Type: model.UserTypeConsumer, + Address: order.ConsigneeAddress, Remark: order.VendorOrderID, } + setUserAddress(db, user, order) + + if user.GetID2() == "" { + user.UserID2 = vendorUserID + } + if user.GetID2() == "" { + user.UserID2 = order.VendorUserID + } + user.Type = model.UserTypeConsumer err = CreateUser(user, oeratorName) - } - if err == nil && vendorUserID != "" { - authBind := &model.AuthBind{ - UserID: user.GetID(), - BindType: model.AuthBindTypeID, - AuthID: vendorUserID, - Type: authType, + globals.SugarLogger.Debug(err) + } else { + if user.GetMobile() == "" && mobileNumber != "" { + user.Mobile = &mobileNumber + dao.UpdateEntity(db, user, "Mobile") + } + } + if err == nil { + order.UserID = user.GetID() + if vendorUserID != "" { + authBind := &model.AuthBind{ + UserID: user.GetID(), + BindType: model.AuthBindTypeID, + AuthID: vendorUserID, + Type: authType, + } + dao.WrapAddIDCULDEntity(authBind, oeratorName) + authBind.Status = model.AuthBindStatusNormal + err = dao.CreateEntity(nil, authBind) } - dao.WrapAddIDCULDEntity(authBind, oeratorName) - authBind.Status = model.AuthBindStatusNormal - err = dao.CreateEntity(nil, authBind) } return err } +func setUserAddress(db *dao.DaoDB, user *model.User, order *model.GoodsOrder) { + user.Address = order.ConsigneeAddress + store := &model.Store{} + store.ID = jxutils.GetSaleStoreIDFromOrder(order) + if err := dao.GetEntity(db, store); err == nil { + user.CityCode = store.CityCode + user.DistrictCode = store.DistrictCode + } else if dao.IsNoRowsError(err) { + if order.ConsigneeLng != 0 && order.ConsigneeLat != 0 { + if user.DistrictCode = api.AutonaviAPI.GetCoordinateDistrictCode(jxutils.IntCoordinate2Standard(order.ConsigneeLng), jxutils.IntCoordinate2Standard(order.ConsigneeLat)); user.DistrictCode > 0 { + if placeInfo, err := dao.GetPlaceByCode(db, user.DistrictCode); err == nil { + user.CityCode = placeInfo.ParentCode + } + } + } + } +} + func GetUserBindAuthInfo(ctx *jxcontext.Context) (authList []*model.AuthBind, err error) { authInfo, err := ctx.GetV2AuthInfo() if err == nil { @@ -192,9 +235,17 @@ func GetUserBindAuthInfo(ctx *jxcontext.Context) (authList []*model.AuthBind, er } func CreateUser(user *model.User, creatorName string) (err error) { - if user == nil || user.UserID2 == "" || user.Name == "" || user.GetMobile() == "" { + globals.SugarLogger.Debugf("CreateUser user:%s, creatorName:%s", utils.Format4Output(user, true), creatorName) + + if user == nil || user.UserID2 == "" || user.Name == "" { return ErrUserIDAndNameMustGiven } + if user.GetMobile() == "" { + user.Mobile = nil + } + if user.GetEmail() == "" { + user.Email = nil + } dao.WrapAddIDCULDEntity(user, creatorName) user.UserID = utils.GetUUID() user.Status = model.UserStatusNormal diff --git a/business/model/order.go b/business/model/order.go index 093283ce6..53b2fa605 100644 --- a/business/model/order.go +++ b/business/model/order.go @@ -32,6 +32,8 @@ type GoodsOrder struct { WaybillTipMoney int64 `json:"waybillTipMoney"` // 京西加的平台配送小费 EarningPrice int64 `json:"earningPrice"` // 结算给门店老板的钱(未扣除可能的三方配送费) Weight int `json:"weight"` // 单位为克 + VendorUserID string `orm:"column(vendor_user_id);size(48)" json:"vendorUserID"` + UserID string `orm:"column(user_id);size(48)" json:"userID"` ConsigneeName string `orm:"size(32)" json:"consigneeName"` ConsigneeMobile string `orm:"size(32)" json:"consigneeMobile"` ConsigneeMobile2 string `orm:"size(32)" json:"consigneeMobile2"` @@ -65,7 +67,6 @@ type GoodsOrder struct { // 以下只是用于传递数据 OriginalData string `orm:"-" json:"-"` Skus []*OrderSku `orm:"-" json:"-"` - VendorUserID string `orm:"-" json:"-"` } func (o *GoodsOrder) TableUnique() [][]string { diff --git a/business/model/user.go b/business/model/user.go index e8138d299..b71e6638a 100644 --- a/business/model/user.go +++ b/business/model/user.go @@ -26,15 +26,20 @@ var ( type User struct { ModelIDCULD - UserID string `orm:"size(48);column(user_id)" json:"userID" compact:"userID"` // 内部唯一标识 - UserID2 string `orm:"size(48);column(user_id2)" json:"userID2" compact:"userID2"` // 外部唯一标识(一般用于登录) - Name string `orm:"size(48);index" json:"name" compact:"name"` // 外部显示标识(当前可以重复) - Mobile *string `orm:"size(32);null" json:"mobile" compact:"mobile"` - Email *string `orm:"size(32);null" json:"email" compact:"email"` - Status int8 `json:"status" compact:"status"` - Type int8 `json:"type" compact:"type"` // 用户类型 - IDCardNo string `orm:"size(18);column(id_card_no)" json:"idCardNo" compact:"idCardNo"` // 身份证号 - Remark string `orm:"size(255)" json:"remark"` + UserID string `orm:"size(48);column(user_id)" json:"userID" compact:"userID"` // 内部唯一标识 + UserID2 string `orm:"size(48);column(user_id2)" json:"userID2" compact:"userID2"` // 外部唯一标识(一般用于登录) + Name string `orm:"size(48);index" json:"name" compact:"name"` // 外部显示标识(当前可以重复) + Mobile *string `orm:"size(32);null" json:"mobile" compact:"mobile"` + Email *string `orm:"size(32);null" json:"email" compact:"email"` + Status int8 `json:"status" compact:"status"` + Type int8 `json:"type" compact:"type"` // 用户类型 + + CityCode int `orm:"default(0)" json:"cityCode"` + DistrictCode int `orm:"default(0);index" json:"districtCode"` + Address string `orm:"size(255)" json:"address"` + + IDCardNo string `orm:"size(18);column(id_card_no)" json:"idCardNo" compact:"idCardNo"` // 身份证号 + Remark string `orm:"size(255)" json:"remark"` } func (*User) TableUnique() [][]string {