import { Injectable } from '@angular/core';
import { MainService } from 'src/app/services/main.service';
import { map } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class AssociatedDataService {

  constructor(
    private mainService: MainService,
  ) { }

  // simple增删改
  simplePost(type, model, postData) {
    return this.mainService.simplePost(type, model, postData);
  }

  // 获取任务类型列表
  getTaskTypes(postData) {
    return this.mainService.getFetchDataList("simple/data", "taskType", postData).pipe(map((v:any) => {
      if(Array.isArray(v.list)){
        v.list = v.list.map(vv => ({id: vv.id, taskName: vv.taskName}));
      }else{
        v.list = [];
      }
      return v;
    }));
  }

  // 查询
  getFetchDataList(postData) {
    return this.mainService.getFetchDataList("simple/data", "otherBindConfig", postData).pipe(map((v:any) => {
      if(Array.isArray(v.list)){
        v.list = v.list.map(vv => ({
          ...vv,
          buildingListNames: vv.buildingList ? vv.buildingList.map(v => v.buildingName).toString() : '',
          deptListNames: vv.deptList ? vv.deptList.map(v => v.dept).toString() : '',
        }));
      }else{
        v.list = [];
      }
      return v;
    }));;
  }

  // 字典
  getDictionary(type) {
    return this.mainService.getDictionary("list", type);
  }
}


