command_result_saibo_httpv1.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package gate_command
  2. import (
  3. "encoding/json"
  4. "git.getensh.com/common/gopkgs/database"
  5. "git.getensh.com/common/gopkgs/logger"
  6. "go.uber.org/zap"
  7. "google.golang.org/grpc/status"
  8. "gorm.io/gorm"
  9. "property-device/errors"
  10. "property-device/impl/v1/gate_card"
  11. dbmodel "property-device/model"
  12. pb_v1 "property-device/pb/v1"
  13. "property-device/utils/gate_utils"
  14. "time"
  15. )
  16. func checkGateCommandResultParamSaibohttpv1(req *pb_v1.GateCommandResultRequest) error {
  17. switch {
  18. case req.Id == 0:
  19. return status.Error(10003, "命令id不能为空")
  20. case req.Sn == "" || req.Protocol == 0:
  21. return status.Error(10003, "设备id不能为空")
  22. }
  23. return nil
  24. }
  25. type CodeGateQueryCmdPostDataBody struct {
  26. ViewId string `form:"ViewId" json:"ViewId"`
  27. UID string `form:"UID" json:"UID"`
  28. UKey string `form:"UKey" json:"UKey"`
  29. SN string `form:"SN" json:"SN"`
  30. CmdID string `form:"CmdID" json:"CmdID"`
  31. CmdCode int `form:"CmdCode" json:"CmdCode"`
  32. CmdParams map[string][]map[string]string `form:"CmdParams" json:"CmdParams"`
  33. CmdStatus int `form:"CmdStatus" json:"CmdStatus"`
  34. }
  35. // 处理同步中的数据
  36. func whiteResultStatusUsed(gateInfo *dbmodel.TGate, exists []string, origin []string) {
  37. if len(exists) > 0 {
  38. ucard := dbmodel.TUserCard{}
  39. where := [][2]interface{}{}
  40. where = dbmodel.WhereAdd(where, "garden_id", gateInfo.GardenId)
  41. where = dbmodel.WhereAdd(where, "card_number in", exists)
  42. ulist, err := ucard.List(database.DB(), where, nil, -1, -1)
  43. if err != nil {
  44. return
  45. }
  46. rids := make([]int64, len(ulist))
  47. for i, v := range ulist {
  48. rids[i] = v.ID
  49. }
  50. if len(rids) == 0 {
  51. return
  52. }
  53. p := dbmodel.TGateCard{}
  54. where = [][2]interface{}{}
  55. where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
  56. where = dbmodel.WhereAdd(where, "record_id in", rids)
  57. values := map[string]interface{}{
  58. "status": gate_utils.WhiteAddStatusAllSuc,
  59. }
  60. err = p.Update(database.DB(), where, values)
  61. if err != nil {
  62. logger.Error("func",
  63. zap.String("call", "whiteResultStatusUsed "),
  64. zap.String("error", err.Error()))
  65. }
  66. for _, v := range ulist {
  67. u := v
  68. updateUserCardDownStatus(&u, true)
  69. }
  70. }
  71. // 添加失败的记录
  72. failed := []string{}
  73. if len(origin) > len(exists) {
  74. em := map[string]bool{}
  75. for _, v := range exists {
  76. em[v] = true
  77. }
  78. for _, v := range origin {
  79. if !em[v] {
  80. failed = append(failed, v)
  81. }
  82. }
  83. ucard := dbmodel.TUserCard{}
  84. where := [][2]interface{}{}
  85. where = dbmodel.WhereAdd(where, "garden_id", gateInfo.GardenId)
  86. where = dbmodel.WhereAdd(where, "card_number in", exists)
  87. ulist, err := ucard.List(database.DB(), where, nil, -1, -1)
  88. if err != nil {
  89. return
  90. }
  91. rids := make([]int64, len(ulist))
  92. for i, v := range ulist {
  93. rids[i] = v.ID
  94. }
  95. if len(rids) == 0 {
  96. return
  97. }
  98. p := dbmodel.TGateCard{}
  99. where = [][2]interface{}{}
  100. where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
  101. where = dbmodel.WhereAdd(where, "record_id in", rids)
  102. values := map[string]interface{}{
  103. "status": gate_utils.WhiteAddStatusPersonFail,
  104. }
  105. err = p.Update(database.DB(), where, values)
  106. if err != nil {
  107. logger.Error("func",
  108. zap.String("call", "whiteResultStatusUsed "),
  109. zap.String("error", err.Error()))
  110. }
  111. for _, v := range ulist {
  112. u := v
  113. updateUserCardDownStatus(&u, false)
  114. }
  115. }
  116. // todo 更新ucardstatus
  117. }
  118. // 处理删除中的数据
  119. func whiteResultStatusDeleted(gateInfo *dbmodel.TGate, notExist []string) {
  120. if len(notExist) == 0 {
  121. return
  122. }
  123. ucard := dbmodel.TUserCard{}
  124. where := [][2]interface{}{}
  125. where = dbmodel.WhereAdd(where, "garden_id", gateInfo.GardenId)
  126. where = dbmodel.WhereAdd(where, "card_number in", notExist)
  127. ulist, err := ucard.List(database.DB(), where, nil, -1, -1)
  128. if err != nil {
  129. return
  130. }
  131. rids := make([]int64, len(ulist))
  132. for i, v := range ulist {
  133. rids[i] = v.ID
  134. }
  135. if len(rids) == 0 {
  136. return
  137. }
  138. p := dbmodel.TGateCard{}
  139. where = [][2]interface{}{}
  140. where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
  141. where = dbmodel.WhereAdd(where, "record_id in", rids)
  142. err = p.Delete(database.DB(), where)
  143. if err != nil {
  144. logger.Error("func",
  145. zap.String("call", "whiteResultStatusDeleted "),
  146. zap.String("error", err.Error()))
  147. }
  148. for _, v := range rids {
  149. gate_card.CheckUserCardDel(v, database.DB())
  150. }
  151. }
  152. func whiteResultHandle(req *pb_v1.GateCommandResultRequest, p *dbmodel.TGateCommand, gateInfo *dbmodel.TGate) {
  153. params := []gate_utils.WhiteParams{}
  154. _ = json.Unmarshal([]byte(p.Param), &params)
  155. resultParam := CodeGateQueryCmdPostDataBody{}
  156. _ = json.Unmarshal([]byte(req.Content), &resultParam)
  157. added := []string{}
  158. deled := []string{}
  159. addedOrigin := []string{}
  160. for _, v := range params {
  161. if v.DeleteFlag != "1" {
  162. addedOrigin = append(addedOrigin, v.CodeVal)
  163. }
  164. }
  165. if array, ok := resultParam.CmdParams["Exists"]; ok {
  166. for _, m := range array {
  167. if m["DeleteFlag"] == "1" {
  168. deled = append(deled, m["CodeVal"])
  169. } else {
  170. added = append(added, m["CodeVal"])
  171. }
  172. }
  173. }
  174. whiteResultStatusUsed(gateInfo, added, addedOrigin)
  175. whiteResultStatusDeleted(gateInfo, deled)
  176. }
  177. func CommandResultSaiboHttpV1(req *pb_v1.GateCommandResultRequest, gateInfo *dbmodel.TGate) error {
  178. if err := checkGateCommandResultParamSaibohttpv1(req); err != nil {
  179. return err
  180. }
  181. p := &dbmodel.TGateCommand{}
  182. where := [][2]interface{}{}
  183. where = dbmodel.WhereAdd(where, "id", req.Id)
  184. where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
  185. err := p.Find(database.DB(), where)
  186. if err != nil && err != gorm.ErrRecordNotFound {
  187. return errors.DataBaseError
  188. }
  189. if p.ID == 0 {
  190. return errors.ErrRecordNotFound
  191. }
  192. now := time.Now()
  193. values := map[string]interface{}{
  194. "status": gate_utils.CommandStatusOver,
  195. "result_status": req.ResultStatus,
  196. "result_status_desc": req.ResultDesc,
  197. "updated_at": now,
  198. }
  199. db := database.DB()
  200. err = p.Update(db, where, values)
  201. if err != nil {
  202. return errors.DataBaseError
  203. }
  204. // 处理白名单
  205. if p.Code == gate_utils.QueryCommand {
  206. whiteResultHandle(req, p, gateInfo)
  207. }
  208. if p.Code != gate_utils.OpenCommand && p.Code != gate_utils.RebootCommand {
  209. p.Delete(database.DB(), where)
  210. }
  211. return nil
  212. }