'use strict'; const MainService = require('../../main'); /** * 规范仓库配置方法入参,避免把原始引用直接传给下游服务。 * * @param {Object} data 请求数据。 * @return {Object} 复制后的请求数据。 */ function normalizeStoreWarehousePayload(data) { if (!data || typeof data !== 'object' || Array.isArray(data)) { return {}; } return { ...data }; } class StoreWarehouseStoreService extends MainService { /** * 创建一个仓库配置 store 服务。 * * @param {Object} ctx 上下文对象。 */ constructor(ctx) { super(ctx); const _noNeedLogin = ''; const _noNeedRight = 'list,sign,confirmSign,unbind,rename,openAccount,getPolicyConfig,getPolicyConfigFields,savePolicyConfig,listApiConfig,saveApiConfig,deleteQimenConfig'; const _noNeedLog = '*'; const _noNeedAuth = 'list,sign,confirmSign,unbind,rename,openAccount,getPolicyConfig,getPolicyConfigFields,savePolicyConfig,listApiConfig,saveApiConfig,deleteQimenConfig'; this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth); } /** * 获取签约列表(货主侧)。 * * @param {Object} data 请求数据。 * @return {Promise} 统一响应结果。 */ async list(data = {}) { const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse; if (!targetService || typeof targetService.listForStore !== 'function') { return this.fail('获取签约列表失败', { code: 500, }); } try { const payload = normalizeStoreWarehousePayload(data); const result = await targetService.listForStore(payload); return this.success('ok', result); } catch (error) { return this.fail(error.message || '获取签约列表失败', { code: Number(error.code) || 500, }); } } /** * 发起签约。 * * @param {Object} data 请求数据。 * @return {Promise} 统一响应结果。 */ async sign(data = {}) { const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse; if (!targetService || typeof targetService.sign !== 'function') { return this.fail('签约失败', { code: 500, }); } try { const payload = normalizeStoreWarehousePayload(data); const result = await targetService.sign(payload); return this.success('ok', result); } catch (error) { return this.fail(error.message || '签约失败', { code: Number(error.code) || 500, }); } } /** * 确认签约:对方主体确认待签约的申请。 * * @param {Object} data 请求数据。 * @return {Promise} 统一响应结果。 */ async confirmSign(data = {}) { const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse; if (!targetService || typeof targetService.confirmSign !== 'function') { return this.fail('确认签约失败', { code: 500 }); } try { const payload = normalizeStoreWarehousePayload(data); const result = await targetService.confirmSign(payload); return this.success('ok', result); } catch (error) { return this.fail(error.message || '确认签约失败', { code: Number(error.code) || 500, }); } } /** * 修改签约名称。 * * @param {Object} data 请求数据。 * @return {Promise} 统一响应结果。 */ async rename(data = {}) { const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse; if (!targetService || typeof targetService.rename !== 'function') { return this.fail('修改签约名称失败', { code: 500, }); } try { const payload = normalizeStoreWarehousePayload(data); const result = await targetService.rename(payload); return this.success('ok', result); } catch (error) { return this.fail(error.message || '修改签约名称失败', { code: Number(error.code) || 500, }); } } /** * 解除签约。 * * @param {Object} data 请求数据。 * @return {Promise} 统一响应结果。 */ async unbind(data = {}) { const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse; if (!targetService || typeof targetService.unbind !== 'function') { return this.fail('解除签约失败', { code: 500, }); } try { const payload = normalizeStoreWarehousePayload(data); const result = await targetService.unbind(payload); return this.success('ok', result); } catch (error) { return this.fail(error.message || '解除签约失败', { code: Number(error.code) || 500, }); } } /** * 人工调整面单余额。 * * @param {Object} data 请求数据。 * @return {Promise} 统一响应结果。 */ async adjustPrintQuantity(data = {}) { const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse; if (!targetService || typeof targetService.adjustPrintQuantity !== 'function') { return this.fail('人工调整面单余额失败', { code: 500, }); } try { const payload = normalizeStoreWarehousePayload(data); const result = await targetService.adjustPrintQuantity(payload); return this.success('ok', result); } catch (error) { return this.fail(error.message || '人工调整面单余额失败', { code: Number(error.code) || 500, }); } } /** * 查询面单余额变更日志(分页)。 * * @param {Object} data 请求数据。 * @param {number|string} data.store_id 货主 ID。 * @param {Object} [data.where] 筛选条件。 * @param {Object} [data.pagination] 分页对象。 * @param {number|string} [data.pagination.page] 页码。 * @param {number|string} [data.pagination.page_size] 每页条数。 * @return {Promise} 统一响应结果。 */ async queryPrintQuantityLogs(data = {}) { const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse; if (!targetService || typeof targetService.queryPrintQuantityLogs !== 'function') { return this.fail('查询面单余额变更日志失败', { code: 500, }); } try { const requestData = normalizeStoreWarehousePayload(data); const { where } = this.prepareWhereContext(requestData); const { store_id, } = requestData; const rawPagination = requestData.pagination && typeof requestData.pagination === 'object' ? requestData.pagination : {}; const { page, page_size, } = rawPagination; const result = await targetService.queryPrintQuantityLogs({ store_id, where, pagination: { page, page_size, }, }); return this.success('ok', result); } catch (error) { return this.fail(error.message || '查询面单余额变更日志失败', { code: Number(error.code) || 500, }); } } /** * 获取合约策略配置。 * * @param {Object} data 请求数据。 * @return {Promise} 统一响应结果。 */ async getPolicyConfig(data = {}) { const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse; if (!targetService || typeof targetService.getPolicyConfig !== 'function') { return this.fail('获取合约策略配置失败', { code: 500, }); } try { const payload = normalizeStoreWarehousePayload(data); const result = await targetService.getPolicyConfig(payload); return this.success('ok', result); } catch (error) { return this.fail(error.message || '获取合约策略配置失败', { code: Number(error.code) || 500, }); } } /** * 获取合约策略字段信息。 * * @param {Object} data 请求数据。 * @return {Promise} 统一响应结果。 */ async getPolicyConfigFields(data = {}) { const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse; if (!targetService || typeof targetService.getPolicyConfigFields !== 'function') { return this.fail('获取合约策略字段信息失败', { code: 500, }); } try { const payload = normalizeStoreWarehousePayload(data); const result = await targetService.getPolicyConfigFields(payload); return this.success('ok', result); } catch (error) { return this.fail(error.message || '获取合约策略字段信息失败', { code: Number(error.code) || 500, }); } } /** * 保存合约策略配置。 * * @param {Object} data 请求数据。 * @return {Promise} 统一响应结果。 */ async savePolicyConfig(data = {}) { const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse; if (!targetService || typeof targetService.savePolicyConfig !== 'function') { return this.fail('保存合约策略配置失败', { code: 500, }); } try { const payload = normalizeStoreWarehousePayload(data); const result = await targetService.savePolicyConfig(payload); return this.success('ok', result); } catch (error) { return this.fail(error.message || '保存合约策略配置失败', { code: Number(error.code) || 500, }); } } /** * 获取接口配置列表。 * * @param {Object} data 请求数据。 * @return {Promise} 统一响应结果。 */ async listApiConfig(data = {}) { const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse; if (!targetService || typeof targetService.listApiConfig !== 'function') { return this.fail('获取接口配置列表失败', { code: 500, }); } try { const payload = normalizeStoreWarehousePayload(data); const result = await targetService.listApiConfig(payload); return this.success('ok', result); } catch (error) { return this.fail(error.message || '获取接口配置列表失败', { code: Number(error.code) || 500, }); } } /** * 保存接口配置。 * * @param {Object} data 请求数据。 * @return {Promise} 统一响应结果。 */ async saveApiConfig(data = {}) { const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse; if (!targetService || typeof targetService.saveApiConfig !== 'function') { return this.fail('保存接口配置失败', { code: 500, }); } try { const payload = normalizeStoreWarehousePayload(data); const result = await targetService.saveApiConfig(payload); return this.success('ok', result); } catch (error) { return this.fail(error.message || '保存接口配置失败', { code: Number(error.code) || 500, }); } } /** * 删除奇门接口配置。 * * @param {Object} data 请求数据。 * @return {Promise} 统一响应结果。 */ async deleteQimenConfig(data = {}) { const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse; if (!targetService || typeof targetService.deleteQimenConfig !== 'function') { return this.fail('删除接口配置失败', { code: 500, }); } try { const payload = normalizeStoreWarehousePayload(data); const result = await targetService.deleteQimenConfig(payload); return this.success('ok', result); } catch (error) { return this.fail(error.message || '删除接口配置失败', { code: Number(error.code) || 500, }); } } /** * 仓库代货主一键开户。 * * @param {Object} data 请求数据。 * @return {Promise} 统一响应结果。 */ async openAccount(data = {}) { const targetService = this.service && this.service.systems && this.service.systems.storeWarehouse; if (!targetService || typeof targetService.openAccount !== 'function') { return this.fail('一键开户失败', { code: 500, }); } try { const payload = normalizeStoreWarehousePayload(data); const result = await targetService.openAccount(payload); return this.success('ok', result); } catch (error) { return this.fail(error.message || '一键开户失败', { code: Number(error.code) || 500, }); } } } module.exports = StoreWarehouseStoreService;