<script>
/* eslint-disable */
/*
 * 个人辅助工具
 * @ lwt
 */
import moment from 'moment'
import 'moment/locale/zh-cn'
const deepMerge = (obj1, obj2) => {
  let key
  for (key in obj2) {
    // 如果target(也就是obj1[key])存在，且是对象的话再去调用deepMerge，否则就是obj1[key]里面没这个对象，需要与obj2[key]合并
    obj1[key] = obj1[key] && obj1[key].toString() === '[object Object]' ? deepMerge(obj1[key], obj2[key]) : obj1[key] = obj2[key]
  }
  return obj1
}
const templateValue = (str, data) => {
  let target_str = ''
  let target_key = ''
  let real_index = -1
  while (str.indexOf('@{') >= 0) {
    let f_index = str.indexOf('@{'),
      l_index = str.indexOf('}')
      target_str = str.substring(f_index,l_index+1)
      target_key = str.substring(f_index+2,l_index)
      real_index = target_key.indexOf('|')
      if (real_index>0) {
        let real_target_key = target_key.substring(0,real_index),
          target_key_format_type = target_key.substring(real_index+1,target_key.length),
          target_key_format = ''

        if (target_key_format_type.indexOf('date')>-1) {
          target_key_format = target_key_format_type.replace('date(','').replace(')','')
          if (data[real_target_key] !== '') {
            str = str.replace(target_str,moment(data[real_target_key]).format(target_key_format))
          } else {
            str = '-'
          }
          str = str.replace('<br>', '\n')
        }
        else if (target_key_format_type.indexOf('toFixed')>-1) {
          target_key_format = target_key_format_type.replace('toFixed(','').replace(')','')
          str = str.replace(target_str,data[real_target_key].toFixed(target_key_format)) 
        }
        else if (target_key_format_type.indexOf('isNull')>-1) {
          target_key_format = target_key_format_type.replace('isNull(','').replace(')','')
          str = str.replace(target_str,data[real_target_key]||target_key_format) 
        }
      }
      else{
        if (target_key.indexOf('&') >= 0) {
          let arr = target_key.split('&')
          let sarr =  data[arr[0]]
          str = ''
          sarr&&sarr.map((item) => {
            if (str === ''){
              str = item[arr[1]]
            } else {
              str += ',' + item[arr[1]]
            }
          })
        } else {
          str = str.replace(target_str,data[target_key] === undefined ? '' : data[target_key]) 
        }
      }
  }
  return str
}
const datasourByStr = (str, data) => {
  if (!str) {
    // console.log('数据源为空')
    return
  }
  let keyArrs = str.split('.')
  let targetData = data

  for (let i = 0; i < keyArrs.length; i++) {
    let key = keyArrs[i]
    targetData = targetData[key]
  }
  return targetData
}
const getFirstEnd = (year, month) => {
  //获取当前月
  let currentMonth = month
  //获取到下一个月，++currentMonth表示本月+1，一元运算
  let nextMonth = ++currentMonth
  //获取到下个月的第一天
  let nextMonthFirstDay=new Date(year, nextMonth,1)
  //一天时间的毫秒数
  let oneDay=1000*60*60*24
  //获取当前月第一天和最后一天
  let curM = (month + 1) < 10 ? '0' + (month + 1) : (month + 1)
  let firstDay = year + '-' + curM + '-' + '01'
  //nextMonthFirstDay-oneDay表示下个月的第一天减一天时间的毫秒数就是本月的最后一天
  let lastDay = moment(nextMonthFirstDay-oneDay).format("YYYY-MM-DD")
  return { start: firstDay, end: lastDay }
}
export default {
  templateValue,
  datasourByStr,
  deepMerge,
  getFirstEnd
}
</script>
