2026-03-25 23:26:10 +08:00

218 lines
6.4 KiB
JavaScript

'use strict';
const MainService = require('../../main');
class OrderBaseService extends MainService {
ensureWarehouseId(data = {}) {
const warehouseId = this.getWarehouseId(data);
if (!warehouseId) {
throw new Error('warehouse_id 不能为空');
}
return warehouseId;
}
getWarehouseId(data = {}) {
const fromParam = Number(data.warehouse_id || data.warehouseId || 0);
const fromUser = Number(this.ctx.user?.store_id || 0);
return fromParam || fromUser;
}
async getOrderModels(warehouseId) {
const DeliveryModel = await this.ctx.model.OrderDeliveryFactory.getCurrentModel(warehouseId);
const OrderItemsModel = await this.ctx.model.OrderItemsFactory.getCurrentModel(warehouseId);
return { DeliveryModel, OrderItemsModel };
}
applyDateRange(where, field, start, end) {
const { Op } = this.app.Sequelize;
const range = {};
if (start) {
range[Op.gte] = this.normalizeDate(start, false);
}
if (end) {
range[Op.lte] = this.normalizeDate(end, true);
}
if (Object.keys(range).length > 0) {
where[field] = range;
}
}
getWhereCondition(whereData = {}, key) {
const condition = whereData[key];
if (condition && typeof condition === 'object' && Object.prototype.hasOwnProperty.call(condition, 'value')) {
return condition;
}
return {
operator: 'eq',
value: condition,
};
}
getWhereValue(whereData = {}, key) {
return this.getWhereCondition(whereData, key).value;
}
getWhereOperator(whereData = {}, key, defaultOperator = 'eq') {
const condition = this.getWhereCondition(whereData, key);
return String(condition.operator || defaultOperator).toLowerCase();
}
hasWhereValue(whereData = {}, key) {
const value = this.getWhereValue(whereData, key);
if (value === undefined || value === null) return false;
if (typeof value === 'string' && value.trim() === '') return false;
if (Array.isArray(value) && value.length === 0) return false;
return true;
}
buildWhere(whereData = {}, warehouseId) {
const { Op } = this.app.Sequelize;
const where = { warehouse_id: warehouseId };
const eqFields = [
'id',
'store_id',
'status',
'post_status',
'storage_status',
'pick_status',
'pack_status',
'inspection_status',
'print_status',
'claim_status',
'outbound_status',
'lock_status',
'intercept_status',
'source_platform_code',
'logistics_code',
'warehouse_code',
'order_type',
];
for (const key of eqFields) {
if (!this.hasWhereValue(whereData, key)) {
continue;
}
const value = this.getWhereValue(whereData, key);
const operator = this.getWhereOperator(whereData, key);
if (operator === 'in') {
where[key] = {
[Op.in]: Array.isArray(value) ? value : [ value ],
};
} else if (operator === 'notin') {
where[key] = {
[Op.notIn]: Array.isArray(value) ? value : [ value ],
};
} else if (operator === 'ne') {
where[key] = {
[Op.ne]: value,
};
} else if (operator === 'like' || operator === 'contains') {
where[key] = {
[Op.like]: `%${value}%`,
};
} else {
where[key] = value;
}
}
if (this.hasWhereValue(whereData, 'delivery_order_code')) {
const value = this.getWhereValue(whereData, 'delivery_order_code');
const operator = this.getWhereOperator(whereData, 'delivery_order_code', 'like');
if (operator === 'eq') {
where.delivery_order_code = value;
} else {
where.delivery_order_code = { [Op.like]: `%${value}%` };
}
}
const deliveryOrderCodes = this.getWhereValue(whereData, 'delivery_order_codes');
if (Array.isArray(deliveryOrderCodes) && deliveryOrderCodes.length > 0) {
where.delivery_order_code = { [Op.in]: deliveryOrderCodes };
}
if (this.hasWhereValue(whereData, 'logistics_name')) {
const value = this.getWhereValue(whereData, 'logistics_name');
where.logistics_name = { [Op.like]: `%${value}%` };
}
if (this.hasWhereValue(whereData, 'buyer_nick')) {
const value = this.getWhereValue(whereData, 'buyer_nick');
where.buyer_nick = { [Op.like]: `%${value}%` };
}
if (this.hasWhereValue(whereData, 'mobile')) {
const value = this.getWhereValue(whereData, 'mobile');
where.mobile = { [Op.like]: `%${value}%` };
}
this.applyDateRange(where, 'create_at', this.getWhereValue(whereData, 'create_at_start'), this.getWhereValue(whereData, 'create_at_end'));
this.applyDateRange(where, 'place_order_time', this.getWhereValue(whereData, 'place_order_time_start'), this.getWhereValue(whereData, 'place_order_time_end'));
this.applyDateRange(where, 'pay_time', this.getWhereValue(whereData, 'pay_time_start'), this.getWhereValue(whereData, 'pay_time_end'));
this.applyDateRange(where, 'delivery_time', this.getWhereValue(whereData, 'delivery_time_start'), this.getWhereValue(whereData, 'delivery_time_end'));
return where;
}
parseDateTime(value, fieldName) {
if (!value) return null;
const d = value instanceof Date ? value : new Date(value);
if (Number.isNaN(d.getTime())) {
throw new Error(`${fieldName} 格式错误`);
}
return d;
}
getPagination(pagination = {}) {
const page = Math.max(Number(pagination.page || 1), 1);
const pageSize = Math.min(Math.max(Number(pagination.pageSize || 20), 1), 200);
const offset = (page - 1) * pageSize;
return { page, pageSize, offset };
}
buildPagination(page, pageSize, total) {
return {
page,
pageSize,
total,
totalPages: Math.ceil(total / pageSize),
hasNext: page * pageSize < total,
hasPrevious: page > 1,
};
}
getOrderLocatorWhere(data = {}, warehouseId) {
const where = { warehouse_id: warehouseId };
if (data.id) {
where.id = data.id;
return where;
}
if (data.order_id) {
where.id = data.order_id;
return where;
}
if (data.delivery_order_code) {
where.delivery_order_code = data.delivery_order_code;
return where;
}
throw new Error('id 或 delivery_order_code 必填');
}
buildListData(rows, total, page, pageSize, extra = {}) {
return {
list: rows,
pagination: this.buildPagination(page, pageSize, total),
...extra,
};
}
}
module.exports = OrderBaseService;