package analysisadminlogic import ( "context" "strings" "time" "lone-services/pkg/modelbase" "lone-services/pkg/utils" "lone-services/pkg/validate" 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" "lone-services/services/analysis/validator" "github.com/zeromicro/go-zero/core/logx" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" ) const bindValidityLayout = "2006-01-02 15:04:05" type BindStoreLogic struct { ctx context.Context svcCtx *svc.ServiceContext logx.Logger } func NewBindStoreLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BindStoreLogic { return &BindStoreLogic{ ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx), } } func (l *BindStoreLogic) BindStore(in *analysis.AdminBindStoreReq) (*analysis.Response, error) { var req validator.AdminBindStoreValidator if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty { return outParams(msg), nil } if _, fail := common.RequireAdmin(l.ctx); fail != nil { return fail, nil } authList, err := common.ListStoreAuth(req.UserId) if err != nil { l.Errorf("admin bind store list auth: %v", err) return common.FailResponse(utils.Fail), nil } if len(authList) == utils.NumberZero { return common.OkResponse(modelbase.PageResult{Items: []dao.AdminBindStoreItem{}}), nil } storeMap, err := l.loadStores(authList) if err != nil { l.Errorf("admin bind store load stores: %v", err) return common.FailResponse(utils.Fail), nil } now := time.Now() items := make([]dao.AdminBindStoreItem, 0, len(authList)) for _, auth := range authList { store, ok := storeMap[auth.StoreId] if !ok { continue } itemStatus := utils.NumberOne if !auth.ValidityTime.IsZero() && auth.ValidityTime.Time.Before(now) { itemStatus = utils.NumberTwo } mobile, _ := utils.DecryptPhone(store.GetUserMobile()) if mobile == utils.StringEmpty { mobile = store.GetUserMobile() } items = append(items, dao.AdminBindStoreItem{ Id: store.GetId(), Name: store.GetName(), UserName: store.GetUserName(), UserMobile: mobile, District: store.GetDistrictIds(), DistrictText: storeDistrictText(store), ValidityTime: auth.ValidityTime.Format(bindValidityLayout), Status: itemStatus, }) } return common.OkResponse(modelbase.PageResult{ Count: int64(len(items)), Items: items, }), nil } func (l *BindStoreLogic) 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) } if len(ids) == utils.NumberZero { return map[int64]*storepb.InfoResponse{}, nil } 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 } out := make(map[int64]*storepb.InfoResponse, len(res.GetItems())) for _, item := range res.GetItems() { out[item.GetId()] = item } return out, nil } func storeDistrictText(info *storepb.InfoResponse) string { if info == nil { return utils.StringEmpty } parts := make([]string, 0, 3) for _, name := range []string{info.GetProvinceName(), info.GetCityName(), info.GetCountyName()} { if name != utils.StringEmpty { parts = append(parts, name) } } return strings.Join(parts, "") }