package gate_card import ( "context" "encoding/json" "fmt" "git.getensh.com/common/gopkgs/database" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" "property-device/errors" dbmodel "property-device/model" pb_v1 "property-device/pb/v1" "property-device/utils/gate_utils" ) func checkGateCardListParam(req *pb_v1.GateCardListRequest) error { switch { case req.GardenId == 0: return status.Error(10003, "小区不能为空") } if req.PageSize == 0 { req.PageSize = 10 } if req.Page == 0 { req.Page = 1 } return nil } func GateUserCardCheck(originStatus int32, id int64) (int32, error) { up := dbmodel.TUserCard{} gp := dbmodel.TGateCard{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "record_id", id) where = dbmodel.WhereAdd(where, "status !=", gate_utils.WhiteAddStatusAllSuc) count, err := gp.Count(database.DB(), where, nil) if err != nil { return 0, errors.DataBaseError } // 全部成功 if count == 0 { where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "id", id) values := map[string]interface{}{"down_status": 2} err = up.Update(database.DB(), where, values) if err != nil { return 0, errors.DataBaseError } return 2, nil } if originStatus == 1 { // 查看是否有下发失败的 where = [][2]interface{}{} where = dbmodel.WhereAdd(where, "record_id", id) where = dbmodel.WhereAdd(where, "status >", gate_utils.WhiteAddStatusAllSuc) count, err = gp.Count(database.DB(), where, nil) if err != nil { return 0, errors.DataBaseError } if count > 0 { where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "id", id) values := map[string]interface{}{"down_status": 3} err = up.Update(database.DB(), where, values) if err != nil { return 3, errors.DataBaseError } return 3, nil } } return 1, nil } func GateUserCardStatusAllCheck(gardeId int64) error { up := dbmodel.TUserCard{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "garden_id", gardeId) where = dbmodel.WhereAdd(where, "down_status !=", 2) list, err := up.List(database.DB(), where, nil, -1, -1) if err != nil { return errors.DataBaseError } for _, v := range list { _, err = GateUserCardCheck(v.DownStatus, v.ID) if err != nil { return err } } return nil } func getGateCardDevices(recordIds []int64) (map[int64][]*pb_v1.GateCardDevice, error) { gp := dbmodel.TGateCard{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "record_id in", recordIds) list, err := gp.List(database.DB(), where, nil, -1, -1) if err != nil { return nil, errors.DataBaseError } m := map[int64]bool{} deviceIds := []int64{} for _, v := range list { if m[v.DeviceId] { continue } m[v.DeviceId] = true deviceIds = append(deviceIds, v.DeviceId) } ret := map[int64][]*pb_v1.GateCardDevice{} if len(deviceIds) == 0 { return ret, nil } gate := dbmodel.TGate{} where = [][2]interface{}{} where = dbmodel.WhereAdd(where, "id in", deviceIds) glist, err := gate.List(database.DB(), where, nil, -1, -1) if err != nil { return nil, errors.DataBaseError } gm := map[int64]dbmodel.TGate{} for _, v := range glist { gm[v.ID] = v } for _, v := range list { item := pb_v1.GateCardDevice{ DeviceId: v.DeviceId, DeviceName: gm[v.DeviceId].DeviceName, Location: gm[v.DeviceId].Location, Direction: gm[v.DeviceId].Direction, } if v.Status == gate_utils.WhiteAddStatusWait || v.Status == gate_utils.WhiteAddStatusPersonSuc { item.DownStatus = 1 } else if v.Status == gate_utils.WhiteAddStatusAllSuc { item.DownStatus = 2 } else { item.DownStatus = 3 } ret[v.RecordId] = append(ret[v.RecordId], &item) } return ret, nil } func GateCardList(ctx context.Context, req *pb_v1.GateCardListRequest) (reply *pb_v1.GateCardListReply, err error) { reply = &pb_v1.GateCardListReply{} // 捕获各个task中的异常并返回给调用者 defer func() { if r := recover(); r != nil { err = fmt.Errorf("%+v", r) e := &status.Status{} if er := json.Unmarshal([]byte(err.Error()), e); er != nil { logger.Error("err", zap.String("system_err", err.Error()), zap.Stack("stacktrace")) } } }() err = checkGateCardListParam(req) if err != nil { return nil, err } if req.Page == 1 { if err = GateUserCardStatusAllCheck(req.GardenId); err != nil { return nil, err } } up := dbmodel.TUserCard{} where := [][2]interface{}{} where = dbmodel.WhereAdd(where, "garden_id", req.GardenId) if req.DownStatus > 0 { where = dbmodel.WhereAdd(where, "down_status", req.DownStatus) } if req.Name != "" { where = dbmodel.WhereAdd(where, "name", req.Name) } reply.Page = req.Page reply.Total, err = up.Count(database.DB(), where, nil) if err != nil { return nil, errors.DataBaseError } if reply.Total == 0 { return reply, nil } list, err := up.List(database.DB(), where, nil, int(req.Page), int(req.PageSize)) if err != nil { return nil, errors.DataBaseError } recordIds := make([]int64, len(list)) for i, v := range list { recordIds[i] = v.ID } // 获取设备信息 deviceM, err := getGateCardDevices(recordIds) if err != nil { return nil, err } reply.List = make([]*pb_v1.GateCardItem, len(list)) for i, v := range list { item := &pb_v1.GateCardItem{ Id: v.ID, CardNumber: v.CardNumber, Name: v.Name, DownStatus: v.DownStatus, GatePermissions: deviceM[v.ID], CreatedAt: v.CreatedAt.Unix(), } reply.List[i] = item } return reply, nil }