data-service.ts 4.91 KB
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map';
import { Menu } from './define-menu';
import { Unit } from './define-unit';
import { Qa } from './define-qa';
import { Keyword } from './define-keyword';
import { Contents } from './define-contents';
import { Storage } from '@ionic/storage';
/*
  Generated class for the DataService provider.

  See https://angular.io/docs/ts/latest/guide/dependency-injection.html
  for more info on providers and Angular 2 DI.
*/
@Injectable()
export class DataService {
    st: Storage;
    st_res: string;
    contents: Contents;
    menu: Menu[];
    unit: Unit[];
    subject: Qa[];
    extunit: Unit[];
    extqa: Qa[];
    keyword: Keyword[];

    constructor(st: Storage) {
        this.st = st;
        this.contents = new Contents;
        this.menu = this.contents.menu;
        this.unit = this.contents.unit;
        this.subject = this.contents.qa;
        this.extunit = this.contents.extunit;
        this.extqa = this.contents.extqa;
        this.keyword = this.contents.keyword;
    }

  public getMenuFirst(): Menu[]{
      var menuFirst: Menu[] = new Array();
      for (var i = 0; i < this.menu.length; i++) {
        if (this.menu[i].grade === '1') {
          menuFirst.push(this.menu[i]);
        }
      }
      return menuFirst;
  }

  public getMenuSecond(): Menu[]{
      var menuSecond: Menu[] = new Array();
      for (var i = 0; i < this.menu.length; i++) {
        if (this.menu[i].grade === '2') {
          menuSecond.push(this.menu[i]);
        }
      }
      return menuSecond;
  }

  public getMenuThird(): Menu[]{
      var menuThird: Menu[] = new Array();
      for (var i = 0; i < this.menu.length; i++) {
        if (this.menu[i].grade === '3') {
          menuThird.push(this.menu[i]);
        }
      }
      return menuThird;
  }

  public getUnitNum(menuId: number): number {
      var num = 0;
      var chk = -1;
      for (var i = 0; i < this.unit.length; i++) {
          if (this.unit[i].menuId === menuId) {
              if(chk != this.unit[i].unitId)
              {
                  num++;
                  chk = this.unit[i].unitId;
              }
          }
      }
      return num;
  }

  public getUnit(menuId: number, unitId:number): Unit[] {
      var subjectList: Unit[] = new Array();
      for (var i = 0; i < this.unit.length; i++) {
          if ((this.unit[i].unitId === unitId) && (this.unit[i].menuId === menuId)) {
              subjectList.push(this.unit[i]);
          }
      }
      return subjectList;
  }

  public getExtUnit(menuId: number): Unit[] {
    var subjectList: Unit[] = new Array();
    for (var i = 0; i < this.extunit.length; i++) {
          if (this.extunit[i].menuId === menuId) {
              subjectList.push(this.extunit[i]);
          }
      }
      return subjectList;
  }

  public getQaCnt(menuId:number): number {
      var num = 0;
      for (var i = 0; i < this.subject.length; i++) {
          if (this.subject[i].menuId === menuId) {
              num++;
          }
      }
      return num;
  }

  public getExtQaCnt(menuId:number): number {
      var num = 0;
      for (var i = 0; i < this.extqa.length; i++) {
          if (this.extqa[i].menuId === menuId) {
              num++;
          }
      }
      return num;
  }

  public getQa(menuId: number, unitId: number): Qa[] {
      var qaItem: Qa[] = new Array();
      for (var i = 0; i < this.subject.length; i++) {
          if ((this.subject[i].menuId === menuId) && (this.subject[i].unitId === unitId)) {
              qaItem.push(this.subject[i]);
          }
      }
      return qaItem;
  }

  public getExtqa(menuId: number, unitId: number): Qa[] {
      var qaItem: Qa[] = new Array();
      for (var i = 0; i < this.extqa.length; i++) {
          if ((this.extqa[i].menuId === menuId) && (this.extqa[i].unitId === unitId)) {
              qaItem.push(this.extqa[i]);
          }
      }
      return qaItem;
  }

  public getKeyword(menuId: number): Keyword {
      var key: Keyword;
      for (var i = 0; i < this.keyword.length; i++) {
          if (this.keyword[i].menuId === menuId) {
              key = this.keyword[i];
          }
      }
      return key;
  }

  public saveDone(key: string) {
      this.st.get(key).then(val => {
          if (val == "done") {
          } else {
              this.st.set(key, "done");
          }
      });
  }

  public saveInfo(key: string, value: string) {
      this.st.get(key).then(val => {
          if (val == null) {
              this.st.set(key, value);
          } else {
              var svdata = val.toString().split(":");
              var nwdata = value.split(":");
              if(+svdata[0] < +nwdata[0])
              {
                  this.st.set(key, value);
              }
          }
      });
  }

  public savePositiveInfo(key: string, value: string) {
      this.st.get(key).then(val => {
          this.st.set(key, value);
      });
  }

  public getInfo(key: string): any {
      return this.st.get(key);
  }

}