WDComp.ets 16.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
/**
 * Comp数据获取管理及相关业务逻辑处理
 *
 * @param <VO> 业务展示数据对象,显示Comp item的数据对象
 * @param <DTO> 网络传输数据对象,与服务器端接口定义的字段一致
 */
import { CompDTO } from '../bean/CompDTO';
import { ItemBean } from '../bean/ItemBean';
import { ItemDTO } from '../bean/ItemDTO';
import List from '@ohos.util.List';
import { WDGroup } from './WDGroup';
import { NetDataStatusType } from '../enum/NetDataStatusType';
import { DataSourceRequest } from '../bean/DataSourceRequest';
import { LayoutCallback } from '../service/LayoutCallback';

import { ArrayUtils, StringUtils } from 'wdKit';
import { CompDataDTO } from '../bean/CompDataDTO';
import { CommonDataService } from '../service/CommonDataService';

export abstract class WDComp<VO extends ItemBean, DTO extends ItemDTO> {
  protected parent: WDGroup;
  protected compDTO: CompDTO;
  protected initParams: Map<string, Object> = new Map<string, Object>();
  // view展示的由DTO转换Bean后的数据列表
  protected items: List<VO> = new List<VO>();
  // 当前组件数据加载状态
  protected status: number = NetDataStatusType.INITIAL;
  /**
   * 标记是否为需要合并请求的Comp
   */
  private isTeamwork: boolean = false;
  private isRefresh: boolean = false;

  /**
   * 标记是否为无限瀑布流
   */
  // private isFeed: boolean = false;
  /**
   * 标记是否为黑白化
   */
  // private isGrayMode: boolean = false;
  // protected data: HashMap<String, List<VO>> = new HashMap<string, List<VO>>();
  // protected dynamicData: HashMap<String, VO> = new HashMap<String, VO>();

  /**
   * 构造函数
   *
   * @param parent WDGroup
   * @param compDTO 服务器返回的CompDTO
   * @param initParams 初始化参数
   */
  constructor(parent: WDGroup, compDTO: CompDTO, initParams: Map<string, Object>) {
    this.parent = parent;
    this.compDTO = compDTO;
    this.initParams = initParams;

    if (parent) {
      this.isTeamwork = parent.getIsTeamWork() && compDTO.dataSource == "CONTENT_POOL_PER_RECOMMAND";
      // this.isGrayMode = parent.getIsGrayMode();
    }
  }

  /**
   * 加载第一页的数据
   */
  public loadFirst(): void {
    this.loadData();
  }

  /**
   * 在分页加载时,加载下一页的数据, 默认加载第一页数据, 加载更多数据时,需要重写该方法
   */
  public loadMore() {
    this.loadFirst();
  }

  /**
   * 重新加载数据/切换comp数据,一般用于换一换功能
   * @param params
   */
  public reloadData(params: Map<String, Object>): void {
    this.loadFirst();
  }

  /**
   * 加载Comp数据
   */
  public loadData() {
    // TODO: 调用onLoadData加载数据
    if (this.isTeamWork() || this.status == NetDataStatusType.LOADING) {
      return;
    }

    this.onLoadData()
  }

  /**
   * 加载comp(默认通用)数据源
   */
  protected onLoadData(): void {
    // TODO: 通过CommonData
    if (this.dataSourceEnabled() && this.compDTO && this.compDTO.dataSourceRequest) {
      // let isBackupDataSource = false;
      let callback: LayoutCallback<DTO> = {
        onSuccess: (dto: DTO): void => {
          this.transformResponseData(dto);
          if (this.items.length > 0) {
            this.onLoadDataFinished()
          } else {
            this.onLoadDataFailed()
          }
          return
        },
        onFailed: (code: number, message: string): void => {
          // todo:
          this.onLoadDataFailed()
          return
        }
      };
      let service: CommonDataService = new CommonDataService();

      for (let index = 0; index < this.compDTO.dataSourceRequest.length; index++) {
        let request: DataSourceRequest = this.compDTO.dataSourceRequest[index]
        // request.locationValue = getLocation();
        if (this.isTeamWork()) {
          continue;
        }
        this.status = NetDataStatusType.LOADING;
        service.loadDataSource<DTO>(this.compDTO, request, callback)
      }
    }
  }

  /**
   * 数据转换:转换网络请求的response DTO为业务展示的bean
   *
   * @param dto 网络请求的数据
   */
  protected transformResponseData(dto: DTO): void {
    // TODO: 调用convertDto2Vo转换并记录数据
    let beanList: List<VO> = this.convertDto2Vo(dto);
    if (beanList && beanList.length > 0) {
      if (this.isRefresh) {
        this.items.clear();
        this.isRefresh = false;
      }
      this.items = ArrayUtils.addAll(this.items, beanList)
    }
  }

