// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package event import ( "context" "encoding/json" "fmt" "git.getensh.com/common/gopkgs/database" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" "gorm.io/gorm" "property-garden/errors" "property-garden/impl/v1/oss_utils" dbmodel "property-garden/model" pb_v1 "property-garden/pb/v1" "property-garden/utils" "strings" "time" ) func checkEventUpdateParam(req *pb_v1.EventUpdateRequest) error { switch { case req.GardenId < 1: return status.Error(10003, "小区不能为空") case req.Title == "": return status.Error(10003, "标题不能为空") case req.Content == "": return status.Error(10003, "内容不能为空") case req.Start < 1: return status.Error(10003, "活动开始时间不能为空") case req.End < 1: return status.Error(10003, "活动结束时间不能为空") case req.End < req.Start: return status.Error(10003, "结束时间不能晚于开始时间") case req.SignUpEnd < 1: return status.Error(10003, "报名截止时间不能为空") case req.SignUpEnd > req.End: return status.Error(10003, "报名截止时间不能晚于活动结束时间") case req.PeopleLimit < 1: return status.Error(10003, "人数限制不能为空") case req.Addr == "": return status.Error(10003, "活动地址不能为空") case req.Id < 1: return status.Error(10003, "id不能为空") } pic := []string{} for _, v := range req.Pic { if oss_utils.IsFix(v) { continue } pic = append(pic, v) } req.Pic = pic return nil } // func EventUpdate(ctx context.Context, req *pb_v1.EventUpdateRequest) (reply *pb_v1.EventUpdateReply, err error) { reply = &pb_v1.EventUpdateReply{} // 捕获各个task中的异常并返回给调用者 defer func() { if r := recover(); r != nil { err = fmt.Errorf("%+v", r) e := &status.Status{} if er := json.Unmarshal([]byte(err.Error()), e); er != nil { logger.Error("err", zap.String("system_err", err.Error()), zap.Stack("stacktrace")) } } }() // 参数检查 err = checkEventUpdateParam(req) if err != nil { return nil, err } dbname := utils.GetGardenDbName(req.GardenId) now := time.Now() p := dbmodel.NewEvent(dbname) where := map[string]interface{}{ "id": req.Id, //"garden_id":req.GardenId, } old := dbmodel.NewEvent(dbname) err = old.Find(database.DB(), where) if err != nil { if err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } } values := map[string]interface{}{ "title": req.Title, "content": req.Content, "start": req.Start, "end": req.End, "sign_up_end": req.SignUpEnd, "people_limit": req.PeopleLimit, "addr": req.Addr, "fee": req.Fee, "updated_at": now, } values["pic"] = "" if len(req.Pic) > 0 { values["pic"] = utils.StringJoin(req.Pic, ";") } db := database.DB().Begin() err = p.Update(db, where, values) if err != nil { db.Rollback() return nil, errors.DataBaseError } inList := oss_utils.GetObjFromText(req.Content) outList := oss_utils.GetObjFromText(old.Content) pics := strings.Split(old.Pic, ";") if len(pics) > 0 { outList = append(outList, pics...) } if len(req.Pic) > 0 { inList = append(inList, req.Pic...) } if len(outList) > 0 || len(outList) > 0 { if err := oss_utils.OssObjAdd(inList, outList); err != nil { db.Rollback() return nil, err } } db.Commit() return reply, nil }