204 lines
6.1 KiB
JavaScript
204 lines
6.1 KiB
JavaScript
'use strict';
|
|
|
|
const OrderBaseService = require('./base');
|
|
|
|
class OrderIndexService extends OrderBaseService {
|
|
constructor(ctx) {
|
|
super(ctx);
|
|
const _noNeedLogin = '';
|
|
const _noNeedRight = 'list,detail,listItems,stats,getreviewOrderlist,updateStatus,updateDeliveryTime,reviewOrder';
|
|
const _noNeedLog = 'list,detail,listItems,stats,getreviewOrderlist';
|
|
const _noNeedAuth = 'list,detail,listItems,stats,getreviewOrderlist,updateStatus,updateDeliveryTime,reviewOrder';
|
|
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth);
|
|
}
|
|
|
|
async list(data = {}) {
|
|
try {
|
|
const warehouseId = this.ensureWarehouseId(data);
|
|
const { DeliveryModel } = await this.getOrderModels(warehouseId);
|
|
const where = this.buildWhere(data.where || {}, warehouseId);
|
|
const { page, pageSize, offset } = this.getPagination(data.pagination);
|
|
|
|
const result = await DeliveryModel.findAndCountAll({
|
|
where,
|
|
offset,
|
|
limit: pageSize,
|
|
order: [ [ 'updated_at', 'DESC' ], [ 'id', 'DESC' ] ],
|
|
raw: true,
|
|
});
|
|
|
|
return this.success('ok', {
|
|
data: this.buildListData(result.rows, result.count, page, pageSize),
|
|
code: 200,
|
|
});
|
|
} catch (error) {
|
|
return this.fail(error.message, { code: 500 });
|
|
}
|
|
}
|
|
|
|
async detail(data = {}) {
|
|
try {
|
|
const warehouseId = this.ensureWarehouseId(data);
|
|
const { DeliveryModel, OrderItemsModel } = await this.getOrderModels(warehouseId);
|
|
const where = this.getOrderLocatorWhere(data, warehouseId);
|
|
|
|
const order = await DeliveryModel.findOne({ where, raw: true });
|
|
if (!order) {
|
|
return this.fail('订单不存在', { code: 404 });
|
|
}
|
|
|
|
const items = await OrderItemsModel.findAll({
|
|
where: { order_id: order.id },
|
|
order: [ [ 'id', 'ASC' ] ],
|
|
raw: true,
|
|
});
|
|
|
|
return this.success('ok', {
|
|
data: { order, items },
|
|
code: 200,
|
|
});
|
|
} catch (error) {
|
|
return this.fail(error.message, { code: 500 });
|
|
}
|
|
}
|
|
|
|
async listItems(data = {}) {
|
|
try {
|
|
const warehouseId = this.ensureWarehouseId(data);
|
|
const { DeliveryModel, OrderItemsModel } = await this.getOrderModels(warehouseId);
|
|
const where = this.getOrderLocatorWhere(data, warehouseId);
|
|
|
|
const order = await DeliveryModel.findOne({
|
|
where,
|
|
attributes: [ 'id', 'delivery_order_code' ],
|
|
raw: true,
|
|
});
|
|
if (!order) {
|
|
return this.fail('订单不存在', { code: 404 });
|
|
}
|
|
|
|
const items = await OrderItemsModel.findAll({
|
|
where: { order_id: order.id },
|
|
order: [ [ 'id', 'ASC' ] ],
|
|
raw: true,
|
|
});
|
|
|
|
return this.success('ok', {
|
|
data: { order, items },
|
|
code: 200,
|
|
});
|
|
} catch (error) {
|
|
return this.fail(error.message, { code: 500 });
|
|
}
|
|
}
|
|
|
|
async stats(data = {}) {
|
|
try {
|
|
const warehouseId = this.ensureWarehouseId(data);
|
|
const { Sequelize } = this.app;
|
|
const { DeliveryModel } = await this.getOrderModels(warehouseId);
|
|
const where = this.buildWhere(data.where || {}, warehouseId);
|
|
|
|
const total = await DeliveryModel.count({ where });
|
|
const byStatusRows = await DeliveryModel.findAll({
|
|
where,
|
|
attributes: [ 'status', [ Sequelize.fn('COUNT', Sequelize.col('id')), 'count' ] ],
|
|
group: [ 'status' ],
|
|
raw: true,
|
|
});
|
|
const byOutboundRows = await DeliveryModel.findAll({
|
|
where,
|
|
attributes: [ 'outbound_status', [ Sequelize.fn('COUNT', Sequelize.col('id')), 'count' ] ],
|
|
group: [ 'outbound_status' ],
|
|
raw: true,
|
|
});
|
|
|
|
return this.success('ok', {
|
|
data: {
|
|
total,
|
|
byStatus: byStatusRows,
|
|
byOutboundStatus: byOutboundRows,
|
|
},
|
|
code: 200,
|
|
});
|
|
} catch (error) {
|
|
return this.fail(error.message, { code: 500 });
|
|
}
|
|
}
|
|
|
|
async getreviewOrderlist(data = {}) {
|
|
return await this.ctx.service.systemMethods.order.reviewOrder.getreviewOrderlist(data);
|
|
}
|
|
|
|
async updateStatus(data = {}) {
|
|
try {
|
|
const warehouseId = this.ensureWarehouseId(data);
|
|
const { DeliveryModel } = await this.getOrderModels(warehouseId);
|
|
const where = this.getOrderLocatorWhere(data, warehouseId);
|
|
|
|
const allowedFields = [
|
|
'status',
|
|
'post_status',
|
|
'storage_status',
|
|
'pick_status',
|
|
'pack_status',
|
|
'inspection_status',
|
|
'print_status',
|
|
'claim_status',
|
|
'outbound_status',
|
|
'lock_status',
|
|
'intercept_status',
|
|
];
|
|
|
|
const updateData = {};
|
|
for (const key of allowedFields) {
|
|
if (data[key] !== undefined && data[key] !== null && data[key] !== '') {
|
|
updateData[key] = data[key];
|
|
}
|
|
}
|
|
|
|
if (data.delivery_time) {
|
|
updateData.delivery_time = this.parseDateTime(data.delivery_time, 'delivery_time');
|
|
}
|
|
|
|
if (!updateData.delivery_time && updateData.outbound_status === 'fin') {
|
|
updateData.delivery_time = new Date();
|
|
}
|
|
|
|
if (Object.keys(updateData).length === 0) {
|
|
return this.fail('没有可更新字段', { code: 400 });
|
|
}
|
|
|
|
updateData.updated_at = new Date();
|
|
|
|
const [ affected ] = await DeliveryModel.update(updateData, { where });
|
|
if (!affected) {
|
|
return this.fail('订单不存在或未更新', { code: 404 });
|
|
}
|
|
|
|
const order = await DeliveryModel.findOne({ where, raw: true });
|
|
return this.success('更新成功', { data: order, code: 200 });
|
|
} catch (error) {
|
|
return this.fail(error.message, { code: 500 });
|
|
}
|
|
}
|
|
|
|
async updateDeliveryTime(data = {}) {
|
|
if (!data.delivery_time) {
|
|
return this.fail('delivery_time 必填', { code: 400 });
|
|
}
|
|
|
|
const nextData = { ...data };
|
|
if (nextData.syncOutboundStatus === true || nextData.syncOutboundStatus === 1 || nextData.syncOutboundStatus === '1') {
|
|
nextData.outbound_status = 'fin';
|
|
}
|
|
|
|
return await this.updateStatus(nextData);
|
|
}
|
|
|
|
async reviewOrder(data = {}) {
|
|
return await this.ctx.service.systemMethods.order.reviewOrder.reviewOrder(data);
|
|
}
|
|
}
|
|
|
|
module.exports = OrderIndexService; |