  // 根据compDTO.displayCount字段值,在列表尾部补全数据或把列表尾部数据截断
  protected complementTrimData(originalList: List<VO>): List<VO> {
    let displayCount: number = this.getCompDisplayCount(); // 展示数量
    if (!originalList || displayCount == 0 || originalList.length == displayCount) {
      return originalList;
    }

    if (originalList.length < displayCount) {
      let newItemList: List<VO> = originalList;
      // 在尾部补全数据
      let complementCount = displayCount - originalList.length;
      let complementIndex = 0
      while (complementIndex < complementCount) {
        // 从原有数据前(循环)依次取数据,补充到列表后部,
        let itemIndex = complementIndex % originalList.length // 取模(余数)
        newItemList.add(originalList.get(itemIndex))
        complementIndex++
      }
      return newItemList;
    } else if (originalList.length > displayCount) {
      // 截取/截断
      // return originalList.getSubList(0, displayCount); // 异常报错
      let newItemList: List<VO> = new List<VO>()
      let itemIndex = 0
      while (itemIndex < displayCount) {
        newItemList.add(originalList.get(itemIndex))
        itemIndex++
      }
      return newItemList;
    } else {
      return originalList;
    }
  }

  /**
   * 留给业务实现的数据转换
   */
  protected abstract convertDto2Vo(dto: DTO): List<VO>;

  /**
   * 获取Comp的数据源,将一个data DTO设置为Comp的数据;设置comp对应的compDataDTO
   */
  public abstract setData(compDataDTO: CompDataDTO): void;

  /**
   * 使用动态数据替换静态数据
   * @param programme 动态数据
   */
  // public replaceStaticData(data: ProgrammeData): boolean {
  //   // TODO: 数据替换
  //   // let item: VO = this.dynamicData.get(data.position);
  //   // if (item instanceof ProgrammeBean) {
  //   //   // item.transform(data);
  //   //   return true;
  //   // } else {
  //   return false;
  //   // }
  // }

  /**
   * 是否正在加载中
   * @returns
   */
  public getIsLoading(): boolean {
    // TODO: 是否正在加载中
    return this.status == NetDataStatusType.LOADING;
  }

  /**
   * 是否加载完毕
   * @returns
   */
  public isLoaded(): boolean {
    // TODO: 是否已经加载完毕
    return this.status == NetDataStatusType.LOADED || this.status == NetDataStatusType.FAILED || this.status == NetDataStatusType.CANCEL;
  }

  /**
   * 是否(成功)加载完毕
   * @returns
   */
  public isLoadedSuccess(): boolean {
    return this.status == NetDataStatusType.LOADED;
  }

  /**
   * 是否还有更多数据待加载 / 判断是否还有下一页/,需要重写该方法
   */
  public hasMoreData(): boolean {
    // TODO: 判断是否有存在更多数据
    return false;
  }

  /**
   * Group中所有的Comp是否都已加载完毕
   * @returns
   */
  public hasDataLoaded(): boolean {
    // TODO: 所有Comp是否都已经加载完数据
    return false;
  }

  /**
   * 是否需要加载动态数据
   * @returns
   */
  public hasDynamicData(): boolean {
    // TODO: 判断数据源的dataSourceType是否为“POMS_NODE”,是则需要加载动态数据。
    if (this.compDTO && this.compDTO.dataSourceRequest && this.compDTO.dataSourceRequest.length > 0) {
      for (let index = 0; index < this.compDTO.dataSourceRequest.length; index++) {
        let request: DataSourceRequest = this.compDTO.dataSourceRequest[index]
        if (request.dataSourceType == "POMS_NODE") {
          return true;
        }
      }
    }
    return false;
  }

  public isTeamWork(): boolean {
    return this.isTeamwork;
  }

  protected dataSourceEnabled(): boolean {
    return true;
  }

  /**
   * 获取该comp所在的group
   */
  public getParent(): WDGroup {
    return this.parent;
  }

  /**
   * 获取comp对应的compDTO
   */
  public getCompDTO(): CompDTO {
    return this.compDTO;
  }

  /**
   * 获取comp Id
   */
  public getId(): string {
    return!this.compDTO ? "" : this.compDTO.id;
  }

  /**
   * 获取Comp Type
   */
  public getCompType(): string {
    return!this.compDTO ? "" : this.compDTO.compType;
  }

