ApplyTemplateString.js 3.83 KB
Newer Older
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
// 子表根据基础数据带出基础数据
export default {
  execute: function () {
    if (arguments && arguments[0] && arguments[1]) {
      const context = arguments[1]
      const originField = arguments[0][0]
      const targetField = arguments[0][1]
      let templateString = arguments[0][2].StaticValue
      let args = arguments[0][3].StaticValue
      const sep = arguments[0][4] && arguments[0][4].StaticValue || ''
      args = args.replace(/ /g, '')
      const argList = args.split(',')
      const originTableType = context.getTableType(originField.FieldTableCode)
      const originTableObject = context.getSubData(originTableType.title)
      const requestParam = {
        pagination: false,
        searchKey: "",
        queryDataStructure: "ALL_WITH_REF",
        stopflag: -1,
        authType: "NONE"
      }
      const replaceMap = new Map()
      const replaceTemplatePart = templateString.match(/{{.+?}}/g)
      replaceTemplatePart.forEach((part) => {
        replaceMap.set(part, [])
      })
      const getBaseDataList = (originFieldValue) => {
        const promistList = []
        for (let i = 0; i < argList.length; i += 2) {
          promistList.push(this.getMDObject({
            ...requestParam,
            tableName: argList[i + 1],
            objectcode: originFieldValue[argList[i]],
          }))
        }
        return Promise.all(promistList)
      }
      const getFormattedString = (originFieldValue, basedataObjectList, templateString) => {
        const matchList = templateString.match(/`.+?`/g)
        matchList.forEach((str) => {
          let value = str.substring(1, str.length - 1)
          value = value.split('.')
          let pos = value[0].substring(1)
          pos = Number(pos)
          let ans
          if (pos == 0) {
            ans = originFieldValue
          }
          else {
            ;--pos
            if (pos >= basedataObjectList.length) {
              console.error(`${str}下标大于参数列表长度`)
              return
            }
            ans = basedataObjectList[pos]
          }
          for (let i = 1; i < value.length; ++i) {
            ans = ans[value[i]]
          }
          templateString = templateString.replace(str, ans)
        })
        return templateString
      }
      if (originTableObject instanceof Array) {
        const getBasedataPromiseList = []
        let count = 0
        for (let i = 0; i < originTableObject.length; ++i) {
          const originObject = originTableObject[i]
          const curOriginFieldValue = originObject.getValue(originField.FieldCode)
          if (!curOriginFieldValue || !curOriginFieldValue.objectcode) {
            continue
          }
          getBasedataPromiseList.push(getBaseDataList(curOriginFieldValue).then((basedataObjectList) => {
            replaceTemplatePart.forEach((templatePart) => {
              const formattedString = getFormattedString(curOriginFieldValue, basedataObjectList, templatePart.substring(2, templatePart.length - 2))
              replaceMap.get(templatePart)[i] = formattedString
            })
          }))
        }
        Promise.all(getBasedataPromiseList).then(() => {
          for (let key of replaceMap.keys()) {
            templateString = templateString.replace(key, replaceMap.get(key).filter(o => !!o).join(sep))
          }
          context.getMasterData().setValue(targetField.FieldTableCode + '.' + targetField.FieldCode, templateString, null, 'none')
        })
      }
      else {
        context.getMasterData().setValue(targetField.FieldTableCode + '.' + targetField.FieldCode, '', null, 'none')
      }
    }
  },
  getMDObject(param) {
    return GMS.$http.post("/baseData/data/list", param).then(data => {
      return data.data && data.data.rows && data.data.rows[0] || {}
    }).catch((resp) => {
    });
  },
  getResultType: function () {
    return FMR.ConstDataTypes.General;
  }
}