123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- package gate_command
- import (
- "encoding/json"
- "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"
- "property-device/impl/v1/gate_card"
- dbmodel "property-device/model"
- pb_v1 "property-device/pb/v1"
- "property-device/utils/gate_utils"
- "time"
- )
- func checkGateCommandResultParamSaibohttpv1(req *pb_v1.GateCommandResultRequest) error {
- switch {
- case req.Id == 0:
- return status.Error(10003, "命令id不能为空")
- case req.Sn == "" || req.Protocol == 0:
- return status.Error(10003, "设备id不能为空")
- }
- return nil
- }
- type CodeGateQueryCmdPostDataBody struct {
- ViewId string `form:"ViewId" json:"ViewId"`
- UID string `form:"UID" json:"UID"`
- UKey string `form:"UKey" json:"UKey"`
- SN string `form:"SN" json:"SN"`
- CmdID string `form:"CmdID" json:"CmdID"`
- CmdCode int `form:"CmdCode" json:"CmdCode"`
- CmdParams map[string][]map[string]string `form:"CmdParams" json:"CmdParams"`
- CmdStatus int `form:"CmdStatus" json:"CmdStatus"`
- }
- // 处理同步中的数据
- func whiteResultStatusUsed(gateInfo *dbmodel.TGate, exists []string, origin []string) {
- if len(exists) > 0 {
- ucard := dbmodel.TUserCard{}
- where := [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "garden_id", gateInfo.GardenId)
- where = dbmodel.WhereAdd(where, "card_number in", exists)
- ulist, err := ucard.List(database.DB(), where, nil, -1, -1)
- if err != nil {
- return
- }
- rids := make([]int64, len(ulist))
- for i, v := range ulist {
- rids[i] = v.ID
- }
- if len(rids) == 0 {
- return
- }
- p := dbmodel.TGateCard{}
- where = [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
- where = dbmodel.WhereAdd(where, "record_id in", rids)
- values := map[string]interface{}{
- "status": gate_utils.WhiteAddStatusAllSuc,
- }
- err = p.Update(database.DB(), where, values)
- if err != nil {
- logger.Error("func",
- zap.String("call", "whiteResultStatusUsed "),
- zap.String("error", err.Error()))
- }
- for _, v := range ulist {
- u := v
- updateUserCardDownStatus(&u, true)
- }
- }
- // 添加失败的记录
- failed := []string{}
- if len(origin) > len(exists) {
- em := map[string]bool{}
- for _, v := range exists {
- em[v] = true
- }
- for _, v := range origin {
- if !em[v] {
- failed = append(failed, v)
- }
- }
- ucard := dbmodel.TUserCard{}
- where := [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "garden_id", gateInfo.GardenId)
- where = dbmodel.WhereAdd(where, "card_number in", exists)
- ulist, err := ucard.List(database.DB(), where, nil, -1, -1)
- if err != nil {
- return
- }
- rids := make([]int64, len(ulist))
- for i, v := range ulist {
- rids[i] = v.ID
- }
- if len(rids) == 0 {
- return
- }
- p := dbmodel.TGateCard{}
- where = [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
- where = dbmodel.WhereAdd(where, "record_id in", rids)
- values := map[string]interface{}{
- "status": gate_utils.WhiteAddStatusPersonFail,
- }
- err = p.Update(database.DB(), where, values)
- if err != nil {
- logger.Error("func",
- zap.String("call", "whiteResultStatusUsed "),
- zap.String("error", err.Error()))
- }
- for _, v := range ulist {
- u := v
- updateUserCardDownStatus(&u, false)
- }
- }
- // todo 更新ucardstatus
- }
- // 处理删除中的数据
- func whiteResultStatusDeleted(gateInfo *dbmodel.TGate, notExist []string) {
- if len(notExist) == 0 {
- return
- }
- ucard := dbmodel.TUserCard{}
- where := [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "garden_id", gateInfo.GardenId)
- where = dbmodel.WhereAdd(where, "card_number in", notExist)
- ulist, err := ucard.List(database.DB(), where, nil, -1, -1)
- if err != nil {
- return
- }
- rids := make([]int64, len(ulist))
- for i, v := range ulist {
- rids[i] = v.ID
- }
- if len(rids) == 0 {
- return
- }
- p := dbmodel.TGateCard{}
- where = [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
- where = dbmodel.WhereAdd(where, "record_id in", rids)
- err = p.Delete(database.DB(), where)
- if err != nil {
- logger.Error("func",
- zap.String("call", "whiteResultStatusDeleted "),
- zap.String("error", err.Error()))
- }
- for _, v := range rids {
- gate_card.CheckUserCardDel(v, database.DB())
- }
- }
- func whiteResultHandle(req *pb_v1.GateCommandResultRequest, p *dbmodel.TGateCommand, gateInfo *dbmodel.TGate) {
- params := []gate_utils.WhiteParams{}
- _ = json.Unmarshal([]byte(p.Param), ¶ms)
- resultParam := CodeGateQueryCmdPostDataBody{}
- _ = json.Unmarshal([]byte(req.Content), &resultParam)
- added := []string{}
- deled := []string{}
- addedOrigin := []string{}
- for _, v := range params {
- if v.DeleteFlag != "1" {
- addedOrigin = append(addedOrigin, v.CodeVal)
- }
- }
- if array, ok := resultParam.CmdParams["Exists"]; ok {
- for _, m := range array {
- if m["DeleteFlag"] == "1" {
- deled = append(deled, m["CodeVal"])
- } else {
- added = append(added, m["CodeVal"])
- }
- }
- }
- whiteResultStatusUsed(gateInfo, added, addedOrigin)
- whiteResultStatusDeleted(gateInfo, deled)
- }
- func CommandResultSaiboHttpV1(req *pb_v1.GateCommandResultRequest, gateInfo *dbmodel.TGate) error {
- if err := checkGateCommandResultParamSaibohttpv1(req); err != nil {
- return err
- }
- 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 p.Code == gate_utils.QueryCommand {
- whiteResultHandle(req, p, gateInfo)
- }
- if p.Code != gate_utils.OpenCommand && p.Code != gate_utils.RebootCommand {
- p.Delete(database.DB(), where)
- }
- return nil
- }
|