  /**
   * 获取Comp Style
   */
  public getCompStyle(): string {
    return!this.compDTO ? "" : this.compDTO.compStyle;
  }

  /**
   * 获取组件DisplayCount
   */
  public getCompDisplayCount(): number {
    if (!this.compDTO) {
      return 0
    }
    return StringUtils.parseNumber(this.compDTO.displayCount)
  }

  /**
   * 检查父Group是否需要加载数据,默认为true, 某些Comp的数据源是从{@link CompDTO}的extraData或其他字段中获取,
   * 如Label, button等, 则该Comp无需父group再去请求数据
   */
  public loadWithGroupData(): boolean {
    return true;
  }

  /**
   * 检查是否独立加载数据
   *
   * @returns 如果该comp是独立加载数据的返回true, 否则返回false.
   */
  public separateLoad(): boolean {
    if (!this.compDTO) {
      return false;
    }
    // if (this.compDTO.compType === CompType.LABEL || this.compDTO.compType === CompType.BUTTON) {
    //   return false
    // }
    if (!this.compDTO.dataSourceRequest || this.compDTO.dataSourceRequest.length == 0) {
      return false;
    }
    return true;
  }

  // public getIsFeed(): boolean {
  //   return this.isFeed;
  // }
  //
  // public setIsFeed(isFeed: boolean): void {
  //   this.isFeed = isFeed;
  // }
  //
  // public getIsGrayMode(): boolean {
  //   return this.isGrayMode;
  // }
  //
  // public setIsGrayMode(isGrayMode: boolean): void {
  //   this.isGrayMode = isGrayMode;
  // }

  // public isLongFeed(): boolean {
  //   return this.compDTO.dataSource == "SHORT_WITH_LONG_FEED" || this.compDTO.dataSource == "FOCUS_FEED";
  // }

  /**
   * 获取所有的数据项
   */
  public getItems(): List<VO> {
    return this.items;
  }

  /**
   * 向Comp中添加一个数据源/
   * 向Comp中指定位置{@code index}位置添加一个数据源
   */
  public addItem(item: VO, index?: number): void {
    if (item == null) {
      return;
    }

    if (this.items == null) {
      this.items = new List<VO>();
    }

    if (index == undefined) {
      this.items.add(item);
    } else {
      if (index >= this.items.length) {
        this.items.add(item);
      } else if (index < 0) {
        this.items.insert(item, 0);
      } else {
        this.items.insert(item, index);
      }
    }
  }

  // public getPath():string  {return ''}

  /**
   * 获取初始化参数
   */
  // public getInitParams(): Map<String, Object> {
  //   return this.initParams;
  // }

  /**
   * 获取在index位置的数据项
   */
  public getItem(index: number): VO | null {
    if (this.items == null || index >= this.items.length || index < 0) {
      return null;
    }

    return this.items.get(index);
  }

  /**
   * 获取Comp item 数量
   */
  public getItemCount(): number {
    return this.items == null ? 0 : this.items.length;
  }

  public getAllItemCount(): number {
    return this.getItemCount();
  }

  /**
   * 判断数据是否完整,当{@link #needCheckIntegrity()} 返回true时
   * @returns 如果数据完整的返回true, 否则返回false.
   */
  // public isDataIntegrity(): boolean {
  //   return this.items != null && this.items.length > 0;
  // }

  /**
   * 判断是否需要检查数据完整性
   */
  // public needCheckIntegrity(): boolean {
  //   return false;
  // }

  // public  changeProgramme( programmeBean:ProgrammeBean) :void{
  // }

  /**
   * 用于在合并请求的场景下请求备用数据源
   */
  // public loadBackupDataSource(isRefresh: boolean): void {
  //   if (this.getIsLoading()) {
  //     return;
  //   }
  //
  //   this.isRefresh = isRefresh;
  //   this.onLoadBackupData();
  // }
  //
  // protected onLoadBackupData(): void {
  // }

  // public onDynamicDataCallback(isReplaceStaticData: boolean): void {
  // }

  /**
   * 是否清理group数据,默认清除
   */
  // public isClearItemsData(): boolean {
  //   return true;
  // }

  // protected onDataRequestSuccess(poolId: string, items: List<VO>): void {
  // }

