// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package household import ( "context" "encoding/json" "fmt" "gorm.io/gorm" "property-garden/errors" "property-garden/impl/v1/statistic" dbmodel "property-garden/model" "property-garden/pb" pb_v1 "property-garden/pb/v1" "property-garden/utils" "time" "git.getensh.com/common/gopkgs/database" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" ) func checkHouseholdSyncParam(req *pb_v1.HouseholdSyncRequest) error { if req.HouseId < 1 || req.UnitNumber < 1 || req.HouseNumber == "" || req.BuildingNumber == "" || req.GardenId < 1 || req.BuildingId < 1 || req.Uid < 1 || req.UserType < 1 { return errors.ParamsError } return nil } // func HouseholdSync(ctx context.Context, req *pb_v1.HouseholdSyncRequest) (reply *pb_v1.HouseholdSyncReply, err error) { reply = &pb_v1.HouseholdSyncReply{} // 捕获各个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 = checkHouseholdSyncParam(req) if err != nil { return nil, err } dbname := utils.GetGardenDbName(req.GardenId) // 更新房屋入驻状态 house := dbmodel.NewHouse(dbname) where := map[string]interface{}{ "id": req.HouseId, } values := map[string]interface{}{ "status": 2, } db := database.DB().Begin() defer func() { if err != nil { db.Rollback() } }() err = house.Update(db, where, values) if err != nil { return nil, errors.DataBaseError } // 插入房屋申请记录 p := dbmodel.THouseApprovedGarden{ OpenId: req.OpenId, Uid: req.Uid, CreatedAt: time.Unix(req.CreatedAt, 0), UpdatedAt: time.Unix(req.UpdatedAt, 0), UserType: req.UserType, HouseId: req.HouseId, BuildingNumber: req.BuildingNumber, UnitNumber: req.UnitNumber, HouseNumber: req.HouseNumber, BuildingId: req.BuildingId, UnitId: req.UnitId, Appendix: req.Appendix, Feedback: req.Feedback, Phone: req.Phone, IdNumber: req.IdNumber, IdType: req.IdType, Name: req.Name, ApprovedAt: time.Unix(req.ApprovedAt, 0), ID: req.Id, PublicOpenId: req.PublicOpenId, } p.SetTable(dbname) err = p.Insert(db) if err != nil { return nil, errors.DataBaseError } statisticReq := pb_v1.RepairStatisticSetRequest{ HandleType: statistic.HandleTypeHousehold, FinishIncrease: int64(1), GardenId: req.GardenId, } err = statistic.RepairStatisticSetWithDb(&statisticReq, db) if err != nil { return nil, err } dReq := pb_v1.HouseholdGateSyncCheckRequest{GardenId: req.GardenId, Uid: req.Uid, UnitId: req.UnitId} _, err = pb.Device.HouseholdGateSyncCheck(ctx, &dReq) if err != nil { return nil, err } // 修改或插入业主信息 u := dbmodel.NewHouseholdUser(dbname) where = map[string]interface{}{ "id": req.HouseholdUser.Id, } err = u.Find(db, where) if err != nil && err != gorm.ErrRecordNotFound { return nil, errors.DataBaseError } u.SetTable(dbname) if u.ID == 0 { u.ID = req.HouseholdUser.Id u.Phone = req.HouseholdUser.Phone u.IdNumber = req.HouseholdUser.IdNumber u.IdType = req.HouseholdUser.IdType u.OpenId = req.HouseholdUser.OpenId u.UnionId = req.HouseholdUser.UnionId u.NickName = req.HouseholdUser.NickName u.RealName = req.HouseholdUser.RealName u.Avatar = req.HouseholdUser.Avatar u.CreatedAt = time.Now() u.UpdatedAt = time.Now() u.PublicOpenId = req.HouseholdUser.PublicOpenId err = u.Insert(db) if err == nil { mreq := pb_v1.ObjStatisticSetRequest{ ObjType: statistic.ObjTypeUser, TotalIncrease: int64(1), GardenId: req.GardenId, } err = statistic.ObjStatisticSetWithDb(&mreq, db) } } else { values := map[string]interface{}{ "phone": req.HouseholdUser.Phone, "id_number": req.HouseholdUser.IdNumber, "id_type": req.HouseholdUser.IdType, "open_id": req.HouseholdUser.OpenId, "union_id": req.HouseholdUser.UnionId, "nick_name": req.HouseholdUser.NickName, "real_name": req.HouseholdUser.RealName, "avatar": req.HouseholdUser.Avatar, "public_open_id": req.HouseholdUser.PublicOpenId, } err = u.Update(db, where, values) } if err != nil { return nil, errors.DataBaseError } db.Commit() return reply, nil }