// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package house_rent 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" "property-garden/errors" "property-garden/impl/v1/oss_utils" dbmodel "property-garden/model" "property-garden/pb" pb_v1 "property-garden/pb/v1" "property-garden/utils" "strings" ) func checkGardenHouseRentSyncParam(req *pb_v1.GardenHouseRentSyncRequest) error { switch { case req.GardenId < 1: return status.Error(10003, "小区不能为空") case len(req.Datas) == 0: return status.Error(10003, "内容不能为空") } return nil } // func GardenHouseRentSync(ctx context.Context, req *pb_v1.GardenHouseRentSyncRequest) (reply *pb_v1.GardenHouseRentSyncReply, err error) { reply = &pb_v1.GardenHouseRentSyncReply{} // 捕获各个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 = checkGardenHouseRentSyncParam(req) if err != nil { return nil, err } dbname := utils.GetGardenDbName(req.GardenId) p := dbmodel.THouseRent{} err = json.Unmarshal(req.Datas, &p) if err != nil { return nil, errors.ParamsError } if p.ID == 0 { return nil, status.Error(10003, "id不能为空") } p.SetTable(dbname) db := database.DB().Begin() oldHousePics := []string{} oldCertPics := []string{} if p.ID == 0 { return nil, status.Error(10003, "同步id不能为空") } if req.Insert { err = p.Insert(db) } else { where := map[string]interface{}{ "id": p.ID, "garden_id": req.GardenId, } err = p.Find(db, where) if err == nil { oldHousePics = strings.Split(p.HousePic, ";") oldCertPics = strings.Split(p.CertPic, ";") values := map[string]interface{}{} json.Unmarshal(req.Datas, &values) values["approved_at"] = p.ApprovedAt values["updated_at"] = p.UpdatedAt values["created_at"] = p.CreatedAt err = p.Update(db, where, values) } } if err != nil { db.Rollback() return nil, errors.DataBaseError } inList := []string{} outList := []string{} inList = append(inList, strings.Split(p.HousePic, ";")...) inList = append(inList, strings.Split(p.CertPic, ";")...) outList = append(outList, oldHousePics...) outList = append(outList, oldCertPics...) if err := oss_utils.OssObjAdd(inList, outList); err != nil { db.Rollback() return nil, err } if req.Increase != 0 { mreq := pb_v1.GardenRentSellCountRequest{GardenId: req.GardenId, RentIncrease: req.Increase} _, err := pb.System.GardenRentSellCount(ctx, &mreq) if err != nil { db.Rollback() return nil, err } } db.Commit() return reply, nil }