Example #1
0
        public override SetUserPosResponseBody ExecuteCore()
        {
            SetUserPosResponseBody res = new SetUserPosResponseBody();

            //{
            //    "UserId": 1,
            //    "UserPos": {
            //        "Country": "中国",
            //        "Province": "上海市",
            //        "City": "上海市",
            //        "District": "闵行区",
            //        "Details": "上海市闵行区浦申路靠近上海闵行区金色阳光世博幼儿园",
            //        "Latitude": 31.0732898712158,
            //        "Longitude": 121.507202148438
            //    }
            //}

            using (HWLEntities db = new HWLEntities())
            {
                //检测是否存在country province city district 没有就添加
                t_country country = db.t_country.Where(c => c.name == this.request.Country).FirstOrDefault();
                if (country == null)
                {
                    country = new t_country()
                    {
                        id   = 0,
                        name = this.request.Country,
                    };
                    db.t_country.Add(country);
                    db.SaveChanges();
                }

                t_province province = db.t_province.Where(p => p.name == this.request.Province).FirstOrDefault();
                if (province == null)
                {
                    province = new t_province()
                    {
                        id         = 0,
                        country_id = country.id,
                        name       = this.request.Province,
                    };
                    db.t_province.Add(province);
                    db.SaveChanges();
                }

                t_city city = db.t_city.Where(c => c.name == this.request.City).FirstOrDefault();
                if (city == null)
                {
                    city = new t_city()
                    {
                        id          = 0,
                        province_id = province.id,
                        name        = this.request.City,
                    };
                    db.t_city.Add(city);
                    db.SaveChanges();
                }

                t_district district = db.t_district.Where(p => p.name == this.request.District).FirstOrDefault();
                if (district == null)
                {
                    district = new t_district()
                    {
                        id      = 0,
                        city_id = city.id,
                        name    = this.request.District,
                    };
                    db.t_district.Add(district);
                    db.SaveChanges();
                }

                //检测用户是否已经存在当前位置信息(条件,用户id,位置id,位置详情)
                t_user_pos upos = db.t_user_pos.Where(u => u.user_id == this.request.UserId &&
                                                      u.country_id == country.id &&
                                                      u.province_id == province.id &&
                                                      u.city_id == city.id &&
                                                      u.district_id == district.id &&
                                                      u.pos_details == this.request.Details
                                                      ).FirstOrDefault();
                if (upos == null)
                {
                    //向用户位置表中加入数据
                    upos = new t_user_pos()
                    {
                        id          = 0,
                        create_date = DateTime.Now,
                        update_date = DateTime.Now,
                        geohash_key = Geohash.Encode(this.request.Latitude, this.request.Longitude),
                        lat         = this.request.Latitude,
                        lon         = this.request.Longitude,
                        pos_details = this.request.Details,
                        user_id     = this.request.UserId,
                        city_id     = city.id,
                        country_id  = country.id,
                        district_id = district.id,
                        province_id = province.id,
                    };
                    db.t_user_pos.Add(upos);
                }
                else
                {
                    upos.update_date = DateTime.Now;
                }

                db.SaveChanges();
                res.Status    = ResultStatus.Success;
                res.UserPosId = upos.id;

                //保存用户的位置到redis
                Redis.UserAction userAction = new Redis.UserAction();
                userAction.SavePos(upos.user_id, upos.lon, upos.lat);

                //获取当前位置附近的组
                Redis.GroupAction groupAction = new Redis.GroupAction();
                res.UserGroupGuid = groupAction.GetNearGroupGuid(upos.lon, upos.lat);
                if (string.IsNullOrEmpty(res.UserGroupGuid))
                {
                    //如果没有组数据,创建一个组
                    res.UserGroupGuid = groupAction.CreateNearGroupPos(upos.lon, upos.lat);
                    //将用户加入到组中
                    groupAction.SaveGroupUser(res.UserGroupGuid, upos.user_id);
                }
                else
                {
                    if (groupAction.ExistsInGroup(res.UserGroupGuid, upos.user_id))
                    {
                        //如果用户当前所在位置与上次所在的位置不一样,则需要将用户从上次的群组里面移出来
                        if (!string.IsNullOrEmpty(this.request.LastGroupGuid) && this.request.LastGroupGuid != res.UserGroupGuid)
                        {
                            groupAction.DeleteGroupUser(this.request.LastGroupGuid, upos.user_id);
                        }

                        return(res);
                    }
                    else
                    {
                        //将用户加入到组中
                        groupAction.SaveGroupUser(res.UserGroupGuid, upos.user_id);
                    }
                }

                //如果用户当前所在位置与上次所在的位置不一样,则需要将用户从上次的群组里面移出来
                if (!string.IsNullOrEmpty(this.request.LastGroupGuid) && this.request.LastGroupGuid != res.UserGroupGuid)
                {
                    groupAction.DeleteGroupUser(this.request.LastGroupGuid, upos.user_id);
                }

                //返回组用户列表
                List <int> userIds = groupAction.GetGroupUserIds(res.UserGroupGuid);
                if (userIds == null || userIds.Count <= 0)
                {
                    return(res);
                }

                res.GroupUserInfos = db.t_user.Where(u => userIds.Contains(u.id))
                                     .Select(u => new UserSecretInfo()
                {
                    UserId    = u.id,
                    UserName  = u.name,
                    UserImage = u.head_image,
                }).ToList();
            }

            return(res);
        }
