123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- // 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
- }
|