export default {
  methods: {
    /**
     * 处理查询参数
     * @param {Object} queryParams - 原始查询参数
     * @param {string} name - 接口名称
     * @returns {Object} 处理后的参数
     */
    handleParams(queryParams, name = "getInstallationOrderList") {
      const { orderTimeRange, completeTimeRange, ...restParams } = queryParams;

      return {
        name,
        page: queryParams.pageNum,
        offset: queryParams.pageNum,
        pagecount: queryParams.pageSize,
        returntype: "Map",
        parammaps: {
          serviceStaffIds: restParams.serviceStaffIds || "",
          customerName: restParams.customerName || "",
          statusName: restParams.statusName || "",
          projectId: restParams.projectId || "",
          orderNo: restParams.orderNo || "",
          orderStartTime: orderTimeRange?.[0]
            ? this.formatDate(orderTimeRange[0])
            : "",
          orderEndTime: orderTimeRange?.[1]
            ? this.formatDate(orderTimeRange[1])
            : "",
          completeStartTime: completeTimeRange?.[0]
            ? this.formatDate(completeTimeRange[0])
            : "",
          completeEndTime: completeTimeRange?.[1]
            ? this.formatDate(completeTimeRange[1])
            : "",
          contractNo: restParams.contractNo || "",
          deliveryNo: restParams.deliveryNo || "",
          userId: restParams.userId || "",
          roleId: restParams.roleId || "",
        },
      };
    },

    /**
     * 处理导出参数
     * @param {Object} queryParams - 原始查询参数
     * @param {string} name - 接口名称
     * @returns {Object} 处理后的导出参数
     */
    handleExportParams(queryParams, name) {
      if (!name) {
        throw new Error("导出接口名称不能为空");
      }

      const {
        orderTimeRange,
        completeTimeRange,
        pageNum,
        pageSize,
        ...restParams
      } = queryParams;

      return {
        name,
        returntype: "Map",
        parammaps: {
          serviceStaffIds: restParams.serviceStaffIds || "",
          customerName: restParams.customerName || "",
          statusName: restParams.statusName || "",
          projectId: restParams.projectId || "",
          orderNo: restParams.orderNo || "",
          orderStartTime: orderTimeRange?.[0]
            ? this.formatDate(orderTimeRange[0])
            : "",
          orderEndTime: orderTimeRange?.[1]
            ? this.formatDate(orderTimeRange[1])
            : "",
          completeStartTime: completeTimeRange?.[0]
            ? this.formatDate(completeTimeRange[0])
            : "",
          completeEndTime: completeTimeRange?.[1]
            ? this.formatDate(completeTimeRange[1])
            : "",
          contractNo: restParams.contractNo || "",
          deliveryNo: restParams.deliveryNo || "",
          userId: restParams.userId || "",
          roleId: restParams.roleId || "",
        },
      };
    },

    /**
     * 格式化日期为yyyy-MM-dd
     * @param {Date} date - 日期对象
     * @returns {string} 格式化后的日期字符串
     */
    formatDate(date) {
      if (!date) return "";
      const d = new Date(date);
      const year = d.getFullYear();
      const month = String(d.getMonth() + 1).padStart(2, "0");
      const day = String(d.getDate()).padStart(2, "0");
      return `${year}-${month}-${day}`;
    },
  },
};