  // public onDataLoaded(poolId: string): void {
  //   if (this.items.length != 0) {
  //     for (let index = 0; index < this.items.length; index++) {
  //       let item: VO = this.items[index]
  //       if (StringUtils.isNotEmpty(item.position)) {
  //         this.dynamicData.set(item.position, item);
  //       }
  //     }
  //   }
  //   if (poolId != null) {
  //     this.data.set(poolId, this.items);
  //     this.onDataRequestSuccess(poolId, this.items);
  //     let defaultId: string | null = this.getFirstPoolId();
  //     if (defaultId != null) {
  //       this.items = this.data.get(defaultId);
  //       if (defaultId == poolId) {
  //         this.onLoadDataFinished();
  //       }
  //     }
  //   } else {
  //     this.data.set("DEFAULT", this.items);
  //     this.onLoadDataFinished();
  //   }
  // }

  // private getFirstPoolId(): string | null {
  //   if (!this.compDTO || this.compDTO.dataSourceRequest == null || this.compDTO.dataSourceRequest.length == 0) {
  //     return null;
  //   } else {
  //     return this.compDTO.dataSourceRequest[0].poolId ?? null;
  //   }
  // }

  // protected Class getDtoCls(): {
  //   return null;
  // }

  protected doCompDataFilter(data: DTO): DTO {
    return data;
  }

  public getStatus(): number {
    return this.status;
  }

  /**
   * 获取页面位置
   */
  // public getLocation(): string {
  //   if (this.compDTO == null) {
  //     return "";
  //   }
  //   return this.compDTO.location;
  // }

  /**
   * 获取location stamp
   *
   * @return
   */
  // public getLocationStamp(): string {
  //   let group: WDGroup = this.getParent();
  //   if (group == null) {
  //     return "000000";
  //   }
  //   let page: MGPage = group.getParent();
  //
  //   if (page == null) {
  //     return "000000";
  //   }
  //
  //   let compIndex: number = group.indexOfMGComp(this) + 1;
  //   let groupIndex: number = page.indexOfGroup(group) + 1;
  //   return String.format("%03d%03d", groupIndex, compIndex);
  // }

  /**
   * 判断是否是瀑布流
   */
  // public isWaterfallFlow(): boolean {
  //   if (this.compDTO == null) {
  //     return false;
  //   }
  //
  //   return "1" == this.compDTO.isWaterfallFlow;
  // }

  /**
   * 获取Comp所在页面的page id
   */
  public getPageId(): string {
    if (!this.getParent() || !this.getParent().getParent()) {
      return "";
    }
    return this.getParent().getParent().getId();
  }

  // public getSessionId(): string {
  //   if (this.getParent() == null || this.getParent().getParent() == null) {
  //     return "";
  //   }
  //   return this.getParent().getParent().getSessionId();
  // }

  /**
   * 获取Group Id
   */
  public getGroupId(): string {
    if (!this.getParent()) {
      return "";
    }
    return this.getParent().getId();
  }

  /**
   * 判断是否包含广告
   */
  // public containsAd(): boolean {
  //   return this.compDTO != null && this.compDTO.isContainAD == 1;
  // }

  // public setCompDTO(compDTO: CompDTO): void {
  //   this.compDTO = compDTO;
  // }

  // private setLocation(dto:DTO):void {    }

  // private appendLocation(url:string, location:string):string {}

  /**
   * 判断comp中是否有extra data
   */
  // public hasExtraData(): boolean {
  //   if (this.compDTO == null) {
  //     return false;
  //   }
  //
  //   return this.compDTO.extraData != null;
  // }

  /**
   * 获取comp的extra data
   */
  // public getExtraData(): ExtraData | undefined {
  //   if (this.compDTO == null) {
  //     return undefined;
  //   }
  //   return this.compDTO.extraData;
  // }

  /**
   * 判断是否有分割线
   */
  // public hasSeparator(): boolean {
  //   // if (this.compDTO == null) {
  //   return false;
  //   // }
  //   // return this.compDTO.isSegmentLine == 1;
  // }

  /**
   * 请求结束后通知刷新界面
   */
  protected onLoadDataFinished(): void {
    if (!this.getParent() || !this.getParent().getParent()) {
      return;
    }
    this.status = NetDataStatusType.LOADED;
    this.getParent().getParent().notifyCompDataLoaded(this);
  }

  /**
   * 请求失败后通知界面
   */
  protected onLoadDataFailed(): void {
    if (!this.getParent() || !this.getParent().getParent()) {
      return;
    }
    this.status = NetDataStatusType.FAILED;
    this.getParent().getParent().notifyCompDataFailed(this);
  }

  /**
   * 请求结束后异步通知刷新界面
   */
  // protected notifyLoadDataFinished(): void {
  //   this.onLoadDataFinished();
  // }

}