255 lines
5.7 KiB
JavaScript
255 lines
5.7 KiB
JavaScript
'use strict';
|
|
|
|
const MainService = require('../../main');
|
|
|
|
class packageMaterialService extends MainService {
|
|
/**
|
|
* 初始化包材设置模块的权限配置。
|
|
*
|
|
* @param {Egg.Context} ctx 上下文对象
|
|
*/
|
|
constructor(ctx) {
|
|
super(ctx);
|
|
const _noNeedLogin = '';
|
|
const _noNeedRight = 'get,list,create,update,delete';
|
|
const _noNeedLog = '*';
|
|
const _noNeedAuth = 'get,list,create,update,delete';
|
|
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth);
|
|
}
|
|
|
|
/**
|
|
* 调用包材系统服务。
|
|
*
|
|
* @param {string} methodName 系统服务方法名
|
|
* @param {Object} data 请求参数
|
|
* @param {string} defaultMessage 默认错误信息
|
|
* @return {Promise<Object>} 统一返回结果
|
|
*/
|
|
async callSystemPackageMaterial(methodName, data, defaultMessage) {
|
|
try {
|
|
const result = await this.service.systems.setting.packageMaterial[methodName](data);
|
|
return this.success('ok', result);
|
|
} catch (error) {
|
|
return this.fail(error.message || defaultMessage, {
|
|
code: Number(error.code) || 500,
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 归一化删除 ID 列表。
|
|
*
|
|
* @param {Object} data 请求参数
|
|
* @return {Array<number>} 删除 ID 列表
|
|
*/
|
|
resolveDeleteIds(data = {}) {
|
|
const {
|
|
ids,
|
|
id,
|
|
material_id: materialId,
|
|
materialId: camelMaterialId,
|
|
} = data || {};
|
|
|
|
const rawIds = ids !== undefined
|
|
? ids
|
|
: (id !== undefined ? id : (materialId !== undefined ? materialId : camelMaterialId));
|
|
|
|
const values = Array.isArray(rawIds)
|
|
? rawIds
|
|
: String(rawIds || '')
|
|
.split(',')
|
|
.map(item => item.trim())
|
|
.filter(Boolean);
|
|
|
|
const normalizedIds = [];
|
|
const seenIds = new Set();
|
|
|
|
for (const value of values) {
|
|
const normalized = Number(value);
|
|
if (!Number.isInteger(normalized) || normalized <= 0 || seenIds.has(normalized)) {
|
|
continue;
|
|
}
|
|
|
|
seenIds.add(normalized);
|
|
normalizedIds.push(normalized);
|
|
}
|
|
|
|
return normalizedIds;
|
|
}
|
|
|
|
/**
|
|
* 查询包材列表。
|
|
*
|
|
* @param {Object} data 请求参数
|
|
* @return {Promise<Object>} 列表结果
|
|
*/
|
|
async get(data = {}) {
|
|
const {
|
|
where = {},
|
|
pagination: inputPagination,
|
|
page,
|
|
page_size: pageSize,
|
|
pageSize: camelPageSize,
|
|
...rest
|
|
} = data || {};
|
|
|
|
const pagination = {
|
|
...(inputPagination && typeof inputPagination === 'object' ? inputPagination : {}),
|
|
};
|
|
|
|
if (page !== undefined) {
|
|
pagination.page = page;
|
|
}
|
|
|
|
const resolvedPageSize = pageSize !== undefined ? pageSize : camelPageSize;
|
|
if (resolvedPageSize !== undefined) {
|
|
pagination.pageSize = resolvedPageSize;
|
|
}
|
|
|
|
const payload = {
|
|
...rest,
|
|
where,
|
|
pagination,
|
|
};
|
|
|
|
this.prepareWhereContext(payload, {
|
|
specialFieldMap: {
|
|
id: {
|
|
field: 'id',
|
|
},
|
|
material_code: {
|
|
field: 'material_code',
|
|
},
|
|
material_name: {
|
|
field: 'material_name',
|
|
},
|
|
material_type: {
|
|
field: 'material_type',
|
|
},
|
|
status: {
|
|
field: 'status',
|
|
},
|
|
quickWhere: {
|
|
bucket: 'quick',
|
|
},
|
|
},
|
|
});
|
|
|
|
return await this.callSystemPackageMaterial('get', payload, '获取包材列表失败');
|
|
}
|
|
|
|
/**
|
|
* 兼容旧式 list 命名,内部转发至 get。
|
|
*
|
|
* @param {Object} data 请求参数
|
|
* @return {Promise<Object>} 列表结果
|
|
*/
|
|
async list(data = {}) {
|
|
return await this.get(data);
|
|
}
|
|
|
|
/**
|
|
* 创建包材。
|
|
*
|
|
* @param {Object} data 请求参数
|
|
* @return {Promise<Object>} 创建结果
|
|
*/
|
|
async create(data = {}) {
|
|
const {
|
|
material_code: materialCode,
|
|
material_name: materialName,
|
|
material_type: materialType,
|
|
spec,
|
|
length,
|
|
width,
|
|
height,
|
|
weight,
|
|
cost_price: costPrice,
|
|
status,
|
|
} = data || {};
|
|
|
|
const payload = {
|
|
material_code: materialCode,
|
|
material_name: materialName,
|
|
material_type: materialType,
|
|
spec,
|
|
length,
|
|
width,
|
|
height,
|
|
weight,
|
|
cost_price: costPrice,
|
|
status,
|
|
};
|
|
|
|
return await this.callSystemPackageMaterial('create', payload, '创建包材失败');
|
|
}
|
|
|
|
/**
|
|
* 更新包材。
|
|
*
|
|
* @param {Object} data 请求参数
|
|
* @return {Promise<Object>} 更新结果
|
|
*/
|
|
async update(data = {}) {
|
|
const {
|
|
id,
|
|
material_id: materialId,
|
|
materialId: camelMaterialId,
|
|
material_code: materialCode,
|
|
material_name: materialName,
|
|
material_type: materialType,
|
|
spec,
|
|
length,
|
|
width,
|
|
height,
|
|
weight,
|
|
cost_price: costPrice,
|
|
status,
|
|
} = data || {};
|
|
|
|
const payload = {
|
|
id: id !== undefined ? id : (materialId !== undefined ? materialId : camelMaterialId),
|
|
material_code: materialCode,
|
|
material_name: materialName,
|
|
material_type: materialType,
|
|
spec,
|
|
length,
|
|
width,
|
|
height,
|
|
weight,
|
|
cost_price: costPrice,
|
|
status,
|
|
};
|
|
|
|
return await this.callSystemPackageMaterial('update', payload, '更新包材失败');
|
|
}
|
|
|
|
/**
|
|
* 删除包材。
|
|
*
|
|
* @param {Object} data 请求参数
|
|
* @return {Promise<Object>} 删除结果
|
|
*/
|
|
async delete(data = {}) {
|
|
const {
|
|
ids,
|
|
id,
|
|
material_id: materialId,
|
|
materialId: camelMaterialId,
|
|
} = data || {};
|
|
|
|
const payload = {
|
|
ids: this.resolveDeleteIds({
|
|
ids,
|
|
id,
|
|
material_id: materialId,
|
|
materialId: camelMaterialId,
|
|
}),
|
|
};
|
|
|
|
return await this.callSystemPackageMaterial('delete', payload, '删除包材失败');
|
|
}
|
|
}
|
|
|
|
module.exports = packageMaterialService;
|