card_yufan_httpv1_whiter.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package gate_card
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "git.getensh.com/common/gopkgs/database"
  6. "github.com/tidwall/gjson"
  7. dbmodel "property-device/model"
  8. "property-device/utils/gate_utils"
  9. "time"
  10. )
  11. type CardYufanHttpv1Whiter struct {
  12. Gcard *dbmodel.TGateCard
  13. Device *dbmodel.TGate
  14. UCard *dbmodel.TUserCard
  15. command bool
  16. ByIp bool
  17. }
  18. func whiteCardSetFail(ucard *dbmodel.TUserCard, device *dbmodel.TGate, status int32, msg string) {
  19. gcard := dbmodel.TGateCard{}
  20. where := [][2]interface{}{}
  21. where = dbmodel.WhereAdd(where, "device_id", device.ID)
  22. where = dbmodel.WhereAdd(where, "record_id", ucard.ID)
  23. values := map[string]interface{}{"status": status, "msg": msg}
  24. gcard.Update(database.DB(), where, values)
  25. uc := dbmodel.TUserCard{}
  26. where = [][2]interface{}{}
  27. where = dbmodel.WhereAdd(where, "id", ucard.ID)
  28. values = map[string]interface{}{"down_status": 3}
  29. uc.Update(database.DB(), where, nil)
  30. }
  31. func whiteCardPersonSuccess(ucard *dbmodel.TUserCard, device *dbmodel.TGate, status int32) {
  32. gp := dbmodel.TGateCard{}
  33. where := [][2]interface{}{}
  34. where = dbmodel.WhereAdd(where, "device_id", device.ID)
  35. where = dbmodel.WhereAdd(where, "record_id", ucard.ID)
  36. values := map[string]interface{}{"status": status}
  37. gp.Update(database.DB(), where, values)
  38. }
  39. func (p *CardYufanHttpv1Whiter) PersonAdd() {
  40. if p.ByIp {
  41. p.PersonAddByIp()
  42. return
  43. }
  44. task := gate_utils.PersonRegesterTask{
  45. TaskNo: "",
  46. InterfaceName: gate_utils.PersonRegersterTaskName,
  47. Result: true,
  48. Person: gate_utils.PersonRegesterData{
  49. Id: p.UCard.Uid,
  50. Name: p.UCard.Name,
  51. IdCardNum: p.UCard.CardNumber,
  52. IdNumber: "",
  53. FacePermission: 2,
  54. IdCardPermission: 2,
  55. FaceAndCardPermission: 2,
  56. IDPermission: 1,
  57. Tag: "",
  58. Phone: "",
  59. },
  60. }
  61. bytes, _ := json.Marshal(task)
  62. now := time.Now()
  63. gcmd := &dbmodel.TGateCommand{
  64. CreatedAt: now,
  65. UpdatedAt: now,
  66. DeviceId: p.Gcard.DeviceId,
  67. Param: string(bytes),
  68. Desc: gate_utils.CommandCodeMap[gate_utils.DownCommand],
  69. Status: gate_utils.CommandStatusWait,
  70. ResultStatus: 0,
  71. ResultStatusDesc: "",
  72. Code: gate_utils.DownCommand,
  73. }
  74. if err := gcmd.Insert(database.DB()); err != nil {
  75. whiteCardSetFail(p.UCard, p.Device, gate_utils.WhiteAddStatusPersonFail, err.Error())
  76. return
  77. }
  78. p.command = true
  79. return
  80. }
  81. func (p *CardYufanHttpv1Whiter) PersonDel() {
  82. if p.ByIp {
  83. p.PersonDelByIp()
  84. return
  85. }
  86. task := gate_utils.PersonDelTask{
  87. Result: true,
  88. TaskNo: "",
  89. InterfaceName: gate_utils.PersonDelTaskName,
  90. }
  91. task.Id = p.UCard.Uid
  92. bytes, _ := json.Marshal(task)
  93. now := time.Now()
  94. cmd := dbmodel.TGateCommand{
  95. CreatedAt: now,
  96. UpdatedAt: now,
  97. DeviceId: p.Gcard.DeviceId,
  98. Param: string(bytes),
  99. Desc: gate_utils.CommandCodeMap[gate_utils.AddPicCommand],
  100. Status: gate_utils.CommandStatusWait,
  101. ResultStatus: 0,
  102. ResultStatusDesc: "",
  103. Code: gate_utils.DelCommand,
  104. }
  105. err := cmd.Insert(database.DB())
  106. if err != nil {
  107. return
  108. }
  109. p.command = true
  110. return
  111. }
  112. func (p *CardYufanHttpv1Whiter) PersonAddByIp() {
  113. personReq := gate_utils.PersonRegesterData{
  114. Id: p.UCard.Uid,
  115. Name: p.UCard.Name,
  116. IdCardNum: p.UCard.CardNumber,
  117. IdNumber: "",
  118. FacePermission: 2,
  119. IdCardPermission: 2,
  120. FaceAndCardPermission: 2,
  121. IDPermission: 1,
  122. Tag: "",
  123. Phone: "",
  124. }
  125. body := map[string]interface{}{
  126. "pass": p.Device.Password,
  127. "person": personReq,
  128. }
  129. bytes, _ := json.Marshal(body)
  130. h := gate_utils.HttpRequestWithHead{
  131. Method: "POST",
  132. Body: bytes,
  133. TimeOut: 20 * time.Second,
  134. Head: map[string]string{"Content-Type": "application/x-www-form-urlencoded"},
  135. Url: fmt.Sprintf("http://%s:%d/person/create", p.Device.Ip, p.Device.Port),
  136. }
  137. bytes, err := h.Request()
  138. if err != nil {
  139. whiteCardSetFail(p.UCard, p.Device, gate_utils.WhiteAddStatusPersonFail, err.Error())
  140. return
  141. }
  142. code := gjson.GetBytes(bytes, "code").String()
  143. msg := gjson.GetBytes(bytes, "msg").String()
  144. if code == "LAN_SUS-0" || code == "LAN_EXP-3005" {
  145. whiteCardPersonSuccess(p.UCard, p.Device, gate_utils.WhiteAddStatusPersonSuc)
  146. } else {
  147. whiteCardSetFail(p.UCard, p.Device, gate_utils.WhiteAddStatusPersonFail, fmt.Sprintf("%s-%s", code, msg))
  148. return
  149. }
  150. return
  151. }
  152. func (p *CardYufanHttpv1Whiter) PersonDelByIp() {
  153. body := map[string]interface{}{
  154. "pass": p.Device.Password,
  155. "id": p.UCard.Uid,
  156. }
  157. bytes, _ := json.Marshal(body)
  158. h := gate_utils.HttpRequestWithHead{
  159. Method: "POST",
  160. Body: bytes,
  161. TimeOut: 20 * time.Second,
  162. Head: map[string]string{"Content-Type": "application/x-www-form-urlencoded"},
  163. Url: fmt.Sprintf("http://%s:%d/person/delete", p.Device.Ip, p.Device.Port),
  164. }
  165. bytes, err := h.Request()
  166. if err != nil {
  167. return
  168. }
  169. code := gjson.GetBytes(bytes, "code").String()
  170. if code == "LAN_SUS-0" {
  171. gp := dbmodel.TGateCard{}
  172. where := [][2]interface{}{}
  173. where = dbmodel.WhereAdd(where, "id", p.Device.ID)
  174. where = dbmodel.WhereAdd(where, "record_id", p.UCard.ID)
  175. err = gp.Delete(database.DB(), where)
  176. if err != nil {
  177. return
  178. }
  179. CheckUserCardDel(p.UCard.ID, database.DB())
  180. }
  181. return
  182. }
  183. func (p *CardYufanHttpv1Whiter) Command() bool {
  184. return p.command
  185. }