package gate_card import ( "encoding/json" "fmt" "git.getensh.com/common/gopkgs/database" "github.com/tidwall/gjson" dbmodel "property-device/model" "property-device/utils/gate_utils" "time" ) type CardYufanHttpv1Whiter struct { Gcard *dbmodel.TGateCard Device *dbmodel.TGate UCard *dbmodel.TUserCard command bool ByIp bool } func whiteCardSetFail(ucard *dbmodel.TUserCard, device *dbmodel.TGate, status int32, msg string) { gcard := dbmodel.TGateCard{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "device_id", device.ID) where = dbmodel.WhereAdd(where, "record_id", ucard.ID) values := map[string]interface{}{"status": status, "msg": msg} gcard.Update(database.DB(), where, values) uc := dbmodel.TUserCard{} where = [][2]interface{}{} where = dbmodel.WhereAdd(where, "id", ucard.ID) values = map[string]interface{}{"down_status": 3} uc.Update(database.DB(), where, nil) } func whiteCardPersonSuccess(ucard *dbmodel.TUserCard, device *dbmodel.TGate, status int32) { gp := dbmodel.TGateCard{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "device_id", device.ID) where = dbmodel.WhereAdd(where, "record_id", ucard.ID) values := map[string]interface{}{"status": status} gp.Update(database.DB(), where, values) } func (p *CardYufanHttpv1Whiter) PersonAdd() { if p.ByIp { p.PersonAddByIp() return } task := gate_utils.PersonRegesterTask{ TaskNo: "", InterfaceName: gate_utils.PersonRegersterTaskName, Result: true, Person: gate_utils.PersonRegesterData{ Id: p.UCard.Uid, Name: p.UCard.Name, IdCardNum: p.UCard.CardNumber, IdNumber: "", FacePermission: 2, IdCardPermission: 2, FaceAndCardPermission: 2, IDPermission: 1, Tag: "", Phone: "", }, } bytes, _ := json.Marshal(task) now := time.Now() gcmd := &dbmodel.TGateCommand{ CreatedAt: now, UpdatedAt: now, DeviceId: p.Gcard.DeviceId, Param: string(bytes), Desc: gate_utils.CommandCodeMap[gate_utils.DownCommand], Status: gate_utils.CommandStatusWait, ResultStatus: 0, ResultStatusDesc: "", Code: gate_utils.DownCommand, } if err := gcmd.Insert(database.DB()); err != nil { whiteCardSetFail(p.UCard, p.Device, gate_utils.WhiteAddStatusPersonFail, err.Error()) return } p.command = true return } func (p *CardYufanHttpv1Whiter) PersonDel() { if p.ByIp { p.PersonDelByIp() return } task := gate_utils.PersonDelTask{ Result: true, TaskNo: "", InterfaceName: gate_utils.PersonDelTaskName, } task.Id = p.UCard.Uid bytes, _ := json.Marshal(task) now := time.Now() cmd := dbmodel.TGateCommand{ CreatedAt: now, UpdatedAt: now, DeviceId: p.Gcard.DeviceId, Param: string(bytes), Desc: gate_utils.CommandCodeMap[gate_utils.AddPicCommand], Status: gate_utils.CommandStatusWait, ResultStatus: 0, ResultStatusDesc: "", Code: gate_utils.DelCommand, } err := cmd.Insert(database.DB()) if err != nil { return } p.command = true return } func (p *CardYufanHttpv1Whiter) PersonAddByIp() { personReq := gate_utils.PersonRegesterData{ Id: p.UCard.Uid, Name: p.UCard.Name, IdCardNum: p.UCard.CardNumber, IdNumber: "", FacePermission: 2, IdCardPermission: 2, FaceAndCardPermission: 2, IDPermission: 1, Tag: "", Phone: "", } body := map[string]interface{}{ "pass": p.Device.Password, "person": personReq, } bytes, _ := json.Marshal(body) h := gate_utils.HttpRequestWithHead{ Method: "POST", Body: bytes, TimeOut: 20 * time.Second, Head: map[string]string{"Content-Type": "application/x-www-form-urlencoded"}, Url: fmt.Sprintf("http://%s:%d/person/create", p.Device.Ip, p.Device.Port), } bytes, err := h.Request() if err != nil { whiteCardSetFail(p.UCard, p.Device, gate_utils.WhiteAddStatusPersonFail, err.Error()) return } code := gjson.GetBytes(bytes, "code").String() msg := gjson.GetBytes(bytes, "msg").String() if code == "LAN_SUS-0" || code == "LAN_EXP-3005" { whiteCardPersonSuccess(p.UCard, p.Device, gate_utils.WhiteAddStatusPersonSuc) } else { whiteCardSetFail(p.UCard, p.Device, gate_utils.WhiteAddStatusPersonFail, fmt.Sprintf("%s-%s", code, msg)) return } return } func (p *CardYufanHttpv1Whiter) PersonDelByIp() { body := map[string]interface{}{ "pass": p.Device.Password, "id": p.UCard.Uid, } bytes, _ := json.Marshal(body) h := gate_utils.HttpRequestWithHead{ Method: "POST", Body: bytes, TimeOut: 20 * time.Second, Head: map[string]string{"Content-Type": "application/x-www-form-urlencoded"}, Url: fmt.Sprintf("http://%s:%d/person/delete", p.Device.Ip, p.Device.Port), } bytes, err := h.Request() if err != nil { return } code := gjson.GetBytes(bytes, "code").String() if code == "LAN_SUS-0" { gp := dbmodel.TGateCard{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "id", p.Device.ID) where = dbmodel.WhereAdd(where, "record_id", p.UCard.ID) err = gp.Delete(database.DB(), where) if err != nil { return } CheckUserCardDel(p.UCard.ID, database.DB()) } return } func (p *CardYufanHttpv1Whiter) Command() bool { return p.command }