123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- 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
- }
|