'use strict'; const MainService = require('../../main'); const SUPPORTED_BUSINESS_TYPES = [ 'STOCK', 'RECEIPT', 'PICKING', 'OUTBOUND', 'ENTRY_ORDER' ]; class printTemplateService extends MainService { constructor(ctx) { super(ctx); const _noNeedLogin = ''; const _noNeedRight = 'list,save,create,delete,listTemplatesByType'; const _noNeedLog = '*'; const _noNeedAuth = 'list,save,create,delete,listTemplatesByType'; this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth); } async list() { const { service } = this; const result = await service.systems.setting.printTemplate.getTree(); return this.success('ok', { presetTree: result, code: 200 }); } /** * 规范化并校验业务类型 * @param {string} businessType 业务类型 * @return {string} 标准化后的业务类型 */ normalizeAndValidateBusinessType(businessType) { const normalizedBusinessType = String(businessType || '').trim().toUpperCase(); if (!normalizedBusinessType) { throw new Error('business_type 参数不能为空'); } if (!SUPPORTED_BUSINESS_TYPES.includes(normalizedBusinessType)) { throw new Error(`business_type 参数非法,仅支持:${SUPPORTED_BUSINESS_TYPES.join('、')}`); } return normalizedBusinessType; } /** * 保存打印模板 * @param {Object} data 入参 * @return {Promise} 保存结果 */ async save(data) { const { service } = this; const payload = data || {}; try { if (payload.business_type !== undefined && payload.business_type !== null && String(payload.business_type).trim() !== '') { payload.business_type = this.normalizeAndValidateBusinessType(payload.business_type); } } catch (error) { return this.fail(error.message, { code: 400 }); } try { const result = await service.systems.setting.printTemplate.save(payload); return this.success('ok', result); } catch (error) { const message = error && error.message ? error.message : '保存打印模板失败'; if (message.includes('business_type 参数')) { return this.fail(message, { code: 400 }); } return this.fail(message, { code: 500 }); } } /** * 创建打印模板 * @param {Object} data 入参 * @return {Promise} 创建结果 */ async create(data) { const { service } = this; const { template_name, business_type, page_width, page_height } = data || {}; if (!template_name || String(template_name).trim() === '') { return this.fail('template_name 参数不能为空', { code: 400 }); } let normalizedBusinessType = ''; try { normalizedBusinessType = this.normalizeAndValidateBusinessType(business_type); } catch (error) { return this.fail(error.message, { code: 400 }); } if (page_width === undefined || page_width === null || page_width === '') { return this.fail('page_width 参数不能为空', { code: 400 }); } if (page_height === undefined || page_height === null || page_height === '') { return this.fail('page_height 参数不能为空', { code: 400 }); } try { const result = await service.systems.setting.printTemplate.createTemplate({ ...data, business_type: normalizedBusinessType, }); return this.success('ok', result); } catch (error) { if (error.message === '同一业务类型和平台下已存在自定义模板') { return this.fail(error.message, { code: 409 }); } if (error.message && error.message.includes('business_type 参数')) { return this.fail(error.message, { code: 400 }); } return this.fail(error.message || '创建打印模板失败', { code: 500 }); } } /** * 删除打印模板 * @param {Object} data 入参 * @return {Promise} 删除结果 */ async delete(data) { const { service } = this; const { id } = data || {}; const templateId = Number(id); if (!templateId || Number.isNaN(templateId)) { return this.fail('id 参数不能为空', { code: 400 }); } try { const result = await service.systems.setting.printTemplate.deleteTemplate(templateId); return this.success('ok', result); } catch (error) { if (error.message === '模板不存在或无权限') { return this.fail(error.message, { code: 404 }); } return this.fail(error.message || '删除打印模板失败', { code: 500 }); } } /** * 按业务类型查询模板 * @param {Object} data 入参 * @return {Promise} 查询结果 */ async listTemplatesByType(data) { const { service } = this; const { businessType } = data || {}; let normalizedBusinessType = ''; try { normalizedBusinessType = this.normalizeAndValidateBusinessType(businessType); } catch (error) { return this.fail(error.message, { code: 400 }); } const list = await service.systems.setting.printTemplate.listTemplatesByType(normalizedBusinessType); return this.success('ok', { data: list, code: 200 }); } } module.exports = printTemplateService;