command_result_saibo_mqttv1.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. package gate_command
  2. import (
  3. "encoding/json"
  4. "git.getensh.com/common/gopkgs/database"
  5. "google.golang.org/grpc/status"
  6. "gorm.io/gorm"
  7. "property-device/errors"
  8. "property-device/impl/v1/gate_pic"
  9. dbmodel "property-device/model"
  10. pb_v1 "property-device/pb/v1"
  11. "property-device/utils/gate_utils"
  12. "strings"
  13. "time"
  14. )
  15. func checkGateCommandResultParamSaiboMqttv1(req *pb_v1.GateCommandResultRequest) error {
  16. switch {
  17. case req.Id == 0 && req.CmdCode != gate_utils.DelCommand && req.CmdCode != gate_utils.DownCommand:
  18. return status.Error(10003, "命令id不能为空")
  19. case req.Sn == "" || req.Protocol == 0:
  20. return status.Error(10003, "设备id不能为空")
  21. }
  22. return nil
  23. }
  24. func updateUserPicDownStatus(u *dbmodel.TUserPic, suc bool) {
  25. p := dbmodel.TUserPic{}
  26. if !suc {
  27. values := map[string]interface{}{
  28. "down_status": 3,
  29. }
  30. where := [][2]interface{}{}
  31. where = dbmodel.WhereAdd(where, "id", u.ID)
  32. _ = p.Update(database.DB(), where, values)
  33. return
  34. }
  35. gp := dbmodel.TGatePic{}
  36. where := [][2]interface{}{}
  37. where = dbmodel.WhereAdd(where, "record_id", u.ID)
  38. where = dbmodel.WhereAdd(where, "status !=", gate_utils.WhiteAddStatusAllSuc)
  39. count, err := gp.Count(database.DB(), where, nil)
  40. if err != nil {
  41. return
  42. }
  43. if count > 0 {
  44. return
  45. }
  46. values := map[string]interface{}{
  47. "down_status": 2,
  48. }
  49. where = [][2]interface{}{}
  50. where = dbmodel.WhereAdd(where, "id", u.ID)
  51. _ = p.Update(database.DB(), where, values)
  52. }
  53. func updateUserCardDownStatus(u *dbmodel.TUserCard, suc bool) {
  54. p := dbmodel.TUserCard{}
  55. if !suc {
  56. values := map[string]interface{}{
  57. "down_status": 3,
  58. }
  59. where := [][2]interface{}{}
  60. where = dbmodel.WhereAdd(where, "id", u.ID)
  61. _ = p.Update(database.DB(), where, values)
  62. return
  63. }
  64. gp := dbmodel.TGateCard{}
  65. where := [][2]interface{}{}
  66. where = dbmodel.WhereAdd(where, "record_id", u.ID)
  67. where = dbmodel.WhereAdd(where, "status !=", gate_utils.WhiteAddStatusAllSuc)
  68. count, err := gp.Count(database.DB(), where, nil)
  69. if err != nil {
  70. return
  71. }
  72. if count > 0 {
  73. return
  74. }
  75. values := map[string]interface{}{
  76. "down_status": 2,
  77. }
  78. where = [][2]interface{}{}
  79. where = dbmodel.WhereAdd(where, "id", u.ID)
  80. _ = p.Update(database.DB(), where, values)
  81. }
  82. func downCommandPicHandle(req *pb_v1.GateCommandResultRequest, gateInfo *dbmodel.TGate, res *gate_utils.EditPersonsNewRes) error {
  83. u := dbmodel.TUserPic{}
  84. where := [][2]interface{}{}
  85. if len(res.Info.AddErrInfo) > 0 {
  86. uid := res.Info.AddErrInfo[0].CustomId
  87. where = dbmodel.WhereAdd(where, "garden_id", gateInfo.GardenId)
  88. where = dbmodel.WhereAdd(where, "uid", uid)
  89. err := u.Find(database.DB(), where)
  90. if err != nil && err != gorm.ErrRecordNotFound {
  91. return errors.DataBaseError
  92. }
  93. if u.ID == 0 {
  94. return errors.ErrRecordNotFound
  95. }
  96. pic := dbmodel.TGatePic{}
  97. where = [][2]interface{}{}
  98. where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
  99. where = dbmodel.WhereAdd(where, "record_id", u.ID)
  100. values := map[string]interface{}{
  101. "status": gate_utils.WhiteAddStatusPicFail,
  102. "msg": res.Info.AddErrInfo[0].ErrCode,
  103. }
  104. err = pic.Update(database.DB(), where, values)
  105. if err != nil {
  106. return errors.DataBaseError
  107. }
  108. updateUserPicDownStatus(&u, false)
  109. }
  110. if len(res.Info.AddSucInfo) > 0 {
  111. uid := res.Info.AddSucInfo[0].CustomId
  112. where = dbmodel.WhereAdd(where, "garden_id", gateInfo.GardenId)
  113. where = dbmodel.WhereAdd(where, "uid", uid)
  114. err := u.Find(database.DB(), where)
  115. if err != nil && err != gorm.ErrRecordNotFound {
  116. return errors.DataBaseError
  117. }
  118. if u.ID == 0 {
  119. return errors.ErrRecordNotFound
  120. }
  121. pic := dbmodel.TGatePic{}
  122. where = [][2]interface{}{}
  123. where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
  124. where = dbmodel.WhereAdd(where, "record_id", u.ID)
  125. values := map[string]interface{}{
  126. "status": gate_utils.WhiteAddStatusAllSuc,
  127. "msg": "",
  128. }
  129. err = pic.Update(database.DB(), where, values)
  130. if err != nil {
  131. return errors.DataBaseError
  132. }
  133. updateUserPicDownStatus(&u, true)
  134. }
  135. return nil
  136. }
  137. /*
  138. func downCommandCardHandle(req *pb_v1.GateCommandResultRequest, gateInfo *dbmodel.TGate, res *gate_utils.EditPersonsNewRes) error {
  139. if len(res.Info.AddErrInfo) > 0 {
  140. uid := res.Info.AddErrInfo[0].CustomId
  141. card := dbmodel.TGateCard{}
  142. where := [][2]interface{}{}
  143. where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
  144. where = dbmodel.WhereAdd(where, "uid", uid)
  145. values := map[string]interface{}{
  146. "status": gate_utils.WhiteAddStatusPersonFail,
  147. "msg": res.Info.AddErrInfo[0].ErrCode,
  148. }
  149. err := card.Update(database.DB(), where, values)
  150. if err != nil {
  151. return errors.DataBaseError
  152. }
  153. }
  154. if len(res.Info.AddSucInfo) > 0 {
  155. uid := res.Info.AddErrInfo[0].CustomId
  156. card := dbmodel.TGateCard{}
  157. where := [][2]interface{}{}
  158. where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
  159. where = dbmodel.WhereAdd(where, "uid", uid)
  160. values := map[string]interface{}{
  161. "status": gate_utils.WhiteAddStatusAllSuc,
  162. "msg": "",
  163. }
  164. err := card.Update(database.DB(), where, values)
  165. if err != nil {
  166. return errors.DataBaseError
  167. }
  168. }
  169. return nil
  170. }
  171. */
  172. func delCommandPicHandle(req *pb_v1.GateCommandResultRequest, gateInfo *dbmodel.TGate, res *gate_utils.DeletePersonsCmdRes) error {
  173. u := dbmodel.TUserPic{}
  174. where := [][2]interface{}{}
  175. if len(res.Info.DelSucInfo) > 0 {
  176. uids := make([]string, len(res.Info.DelSucInfo))
  177. for i, v := range res.Info.DelSucInfo {
  178. uids[i] = v.CustomId
  179. }
  180. where = dbmodel.WhereAdd(where, "garden_id", gateInfo.GardenId)
  181. where = dbmodel.WhereAdd(where, "uid in", uids)
  182. ulist, err := u.List(database.DB(), where, nil, -1, -1)
  183. if err != nil {
  184. return errors.DataBaseError
  185. }
  186. recordIds := make([]int64, len(ulist))
  187. for i, v := range ulist {
  188. recordIds[i] = v.ID
  189. }
  190. if len(recordIds) == 0 {
  191. return nil
  192. }
  193. pic := dbmodel.TGatePic{}
  194. where = [][2]interface{}{}
  195. where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
  196. where = dbmodel.WhereAdd(where, "record_id in", recordIds)
  197. err = pic.Delete(database.DB(), where)
  198. if err != nil {
  199. return errors.DataBaseError
  200. }
  201. for _, v := range recordIds {
  202. gate_pic.CheckUserPicDel(v, database.DB())
  203. }
  204. }
  205. return nil
  206. }
  207. /*
  208. func delCommandCardHandle(req *pb_v1.GateCommandResultRequest, gateInfo *dbmodel.TGate, res *gate_utils.DeletePersonsCmdRes) error {
  209. if len(res.Info.DelSucInfo) > 0 {
  210. uids := make([]string, len(res.Info.DelSucInfo))
  211. for i, v := range res.Info.DelSucInfo {
  212. uids[i] = v.CustomId
  213. }
  214. card := dbmodel.TGateCard{}
  215. where := [][2]interface{}{}
  216. where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
  217. where = dbmodel.WhereAdd(where, "uid in", uids)
  218. err := card.Delete(database.DB(), where)
  219. if err != nil {
  220. return errors.DataBaseError
  221. }
  222. }
  223. return nil
  224. }
  225. */
  226. func CommandResultSaiboMqttV1(req *pb_v1.GateCommandResultRequest, gateInfo *dbmodel.TGate) error {
  227. if err := checkGateCommandResultParamSaiboMqttv1(req); err != nil {
  228. return err
  229. }
  230. if req.CmdCode != gate_utils.DownCommand && req.CmdCode != gate_utils.DelCommand {
  231. p := &dbmodel.TGateCommand{}
  232. where := [][2]interface{}{}
  233. where = dbmodel.WhereAdd(where, "id", req.Id)
  234. where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
  235. err := p.Find(database.DB(), where)
  236. if err != nil && err != gorm.ErrRecordNotFound {
  237. return errors.DataBaseError
  238. }
  239. if p.ID == 0 {
  240. return errors.ErrRecordNotFound
  241. }
  242. now := time.Now()
  243. values := map[string]interface{}{
  244. "status": gate_utils.CommandStatusOver,
  245. "result_status": req.ResultStatus,
  246. "result_status_desc": req.ResultDesc,
  247. "updated_at": now,
  248. }
  249. db := database.DB()
  250. err = p.Update(db, where, values)
  251. if err != nil {
  252. return errors.DataBaseError
  253. }
  254. }
  255. // 处理白名单
  256. if req.CmdCode == gate_utils.DownCommand {
  257. res := gate_utils.EditPersonsNewRes{}
  258. json.Unmarshal([]byte(req.Content), &res)
  259. uid := ""
  260. if len(res.Info.AddSucInfo) > 0 {
  261. uid = res.Info.AddSucInfo[0].CustomId
  262. } else if len(res.Info.AddErrInfo) > 0 {
  263. uid = res.Info.AddErrInfo[0].CustomId
  264. }
  265. if uid == "" {
  266. return nil
  267. }
  268. if strings.Contains(uid, "auto") {
  269. return nil
  270. //return downCommandCardHandle(req, gateInfo, &res)
  271. }
  272. return downCommandPicHandle(req, gateInfo, &res)
  273. }
  274. if req.CmdCode == gate_utils.DelCommand {
  275. res := gate_utils.DeletePersonsCmdRes{}
  276. json.Unmarshal([]byte(req.Content), &res)
  277. uid := ""
  278. if len(res.Info.DelSucInfo) > 0 {
  279. uid = res.Info.DelSucInfo[0].CustomId
  280. } else if len(res.Info.DelErrInfo) > 0 {
  281. uid = res.Info.DelErrInfo[0].CustomId
  282. }
  283. if uid == "" {
  284. return nil
  285. }
  286. if strings.Contains(uid, "auto") {
  287. return nil
  288. //return delCommandCardHandle(req, gateInfo, &res)
  289. }
  290. return delCommandPicHandle(req, gateInfo, &res)
  291. }
  292. return nil
  293. }