func (mr *MsgRepository) GetMsgs(userID types.ID, sinceID types.ID, size int) ([]*domain.Msg, error) { log.Debug(userID, sinceID, size) tableName := getUserMsgTable(userID) msgs := make([]*domain.Msg, 0, size) var seqID int64 var err error defer func() { if err != nil { msgs = msgs[0:0] log.Error("[GetUserMsgs]", tableName, sinceID, size, err) } if err == sql.ErrNoRows { err = nil } }() if sinceID > 0 { err = mr.db.QueryRow("select id from "+tableName+" where msg_id=?", sinceID).Scan(&seqID) if err == sql.ErrNoRows { err = mr.db.QueryRow("select id from "+tableName+" where msg_id>? order by id limit 1", sinceID).Scan(&seqID) } if err != nil { return nil, err } } var rows *sql.Rows query := "select " + msgFields + " from " + tableName + " where user_id=? and id>? order by id limit ?" rows, err = mr.db.Query(query, userID, seqID, size) if err != nil { return nil, err } defer rows.Close() var cdata, atData []byte for rows.Next() { msg := &domain.Msg{} err = rows.Scan(&msg.ID, &msg.LocalID, &msg.SenderID, &msg.ReceiverID, &cdata, &msg.CreatedAt, &atData) if err != nil { break } gjson.Unmarshal(cdata, &msg.Content) if len(atData) > 0 { gjson.Unmarshal(atData, &msg.AtUserIDs) } msgs = append(msgs, msg) } if len(msgs) == 0 { log.Debug(query) } return msgs, nil }
func (b *BaiduGeocoder) ReverseGeocode(lng float64, lat float64) (*Placemark, error) { params := map[string]string{} params["ak"] = b.ak params["location"] = fmt.Sprintf("%f,%f", lat, lng) params["output"] = "json" //坐标的类型,目前支持的坐标类型包括:bd09ll(百度经纬度坐标)、gcj02ll(国测局经纬度坐标)、wgs84ll( GPS经纬度) params["coordtype"] = "wgs84ll" // params["pois"] = "1" // params["callback"] = "renderReverse" url := b.host for k, v := range params { url += k + "=" + v + "&" } log.Debug(url) resp, err := http.Get(url) if err != nil { return nil, err } data, _ := ioutil.ReadAll(resp.Body) defer resp.Body.Close() var result types.M if err = json.Unmarshal(data, &result); err != nil { log.Error(url, err) log.Error(string(data)) return nil, err } if result.GetInt("status") != 0 { return nil, errors.New("failed to reverse geocoding") } place := &Placemark{} addr := result.GetM("result").GetM("addressComponent") place.CountryName = addr.GetStr("country") place.ProvinceName = addr.GetStr("province") pName := []rune(place.ProvinceName) if len(pName) > 2 { switch pName[len(pName)-1] { case '省', '市': place.ProvinceName = string(pName[0 : len(pName)-1]) } } place.CityName = addr.GetStr("city") cName := []rune(place.CityName) if len(cName) > 2 { switch cName[len(cName)-1] { case '市', '区', '县': place.CityName = string(cName[0 : len(cName)-1]) } } place.Address = result.GetM("result").GetStr("formatted_address") place.Country = GetPlaceCode(place.CountryName, LevelCountry) place.Province = GetPlaceCode(place.ProvinceName, LevelProvince) place.City = GetPlaceCode(place.CityName, LevelCity) fmt.Println(*place) return nil, err }
func (mr *MsgRepository) FindMsg(m *domain.Msg) (*domain.Msg, error) { var cdata, atData []byte msg := &domain.Msg{} err := mr.db.QueryRow("select "+msgFields+" from "+getUserMsgTable(msg.SenderID)+` where chat_type=? and local_id=? and sender_id=? and receiver_id=?`, m.ChatType, m.LocalID, m.SenderID, m.ReceiverID). Scan(&msg.ID, &msg.LocalID, &msg.SenderID, &msg.ReceiverID, &cdata, &msg.CreatedAt, &atData) if err != nil { if err == sql.ErrNoRows { err = nil } return nil, err } gjson.Unmarshal(cdata, &msg.Content) if len(atData) > 0 { gjson.Unmarshal(atData, &msg.AtUserIDs) } if reflect.DeepEqual(m.Content, msg.Content) { return msg, nil } return nil, nil }
func init() { codeToPlace = make(map[int]*Place, 1024) var geoProvinces []*geoPlace err := json.Unmarshal([]byte(geoChinaJSONString), &geoProvinces) if err != nil { panic(err) } provinces := convertToPlaces(geoProvinces, "zh") cn := &Place{} cn.Code = 1 cn.Name = globalCountries[cn.Code]["zh"] cn.Places = provinces codeToPlace[cn.Code] = cn cachePlaces(cn.Places) }
func GetIdentity(accountID types.ID) (*Identity, error) { i := &Identity{} i.AccountID = accountID var photos []byte err := _db.QueryRow("select type, number, photos, created_at from identities where user_id=?", accountID). Scan(&i.Type, &i.Number, &photos, &i.CreatedAt) if err != nil { if err == sql.ErrNoRows { err = nil } else { log.Error(err) } return nil, err } json.Unmarshal(photos, &i.Photos) return i, nil }