package gate_command import ( "encoding/json" "git.getensh.com/common/gopkgs/database" "google.golang.org/grpc/status" "gorm.io/gorm" "property-device/errors" "property-device/impl/v1/gate_pic" dbmodel "property-device/model" pb_v1 "property-device/pb/v1" "property-device/utils/gate_utils" "strings" "time" ) func checkGateCommandResultParamSaiboMqttv1(req *pb_v1.GateCommandResultRequest) error { switch { case req.Id == 0 && req.CmdCode != gate_utils.DelCommand && req.CmdCode != gate_utils.DownCommand: return status.Error(10003, "命令id不能为空") case req.Sn == "" || req.Protocol == 0: return status.Error(10003, "设备id不能为空") } return nil } func updateUserPicDownStatus(u *dbmodel.TUserPic, suc bool) { p := dbmodel.TUserPic{} if !suc { values := map[string]interface{}{ "down_status": 3, } where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "id", u.ID) _ = p.Update(database.DB(), where, values) return } gp := dbmodel.TGatePic{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "record_id", u.ID) where = dbmodel.WhereAdd(where, "status !=", gate_utils.WhiteAddStatusAllSuc) count, err := gp.Count(database.DB(), where, nil) if err != nil { return } if count > 0 { return } values := map[string]interface{}{ "down_status": 2, } where = [][2]interface{}{} where = dbmodel.WhereAdd(where, "id", u.ID) _ = p.Update(database.DB(), where, values) } func updateUserCardDownStatus(u *dbmodel.TUserCard, suc bool) { p := dbmodel.TUserCard{} if !suc { values := map[string]interface{}{ "down_status": 3, } where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "id", u.ID) _ = p.Update(database.DB(), where, values) return } gp := dbmodel.TGateCard{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "record_id", u.ID) where = dbmodel.WhereAdd(where, "status !=", gate_utils.WhiteAddStatusAllSuc) count, err := gp.Count(database.DB(), where, nil) if err != nil { return } if count > 0 { return } values := map[string]interface{}{ "down_status": 2, } where = [][2]interface{}{} where = dbmodel.WhereAdd(where, "id", u.ID) _ = p.Update(database.DB(), where, values) } func downCommandPicHandle(req *pb_v1.GateCommandResultRequest, gateInfo *dbmodel.TGate, res *gate_utils.EditPersonsNewRes) error { u := dbmodel.TUserPic{} where := [][2]interface{}{} if len(res.Info.AddErrInfo) > 0 { uid := res.Info.AddErrInfo[0].CustomId where = dbmodel.WhereAdd(where, "garden_id", gateInfo.GardenId) where = dbmodel.WhereAdd(where, "uid", uid) err := u.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return errors.DataBaseError } if u.ID == 0 { return errors.ErrRecordNotFound } pic := dbmodel.TGatePic{} where = [][2]interface{}{} where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID) where = dbmodel.WhereAdd(where, "record_id", u.ID) values := map[string]interface{}{ "status": gate_utils.WhiteAddStatusPicFail, "msg": res.Info.AddErrInfo[0].ErrCode, } err = pic.Update(database.DB(), where, values) if err != nil { return errors.DataBaseError } updateUserPicDownStatus(&u, false) } if len(res.Info.AddSucInfo) > 0 { uid := res.Info.AddSucInfo[0].CustomId where = dbmodel.WhereAdd(where, "garden_id", gateInfo.GardenId) where = dbmodel.WhereAdd(where, "uid", uid) err := u.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return errors.DataBaseError } if u.ID == 0 { return errors.ErrRecordNotFound } pic := dbmodel.TGatePic{} where = [][2]interface{}{} where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID) where = dbmodel.WhereAdd(where, "record_id", u.ID) values := map[string]interface{}{ "status": gate_utils.WhiteAddStatusAllSuc, "msg": "", } err = pic.Update(database.DB(), where, values) if err != nil { return errors.DataBaseError } updateUserPicDownStatus(&u, true) } return nil } /* func downCommandCardHandle(req *pb_v1.GateCommandResultRequest, gateInfo *dbmodel.TGate, res *gate_utils.EditPersonsNewRes) error { if len(res.Info.AddErrInfo) > 0 { uid := res.Info.AddErrInfo[0].CustomId card := dbmodel.TGateCard{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID) where = dbmodel.WhereAdd(where, "uid", uid) values := map[string]interface{}{ "status": gate_utils.WhiteAddStatusPersonFail, "msg": res.Info.AddErrInfo[0].ErrCode, } err := card.Update(database.DB(), where, values) if err != nil { return errors.DataBaseError } } if len(res.Info.AddSucInfo) > 0 { uid := res.Info.AddErrInfo[0].CustomId card := dbmodel.TGateCard{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID) where = dbmodel.WhereAdd(where, "uid", uid) values := map[string]interface{}{ "status": gate_utils.WhiteAddStatusAllSuc, "msg": "", } err := card.Update(database.DB(), where, values) if err != nil { return errors.DataBaseError } } return nil } */ func delCommandPicHandle(req *pb_v1.GateCommandResultRequest, gateInfo *dbmodel.TGate, res *gate_utils.DeletePersonsCmdRes) error { u := dbmodel.TUserPic{} where := [][2]interface{}{} if len(res.Info.DelSucInfo) > 0 { uids := make([]string, len(res.Info.DelSucInfo)) for i, v := range res.Info.DelSucInfo { uids[i] = v.CustomId } where = dbmodel.WhereAdd(where, "garden_id", gateInfo.GardenId) where = dbmodel.WhereAdd(where, "uid in", uids) ulist, err := u.List(database.DB(), where, nil, -1, -1) if err != nil { return errors.DataBaseError } recordIds := make([]int64, len(ulist)) for i, v := range ulist { recordIds[i] = v.ID } if len(recordIds) == 0 { return nil } pic := dbmodel.TGatePic{} where = [][2]interface{}{} where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID) where = dbmodel.WhereAdd(where, "record_id in", recordIds) err = pic.Delete(database.DB(), where) if err != nil { return errors.DataBaseError } for _, v := range recordIds { gate_pic.CheckUserPicDel(v, database.DB()) } } return nil } /* func delCommandCardHandle(req *pb_v1.GateCommandResultRequest, gateInfo *dbmodel.TGate, res *gate_utils.DeletePersonsCmdRes) error { if len(res.Info.DelSucInfo) > 0 { uids := make([]string, len(res.Info.DelSucInfo)) for i, v := range res.Info.DelSucInfo { uids[i] = v.CustomId } card := dbmodel.TGateCard{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID) where = dbmodel.WhereAdd(where, "uid in", uids) err := card.Delete(database.DB(), where) if err != nil { return errors.DataBaseError } } return nil } */ func CommandResultSaiboMqttV1(req *pb_v1.GateCommandResultRequest, gateInfo *dbmodel.TGate) error { if err := checkGateCommandResultParamSaiboMqttv1(req); err != nil { return err } if req.CmdCode != gate_utils.DownCommand && req.CmdCode != gate_utils.DelCommand { p := &dbmodel.TGateCommand{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "id", req.Id) where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID) err := p.Find(database.DB(), where) if err != nil && err != gorm.ErrRecordNotFound { return errors.DataBaseError } if p.ID == 0 { return errors.ErrRecordNotFound } now := time.Now() values := map[string]interface{}{ "status": gate_utils.CommandStatusOver, "result_status": req.ResultStatus, "result_status_desc": req.ResultDesc, "updated_at": now, } db := database.DB() err = p.Update(db, where, values) if err != nil { return errors.DataBaseError } } // 处理白名单 if req.CmdCode == gate_utils.DownCommand { res := gate_utils.EditPersonsNewRes{} json.Unmarshal([]byte(req.Content), &res) uid := "" if len(res.Info.AddSucInfo) > 0 { uid = res.Info.AddSucInfo[0].CustomId } else if len(res.Info.AddErrInfo) > 0 { uid = res.Info.AddErrInfo[0].CustomId } if uid == "" { return nil } if strings.Contains(uid, "auto") { return nil //return downCommandCardHandle(req, gateInfo, &res) } return downCommandPicHandle(req, gateInfo, &res) } if req.CmdCode == gate_utils.DelCommand { res := gate_utils.DeletePersonsCmdRes{} json.Unmarshal([]byte(req.Content), &res) uid := "" if len(res.Info.DelSucInfo) > 0 { uid = res.Info.DelSucInfo[0].CustomId } else if len(res.Info.DelErrInfo) > 0 { uid = res.Info.DelErrInfo[0].CustomId } if uid == "" { return nil } if strings.Contains(uid, "auto") { return nil //return delCommandCardHandle(req, gateInfo, &res) } return delCommandPicHandle(req, gateInfo, &res) } return nil }