123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- package gate_pic
- 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-device/errors"
- dbmodel "property-device/model"
- pb_v1 "property-device/pb/v1"
- "property-device/utils/gate_utils"
- "strconv"
- "time"
- )
- func checkGateUserPicSyncParam(req *pb_v1.GateUserPicSyncRequest) error {
- switch {
- case req.GardenId == 0:
- return status.Error(10003, "小区不能为空")
- case req.Id == 0:
- return status.Error(10003, "记录id不能为空")
- }
- return nil
- }
- func GateUserPicSync(ctx context.Context, req *pb_v1.GateUserPicSyncRequest) (reply *pb_v1.GateUserPicSyncReply, err error) {
- reply = &pb_v1.GateUserPicSyncReply{}
- // 捕获各个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 = checkGateUserPicSyncParam(req)
- if err != nil {
- return nil, err
- }
- up := dbmodel.TUserPic{}
- where := [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "id", req.Id)
- err = up.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return nil, errors.DataBaseError
- }
- if up.ID == 0 {
- return nil, errors.ErrRecordNotFound
- }
- if up.ApproveStatus != 2 {
- return nil, status.Error(10003, "当前状态不能同步")
- }
- if time.Now().Unix()-up.UpdatedAt.Unix() < 60 {
- return nil, status.Error(10003, "下发操作间隔时间不能小于1分钟")
- }
- uid, _ := strconv.ParseInt(up.Uid, 10, 64)
- unitInfo, _, _, err := gate_utils.GetUserUnitIds(req.GardenId, []int64{uid})
- if err != nil {
- return nil, err
- }
- if len(unitInfo[uid]) == 0 {
- return reply, nil
- }
- devices, err := getAllPicDevices(unitInfo[uid], up.GardenId)
- if err != nil {
- return nil, errors.DataBaseError
- }
- //if !checkOnlineStatus(devices) {
- // return nil, status.Error(10003, "当前有设备未在线,无法完成下发,请检查设备后审批")
- //}
- gps, err := GetGatePicsByRecordId(up.ID, false)
- if err != nil {
- return nil, errors.DataBaseError
- }
- needAdds, needUpdates := whichDeviceNeedAdd(devices, gps)
- db := database.DB().Begin()
- if err := WhitePicAddToDb(up, needAdds, db); err != nil {
- db.Rollback()
- return nil, err
- }
- db.Commit()
- WhitePicAddToDevice(up, needAdds)
- WhitePicUpdate(up, needUpdates, gps)
- values := map[string]interface{}{
- "updated_at": time.Now(),
- }
- _ = up.Update(database.DB(), where, values)
- return reply, nil
- }
|