package analysisuserlogic import ( "context" "time" "lone-services/pkg/utils" analysis "lone-services/rpc/analysis/pb" storepb "lone-services/rpc/store/pb" "lone-services/services/analysis/internal/dao" "lone-services/services/analysis/internal/logic/common" "lone-services/services/analysis/internal/svc" "github.com/zeromicro/go-zero/core/logx" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" ) const authTimeLayout = "2006-01-02 15:04" type UserAuthListLogic struct { ctx context.Context svcCtx *svc.ServiceContext logx.Logger } func NewUserAuthListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserAuthListLogic { return &UserAuthListLogic{ ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx), } } func (l *UserAuthListLogic) UserAuthList(in *analysis.UserAuthListReq) (*analysis.Response, error) { bizId, _, fail := common.RequireAnalysisUserId(l.ctx) if fail != nil { return fail, nil } authList, err := common.ListStoreAuth(bizId) if err != nil { l.Errorf("user auth list: %v", err) return common.FailResponse(utils.Fail), nil } if len(authList) == utils.NumberZero { return common.OkResponse([]dao.StoreAuthItem{}), nil } storeMap, err := l.loadStores(authList) if err != nil { l.Errorf("user auth list stores: %v", err) return common.FailResponse(utils.Fail), nil } now := time.Now() list := make([]dao.StoreAuthItem, 0, len(authList)) for _, row := range authList { store, ok := storeMap[row.StoreId] if !ok { continue } itemStatus := int(dao.StatusEnabled) if row.ValidityTime.Time.Before(now) { itemStatus = int(dao.StatusDisabled) } list = append(list, dao.StoreAuthItem{ Id: row.Id, StoreId: store.Id, Name: store.Name, Address: store.Address, ValidityTime: row.ValidityTime.Format(authTimeLayout), CreatedAt: row.CreatedAt.Format(authTimeLayout), Status: itemStatus, }) } return common.OkResponse(list), nil } func (l *UserAuthListLogic) loadStores(authList []dao.UserStoreAuth) (map[int64]*storepb.InfoResponse, error) { ids := make([]int64, 0, len(authList)) seen := make(map[int64]struct{}, len(authList)) for _, row := range authList { if _, ok := seen[row.StoreId]; ok { continue } seen[row.StoreId] = struct{}{} ids = append(ids, row.StoreId) } svcName := utils.GetConfigString("services.store") if svcName == utils.StringEmpty { return nil, status.Error(codes.FailedPrecondition, "store service empty") } cli, err := svc.GetRpcClient(svcName) if err != nil { return nil, err } res, err := storepb.NewStoreClient(cli.Conn()).GetInfoByIds(l.ctx, &storepb.IdsRequest{Ids: ids}) if err != nil { if st, ok := status.FromError(err); ok && st.Message() == utils.ErrorNotFund.Msg { return map[int64]*storepb.InfoResponse{}, nil } return nil, err } storeMap := make(map[int64]*storepb.InfoResponse, len(res.GetItems())) for _, item := range res.GetItems() { storeMap[item.Id] = item } return storeMap, nil }