import request from '@/config/axios'

// ERP 销售全局统计 VO
export interface ErpSaleSummaryRespVO {
  todayPrice: number // 今日销售金额
  yesterdayPrice: number // 昨日销售金额
  monthPrice: number // 本月销售金额
  yearPrice: number // 今年销售金额
}

// ERP 销售时间段统计 VO
export interface ErpSaleTimeSummaryRespVO {
  time: string // 时间
  price: number // 销售金额
}

// ERP 销售趋势统计 VO
export interface ErpSaleTrendRespVO {
  date: string // 日期
  salePrice: number // 销售金额
  saleCount: number // 销售数量
  orderCount: number // 订单数量
  returnPrice: number // 退货金额
}

// ERP 客户销售排名 VO
export interface ErpSaleCustomerSummaryRespVO {
  customerId: number // 客户编号
  customerName: string // 客户名称
  totalPrice: number // 销售金额
  totalCount: number // 销售数量
  orderCount: number // 订单数量
}

// ERP 产品销售排名 VO
export interface ErpSaleProductSummaryRespVO {
  productId: number // 产品编号
  productName: string // 产品名称
  totalPrice: number // 销售金额
  totalCount: number // 销售数量
}

// ERP 销售人员销售排名 VO
export interface ErpSaleUserSummaryRespVO {
  userId: number // 用户编号
  userName: string // 用户名称
  totalPrice: number // 销售金额
  totalCount: number // 销售数量
  orderCount: number // 订单数量
  customerCount: number // 客户数量
}

// ERP 销售分析统计 VO
export interface ErpSaleAnalysisRespVO {
  totalSalePrice: number // 总销售金额
  totalSaleCount: number // 总销售数量
  totalOrderCount: number // 总订单数
  totalReturnPrice: number // 总退货金额
  customerCount: number // 客户数量
  avgDailySalePrice: number // 日均销售额
  growthRate: number // 环比增长率
}

// ERP 销售对比统计 VO
export interface ErpSaleCompareRespVO {
  timeLabel: string // 时间标签（如：2024-01 或 2024）
  salePrice: number // 销售金额
  returnPrice: number // 退货金额
  netSalePrice: number // 净销售额
  orderCount: number // 订单数量
  saleCount: number // 销售数量
  customerCount: number // 客户数量
}

// ERP 销售统计 API
export const SaleStatisticsApi = {
  // 获得销售统计
  getSaleSummary: async (): Promise<ErpSaleSummaryRespVO> => {
    return await request.get({ url: `/erp/sale-statistics/summary` })
  },

  // 获得销售时间段统计
  getSaleTimeSummary: async (): Promise<ErpSaleTimeSummaryRespVO[]> => {
    return await request.get({ url: `/erp/sale-statistics/time-summary` })
  },

  // 获得销售趋势统计（按天）
  getSaleTrend: async (beginTime: string, endTime: string): Promise<ErpSaleTrendRespVO[]> => {
    return await request.get({
      url: `/erp/sale-statistics/trend`,
      params: { beginTime, endTime }
    })
  },

  // 获得客户销售排名
  getCustomerSaleRanking: async (
    beginTime: string,
    endTime?: string,
    limit?: number
  ): Promise<ErpSaleCustomerSummaryRespVO[]> => {
    return await request.get({
      url: `/erp/sale-statistics/customer-ranking`,
      params: { beginTime, endTime, limit }
    })
  },

  // 获得产品销售排名
  getProductSaleRanking: async (
    beginTime: string,
    endTime?: string,
    limit?: number
  ): Promise<ErpSaleProductSummaryRespVO[]> => {
    return await request.get({
      url: `/erp/sale-statistics/product-ranking`,
      params: { beginTime, endTime, limit }
    })
  },

  // 获得销售人员销售排名
  getUserSaleRanking: async (
    beginTime: string,
    endTime?: string,
    limit?: number
  ): Promise<ErpSaleUserSummaryRespVO[]> => {
    return await request.get({
      url: `/erp/sale-statistics/user-ranking`,
      params: { beginTime, endTime, limit }
    })
  },

  // 获得销售分析统计
  getSaleAnalysis: async (beginTime: string, endTime: string): Promise<ErpSaleAnalysisRespVO> => {
    return await request.get({
      url: `/erp/sale-statistics/analysis`,
      params: { beginTime, endTime }
    })
  },

  // 获得按月对比统计
  getSaleCompareByMonth: async (beginTime: string, endTime: string): Promise<ErpSaleCompareRespVO[]> => {
    return await request.get({
      url: `/erp/sale-statistics/compare-by-month`,
      params: { beginTime, endTime }
    })
  },

  // 获得按年对比统计
  getSaleCompareByYear: async (beginTime: string, endTime: string): Promise<ErpSaleCompareRespVO[]> => {
    return await request.get({
      url: `/erp/sale-statistics/compare-by-year`,
      params: { beginTime, endTime }
    })
  },

  // AI分析销售统计数据
  analyzeWithAI: async (analysisData: any) => {
    return await request.post({ url: `/erp/sale-statistics/ai-analysis`, data: analysisData })
  }
}
