/*
 * @Author: ChenYaJin
 * @Date: 2021-06-08 16:11:26
 * @LastEditors: LiZhiWei
 * @LastEditTime: 2025-04-24 15:46:55
 * @Description: 文件柜-公用方法
 */

const inventoryCodeName = new Map([
    ['qualityInspection', '巡查'],
    ['securityPatrol', '巡逻'],
    ['deviceInspection', '巡检'],
    ['deviceCuring', '保养'],
    ['deviceMaintain', '维修'],
    ['cleaningWork', '保洁'],
    ['greeningWork', '绿化']
  ])
  
  export { inventoryCodeName }
  
  export default {
    // 根据子节点id,找出父节点列表
    getParentList (data, id) {
      let childList = []
      data.forEach((item, index) => {
        if (item.id === id) {
          childList = [item.label]
          return false
        } else {
          if (item.children) {
            let newCidList = [item.label]
            let list = this.nodeFun(item.children, id, newCidList)
            if (list) {
              childList = list
            }
          }
        }
      })
      return childList
    },
    nodeFun (newVal, newId, newCidList) {
      let flag = false
      newVal.forEach(j => {
        if (j.id === newId) {
          newCidList.push(j.label)
          flag = true
        } else {
          if (j.children) {
            let childList = JSON.parse(JSON.stringify(newCidList))
            childList.push(j.label)
            let list = this.nodeFun(j.children, newId, childList)
            if (list) {
              newCidList = list
              flag = true
            }
          }
        }
      })
      if (flag) {
        return newCidList
      }
    },
    /**
     * 渲染文字水印背景
     * @{Element} targetElement 目标元素
     * @{params} text 水印字符串
     */
    addWaterMarker (targetElement, text = '') {
      var cpyName = text
      if (text && text.length > 16) {
        cpyName = text.substring(0, 16)
      }
      var can = document.createElement('canvas')
      can.width = 180
      can.height = 110
      can.style.display = 'none'
      can.style.zIndex = '999999'
  
      var cans = can.getContext('2d')
      cans.rotate(-25 * Math.PI / 180)
      cans.font = '800 16px Microsoft JhengHei'
      cans.fillStyle = '#dcdee2'
      cans.textAlign = 'center'
      cans.textBaseline = 'Middle'
      if (cans.measureText(cpyName).width > 180) {
        var size = 180 / cpyName.length
        cans.font = '600 ' + size + 'px ' + ' Microsoft JhengHei'
      }
      cans.fillText(cpyName, 60, 60)
      targetElement.style.backgroundImage = 'url(' + can.toDataURL('image/png') + ')'
    },
    /**
     * 时间格式转换
     * @param {string | Date} dateString 时间
     * @returns {string} yyyy年mm月ddr日
     */
    dateFormat (dateString) {
      const year = new Date(dateString).getFullYear()
      const month = new Date(dateString).getMonth() + 1
      const day = new Date(dateString).getDate()
      return `${year}年${month}月${day}日`
    },
    /**
     * 文件大小【KB】转换
     * @param {number} size KB大小
     * @param {string} defaultUnit size=0时，默认的单位
     * @returns {string}
     */
    fileSizeFormat (size, defaultUnit = ' KB') {
      const kbToGb = 1048576
      const kbToMb = 1024
      const isNegative = Number(size) < 0
      const positiveSize = Math.abs(size)
      const positiveFormat = Number(positiveSize) >= kbToGb ? Math.floor((Number(positiveSize) / kbToGb * 100)) / 100 + ' GB'
        : Number(positiveSize) >= kbToMb ? Math.floor((Number(positiveSize) / kbToMb * 100)) / 100 + ' MB'
          : positiveSize + ' KB'
      const format = isNegative ? '-' + positiveFormat : positiveFormat
      return positiveSize ? format : 0 + defaultUnit
    }
  }