464 lines
12 KiB
JavaScript

'use strict';
const MainService = require('../main');
class warehouseService extends MainService {
constructor(ctx) {
super(ctx);
const _noNeedLogin = '';
const _noNeedRight = 'detail,getSpacesByRack,createRackWithSpaces,getSpaceInfo,checkBarcodeUnique';
const _noNeedLog = '*';
const _noNeedAuth = 'detail,getSpacesByRack,getSpaceInfo,checkBarcodeUnique,getWarehouseZones,getWarehouseTree,getChildren,toggleActive,getPath,getPickPath,getAreaRacks';
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth);
}
async getWarehouseZones() {
const { ctx, service } = this;
console.log('获取仓库主体下所有实体仓库');
try {
const zones = await service.systems.locationZone.getWarehouseLevelZones(ctx.user.store_id);
return this.success('获取成功', { zones, code: 200 });
} catch (error) {
console.log(error);
return this.fail('获取失败' + error.message, { code: 0 });
}
}
/**
* 获取仓库树状结构
* @param data
*/
async getWarehouseTree(data) {
const { ctx, service } = this;
const { warehouseId } = data;
try {
if (!warehouseId) {
return this.fail('仓库ID不能为空');
}
const tree = await service.systems.locationZone.getZoneTreeByWarehouse(parseInt(warehouseId));
return this.success('获取成功', tree);
} catch (error) {
return this.fail(error.message);
}
}
/**
* 获取子节点(懒加载)
* @param data
*/
async getChildren(data) {
const { ctx, service } = this;
const { zoneId } = data;
try {
if (!zoneId) {
throw new Error('分区ID不能为空');
}
const children = await service.systems.locationZone.getChildrenByZoneId(parseInt(zoneId));
ctx.success(children);
} catch (error) {
this.fail(error.message);
}
}
/**
* 创建分区
* @param data
*/
async create(data) {
const { ctx, service } = this;
try {
const result = await service.systems.locationZone.createZone(data);
return this.success(result.message, { data: result.data, code: 200 });
} catch (error) {
return this.fail(error.message);
}
}
/**
* 更新分区
* @param datas
*/
async update(datas) {
const { ctx, service } = this;
console.log('接收的数据', datas);
const { id } = datas;
const { length, width, height, zone_code, zone_name, x, y, z, direction, rack_columns, rack_levels, rack_rows, cell_width, cell_length, cell_height, aisles } = datas;
try {
if (!id) {
return this.fail('分区ID不能为空');
}
const result = await service.systems.locationZone.updateZone(parseInt(id), { length, width, height, zone_code, zone_name, x, y, z, direction, rack_columns, rack_levels, rack_rows, cell_width, cell_length, cell_height, aisles });
return this.success(result.message, { data: result.data, code: 200 });
} catch (error) {
return this.fail(error.message);
}
}
/**
* 删除分区
* @param data
*/
async delete(data) {
const { ctx, service } = this;
const { id } = data;
try {
if (!id) {
return this.fail('分区ID不能为空');
}
const result = await service.systems.locationZone.deleteZone(parseInt(id));
console.log('删除返回', result);
return this.success(result.message, { code: 200 });
} catch (error) {
console.log('error', error);
return this.fail(error.message);
}
}
/**
* 获取分区详情
* @param data
*/
async detail(data) {
const { ctx, service } = this;
const { id } = data;
try {
if (!id) {
return this.fail('分区ID不能为空');
}
const zone = await service.systems.locationZone.getZoneDetail(parseInt(id));
return this.success('获取成功', zone);
} catch (error) {
return this.fail(error.message);
}
}
async checkBarcodeUnique(data) {
const { ctx, service } = this;
const { id, barcode } = data;
console.log(data);
try {
if (!id) {
return this.fail('库位ID不能为空');
}
if (!barcode) {
return this.fail('条码不能为空');
}
const data = await service.systems.locationZone.checkBarcodeUnique(parseInt(id), barcode);
return this.success('获取成功', { data, code: 200 });
} catch (error) {
return this.fail(error.message);
}
}
/**
* 切换分区状态
* @param data
*/
async toggleActive(data) {
const { ctx, service } = this;
const { id, isActive } = data;
try {
if (!id) {
throw new Error('分区ID不能为空');
}
const result = await service.systems.locationZone.toggleZoneActive(
parseInt(id),
isActive
);
ctx.success(null, result.message);
} catch (error) {
this.fail(error.message);
}
}
/**
* 获取分区路径
* @param data
*/
async getPath(data) {
const { ctx, service } = this;
const { id } = data;
try {
if (!id) {
throw new Error('分区ID不能为空');
}
const path = await service.systems.locationZone.getZonePath(parseInt(id));
ctx.success(path);
} catch (error) {
this.fail(error.message);
}
}
async createRack() {
const { ctx } = this;
const params = ctx.request.body;
// 验证参数
ctx.validate({
warehouse_id: { type: 'int', required: true },
zone_code: { type: 'string', required: true },
zone_name: { type: 'string', required: true },
parent_id: { type: 'int', required: false },
x: { type: 'int', required: true },
y: { type: 'int', required: true },
z: { type: 'int', required: false, default: 0 },
length: { type: 'int', required: true },
width: { type: 'int', required: true },
height: { type: 'int', required: true },
rack_columns: { type: 'int', required: true },
rack_rows: { type: 'int', required: true },
rack_levels: { type: 'int', required: true },
direction: { type: 'enum', values: [ 'north', 'south', 'east', 'west' ], required: false },
color: { type: 'string', required: false },
});
try {
const rack = await ctx.service.location.createRackWithSpaces(params);
ctx.success(rack, '货架创建成功');
} catch (error) {
this.fail(error.message, 400);
}
}
/**
* 获取拣货路径
*/
async getPickPath() {
const { ctx } = this;
const { warehouse_id } = ctx.query;
if (!warehouse_id) {
this.fail('仓库ID不能为空', 400);
return;
}
try {
const options = {
includeSpaces: ctx.query.include_spaces === 'true',
limit: parseInt(ctx.query.limit) || 50,
sortBy: ctx.query.sort_by || 'path_sequence',
};
const result = await ctx.service.location.getPickPath(warehouse_id, options);
ctx.success(result);
} catch (error) {
this.fail(error.message, 500);
}
}
/**
* 更新货架位置
*/
async updateRackPosition() {
const { ctx } = this;
const { rack_id } = ctx.params;
const { x, y, z } = ctx.request.body;
ctx.validate({
x: { type: 'int', required: true },
y: { type: 'int', required: true },
z: { type: 'int', required: false },
});
try {
const rack = await ctx.service.location.updateRackPosition(rack_id, { x, y, z });
ctx.success(rack, '货架位置更新成功');
} catch (error) {
this.fail(error.message, error.status || 400);
}
}
/**
* 重新计算拣货路径
*/
async recalculatePickPath() {
const { ctx } = this;
const { warehouse_id } = ctx.request.body;
if (!warehouse_id) {
this.fail('仓库ID不能为空', 400);
return;
}
try {
await ctx.service.location._calculatePickPathForWarehouse(warehouse_id);
ctx.success(null, '拣货路径重新计算成功');
} catch (error) {
this.fail(error.message, 500);
}
}
/**
* 更新邻居关系
*/
async updateNeighbors() {
const { ctx } = this;
const { warehouse_id } = ctx.request.body;
if (!warehouse_id) {
this.fail('仓库ID不能为空', 400);
return;
}
try {
await ctx.service.location.updateNeighborRelations(warehouse_id);
ctx.success(null, '邻居关系更新成功');
} catch (error) {
this.fail(error.message, 500);
}
}
/**
* 获取库区内的货架列表(按排序)
*/
async getAreaRacks() {
const { ctx } = this;
const { area_id } = ctx.query;
if (!area_id) {
this.fail('库区ID不能为空', 400);
return;
}
try {
const racks = await ctx.model.LocationZone.findAll({
where: {
parent_id: area_id,
zone_type: 'rack',
is_active: true,
deleted_at: null,
},
order: [[ 'path_sequence', 'ASC' ]],
include: [{
model: ctx.model.LocationSpace,
as: 'spaces',
where: {
is_enabled: true,
deleted_at: null,
},
required: false,
}],
});
ctx.success(racks);
} catch (error) {
this.fail(error.message, 500);
}
}
async getSpacesByRack(data) {
const { ctx } = this;
const { id } = data;
if (!id) {
return this.fail('货架ID不能为空');
}
try {
const spaces = await ctx.model.LocationSpace.findAll({
where: {
zone_id: id,
deleted_at: null,
},
order: [[ 'pick_sequence', 'ASC' ]],
});
return this.success('获取成功', { spaces, code: 200 });
} catch (e) {
return this.fail(e.message, 500);
}
}
async createRackWithSpaces(data) {
const { ctx, app, service } = this;
const { id } = data;
if (!id) {
return this.fail('货架ID不能为空');
}
const transaction = await ctx.model.transaction();
try {
const rack = await ctx.model.LocationZone.findOne({
where: {
id,
deleted_at: null,
},
transaction,
});
if (!rack) {
throw new Error('货架不存在');
}
if (rack.zone_type != 'rack') {
throw new Error('仓库元素不为货架,拒绝生成仓位');
}
const message = await service.systems.locationZone.generateSpacesForRack(id, rack, transaction);
await transaction.commit();
return this.success(message, { code: 200 });
} catch (e) {
console.log('创建错误', e);
await transaction.rollback();
return this.fail(e.message, 500);
}
}
async getSpaceInfo(data) {
const { ctx, service } = this;
const { id } = data;
try {
const data = await service.systems.locationZone.getSpaceInfo(id);
return this.success('获取成功', { data, code: 200 });
} catch (error) {
console.log(error);
return this.fail('获取失败' + error.message, { code: 0 });
}
}
async updateSpaceInfo(data) {
const { ctx, app, service } = this;
console.log('修改的数据', data);
const { id } = data;
if (!id) {
return this.fail('仓位ID不能为空');
}
const transaction = await ctx.model.transaction();
try {
const space = await ctx.model.LocationSpace.findOne({
where: {
id,
warehouse_id: ctx.user.store_id,
deleted_at: null,
},
transaction,
});
if (!space) {
throw new Error('仓位不存在');
}
if (data.custom_barcode) {
const { isUnique } = await service.systems.locationZone.checkBarcodeUnique(parseInt(id), data.custom_barcode);
if (!isUnique) {
throw new Error('自定义条码已存在');
}
}
const update_data = {
location_type_id: data.location_type_id || space.location_type_id,
custom_name: data.custom_name || null,
custom_barcode: data.custom_barcode || null,
space_status: data.space_status || space.space_status,
pick_sequence: data.pick_sequence || 0,
};
await space.update(update_data);
await transaction.commit();
return this.success('保存成功', { code: 200 });
} catch (e) {
await transaction.rollback();
return this.fail('保存失败:' + e.message, { code: 500 });
}
}
}
module.exports = warehouseService;