command_result_yufan_httpv1.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. package gate_command
  2. import (
  3. "encoding/json"
  4. "git.getensh.com/common/gopkgs/database"
  5. "github.com/tidwall/gjson"
  6. "google.golang.org/grpc/status"
  7. "gorm.io/gorm"
  8. "property-device/errors"
  9. "property-device/impl/v1/gate_card"
  10. "property-device/impl/v1/gate_pic"
  11. dbmodel "property-device/model"
  12. pb_v1 "property-device/pb/v1"
  13. "property-device/utils/gate_utils"
  14. "strings"
  15. "time"
  16. )
  17. /*
  18. {
  19. " deviceKey ":"84E0F4200CCB02FA",
  20. "taskNo":"TASK-001",
  21. "result":{
  22. "data":{
  23. "createTime":1551772471550,
  24. "faceAndCardPermission":2,
  25. "facePermission":2,
  26. "iDPermission ":2,
  27. "id ":"52e328 fc3cba43de81937cab8ddcc6cc ",
  28. "idCardPermission ":2,
  29. "idcardNum ":"511323199204250569 ",
  30. "name ":"紫微 1 "
  31. },
  32. "msg ":"人员信息添加成功 ",
  33. "result ":1,
  34. "success ":true
  35. }
  36. }
  37. */
  38. func checkGateCommandResultParamYufanhttpv1(req *pb_v1.GateCommandResultRequest) error {
  39. switch {
  40. case req.Id == 0:
  41. return status.Error(10003, "命令id不能为空")
  42. case req.Sn == "" || req.Protocol == 0:
  43. return status.Error(10003, "设备id不能为空")
  44. }
  45. return nil
  46. }
  47. // 添加人员
  48. func yufanDownPersonCommandResult(req *pb_v1.GateCommandResultRequest, param *gate_utils.PersonRegesterTask, gateInfo *dbmodel.TGate) error {
  49. values := map[string]interface{}{
  50. "status": gate_utils.WhiteAddStatusPersonSuc,
  51. "updated_at": time.Now(),
  52. "msg": "",
  53. }
  54. if req.ResultStatus != 1 {
  55. values["status"] = gate_utils.WhiteAddStatusPersonFail
  56. values["msg"] = gjson.GetBytes([]byte(req.Content), "result").Get("msg").String()
  57. }
  58. u := dbmodel.TUserPic{}
  59. where := [][2]interface{}{}
  60. uid := param.Person.Id
  61. where = dbmodel.WhereAdd(where, "garden_id", gateInfo.GardenId)
  62. where = dbmodel.WhereAdd(where, "uid", uid)
  63. err := u.Find(database.DB(), where)
  64. if err != nil && err != gorm.ErrRecordNotFound {
  65. return errors.DataBaseError
  66. }
  67. if u.ID == 0 {
  68. return errors.ErrRecordNotFound
  69. }
  70. pic := dbmodel.TGatePic{}
  71. where = [][2]interface{}{}
  72. where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
  73. where = dbmodel.WhereAdd(where, "record_id", u.ID)
  74. err = pic.Find(database.DB(), where)
  75. if err != nil && err != gorm.ErrRecordNotFound {
  76. return errors.DataBaseError
  77. }
  78. if pic.ID == 0 {
  79. return errors.ErrRecordNotFound
  80. }
  81. err = pic.Update(database.DB(), where, values)
  82. if err != nil {
  83. return errors.DataBaseError
  84. }
  85. // 人员添加成功后添加照片
  86. if req.ResultStatus == 1 {
  87. if pic.FaceId == "" {
  88. task := gate_utils.TaskPhotoRegister{
  89. PersonId: u.Uid,
  90. Result: true,
  91. TaskNo: "",
  92. InterfaceName: gate_utils.PhotoRegisterByUrlTaskName,
  93. ImgUrl: u.PicUrl,
  94. FaceId: u.Uid,
  95. }
  96. bytes, _ := json.Marshal(task)
  97. now := time.Now()
  98. cmd := dbmodel.TGateCommand{
  99. CreatedAt: now,
  100. UpdatedAt: now,
  101. DeviceId: gateInfo.ID,
  102. Param: string(bytes),
  103. Desc: gate_utils.CommandCodeMap[gate_utils.AddPicCommand],
  104. Status: gate_utils.CommandStatusWait,
  105. ResultStatus: 0,
  106. ResultStatusDesc: "",
  107. Code: gate_utils.AddPicCommand,
  108. }
  109. err = cmd.Insert(database.DB())
  110. if err == nil {
  111. gate_utils.CommandCacheIncrease(gateInfo.Sn, gateInfo.Protocol)
  112. }
  113. } else {
  114. task := gate_utils.PhotoUpdateTask{
  115. Result: true,
  116. TaskNo: "",
  117. InterfaceName: gate_utils.PhotoUpdateByUrlTaskName,
  118. }
  119. task.PersonId = u.Uid
  120. task.Url = u.PicUrl
  121. task.FaceId = pic.FaceId
  122. bytes, _ := json.Marshal(task)
  123. now := time.Now()
  124. cmd := dbmodel.TGateCommand{
  125. CreatedAt: now,
  126. UpdatedAt: now,
  127. DeviceId: gateInfo.ID,
  128. Param: string(bytes),
  129. Desc: gate_utils.CommandCodeMap[gate_utils.AddPicCommand],
  130. Status: gate_utils.CommandStatusWait,
  131. ResultStatus: 0,
  132. ResultStatusDesc: "",
  133. Code: gate_utils.UpdatePicCommand,
  134. }
  135. err = cmd.Insert(database.DB())
  136. if err == nil {
  137. gate_utils.CommandCacheIncrease(gateInfo.Sn, gateInfo.Protocol)
  138. }
  139. }
  140. }
  141. return nil
  142. }
  143. // 照片更新
  144. func yufanPicUpdateCommandResult(req *pb_v1.GateCommandResultRequest, param *gate_utils.PhotoUpdateTask, gateInfo *dbmodel.TGate) error {
  145. values := map[string]interface{}{
  146. "status": gate_utils.WhiteAddStatusAllSuc,
  147. "updated_at": time.Now(),
  148. "msg": "",
  149. }
  150. if req.ResultStatus != 1 {
  151. values["status"] = gate_utils.WhiteAddStatusPicFail
  152. values["msg"] = gjson.GetBytes([]byte(req.Content), "result").Get("msg").String()
  153. }
  154. u := dbmodel.TUserPic{}
  155. where := [][2]interface{}{}
  156. uid := param.PersonId
  157. where = dbmodel.WhereAdd(where, "garden_id", gateInfo.GardenId)
  158. where = dbmodel.WhereAdd(where, "uid", uid)
  159. err := u.Find(database.DB(), where)
  160. if err != nil && err != gorm.ErrRecordNotFound {
  161. return errors.DataBaseError
  162. }
  163. if u.ID == 0 {
  164. return errors.ErrRecordNotFound
  165. }
  166. pic := dbmodel.TGatePic{}
  167. where = [][2]interface{}{}
  168. where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
  169. where = dbmodel.WhereAdd(where, "record_id", u.ID)
  170. err = pic.Update(database.DB(), where, values)
  171. if err != nil {
  172. return errors.DataBaseError
  173. }
  174. return nil
  175. }
  176. // 人员添加照片
  177. func yufanAddPicCommandResult(req *pb_v1.GateCommandResultRequest, param *gate_utils.TaskPhotoRegister, gateInfo *dbmodel.TGate) error {
  178. values := map[string]interface{}{
  179. "status": gate_utils.WhiteAddStatusAllSuc,
  180. "updated_at": time.Now(),
  181. "msg": "",
  182. }
  183. if req.ResultStatus != 1 {
  184. values["status"] = gate_utils.WhiteAddStatusPicFail
  185. values["msg"] = gjson.GetBytes([]byte(req.Content), "result").Get("msg").String()
  186. } else {
  187. faceId := gjson.GetBytes([]byte(req.Content), "result").Get("data").String()
  188. values["face_id"] = faceId
  189. }
  190. u := dbmodel.TUserPic{}
  191. where := [][2]interface{}{}
  192. uid := param.PersonId
  193. where = dbmodel.WhereAdd(where, "garden_id", gateInfo.GardenId)
  194. where = dbmodel.WhereAdd(where, "uid", uid)
  195. err := u.Find(database.DB(), where)
  196. if err != nil && err != gorm.ErrRecordNotFound {
  197. return errors.DataBaseError
  198. }
  199. if u.ID == 0 {
  200. return errors.ErrRecordNotFound
  201. }
  202. pic := dbmodel.TGatePic{}
  203. where = [][2]interface{}{}
  204. where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
  205. where = dbmodel.WhereAdd(where, "record_id", u.ID)
  206. err = pic.Update(database.DB(), where, values)
  207. if err != nil {
  208. return errors.DataBaseError
  209. }
  210. updateUserPicDownStatus(&u, req.ResultStatus == 1)
  211. return nil
  212. }
  213. // 添加卡号
  214. func yufanDownCardCommandResult(req *pb_v1.GateCommandResultRequest, param *gate_utils.PersonRegesterTask, gateInfo *dbmodel.TGate) error {
  215. values := map[string]interface{}{
  216. "status": gate_utils.WhiteAddStatusAllSuc,
  217. "updated_at": time.Now(),
  218. "msg": "",
  219. }
  220. if req.ResultStatus != 1 {
  221. values["status"] = gate_utils.WhiteAddStatusPicFail
  222. values["msg"] = gjson.GetBytes([]byte(req.Content), "result").Get("msg").String()
  223. }
  224. u := dbmodel.TUserCard{}
  225. where := [][2]interface{}{}
  226. cardNumber := param.Person.IdCardNum
  227. where = dbmodel.WhereAdd(where, "garden_id", gateInfo.GardenId)
  228. where = dbmodel.WhereAdd(where, "card_number", cardNumber)
  229. err := u.Find(database.DB(), where)
  230. if err != nil && err != gorm.ErrRecordNotFound {
  231. return errors.DataBaseError
  232. }
  233. if u.ID == 0 {
  234. return errors.ErrRecordNotFound
  235. }
  236. pic := dbmodel.TGateCard{}
  237. where = [][2]interface{}{}
  238. where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
  239. where = dbmodel.WhereAdd(where, "record_id", u.ID)
  240. err = pic.Update(database.DB(), where, values)
  241. if err != nil {
  242. return errors.DataBaseError
  243. }
  244. updateUserCardDownStatus(&u, req.ResultStatus == 1)
  245. return nil
  246. }
  247. // 删除人员
  248. func yufanDelPersonCommandResult(req *pb_v1.GateCommandResultRequest, param *gate_utils.PersonDelTask, gateInfo *dbmodel.TGate) error {
  249. if req.ResultStatus != 1 {
  250. return nil
  251. }
  252. u := dbmodel.TUserPic{}
  253. where := [][2]interface{}{}
  254. uids := strings.Split(param.Id, ",")
  255. if len(uids) == 0 {
  256. return nil
  257. }
  258. where = dbmodel.WhereAdd(where, "garden_id", gateInfo.GardenId)
  259. where = dbmodel.WhereAdd(where, "uid in", uids)
  260. ulist, err := u.List(database.DB(), where, nil, -1, -1)
  261. if err != nil {
  262. return errors.DataBaseError
  263. }
  264. recordIds := make([]int64, len(ulist))
  265. for i, v := range ulist {
  266. recordIds[i] = v.ID
  267. }
  268. if len(recordIds) == 0 {
  269. return nil
  270. }
  271. pic := dbmodel.TGatePic{}
  272. where = [][2]interface{}{}
  273. where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
  274. where = dbmodel.WhereAdd(where, "record_id in", recordIds)
  275. err = pic.Delete(database.DB(), where)
  276. if err != nil {
  277. return errors.DataBaseError
  278. }
  279. for _, v := range recordIds {
  280. gate_pic.CheckUserPicDel(v, database.DB())
  281. }
  282. return nil
  283. }
  284. // 删除卡号
  285. func yufanDelCardCommandResult(req *pb_v1.GateCommandResultRequest, param *gate_utils.PersonDelTask, gateInfo *dbmodel.TGate) error {
  286. if req.ResultStatus != 1 {
  287. return nil
  288. }
  289. u := dbmodel.TUserCard{}
  290. where := [][2]interface{}{}
  291. uids := strings.Split(param.Id, ",")
  292. if len(uids) == 0 {
  293. return nil
  294. }
  295. where = dbmodel.WhereAdd(where, "garden_id", gateInfo.GardenId)
  296. where = dbmodel.WhereAdd(where, "uid in", uids)
  297. ulist, err := u.List(database.DB(), where, nil, -1, -1)
  298. if err != nil {
  299. return errors.DataBaseError
  300. }
  301. recordIds := make([]int64, len(ulist))
  302. for i, v := range ulist {
  303. recordIds[i] = v.ID
  304. }
  305. if len(recordIds) == 0 {
  306. return nil
  307. }
  308. pic := dbmodel.TGateCard{}
  309. where = [][2]interface{}{}
  310. where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
  311. where = dbmodel.WhereAdd(where, "record_id in", recordIds)
  312. err = pic.Delete(database.DB(), where)
  313. if err != nil {
  314. return errors.DataBaseError
  315. }
  316. for _, v := range recordIds {
  317. gate_card.CheckUserCardDel(v, database.DB())
  318. }
  319. return nil
  320. }
  321. func CommandResultYufanHttpV1(req *pb_v1.GateCommandResultRequest, gateInfo *dbmodel.TGate) error {
  322. if err := checkGateCommandResultParamYufanhttpv1(req); err != nil {
  323. return err
  324. }
  325. p := &dbmodel.TGateCommand{}
  326. where := [][2]interface{}{}
  327. where = dbmodel.WhereAdd(where, "id", req.Id)
  328. where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
  329. err := p.Find(database.DB(), where)
  330. if err != nil && err != gorm.ErrRecordNotFound {
  331. return errors.DataBaseError
  332. }
  333. if p.ID == 0 {
  334. return errors.ErrRecordNotFound
  335. }
  336. now := time.Now()
  337. values := map[string]interface{}{
  338. "status": gate_utils.CommandStatusOver,
  339. "result_status": req.ResultStatus,
  340. "result_status_desc": req.ResultDesc,
  341. "updated_at": now,
  342. }
  343. db := database.DB()
  344. err = p.Update(db, where, values)
  345. if err != nil {
  346. return errors.DataBaseError
  347. }
  348. switch p.Code {
  349. case gate_utils.DownCommand:
  350. param := gate_utils.PersonRegesterTask{}
  351. json.Unmarshal([]byte(p.Param), &param)
  352. if strings.Contains(param.Person.Id, "auto") {
  353. return yufanDownCardCommandResult(req, &param, gateInfo)
  354. }
  355. return yufanDownPersonCommandResult(req, &param, gateInfo)
  356. case gate_utils.DelCommand:
  357. param := gate_utils.PersonDelTask{}
  358. json.Unmarshal([]byte(p.Param), &param)
  359. if param.Id == "" {
  360. return nil
  361. }
  362. if strings.Contains(param.Id, "auto") {
  363. return yufanDelCardCommandResult(req, &param, gateInfo)
  364. }
  365. return yufanDelPersonCommandResult(req, &param, gateInfo)
  366. case gate_utils.UpdatePicCommand:
  367. param := gate_utils.PhotoUpdateTask{}
  368. json.Unmarshal([]byte(p.Param), &param)
  369. return yufanPicUpdateCommandResult(req, &param, gateInfo)
  370. case gate_utils.AddPicCommand:
  371. param := gate_utils.TaskPhotoRegister{}
  372. json.Unmarshal([]byte(p.Param), &param)
  373. return yufanAddPicCommandResult(req, &param, gateInfo)
  374. }
  375. if p.Code != gate_utils.OpenCommand && p.Code != gate_utils.RebootCommand {
  376. p.Delete(database.DB(), where)
  377. }
  378. return nil
  379. }