123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- 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
- }
|