From df53fcb9de18650720d934aa52fd8119acd5cc7b Mon Sep 17 00:00:00 2001 From: gazebo Date: Mon, 13 May 2019 16:43:02 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E6=94=B9UpdtePlace=E7=9A=84=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- business/jxstore/cms/cms.go | 15 +++++++++++++-- business/model/dao/dao_bz.go | 4 ++-- controllers/cms.go | 19 ++++++++++++++----- globals/refutil/refutil.go | 4 ++++ 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/business/jxstore/cms/cms.go b/business/jxstore/cms/cms.go index 5dddd9c88..e9307cb87 100644 --- a/business/jxstore/cms/cms.go +++ b/business/jxstore/cms/cms.go @@ -20,7 +20,12 @@ const ( ) var ( - serviceInfo map[string]interface{} + serviceInfo map[string]interface{} + allowUpdatePlaceFieldsMap = map[string]bool{ + "name": true, + "enabled": true, + "mtpsPrice": true, + } ) func InitServiceInfo(version string, buildTime time.Time, gitCommit string) { @@ -126,12 +131,18 @@ func UpdatePlaces(ctx *jxcontext.Context, places []map[string]interface{}, userN if len(places) == 0 { return 0, ErrMissingInput } + updateFields := []string{} + for k := range places[0] { + if allowUpdatePlaceFieldsMap[k] { + updateFields = append(updateFields, k) + } + } for _, place := range places { if place["code"] == nil { return 0, ErrMissingInput } placeid := &model.Place{} - valid := dao.NormalMakeMapByFieldList(place, []string{"jdCode", "enabled", "mtpsPrice"}, userName) + valid := dao.NormalMakeMapByFieldList(place, updateFields, userName) if num, err = dao.UpdateEntityLogically(nil, placeid, valid, userName, utils.Params2Map("Code", place["code"])); err != nil { return num, err } diff --git a/business/model/dao/dao_bz.go b/business/model/dao/dao_bz.go index e76829bf6..e2ddbab3a 100644 --- a/business/model/dao/dao_bz.go +++ b/business/model/dao/dao_bz.go @@ -58,7 +58,7 @@ func UpdateEntityByKV(db *DaoDB, item interface{}, kvs map[string]interface{}, c } func UpdateEntityLogically(db *DaoDB, item interface{}, kvs map[string]interface{}, userName string, conditions map[string]interface{}) (num int64, err error) { - if conditions != nil { + if conditions != nil && refutil.IsFieldExist(item, model.FieldDeletedAt) { conditions = utils.MergeMaps(conditions, map[string]interface{}{ model.FieldDeletedAt: utils.DefaultTimeValue, }) @@ -71,7 +71,7 @@ func UpdateEntityLogically(db *DaoDB, item interface{}, kvs map[string]interface // 此函数会更新同步标志 func UpdateEntityLogicallyAndUpdateSyncStatus(db *DaoDB, item interface{}, kvs map[string]interface{}, userName string, conditions map[string]interface{}, syncStatusFieldName string, valueMask int) (num int64, err error) { - if conditions != nil { + if conditions != nil && refutil.IsFieldExist(item, model.FieldDeletedAt) { conditions = utils.MergeMaps(conditions, map[string]interface{}{ model.FieldDeletedAt: utils.DefaultTimeValue, }) diff --git a/controllers/cms.go b/controllers/cms.go index 1a8df5b18..02548e393 100644 --- a/controllers/cms.go +++ b/controllers/cms.go @@ -52,17 +52,26 @@ func (c *CmsController) UpdatePlaces() { // @Title 修改地点信息 // @Description 只支持修改enabled, jd_code和mtps_price这三个属性 // @Param token header string true "认证token" -// @Param code formData int true "地点编号,注意是code不是ID,payload中的code会被忽略" -// @Param payload formData string true "json数据,place对象" +// @Param code formData int true "地点编号,注意是code不是ID" +// @Param enabled formData bool true "是否启用" +// @Param name formData string false "地点名" +// @Param mtpsPrice formData int false "美团配送基础价格" // @Success 200 {object} controllers.CallResult // @Failure 200 {object} controllers.CallResult // @router /UpdatePlace [put] func (c *CmsController) UpdatePlace() { c.callUpdatePlace(func(params *tCmsUpdatePlaceParams) (retVal interface{}, errCode string, err error) { - place := make(map[string]interface{}, 0) - if err = utils.UnmarshalUseNumber([]byte(params.Payload), &place); err == nil { - retVal, err = cms.UpdatePlace(params.Ctx, params.Code, place, params.Ctx.GetUserName()) + payload := map[string]interface{}{ + "code": params.Code, + "enabled": params.Enabled, } + if params.Name != "" { + payload["name"] = params.Name + } + if params.MtpsPrice > 0 { + payload["mtpsPrice"] = params.MtpsPrice + } + retVal, err = cms.UpdatePlaces(params.Ctx, []map[string]interface{}{payload}, params.Ctx.GetUserName()) return retVal, "", err }) } diff --git a/globals/refutil/refutil.go b/globals/refutil/refutil.go index d003db82c..70ed494ff 100644 --- a/globals/refutil/refutil.go +++ b/globals/refutil/refutil.go @@ -20,6 +20,10 @@ func CheckAndGetStructValue(item interface{}) *reflect.Value { return &value } +func IsFieldExist(obj interface{}, fieldName string) bool { + return reflect.Indirect(reflect.ValueOf(obj)).FieldByName(fieldName).IsValid() +} + func GetObjFieldByName(obj interface{}, fieldName string) interface{} { return reflect.Indirect(reflect.ValueOf(obj)).FieldByName(fieldName).Interface() }