// Copyright 2019 getensh.com. All rights reserved. // Use of this source code is governed by getensh.com. package user import ( "context" "encoding/json" "fmt" "git.getensh.com/common/gopkgs/cache" "git.getensh.com/common/gopkgs/database" "git.getensh.com/common/gopkgs/logger" "go.uber.org/zap" "google.golang.org/grpc/status" "property-household/errors" dbmodel "property-household/model" "property-household/pb" pb_v1 "property-household/pb/v1" ) // func WxPublicEmptyUnionId(ctx context.Context, req *pb_v1.WxPublicEmptyUnionIdRequest) (reply *pb_v1.WxPublicEmptyUnionIdReply, err error) { reply = &pb_v1.WxPublicEmptyUnionIdReply{} // 捕获各个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")) } } }() r, err := cache.Redis().SetNxEx("wx_public_task", "1", 3600) if err != nil { return reply, errors.RedisError } if !r { return reply, nil } p := dbmodel.TUserWxPublic{} where := map[string]interface{}{ "union_id":"", } list, err := p.List(database.DB(), where, nil, -1, -1) if err != nil { return } mreq := pb_v1.WxPublicUnionIdRequest{} where = map[string]interface{}{} values := map[string]interface{}{} for _, v := range list { mreq.OpenId = v.OpenId mreply, err := pb.Thirdparty.WxPublicUnionId(context.Background(), &mreq) if err != nil { continue } if mreply.UnionId == "" { continue } p := dbmodel.TUserWxPublic{} where["open_id"] = v.OpenId values["union_id"] = mreply.UnionId err = p.Update(database.DB(), where, values) if err != nil { continue } HandlePublicOpenId(v.OpenId, mreply.UnionId) } return reply, nil }