'use strict'; const MainService = require('../main'); class WorkbenchService extends MainService { constructor(ctx) { super(ctx); const _noNeedLogin = 'business'; const _noNeedRight = 'list,detail,checkBarcode,business'; const _noNeedLog = 'list,detail,checkBarcode'; const _noNeedAuth = 'list,detail,checkBarcode,business'; this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth); } /** * 工作台列表(分页 + 筛选) */ async list(data) { const { service } = this; try { const result = await service.systems.workbench.list(data || {}); return this.success('获取成功', result); } catch (error) { return this.fail(error.message); } } /** * 工作台详情 */ async detail(data) { const { service } = this; try { const { id } = data || {}; if (!id) return this.fail('工作台ID不能为空'); const record = await service.systems.workbench.detail(parseInt(id)); return this.success('获取成功', record); } catch (error) { return this.fail(error.message); } } /** * 创建工作台 */ async create(data) { const { service } = this; try { const record = await service.systems.workbench.create(data); return this.success('创建成功', record); } catch (error) { return this.fail(error.message); } } /** * 更新工作台 */ async update(data) { const { service } = this; try { const { id, ...fields } = data || {}; if (!id) return this.fail('工作台ID不能为空'); const record = await service.systems.workbench.update(parseInt(id), fields); return this.success('更新成功', record); } catch (error) { return this.fail(error.message); } } /** * 删除工作台(软删除) */ async delete(data) { const { service } = this; try { const { id } = data || {}; if (!id) return this.fail('工作台ID不能为空'); await service.systems.workbench.delete(parseInt(id)); return this.success('删除成功'); } catch (error) { return this.fail(error.message); } } /** * 工作台业务入口(免登录、免鉴权) * @param {Object} data - { name, data } */ async business(data) { const { ctx, service } = this; const { name, data: bizData } = data || {}; ctx.logger.info('[Workbench.business] 收到业务请求 name=%s data=%j', name, bizData); try { const result = await service.systems.workbench.business(name, bizData); return this.success('ok', result); } catch (error) { ctx.logger.error('[Workbench.business] 业务处理失败 name=%s error=%s', name, error.message); return this.fail(error.message); } } /** * 工作台绑定摄像头 */ async bindCamera(data) { const { service } = this; try { const { id, camera_ids } = data || {}; if (!id) return this.fail('工作台ID不能为空'); const record = await service.systems.workbench.bindCamera(parseInt(id), camera_ids); return this.success('绑定成功', record); } catch (error) { return this.fail(error.message); } } /** * 检查条码是否已存在 */ async checkBarcode(data) { const { service } = this; try { const { barcode, excludeId } = data || {}; if (!barcode) return this.fail('条码不能为空'); const exists = await service.systems.workbench.checkBarcodeExists( String(barcode).trim(), excludeId ? parseInt(excludeId) : undefined ); return this.success('ok', { exists }); } catch (error) { return this.fail(error.message); } } } module.exports = WorkbenchService;