import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import { XfConsumedetailVO, XfConsumedetailForm, XfConsumedetailQuery, monthAnalyzeQuery} from '@/api/consumption/report/xfConsumedetail/types';
/**
 * 查询消费明细列表
 * @param query
 * @returns {*}
 */
export const listXfConsumedetail = (query?: monthAnalyzeQuery): AxiosPromise<XfConsumedetailVO[]> => {
  return request({
    url: '/backstage/consumption/xfConsumeDetail/list',
    method: 'get',
    params: query
  });
};

/**
 * 查询营业分析数据
 * @param query
 * @returns {*}
 */
export const consumeAnalyze = (query?: XfConsumedetailQuery): AxiosPromise<XfConsumedetailVO[]> => {
  return request({
    url: '/backstage/consumption/xfConsumeDetail/consumeAnalyze',
    method: 'get',
    params: query
  });
};

/**
 * 查询消费明细详细
 * @param consumeId
 */
export const getXfConsumedetail = (consumeId: string | number): AxiosPromise<XfConsumedetailVO> => {
  return request({
    url: '/backstage/consumption/xfConsumeDetail/' + consumeId,
    method: 'get'
  });
};

/**
 * 新增消费明细
 * @param data
 */
export const addXfConsumedetail = (data: XfConsumedetailForm) => {
  return request({
    url: '/backstage/consumption/xfConsumeDetail',
    method: 'post',
    data: data
  });
};

/**
 * 修改消费明细
 * @param data
 */
export const updateXfConsumedetail = (data: XfConsumedetailForm) => {
  return request({
    url: '/backstage/consumption/xfConsumeDetail',
    method: 'put',
    data: data
  });
};

/**
 * 删除消费明细
 * @param consumeId
 */
export const delXfConsumedetail = (consumeId: string | number | Array<string | number>) => {
  return request({
    url: '/backstage/consumption/xfConsumeDetail/' + consumeId,
    method: 'delete'
  });
};

//设置默认查询日期
export const defaultDateByDay = () =>{
    return getDate(-1);
}

export const defaultDateByMonth = () =>{
  return getMonth('s', -1).substring(0, 7)
}

export const defaultDateByYear = () =>{
  return getYear('s', -1).substring(0, 4);
}


//日 
export const getDate = (dates: any) => {
  let date: any = new Date();
  let n: any = dates || 0;
  date.setDate(date.getDate() + n);
  let y: any = date.getFullYear();
  let m: any = date.getMonth() + 1;
  let d: any = date.getDate();
  m = m < 10 ? "0" + m : m;
  d = d < 10 ? "0" + d : d;
  let day = y + "-" + m + "-" + d;
  return day;
}


//周
export const getMonday = (type: any, dates: any) => {
  let now = new Date();
  let nowTime = now.getTime();
  let day = now.getDay();
  let longTime = 24 * 60 * 60 * 1000;
  let n = longTime * 7 * (dates || 0);
  let date: any = ''
  if (type == "s") {
      date = nowTime - (day - 1) * longTime + n;
  };
  if (type == "e") {
      date = nowTime + (7 - day) * longTime + n;
  };
  date = new Date(date);
  let y: any = date.getFullYear();
  let m: any = date.getMonth() + 1;
  let d: any = date.getDate();
  m = m < 10 ? "0" + m : m;
  d = d < 10 ? "0" + d : d;
  let days = y + "-" + m + "-" + d;
  return days;
}

//月份
export const getMonth = (type: any, months: any) => {
  let date: any = new Date();
  let year: any = date.getFullYear();
  let month: any = date.getMonth() + 1;
  if (Math.abs(months) > 12) {
      months = months % 12;
  };
  if (months != 0) {
      if (month + months > 12) {
          year++;
          month = (month + months) % 12;
      } else if (month + months < 1) {
          year--;
          month = 12 + month + months;
      } else {
          month = month + months;
      };
  };
  month = month < 10 ? "0" + month : month;
  let firstday = year + "-" + month + "-" + "01";
  let lastday = "";
  if (month == "01" || month == "03" || month == "05" || month == "07" || month == "08" || month == "10" || month == "12") {
      lastday = year + "-" + month + "-" + 31;
  } else if (month == "02") {
      if ((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0)) {
          lastday = year + "-" + month + "-" + 29;
      } else {
          lastday = year + "-" + month + "-" + 28;
      };
  } else {
      lastday = year + "-" + month + "-" + 30;
  };
  let day = "";
  if (type == "s") {
      day = firstday;
  } else {
      day = lastday;
  };
  return day;
}

//年
export const getYear = (type: any, dates: any) => {
  let date: any = new Date();
  let n: any = dates || 0;
  let year: any = date.getFullYear() + Number(n);
  let day = ''
  if (type == "s") {
      day = year + "-01-01";
  };
  if (type == "e") {
      day = year + "-12-31";
  };
  if (!type) {
      day = year + "-01-01/" + year + "-12-31";
  };
  return day;
}