import moment from 'moment';

// 根据设定查询条件 返回时间范围
const getTimeRange = (type) => {
  let start = '';
  let end = '';
  switch (type) {
    case 'thisYear': // 今年
      start = moment().startOf('year');
      end = moment().endOf('year');
      break;
    case 'lastYear': // 去年
      start = moment()
        .year(moment().year() - 1)
        .startOf('year');
      end = moment()
        .year(moment().year() - 1)
        .endOf('year');
      break;
    case 'nextYear': // 明年
      start = moment()
        .year(moment().year() + 1)
        .startOf('year');
      end = moment()
        .year(moment().year() + 1)
        .endOf('year');
      break;
    case 'lastQuarter': // 上季度
      start = moment()
        .quarter(moment().quarter() - 1)
        .startOf('quarter');
      end = moment()
        .quarter(moment().quarter() - 1)
        .endOf('quarter');
      break;
    case 'thisQuarter': // 本季度
      start = moment().startOf('quarter');
      end = moment().endOf('quarter');
      break;
    case 'nextQuarter': // 下季度
      start = moment()
        .quarter(moment().quarter() + 1)
        .startOf('quarter');
      end = moment()
        .quarter(moment().quarter() + 1)
        .endOf('quarter');
      break;
    case 'lastMonth': // 上月
      start = moment()
        .month(moment().month() - 1)
        .startOf('month');
      end = moment()
        .month(moment().month() - 1)
        .endOf('month');
      break;
    case 'thisMonth': // 本月
      start = moment().startOf('month');
      end = moment().endOf('month');
      break;
    case 'nextMonth': // 下月
      start = moment()
        .month(moment().month() + 1)
        .startOf('month');
      end = moment()
        .month(moment().month() + 1)
        .endOf('month');
      break;
    case 'lastWeek': // 上周
      start = moment().startOf('week').subtract(1, 'week');
      end = moment().endOf('week').subtract(1, 'week');
      break;
    case 'thisWeek': // 本周
      start = moment().startOf('week');
      end = moment().endOf('week');
      break;
    case 'nextWeek': // 下周
      start = moment().startOf('week').subtract(-1, 'week');
      end = moment().endOf('week').subtract(-1, 'week');
      break;
    case 'lastDay': // 昨天
      start = moment().subtract(1, 'days');
      end = moment().subtract(1, 'days');
      break;
    case 'thisDay': // 今天
      start = moment();
      end = moment();
      break;
    case 'nextDay': // 明天
      start = moment().subtract(-1, 'days');
      end = moment().subtract(-1, 'days');
      break;
    default:
      start = moment();
      end = moment();
      break;
  }
  if (!!start && !!end) {
    start = start.format(startFormat);
    end = end.format(endFormat);
  }
  return { startTime: start, endTime: end };
};

const getRange = (date, type) => {
  let start = '';
  let end = '';
  switch (type) {
    case 'day':
      start = moment(date);
      end = moment(date);
      break;
    case 'week':
      start = moment(date).startOf('week');
      end = moment(date).endOf('week');
      break;
    case 'month':
      start = moment(date).startOf('month');
      end = moment(date).endOf('month');
      break;
    case 'year':
      start = moment(date).startOf('year');
      end = moment(date).endOf('year');
      break;
    default:
      start = moment(date);
      end = moment(date);
      break;
  }
  if (!!start && !!end) {
    start = start.format(startFormat);
    end = end.format(endFormat);
  }
  return { startTime: start, endTime: end };
};

export { getTimeRange, getRange };

const startFormat = 'YYYY-MM-DD 00:00:00';
const endFormat = 'YYYY-MM-DD 23:59:59';
