'use strict'; const MainService = require('../../main'); class packageSupplierService 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} 统一返回结果 */ async callSystemPackageSupplier(methodName, data, defaultMessage) { try { const result = await this.service.systems.setting.packageSupplier[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} 删除 ID 列表 */ resolveDeleteIds(data = {}) { const { ids, id, supplier_id: supplierId, supplierId: camelSupplierId, } = data || {}; const rawIds = ids !== undefined ? ids : (id !== undefined ? id : (supplierId !== undefined ? supplierId : camelSupplierId)); 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} 列表结果 */ 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', }, supplier_code: { field: 'supplier_code', }, supplier_name: { field: 'supplier_name', }, contact_name: { field: 'contact_name', }, contact_phone: { field: 'contact_phone', }, status: { field: 'status', }, quickWhere: { bucket: 'quick', }, }, }); return await this.callSystemPackageSupplier('get', payload, '获取包材供应商列表失败'); } /** * 兼容旧式 list 命名,内部转发至 get。 * * @param {Object} data 请求参数 * @return {Promise} 列表结果 */ async list(data = {}) { return await this.get(data); } /** * 创建包材供应商。 * * @param {Object} data 请求参数 * @return {Promise} 创建结果 */ async create(data = {}) { const { supplier_code: supplierCode, supplier_name: supplierName, contact_name: contactName, contact_phone: contactPhone, status, } = data || {}; const payload = { supplier_code: supplierCode, supplier_name: supplierName, contact_name: contactName, contact_phone: contactPhone, status, }; return await this.callSystemPackageSupplier('create', payload, '创建包材供应商失败'); } /** * 更新包材供应商。 * * @param {Object} data 请求参数 * @return {Promise} 更新结果 */ async update(data = {}) { const { id, supplier_id: supplierId, supplierId: camelSupplierId, supplier_code: supplierCode, supplier_name: supplierName, contact_name: contactName, contact_phone: contactPhone, status, } = data || {}; const payload = { id: id !== undefined ? id : (supplierId !== undefined ? supplierId : camelSupplierId), supplier_code: supplierCode, supplier_name: supplierName, contact_name: contactName, contact_phone: contactPhone, status, }; return await this.callSystemPackageSupplier('update', payload, '更新包材供应商失败'); } /** * 删除包材供应商。 * * @param {Object} data 请求参数 * @return {Promise} 删除结果 */ async delete(data = {}) { const { ids, id, supplier_id: supplierId, supplierId: camelSupplierId, } = data || {}; const payload = { ids: this.resolveDeleteIds({ ids, id, supplier_id: supplierId, supplierId: camelSupplierId, }), }; return await this.callSystemPackageSupplier('delete', payload, '删除包材供应商失败'); } } module.exports = packageSupplierService;