123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418 |
- package gate_command
- import (
- "encoding/json"
- "git.getensh.com/common/gopkgs/database"
- "github.com/tidwall/gjson"
- "google.golang.org/grpc/status"
- "gorm.io/gorm"
- "property-device/errors"
- "property-device/impl/v1/gate_card"
- "property-device/impl/v1/gate_pic"
- dbmodel "property-device/model"
- pb_v1 "property-device/pb/v1"
- "property-device/utils/gate_utils"
- "strings"
- "time"
- )
- /*
- {
- " deviceKey ":"84E0F4200CCB02FA",
- "taskNo":"TASK-001",
- "result":{
- "data":{
- "createTime":1551772471550,
- "faceAndCardPermission":2,
- "facePermission":2,
- "iDPermission ":2,
- "id ":"52e328 fc3cba43de81937cab8ddcc6cc ",
- "idCardPermission ":2,
- "idcardNum ":"511323199204250569 ",
- "name ":"紫微 1 "
- },
- "msg ":"人员信息添加成功 ",
- "result ":1,
- "success ":true
- }
- }
- */
- func checkGateCommandResultParamYufanhttpv1(req *pb_v1.GateCommandResultRequest) error {
- switch {
- case req.Id == 0:
- return status.Error(10003, "命令id不能为空")
- case req.Sn == "" || req.Protocol == 0:
- return status.Error(10003, "设备id不能为空")
- }
- return nil
- }
- // 添加人员
- func yufanDownPersonCommandResult(req *pb_v1.GateCommandResultRequest, param *gate_utils.PersonRegesterTask, gateInfo *dbmodel.TGate) error {
- values := map[string]interface{}{
- "status": gate_utils.WhiteAddStatusPersonSuc,
- "updated_at": time.Now(),
- "msg": "",
- }
- if req.ResultStatus != 1 {
- values["status"] = gate_utils.WhiteAddStatusPersonFail
- values["msg"] = gjson.GetBytes([]byte(req.Content), "result").Get("msg").String()
- }
- u := dbmodel.TUserPic{}
- where := [][2]interface{}{}
- uid := param.Person.Id
- where = dbmodel.WhereAdd(where, "garden_id", gateInfo.GardenId)
- where = dbmodel.WhereAdd(where, "uid", uid)
- err := u.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return errors.DataBaseError
- }
- if u.ID == 0 {
- return errors.ErrRecordNotFound
- }
- pic := dbmodel.TGatePic{}
- where = [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
- where = dbmodel.WhereAdd(where, "record_id", u.ID)
- err = pic.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return errors.DataBaseError
- }
- if pic.ID == 0 {
- return errors.ErrRecordNotFound
- }
- err = pic.Update(database.DB(), where, values)
- if err != nil {
- return errors.DataBaseError
- }
- // 人员添加成功后添加照片
- if req.ResultStatus == 1 {
- if pic.FaceId == "" {
- task := gate_utils.TaskPhotoRegister{
- PersonId: u.Uid,
- Result: true,
- TaskNo: "",
- InterfaceName: gate_utils.PhotoRegisterByUrlTaskName,
- ImgUrl: u.PicUrl,
- FaceId: u.Uid,
- }
- bytes, _ := json.Marshal(task)
- now := time.Now()
- cmd := dbmodel.TGateCommand{
- CreatedAt: now,
- UpdatedAt: now,
- DeviceId: gateInfo.ID,
- Param: string(bytes),
- Desc: gate_utils.CommandCodeMap[gate_utils.AddPicCommand],
- Status: gate_utils.CommandStatusWait,
- ResultStatus: 0,
- ResultStatusDesc: "",
- Code: gate_utils.AddPicCommand,
- }
- err = cmd.Insert(database.DB())
- if err == nil {
- gate_utils.CommandCacheIncrease(gateInfo.Sn, gateInfo.Protocol)
- }
- } else {
- task := gate_utils.PhotoUpdateTask{
- Result: true,
- TaskNo: "",
- InterfaceName: gate_utils.PhotoUpdateByUrlTaskName,
- }
- task.PersonId = u.Uid
- task.Url = u.PicUrl
- task.FaceId = pic.FaceId
- bytes, _ := json.Marshal(task)
- now := time.Now()
- cmd := dbmodel.TGateCommand{
- CreatedAt: now,
- UpdatedAt: now,
- DeviceId: gateInfo.ID,
- 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 {
- gate_utils.CommandCacheIncrease(gateInfo.Sn, gateInfo.Protocol)
- }
- }
- }
- return nil
- }
- // 照片更新
- func yufanPicUpdateCommandResult(req *pb_v1.GateCommandResultRequest, param *gate_utils.PhotoUpdateTask, gateInfo *dbmodel.TGate) error {
- values := map[string]interface{}{
- "status": gate_utils.WhiteAddStatusAllSuc,
- "updated_at": time.Now(),
- "msg": "",
- }
- if req.ResultStatus != 1 {
- values["status"] = gate_utils.WhiteAddStatusPicFail
- values["msg"] = gjson.GetBytes([]byte(req.Content), "result").Get("msg").String()
- }
- u := dbmodel.TUserPic{}
- where := [][2]interface{}{}
- uid := param.PersonId
- where = dbmodel.WhereAdd(where, "garden_id", gateInfo.GardenId)
- where = dbmodel.WhereAdd(where, "uid", uid)
- err := u.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return errors.DataBaseError
- }
- if u.ID == 0 {
- return errors.ErrRecordNotFound
- }
- pic := dbmodel.TGatePic{}
- where = [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
- where = dbmodel.WhereAdd(where, "record_id", u.ID)
- err = pic.Update(database.DB(), where, values)
- if err != nil {
- return errors.DataBaseError
- }
- return nil
- }
- // 人员添加照片
- func yufanAddPicCommandResult(req *pb_v1.GateCommandResultRequest, param *gate_utils.TaskPhotoRegister, gateInfo *dbmodel.TGate) error {
- values := map[string]interface{}{
- "status": gate_utils.WhiteAddStatusAllSuc,
- "updated_at": time.Now(),
- "msg": "",
- }
- if req.ResultStatus != 1 {
- values["status"] = gate_utils.WhiteAddStatusPicFail
- values["msg"] = gjson.GetBytes([]byte(req.Content), "result").Get("msg").String()
- } else {
- faceId := gjson.GetBytes([]byte(req.Content), "result").Get("data").String()
- values["face_id"] = faceId
- }
- u := dbmodel.TUserPic{}
- where := [][2]interface{}{}
- uid := param.PersonId
- where = dbmodel.WhereAdd(where, "garden_id", gateInfo.GardenId)
- where = dbmodel.WhereAdd(where, "uid", uid)
- err := u.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return errors.DataBaseError
- }
- if u.ID == 0 {
- return errors.ErrRecordNotFound
- }
- pic := dbmodel.TGatePic{}
- where = [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
- where = dbmodel.WhereAdd(where, "record_id", u.ID)
- err = pic.Update(database.DB(), where, values)
- if err != nil {
- return errors.DataBaseError
- }
- updateUserPicDownStatus(&u, req.ResultStatus == 1)
- return nil
- }
- // 添加卡号
- func yufanDownCardCommandResult(req *pb_v1.GateCommandResultRequest, param *gate_utils.PersonRegesterTask, gateInfo *dbmodel.TGate) error {
- values := map[string]interface{}{
- "status": gate_utils.WhiteAddStatusAllSuc,
- "updated_at": time.Now(),
- "msg": "",
- }
- if req.ResultStatus != 1 {
- values["status"] = gate_utils.WhiteAddStatusPicFail
- values["msg"] = gjson.GetBytes([]byte(req.Content), "result").Get("msg").String()
- }
- u := dbmodel.TUserCard{}
- where := [][2]interface{}{}
- cardNumber := param.Person.IdCardNum
- where = dbmodel.WhereAdd(where, "garden_id", gateInfo.GardenId)
- where = dbmodel.WhereAdd(where, "card_number", cardNumber)
- err := u.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return errors.DataBaseError
- }
- if u.ID == 0 {
- return errors.ErrRecordNotFound
- }
- pic := dbmodel.TGateCard{}
- where = [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
- where = dbmodel.WhereAdd(where, "record_id", u.ID)
- err = pic.Update(database.DB(), where, values)
- if err != nil {
- return errors.DataBaseError
- }
- updateUserCardDownStatus(&u, req.ResultStatus == 1)
- return nil
- }
- // 删除人员
- func yufanDelPersonCommandResult(req *pb_v1.GateCommandResultRequest, param *gate_utils.PersonDelTask, gateInfo *dbmodel.TGate) error {
- if req.ResultStatus != 1 {
- return nil
- }
- u := dbmodel.TUserPic{}
- where := [][2]interface{}{}
- uids := strings.Split(param.Id, ",")
- if len(uids) == 0 {
- return nil
- }
- where = dbmodel.WhereAdd(where, "garden_id", gateInfo.GardenId)
- where = dbmodel.WhereAdd(where, "uid in", uids)
- ulist, err := u.List(database.DB(), where, nil, -1, -1)
- if err != nil {
- return errors.DataBaseError
- }
- recordIds := make([]int64, len(ulist))
- for i, v := range ulist {
- recordIds[i] = v.ID
- }
- if len(recordIds) == 0 {
- return nil
- }
- pic := dbmodel.TGatePic{}
- where = [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
- where = dbmodel.WhereAdd(where, "record_id in", recordIds)
- err = pic.Delete(database.DB(), where)
- if err != nil {
- return errors.DataBaseError
- }
- for _, v := range recordIds {
- gate_pic.CheckUserPicDel(v, database.DB())
- }
- return nil
- }
- // 删除卡号
- func yufanDelCardCommandResult(req *pb_v1.GateCommandResultRequest, param *gate_utils.PersonDelTask, gateInfo *dbmodel.TGate) error {
- if req.ResultStatus != 1 {
- return nil
- }
- u := dbmodel.TUserCard{}
- where := [][2]interface{}{}
- uids := strings.Split(param.Id, ",")
- if len(uids) == 0 {
- return nil
- }
- where = dbmodel.WhereAdd(where, "garden_id", gateInfo.GardenId)
- where = dbmodel.WhereAdd(where, "uid in", uids)
- ulist, err := u.List(database.DB(), where, nil, -1, -1)
- if err != nil {
- return errors.DataBaseError
- }
- recordIds := make([]int64, len(ulist))
- for i, v := range ulist {
- recordIds[i] = v.ID
- }
- if len(recordIds) == 0 {
- return nil
- }
- pic := dbmodel.TGateCard{}
- where = [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
- where = dbmodel.WhereAdd(where, "record_id in", recordIds)
- err = pic.Delete(database.DB(), where)
- if err != nil {
- return errors.DataBaseError
- }
- for _, v := range recordIds {
- gate_card.CheckUserCardDel(v, database.DB())
- }
- return nil
- }
- func CommandResultYufanHttpV1(req *pb_v1.GateCommandResultRequest, gateInfo *dbmodel.TGate) error {
- if err := checkGateCommandResultParamYufanhttpv1(req); err != nil {
- return err
- }
- p := &dbmodel.TGateCommand{}
- where := [][2]interface{}{}
- where = dbmodel.WhereAdd(where, "id", req.Id)
- where = dbmodel.WhereAdd(where, "device_id", gateInfo.ID)
- err := p.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return errors.DataBaseError
- }
- if p.ID == 0 {
- return errors.ErrRecordNotFound
- }
- now := time.Now()
- values := map[string]interface{}{
- "status": gate_utils.CommandStatusOver,
- "result_status": req.ResultStatus,
- "result_status_desc": req.ResultDesc,
- "updated_at": now,
- }
- db := database.DB()
- err = p.Update(db, where, values)
- if err != nil {
- return errors.DataBaseError
- }
- switch p.Code {
- case gate_utils.DownCommand:
- param := gate_utils.PersonRegesterTask{}
- json.Unmarshal([]byte(p.Param), ¶m)
- if strings.Contains(param.Person.Id, "auto") {
- return yufanDownCardCommandResult(req, ¶m, gateInfo)
- }
- return yufanDownPersonCommandResult(req, ¶m, gateInfo)
- case gate_utils.DelCommand:
- param := gate_utils.PersonDelTask{}
- json.Unmarshal([]byte(p.Param), ¶m)
- if param.Id == "" {
- return nil
- }
- if strings.Contains(param.Id, "auto") {
- return yufanDelCardCommandResult(req, ¶m, gateInfo)
- }
- return yufanDelPersonCommandResult(req, ¶m, gateInfo)
- case gate_utils.UpdatePicCommand:
- param := gate_utils.PhotoUpdateTask{}
- json.Unmarshal([]byte(p.Param), ¶m)
- return yufanPicUpdateCommandResult(req, ¶m, gateInfo)
- case gate_utils.AddPicCommand:
- param := gate_utils.TaskPhotoRegister{}
- json.Unmarshal([]byte(p.Param), ¶m)
- return yufanAddPicCommandResult(req, ¶m, gateInfo)
- }
- if p.Code != gate_utils.OpenCommand && p.Code != gate_utils.RebootCommand {
- p.Delete(database.DB(), where)
- }
- return nil
- }
|