- 重构配送门店管理
This commit is contained in:
@@ -82,6 +82,7 @@ func InitServiceInfo(version string, buildTime time.Time, gitCommit string) {
|
||||
"actTypeName": model.ActTypeName,
|
||||
"actStatusName": model.ActStatusName,
|
||||
"actCreateTypeName": model.ActCreateTypeName,
|
||||
"storeAuditStatusName": model.StoreAuditStatusName,
|
||||
},
|
||||
}
|
||||
Init()
|
||||
|
||||
@@ -10,10 +10,10 @@ import (
|
||||
|
||||
"git.rosy.net.cn/jx-callback/business/auth2"
|
||||
|
||||
"git.rosy.net.cn/baseapi/platformapi/dadaapi"
|
||||
"git.rosy.net.cn/baseapi/platformapi/dingdingapi"
|
||||
"git.rosy.net.cn/baseapi/platformapi/jdapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/baseapi/utils/errlist"
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils"
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils/excel"
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
|
||||
@@ -24,7 +24,6 @@ import (
|
||||
"git.rosy.net.cn/jx-callback/business/model/dao"
|
||||
"git.rosy.net.cn/jx-callback/business/model/legacymodel"
|
||||
"git.rosy.net.cn/jx-callback/business/partner"
|
||||
"git.rosy.net.cn/jx-callback/business/partner/delivery/dada"
|
||||
"git.rosy.net.cn/jx-callback/business/partner/purchase/ebai"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"git.rosy.net.cn/jx-callback/globals/api"
|
||||
@@ -71,17 +70,6 @@ var (
|
||||
)
|
||||
|
||||
var (
|
||||
dadaDistrictMap = map[string]string{
|
||||
"苏州工业园区": "工业园区",
|
||||
"郫都区": "郫县",
|
||||
"管城回族区": "管城区",
|
||||
"昆山市": "1",
|
||||
"常熟市": "1",
|
||||
"太仓市": "1",
|
||||
"虞山街道": "虞山镇",
|
||||
"常福街道": "虞山镇",
|
||||
}
|
||||
|
||||
storeKeyPropertyMap = map[string]int{
|
||||
"name": 1,
|
||||
"cityCode": 1,
|
||||
@@ -624,6 +612,9 @@ func UpdateStore(ctx *jxcontext.Context, storeID int, payload map[string]interfa
|
||||
dao.Commit(db)
|
||||
globals.SugarLogger.Debugf("UpdateStore track:%s, before call SyncStore", ctx.GetTrackInfo())
|
||||
_, err = CurVendorSync.SyncStore(ctx, db, -1, store.ID, false, userName)
|
||||
if err2 := updateCourierStores(ctx, storeID); err2 != nil && err == nil {
|
||||
err = err2
|
||||
}
|
||||
}
|
||||
} else {
|
||||
dao.Commit(db)
|
||||
@@ -703,7 +694,7 @@ func CreateStore(ctx *jxcontext.Context, storeExt *StoreExt, userName string) (i
|
||||
dao.WrapAddIDCULDEntity(store, userName)
|
||||
store.ID = existingID
|
||||
if err = dao.CreateEntity(nil, store); err == nil {
|
||||
RefreshMissingDadaStores(ctx, store.ID, false, false)
|
||||
UpdateOrCreateCourierStores(ctx, store.ID, false, false, false)
|
||||
return store.ID, err
|
||||
}
|
||||
return 0, err
|
||||
@@ -986,43 +977,45 @@ func GetStoreCourierMaps(ctx *jxcontext.Context, db *dao.DaoDB, storeID int, ven
|
||||
}
|
||||
|
||||
func AddStoreCourierMap(ctx *jxcontext.Context, db *dao.DaoDB, storeID, vendorID int, storeCourierMap *model.StoreCourierMap) (outStoreCourierMap *model.StoreCourierMap, err error) {
|
||||
return addStoreCourierMap(ctx, db, storeID, vendorID, storeCourierMap, true)
|
||||
}
|
||||
|
||||
func addStoreCourierMap(ctx *jxcontext.Context, db *dao.DaoDB, storeID, vendorID int, storeCourierMap *model.StoreCourierMap, isNeedUpdateRemote bool) (outStoreCourierMap *model.StoreCourierMap, err error) {
|
||||
userName := ctx.GetUserName()
|
||||
if handler := partner.GetDeliveryPlatformFromVendorID(vendorID); handler != nil {
|
||||
dao.WrapAddIDCULDEntity(storeCourierMap, userName)
|
||||
storeCourierMap.StoreID = storeID
|
||||
storeCourierMap.VendorID = vendorID
|
||||
|
||||
if db == nil {
|
||||
db = dao.GetDB()
|
||||
}
|
||||
if vendorID == model.VendorIDDada {
|
||||
storeList, err2 := dao.GetMissingDadaStores(db, storeID, false)
|
||||
if err = err2; err == nil && len(storeList) > 0 {
|
||||
storeList[0].VendorStoreID = storeCourierMap.VendorStoreID
|
||||
err = updateOrCreateDadaStore(storeList[0])
|
||||
} else {
|
||||
globals.SugarLogger.Debugf("AddStoreCourierMap GetMissingDadaStores error:%v, len(storeList):%d", err, len(storeList))
|
||||
if isNeedUpdateRemote {
|
||||
storeDetail, err2 := dao.GetStoreDetail2(db, storeID, "", vendorID)
|
||||
if err = err2; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if storeDetail.VendorStoreID != "" && storeDetail.VendorStoreID != storeCourierMap.VendorStoreID {
|
||||
return nil, fmt.Errorf("门店已经绑定了%s, ID:%s, 如需重新绑定, 请先解绑", model.VendorChineseNames[vendorID], storeDetail.VendorStoreID)
|
||||
}
|
||||
dao.WrapAddIDCULDEntity(storeCourierMap, userName)
|
||||
storeCourierMap.StoreID = storeID
|
||||
storeCourierMap.VendorID = vendorID
|
||||
storeDetail.VendorStoreID = storeCourierMap.VendorStoreID
|
||||
if err = updateCourierStore(ctx, storeDetail); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dao.Begin(db)
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
if r := recover(); r != nil || err != nil {
|
||||
dao.Rollback(db)
|
||||
panic(r)
|
||||
if r != nil {
|
||||
panic(r)
|
||||
}
|
||||
}
|
||||
}()
|
||||
if err = dao.CreateEntity(db, storeCourierMap); err == nil {
|
||||
dao.Commit(db)
|
||||
outStoreCourierMap = storeCourierMap
|
||||
if err == nil {
|
||||
_, err = CurVendorSync.SyncStore(ctx, db, storeCourierMap.VendorID, storeID, false, userName)
|
||||
}
|
||||
} else {
|
||||
dao.Rollback(db)
|
||||
if err = dao.CreateEntity(db, storeCourierMap); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
outStoreCourierMap = storeCourierMap
|
||||
dao.Commit(db)
|
||||
} else {
|
||||
err = ErrCanNotFindVendor
|
||||
}
|
||||
@@ -1034,14 +1027,12 @@ func DeleteStoreCourierMap(ctx *jxcontext.Context, db *dao.DaoDB, storeID, vendo
|
||||
db = dao.GetDB()
|
||||
}
|
||||
storeCourierMap := &model.StoreCourierMap{}
|
||||
if num, err = dao.DeleteEntityLogically(db, storeCourierMap, map[string]interface{}{
|
||||
num, err = dao.DeleteEntityLogically(db, storeCourierMap, map[string]interface{}{
|
||||
model.FieldStatus: model.StoreStatusDisabled,
|
||||
}, userName, map[string]interface{}{
|
||||
model.FieldStoreID: storeID,
|
||||
model.FieldVendorID: vendorID,
|
||||
}); err == nil && num > 0 {
|
||||
_, err = CurVendorSync.SyncStore(ctx, db, vendorID, storeID, false, userName)
|
||||
}
|
||||
})
|
||||
return num, err
|
||||
}
|
||||
|
||||
@@ -1050,6 +1041,7 @@ func UpdateStoreCourierMap(ctx *jxcontext.Context, db *dao.DaoDB, storeID, vendo
|
||||
db = dao.GetDB()
|
||||
}
|
||||
dummyStoreCourierMap := &model.StoreCourierMap{}
|
||||
delete(payload, "auditStatus") // 不允许直接修改auditStatus的值
|
||||
valid := dao.NormalMakeMapByStructObject(payload, dummyStoreCourierMap, userName)
|
||||
if len(valid) > 0 {
|
||||
num, err = dao.UpdateEntityLogically(db, dummyStoreCourierMap, valid, userName, map[string]interface{}{
|
||||
@@ -1060,31 +1052,113 @@ func UpdateStoreCourierMap(ctx *jxcontext.Context, db *dao.DaoDB, storeID, vendo
|
||||
return num, err
|
||||
}
|
||||
|
||||
func RefreshMissingDadaStores(ctx *jxcontext.Context, storeID int, isAsync, isContinueWhenError bool) (hint string, err error) {
|
||||
db := dao.GetDB()
|
||||
storeList, err := dao.GetMissingDadaStores(db, storeID, false)
|
||||
if err != nil {
|
||||
return "", err
|
||||
func updateCourierStore(ctx *jxcontext.Context, storeDetail *dao.StoreDetail2) (err error) {
|
||||
if handlerInfo := partner.GetDeliveryPlatformFromVendorID(storeDetail.VendorID); handlerInfo != nil && handlerInfo.Use4CreateWaybill {
|
||||
if updateHandler, ok := handlerInfo.Handler.(partner.IDeliveryUpdateStoreHandler); ok {
|
||||
storeDetail.CourierStoreName = composeDadaStoreName(storeDetail)
|
||||
err = updateHandler.UpdateStore(ctx, storeDetail)
|
||||
}
|
||||
}
|
||||
task := tasksch.NewParallelTask("RefreshMissingDadaStores", tasksch.NewParallelConfig().SetIsContinueWhenError(isContinueWhenError), ctx,
|
||||
func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
||||
storeDetail := batchItemList[0].(*dao.StoreDetail2)
|
||||
var resultList []interface{}
|
||||
if storeDetail.VendorStoreID == "" {
|
||||
if storeDetail.DistrictName == "" || storeDetail.CityName == "" {
|
||||
return nil, fmt.Errorf("门店:%s的城市码或区码有错误", storeDetail.Name)
|
||||
return err
|
||||
}
|
||||
|
||||
func updateCourierStores(ctx *jxcontext.Context, storeID int) (err error) {
|
||||
db := dao.GetDB()
|
||||
errList := errlist.New()
|
||||
for k, v := range partner.DeliveryPlatformHandlers {
|
||||
if v.Use4CreateWaybill {
|
||||
if _, ok := v.Handler.(partner.IDeliveryUpdateStoreHandler); ok {
|
||||
storeDetail, err2 := dao.GetStoreDetail2(db, storeID, "", k)
|
||||
if err = err2; err2 == nil {
|
||||
err = updateCourierStore(ctx, storeDetail)
|
||||
}
|
||||
db := dao.GetDB()
|
||||
if _, err = AddStoreCourierMap(ctx, db, storeDetail.ID, model.VendorIDDada, &model.StoreCourierMap{
|
||||
VendorStoreID: utils.Int2Str(storeDetail.ID),
|
||||
Status: model.StoreStatusOpened,
|
||||
}); err == nil {
|
||||
resultList = append(resultList, 1)
|
||||
errList.AddErr(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return errList.GetErrListAsOne()
|
||||
}
|
||||
|
||||
func updateOrCreateCourierStore(ctx *jxcontext.Context, storeDetail *dao.StoreDetail2) (isCreated bool, err error) {
|
||||
if handlerInfo := partner.GetDeliveryPlatformFromVendorID(storeDetail.VendorID); handlerInfo != nil && handlerInfo.Use4CreateWaybill {
|
||||
if storeDetail.DistrictName == "" {
|
||||
return false, fmt.Errorf("门店的区码有问题,请检查")
|
||||
}
|
||||
if storeDetail.CityName == "" {
|
||||
return false, fmt.Errorf("门店的城市码有问题,请检查")
|
||||
}
|
||||
storeDetail.CourierStoreName = composeDadaStoreName(storeDetail)
|
||||
if _, err = handlerInfo.Handler.GetStore(ctx, 0, storeDetail.VendorStoreID); err != nil {
|
||||
if !handlerInfo.Handler.IsErrStoreNotExist(err) {
|
||||
storeDetail.VendorStoreID, storeDetail.AuditStatus, err = handlerInfo.Handler.CreateStore(ctx, storeDetail)
|
||||
isCreated = true
|
||||
}
|
||||
} else {
|
||||
if updateHandler, _ := handlerInfo.Handler.(partner.IDeliveryUpdateStoreHandler); updateHandler != nil {
|
||||
err = updateHandler.UpdateStore(ctx, storeDetail)
|
||||
} else {
|
||||
// err = fmt.Errorf("快递平台%s不支持更新门店信息,请手动处理", model.VendorChineseNames[storeDetail.VendorID])
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
err = fmt.Errorf("门店ID:%d,门店名:%s,错误描述:%s", storeDetail.Store.ID, storeDetail.Name, err.Error())
|
||||
globals.SugarLogger.Debugf("updateOrCreateDadaStore storeID:%d failed with error:%v", storeDetail.ID, err)
|
||||
}
|
||||
}
|
||||
return isCreated, err
|
||||
}
|
||||
|
||||
func UpdateOrCreateCourierStores(ctx *jxcontext.Context, storeID int, isForceUpdate, isAsync, isContinueWhenError bool) (hint string, err error) {
|
||||
var storeIDs []int
|
||||
if storeID != 0 {
|
||||
storeIDs = []int{storeID}
|
||||
} else {
|
||||
storesInfo, err2 := GetStores(ctx, "", map[string]interface{}{}, 0, -1, utils.DefaultTimeValue, utils.DefaultTimeValue, 0, 0)
|
||||
if err = err2; err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, v := range storesInfo.Stores {
|
||||
storeIDs = append(storeIDs, v.ID)
|
||||
}
|
||||
}
|
||||
|
||||
task := tasksch.NewParallelTask("UpdateOrCreateCourierStores", tasksch.NewParallelConfig().SetIsContinueWhenError(isContinueWhenError), ctx,
|
||||
func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
||||
var resultList []interface{}
|
||||
storeID := batchItemList[0].(int)
|
||||
errList := errlist.New()
|
||||
db := dao.GetDB()
|
||||
for k, v := range partner.DeliveryPlatformHandlers {
|
||||
if v.Use4CreateWaybill {
|
||||
if _, ok := v.Handler.(partner.IDeliveryUpdateStoreHandler); ok {
|
||||
storeDetail, err2 := dao.GetStoreDetail2(db, storeID, "", k)
|
||||
if err = err2; err2 == nil {
|
||||
isStoreExist := storeDetail.VendorStoreID == ""
|
||||
storeDetail.VendorStoreID = utils.Int2Str(storeDetail.ID)
|
||||
if isForceUpdate || !isStoreExist {
|
||||
var isCreated bool
|
||||
if isCreated, err = updateOrCreateCourierStore(ctx, storeDetail); err == nil && isCreated {
|
||||
storeCourier := &model.StoreCourierMap{
|
||||
VendorStoreID: storeDetail.VendorStoreID,
|
||||
Status: model.StoreStatusOpened,
|
||||
AuditStatus: storeDetail.AuditStatus,
|
||||
}
|
||||
if storeDetail.AuditStatus != model.StoreAuditStatusOnline {
|
||||
storeCourier.Status = model.StoreStatusDisabled
|
||||
}
|
||||
if _, err = addStoreCourierMap(ctx, db, storeDetail.ID, storeDetail.VendorID, storeCourier, false); err == nil {
|
||||
resultList = append(resultList, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
errList.AddErr(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return resultList, err
|
||||
}, storeList)
|
||||
tasksch.HandleTask(task, nil, true).Run()
|
||||
}, storeIDs)
|
||||
tasksch.HandleTask(task, nil, len(storeIDs) > 1).Run()
|
||||
if !isAsync {
|
||||
resultList, err2 := task.GetResult(0)
|
||||
if err = err2; err == nil {
|
||||
@@ -1096,32 +1170,6 @@ func RefreshMissingDadaStores(ctx *jxcontext.Context, storeID int, isAsync, isCo
|
||||
return hint, err
|
||||
}
|
||||
|
||||
func updateOrCreateDadaStore(storeDetail *dao.StoreDetail2) (err error) {
|
||||
if storeDetail.DistrictName == "" {
|
||||
return fmt.Errorf("门店的区码有问题,请检查")
|
||||
}
|
||||
if storeDetail.CityName == "" {
|
||||
return fmt.Errorf("门店的城市码有问题,请检查")
|
||||
}
|
||||
|
||||
storeDetail.CourierStoreName = composeDadaStoreName(storeDetail)
|
||||
if globals.EnableStoreWrite {
|
||||
_, err = api.DadaAPI.ShopDetail(storeDetail.VendorStoreID)
|
||||
if err != nil {
|
||||
if codeErr, ok := err.(*utils.ErrorWithCode); ok && codeErr.IntCode() == dadaapi.ResponseCodeShopNotExist {
|
||||
_, err = api.DadaAPI.ShopAdd(dada.StoreDetail2ShopInfo(storeDetail))
|
||||
}
|
||||
} else {
|
||||
err = api.DadaAPI.ShopUpdate(dada.StoreDetail2ShopInfo(storeDetail))
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
err = fmt.Errorf("门店ID:%d,门店名:%s,错误描述:%s", storeDetail.Store.ID, storeDetail.Name, err.Error())
|
||||
globals.SugarLogger.Debugf("updateOrCreateDadaStore storeID:%d failed with error:%v", storeDetail.ID, err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func composeDadaStoreName(storeDetail *dao.StoreDetail2) (storeName string) {
|
||||
return storeDetail.Name + "-" + storeDetail.VendorStoreID
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package cms
|
||||
|
||||
import (
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"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/partner"
|
||||
@@ -100,3 +101,19 @@ func (s *StoreManager) OnStoreStatusChanged(vendorStoreID string, vendorID int,
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *StoreManager) OnCourierStoreStatusChanged(ctx *jxcontext.Context, vendorStoreID string, vendorID int, auditStatus int) (err error) {
|
||||
if vendorStoreID != "" {
|
||||
db := dao.GetDB()
|
||||
_, err2 := dao.GetStoreDetail2(db, 0, vendorStoreID, vendorID)
|
||||
if err = err2; err == nil {
|
||||
_, err = dao.UpdateEntityLogically(db, &model.StoreCourierMap{}, map[string]interface{}{
|
||||
"AuditStatus": auditStatus,
|
||||
}, ctx.GetUserName(), map[string]interface{}{
|
||||
model.FieldVendorStoreID: vendorStoreID,
|
||||
model.FieldVendorID: vendorID,
|
||||
})
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user