- 改UpdtePlace的调用参数

This commit is contained in:
gazebo
2019-05-13 16:43:02 +08:00
parent 37da5be607
commit df53fcb9de
4 changed files with 33 additions and 9 deletions

View File

@@ -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
}

View File

@@ -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,
})

View File

@@ -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不是IDpayload中的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
})
}

View File

@@ -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()
}