Example #2
0
        public t_user_pos SavePos()
        {
            try
            {
                #region Save t_country,t_province,t_city,t_district,t_town
                t_country country = db.t_country.Where(c => c.name == this.request.Country).FirstOrDefault();
                if (country == null)
                {
                    country = new t_country()
                    {
                        id   = 0,
                        name = this.request.Country,
                    };
                    db.t_country.Add(country);
                    db.SaveChanges();
                }

                t_province province = db.t_province.Where(p => p.name == this.request.Province).FirstOrDefault();
                if (province == null)
                {
                    province = new t_province()
                    {
                        id         = 0,
                        country_id = country.id,
                        name       = this.request.Province,
                    };
                    db.t_province.Add(province);
                    db.SaveChanges();
                }

                t_city city = db.t_city.Where(c => c.name == this.request.City).FirstOrDefault();
                if (city == null)
                {
                    city = new t_city()
                    {
                        id          = 0,
                        province_id = province.id,
                        name        = this.request.City,
                    };
                    db.t_city.Add(city);
                    db.SaveChanges();
                }

                t_district district = db.t_district.Where(p => p.name == this.request.District).FirstOrDefault();
                if (district == null)
                {
                    district = new t_district()
                    {
                        id      = 0,
                        city_id = city.id,
                        name    = this.request.District,
                    };
                    db.t_district.Add(district);
                    db.SaveChanges();
                }

                t_town town = db.t_town.Where(p => p.name == this.request.Town).FirstOrDefault();
                if (town == null)
                {
                    town = new t_town()
                    {
                        id          = 0,
                        district_id = district.id,
                        name        = this.request.Town,
                    };
                    db.t_town.Add(town);
                    db.SaveChanges();
                }
                #endregion

                //t_user_pos upos = db.t_user_pos.Where(u => u.user_id == this.request.UserId &&
                //                                            u.country_id == country.id &&
                //                                            u.province_id == province.id &&
                //                                            u.city_id == city.id &&
                //                                            u.district_id == district.id &&
                //                                            u.pos_details == this.request.Details
                //                                        ).FirstOrDefault();
                t_user_pos upos = db.t_user_pos.Where(u => u.user_id == this.request.UserId &&
                                                      u.lon == request.Longitude &&
                                                      u.lat == request.Latitude
                                                      ).FirstOrDefault();
                if (upos == null)
                {
                    upos = new t_user_pos()
                    {
                        id              = 0,
                        create_date     = DateTime.Now,
                        update_date     = DateTime.Now,
                        geohash_key     = Geohash.Encode(this.request.Latitude, this.request.Longitude),
                        lat             = this.request.Latitude,
                        lon             = this.request.Longitude,
                        pos_details     = this.request.Details,
                        user_id         = this.request.UserId,
                        city_id         = city.id,
                        country_id      = country.id,
                        district_id     = district.id,
                        province_id     = province.id,
                        coordinate_type = request.CoorType,
                        location_type   = request.LocationType,
                        Location_where  = request.LocationWhere,
                        radius          = request.Radius,
                        town_id         = town.id,
                    };
                    db.t_user_pos.Add(upos);
                }
                else
                {
                    upos.update_date = DateTime.Now;
                }

                db.SaveChanges();
                return(upos);
            }
            catch (Exception ex)
            {
                string currentParams = Newtonsoft.Json.JsonConvert.SerializeObject(request);
                LogHelper.Error(currentParams + "___" + ex.ToString(), typeof(SetUserPos));
            }
            return(null);
        }