package gate_pic 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 PicYufanHttpv1Whiter struct { Up *dbmodel.TUserPic Device *dbmodel.TGate Gp *dbmodel.TGatePic PersonAddSuc bool ByIp bool command bool } func whitePicSetFail(up *dbmodel.TUserPic, device *dbmodel.TGate, status int32, msg string) { gp := dbmodel.TGatePic{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "device_id", device.ID) where = dbmodel.WhereAdd(where, "record_id", up.ID) values := map[string]interface{}{"status": status, "msg": msg, "updated_at": time.Now()} gp.Update(database.DB(), where, values) upi := dbmodel.TUserPic{} where = [][2]interface{}{} where = dbmodel.WhereAdd(where, "id", up.ID) values = map[string]interface{}{"down_status": 3} upi.Update(database.DB(), where, nil) } func whitePicPersonSuccess(up *dbmodel.TUserPic, device *dbmodel.TGate, status int32, faceId string) { gp := dbmodel.TGatePic{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "device_id", device.ID) where = dbmodel.WhereAdd(where, "record_id", up.ID) values := map[string]interface{}{"status": status, "updated_at": time.Now()} if faceId != "" { values["face_id"] = faceId } gp.Update(database.DB(), where, values) } func (p *PicYufanHttpv1Whiter) PersonAdd() { if p.ByIp { p.PersonAddByIp() return } task := gate_utils.PersonRegesterTask{ TaskNo: "", InterfaceName: gate_utils.PersonRegersterTaskName, Result: true, Person: gate_utils.PersonRegesterData{ Id: p.Up.Uid, Name: p.Up.Name, IdCardNum: "", //todo 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.Device.ID, 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 { whitePicSetFail(p.Up, p.Device, gate_utils.WhiteAddStatusPersonFail, err.Error()) return } p.command = true return } func (p *PicYufanHttpv1Whiter) PicAdd() { if p.ByIp { p.PicAddByIp() return } // 非ip时,收到人员添加任务执行结果后再执行添加照片任务(command模块里) return } func (p *PicYufanHttpv1Whiter) PicUpdate() { if p.ByIp { p.PicUpdateByIp() return } if p.Gp == nil { return } switch p.Gp.Status { case gate_utils.WhiteAddStatusWait: p.PersonAdd() p.PicAdd() return case gate_utils.WhiteAddStatusPersonSuc: break case gate_utils.WhiteAddStatusAllSuc: break case gate_utils.WhiteAddStatusPersonFail: p.PersonAdd() p.PicAdd() return case gate_utils.WhiteAddStatusPicFail: break } task := gate_utils.PhotoUpdateTask{ Result: true, TaskNo: "", InterfaceName: gate_utils.PhotoUpdateByUrlTaskName, } task.PersonId = p.Up.Uid task.Url = p.Up.PicUrl task.FaceId = p.Gp.FaceId bytes, _ := json.Marshal(task) now := time.Now() cmd := dbmodel.TGateCommand{ CreatedAt: now, UpdatedAt: now, DeviceId: p.Gp.DeviceId, Param: string(bytes), Desc: gate_utils.CommandCodeMap[gate_utils.AddPicCommand], Status: gate_utils.CommandStatusWait, ResultStatus: 0, ResultStatusDesc: "", Code: gate_utils.UpdatePicCommand, } err := cmd.Insert(database.DB()) if err != nil { whitePicSetFail(p.Up, p.Device, gate_utils.WhiteAddStatusPicFail, err.Error()) return } p.command = true return } func (p *PicYufanHttpv1Whiter) PersonDel() { if p.ByIp { p.PersonAddByIp() return } task := gate_utils.PersonDelTask{ Result: true, TaskNo: "", InterfaceName: gate_utils.PersonDelTaskName, } task.Id = p.Up.Uid bytes, _ := json.Marshal(task) now := time.Now() cmd := dbmodel.TGateCommand{ CreatedAt: now, UpdatedAt: now, DeviceId: p.Gp.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 *PicYufanHttpv1Whiter) PersonAddByIp() { personReq := gate_utils.PersonRegesterData{ Id: p.Up.Uid, Name: p.Up.Name, IdCardNum: "", //todo 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 { whitePicSetFail(p.Up, 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" { whitePicPersonSuccess(p.Up, p.Device, gate_utils.WhiteAddStatusPersonSuc, "") } else { whitePicSetFail(p.Up, p.Device, gate_utils.WhiteAddStatusPersonFail, fmt.Sprintf("%s-%s", code, msg)) return } p.PersonAddSuc = true return } func (p *PicYufanHttpv1Whiter) PicAddByIp() { if !p.PersonAddSuc { return } body := map[string]interface{}{ "pass": p.Device.Password, "imgUrl": p.Up.PicUrl, "personId": p.Up.Uid, "faceId": p.Up.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/face/createByUrl", p.Device.Ip, p.Device.Port), } bytes, err := h.Request() if err != nil { whitePicSetFail(p.Up, p.Device, gate_utils.WhiteAddStatusPicFail, err.Error()) return } code := gjson.GetBytes(bytes, "code").String() msg := gjson.GetBytes(bytes, "msg").String() if code == "LAN_SUS-0" || code == "LAN_EXP-4007" { whitePicPersonSuccess(p.Up, p.Device, gate_utils.WhiteAddStatusAllSuc, p.Up.Uid) } else { whitePicSetFail(p.Up, p.Device, gate_utils.WhiteAddStatusPicFail, fmt.Sprintf("%s-%s", code, msg)) return } return } func (p *PicYufanHttpv1Whiter) PicUpdateByIp() { if p.Gp == nil { return } switch p.Gp.Status { case gate_utils.WhiteAddStatusWait: p.PersonAddByIp() p.PicAddByIp() return case gate_utils.WhiteAddStatusPersonSuc: p.PersonAddSuc = true p.PicAddByIp() return case gate_utils.WhiteAddStatusAllSuc: p.PersonAddSuc = true break case gate_utils.WhiteAddStatusPersonFail: p.PersonAddByIp() p.PicAddByIp() return case gate_utils.WhiteAddStatusPicFail: p.PersonAddSuc = true p.PicAddByIp() return } body := map[string]interface{}{ "pass": p.Device.Password, "personId": p.Up.Uid, "faceId": p.Gp.FaceId, "url": p.Up.PicUrl, } bytes, _ := json.Marshal(body) h := gate_utils.HttpRequestWithHead{ Method: "PUT", Body: bytes, TimeOut: 20 * time.Second, Head: map[string]string{"Content-Type": "application/x-www-form-urlencoded"}, Url: fmt.Sprintf("http://%s:%d/face/", p.Device.Ip, p.Device.Port), } bytes, err := h.Request() if err != nil { return } code := gjson.GetBytes(bytes, "code").String() msg := gjson.GetBytes(bytes, "msg").String() if code == "LAN_SUS-0" { whitePicPersonSuccess(p.Up, p.Device, gate_utils.WhiteAddStatusAllSuc, p.Gp.FaceId) } else { whitePicSetFail(p.Up, p.Device, gate_utils.WhiteAddStatusPicFail, fmt.Sprintf("%s-%s", code, msg)) } } func (p *PicYufanHttpv1Whiter) PersonDelByIp() { body := map[string]interface{}{ "pass": p.Device.Password, "id": p.Up.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" || code == "LAN_EXP-3009" { gp := dbmodel.TGatePic{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "device_id", p.Device.ID) where = dbmodel.WhereAdd(where, "record_id", p.Up.ID) err = gp.Delete(database.DB(), where) if err != nil { return } CheckUserPicDel(p.Up.ID, database.DB()) } return } func (p *PicYufanHttpv1Whiter) Command() bool { return p.command }