174 lines
5.1 KiB
JavaScript
174 lines
5.1 KiB
JavaScript
'use strict';
|
||
|
||
const MainService = require('../../main');
|
||
|
||
/**
|
||
* 系统方法层:storage.stockout(非订单出库单)。
|
||
* 说明:该实现为最小可用能力,复用订单分表模型读取当前仓库的出库单与明细。
|
||
*/
|
||
class StorageStockoutService extends MainService {
|
||
constructor(ctx) {
|
||
super(ctx);
|
||
const _noNeedLogin = '';
|
||
const _noNeedRight = 'list,toOrder,listItems';
|
||
const _noNeedLog = 'list,toOrder,listItems';
|
||
const _noNeedAuth = 'list,toOrder,listItems';
|
||
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth);
|
||
}
|
||
|
||
/**
|
||
* 出库单列表查询。
|
||
* @param {Object} data 请求体
|
||
* @param {Object} [data.where] 查询条件
|
||
* @param {Object} [data.pagination] 分页参数
|
||
* @param {Object} [data.sort] 排序参数
|
||
* @return {Promise<Object>} 统一成功包装响应
|
||
*/
|
||
async list(data = {}) {
|
||
const { ctx } = this;
|
||
const { Op } = this.app.Sequelize;
|
||
const { pagination = {}, sort = {} } = data;
|
||
|
||
const { parsed } = this.prepareWhereContext(data, {
|
||
specialFieldMap: {
|
||
quickWhere: {
|
||
bucket: 'quick',
|
||
},
|
||
},
|
||
});
|
||
|
||
const page = Math.max(Number(pagination.page) || 1, 1);
|
||
const pageSize = Math.max(Number(pagination.pageSize) || 20, 1);
|
||
const warehouseId = Number(ctx.user && ctx.user.store_id) || 0;
|
||
const OrderDeliveryModel = await ctx.model.OrderDeliveryFactory.getCurrentModel(warehouseId);
|
||
|
||
const andConditions = [
|
||
{ warehouse_id: warehouseId },
|
||
{ delete_at: null },
|
||
];
|
||
|
||
const dynamicConditions = Array.isArray(parsed && parsed.dynamicConditions) ? parsed.dynamicConditions : [];
|
||
if (dynamicConditions.length > 0) {
|
||
andConditions.push({ [Op.and]: dynamicConditions });
|
||
}
|
||
|
||
const quickWhereCondition = parsed && parsed.rawConditionsByField && parsed.rawConditionsByField.quickWhere;
|
||
const quickValue = quickWhereCondition && Object.prototype.hasOwnProperty.call(quickWhereCondition, 'value')
|
||
? quickWhereCondition.value
|
||
: null;
|
||
if (quickValue !== null && quickValue !== undefined && String(quickValue).trim() !== '') {
|
||
const keyword = String(quickValue).trim();
|
||
andConditions.push({
|
||
[Op.or]: [
|
||
{ delivery_order_code: { [Op.like]: `%${keyword}%` } },
|
||
{ source_order_codes: { [Op.like]: `%${keyword}%` } },
|
||
{ buyer_nick: { [Op.like]: `%${keyword}%` } },
|
||
{ mobile: { [Op.like]: `%${keyword}%` } },
|
||
{ name: { [Op.like]: `%${keyword}%` } },
|
||
],
|
||
});
|
||
}
|
||
|
||
const validSortFields = [
|
||
'id',
|
||
'delivery_order_code',
|
||
'status',
|
||
'outbound_status',
|
||
'updated_at',
|
||
'create_at',
|
||
'delivery_time',
|
||
];
|
||
const sortField = validSortFields.includes(String(sort.sortField || '').trim())
|
||
? String(sort.sortField).trim()
|
||
: 'updated_at';
|
||
const sortOrder = String(sort.sortOrder || 'DESC').toUpperCase() === 'ASC' ? 'ASC' : 'DESC';
|
||
|
||
const finalWhere = { [Op.and]: andConditions };
|
||
const { count, rows } = await OrderDeliveryModel.findAndCountAll({
|
||
where: finalWhere,
|
||
order: [[ sortField, sortOrder ], [ 'id', 'DESC' ]],
|
||
limit: pageSize,
|
||
offset: (page - 1) * pageSize,
|
||
raw: true,
|
||
});
|
||
|
||
return this.success('ok', {
|
||
list: rows,
|
||
pagination: {
|
||
page,
|
||
pageSize,
|
||
total: count,
|
||
totalPages: Math.ceil(count / pageSize),
|
||
hasPrevious: page > 1,
|
||
hasNext: page < Math.ceil(count / pageSize),
|
||
},
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 出库单转订单占位接口。
|
||
* @return {Promise<Object>} 统一失败包装响应
|
||
*/
|
||
async toOrder() {
|
||
return this.fail('接口暂未开放');
|
||
}
|
||
|
||
/**
|
||
* 出库单明细查询。
|
||
* @param {Object} data 请求体
|
||
* @param {number} [data.id] 出库单ID
|
||
* @param {string} [data.delivery_order_code] 出库单号
|
||
* @return {Promise<Object>} 统一成功包装响应
|
||
*/
|
||
async listItems(data = {}) {
|
||
const { ctx } = this;
|
||
const warehouseId = Number(ctx.user && ctx.user.store_id) || 0;
|
||
const id = Number(data.id || 0);
|
||
const deliveryOrderCode = String(data.delivery_order_code || '').trim();
|
||
|
||
if (!id && !deliveryOrderCode) {
|
||
return this.fail('id 或 delivery_order_code 至少传一个');
|
||
}
|
||
|
||
const OrderDeliveryModel = await ctx.model.OrderDeliveryFactory.getCurrentModel(warehouseId);
|
||
const OrderItemsModel = await ctx.model.OrderItemsFactory.getCurrentModel(warehouseId);
|
||
|
||
const where = {
|
||
warehouse_id: warehouseId,
|
||
delete_at: null,
|
||
};
|
||
if (id > 0) {
|
||
where.id = id;
|
||
} else {
|
||
where.delivery_order_code = deliveryOrderCode;
|
||
}
|
||
|
||
const order = await OrderDeliveryModel.findOne({
|
||
where,
|
||
raw: true,
|
||
});
|
||
|
||
if (!order || !order.id) {
|
||
return this.success('ok', {
|
||
order: null,
|
||
list: [],
|
||
});
|
||
}
|
||
|
||
const list = await OrderItemsModel.findAll({
|
||
where: {
|
||
order_id: order.id,
|
||
},
|
||
order: [[ 'id', 'ASC' ]],
|
||
raw: true,
|
||
});
|
||
|
||
return this.success('ok', {
|
||
order,
|
||
list,
|
||
});
|
||
}
|
||
}
|
||
|
||
module.exports = StorageStockoutService;
|