'use strict'; const MainService = require('../main'); class indexService extends MainService { constructor(ctx) { super(ctx); const _noNeedLogin = ''; const _noNeedRight = 'fastWhereTree,stockWarehouse,getProhibitedType,getInfo,getBarcodes,getStoreStocks,queryStockLogs,getStockUnits,getUnitList,createUnit,checkItemCodeUnique,deleteUnit,createProhibited,deleteProhibited,search'; const _noNeedLog = 'fastWhereTree,stockWarehouse,getProhibitedType,getInfo,getBarcodes,getStoreStocks,queryStockLogs,getStockUnits,checkItemCodeUnique,getUnitList,search'; const _noNeedAuth = 'fastWhereTree,stockWarehouse,getProhibitedType,getInfo,getBarcodes,getStoreStocks,queryStockLogs,getStockUnits,checkItemCodeUnique,getUnitList,search'; this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth); } async stockWarehouse(data) { const { service } = this; const { pagination, where } = data; console.log('where', where); const result = await service.systems.stockStore.getWarehouseStock(where, pagination); return this.success('ok', result); } async getProhibitedType() { const { service } = this; const result = await service.systems.stockStore.getProhibitedType(); return this.success('ok', { data: result, code: 200 }); } async getInfo(data) { const { service } = this; const result = await service.systems.stockStore.getInfo(data); return this.success('ok', { data: result, code: 200 }); } async saveWarehouseStock(data) { const { service } = this; const result = await service.systems.stockStore.saveWarehouseStock(data); return this.success('ok', { data: result, code: 200 }); } async createWarehouseStock(data) { const { service } = this; const result = await service.systems.stockStore.createWarehouseStock(data); return this.success('ok', { data: result, code: 200 }); } async getBarcodes(data) { const { service } = this; const result = await service.systems.stockStore.getBarcodes(data); return this.success('ok', { data: result, code: 200 }); } async saveWarehouseStockBarcode(data) { const { service } = this; const result = await service.systems.stockStore.saveWarehouseStockBarcode(data); return this.success('ok', { data: result, code: 200 }); } async getStoreStocks(data) { const { service } = this; const result = await service.systems.stockStore.getStoreStocks(data); return this.success('ok', { data: result, code: 200 }); } async bindItem(data) { const { service } = this; try { const result = await service.systems.stockStore.bindItem(data); return this.success('绑定成功', { data: result, code: 200 }); } catch (e) { return this.success(e.message, { code: 200 }); } } async unbindItem(data) { const { service } = this; try { const result = await service.systems.stockStore.unbindItem(data); return this.success('解绑成功', { data: result, code: 200 }); } catch (e) { return this.success(e.message, { code: 200 }); } } async checkItemCodeUnique(data) { const { ctx, service } = this; const { id, item_code, store_id } = data; console.log(data); try { if (!item_code || !store_id) { return this.fail('参数错误'); } const data = await service.systems.stockStore.checkItemCodeUnique(id, item_code, store_id, ctx.user.store_id); return this.success('获取成功', { data, code: 200 }); } catch (error) { return this.fail(error.message); } } async queryStockLogs(data) { console.log('请求'); const { ctx } = this; try { const { service } = this; data.warehouseId = ctx.user.store_id; const result = await service.systems.operationLog.queryStockLogs(data); return this.success('ok', { data: result, code: 200 }); } catch (e) { console.log(e); return this.success(e.message, { code: 200 }); } } async handleBatchDelete(data) { const { service } = this; try { const result = await service.systems.stockStore.handleBatchDelete(data); return this.success(result.message, { data: result.data, code: result.code }); } catch (e) { console.log(e); return this.success(e.message, { code: 200 }); } } async deleteUnit(data) { const { service } = this; try { const result = await service.systems.stockStore.deleteUnit(data); return this.success(result.message, { data: result.data, code: result.code }); } catch (e) { console.log(e); return this.success(e.message, { code: 200 }); } } async deleteProhibited(data) { const { service } = this; try { const result = await service.systems.stockStore.deleteProhibited(data); return this.success(result.message, { data: result.data, code: result.code }); } catch (e) { console.log(e); return this.success(e.message, { code: 200 }); } } async handleBatchChange(data) { const { service } = this; try { const result = await service.systems.stockStore.handleBatchChange(data); return this.success(result.message, { data: result.data, code: result.code }); } catch (e) { console.log(e); return this.success(e.message, { code: 405 }); } } async getStockUnits() { console.log('query_getStockUnits'); const { ctx } = this; const warehouse_id = ctx.user.store_id; try { // 构建查询条件 const where = { [ctx.app.Sequelize.Op.or]: [ { warehouse_id: null }, // 系统预设单位 { warehouse_id }, // 本仓库的单位 ], }; const units = await ctx.model.StockUnit.findAll({ where, order: [ [ 'warehouse_id', 'ASC' ], // 先显示系统预设单位(warehouse_id为null) [ 'created_at', 'DESC' ], // 按创建时间倒序 ], }); return this.success('ok', { data: units, code: 200 }); } catch (error) { console.log(error); return this.fail(error.message); } } async getUnitList() { console.log('query_getUnitList'); const { ctx } = this; const warehouse_id = ctx.user.store_id; try { // 构建查询条件 const where = { warehouse_id, }; const units = await ctx.model.StockUnit.findAll({ where, order: [ [ 'warehouse_id', 'ASC' ], // 先显示系统预设单位(warehouse_id为null) [ 'created_at', 'DESC' ], // 按创建时间倒序 ], }); return this.success('ok', { data: units, code: 200 }); } catch (error) { console.log(error); return this.fail(error.message); } } async createProhibited(data) { console.log('createProhibited', data); const { ctx } = this; const warehouse_id = ctx.user.store_id || null; const { name } = data; // 参数验证 if (!name || name.trim() === '') { ctx.body = { code: 400, message: '违禁品类型名称不能为空', }; return; } const trimmedName = name.trim(); try { // 1. 检查是否是系统预设单位(warehouse_id为null) const systemUnit = await ctx.model.ProhibitedType.findOne({ where: { type_name: trimmedName, warehouse_id: null, }, }); if (systemUnit) { ctx.body = { code: 400, message: '该违禁品类型是系统预设,不允许添加', }; return; } // 2. 检查当前仓库是否已存在相同名称的单位 const existUnit = await ctx.model.ProhibitedType.findOne({ where: { type_name: trimmedName, warehouse_id, }, }); if (existUnit) { ctx.body = { code: 400, message: '该违禁品类型名称已存在', }; return; } // 3. 创建单位 const unit = await ctx.model.ProhibitedType.create({ type_name: trimmedName, warehouse_id, }); // 4. 获取更新后的单位列表 const where = {}; if (warehouse_id) { where[ctx.app.Sequelize.Op.or] = [ { warehouse_id: null }, // 系统预设单位 { warehouse_id }, // 当前仓库的单位 ]; } else { where.warehouse_id = null; // 只显示系统预设单位 } const units = await ctx.model.ProhibitedType.findAll({ where, order: [ [ 'warehouse_id', 'ASC' ], // 先显示系统预设单位(warehouse_id为null) [ 'created_at', 'DESC' ], // 按创建时间倒序 ], }); return this.success('添加成功', { code: 200 }); } catch (error) { console.log(error); return this.success('创建违禁品类型失败', { code: 405 }); } } async createUnit() { const { ctx } = this; const warehouse_id = ctx.user.store_id || null; const { name } = ctx.request.body; // 参数验证 if (!name || name.trim() === '') { ctx.body = { code: 400, message: '单位名称不能为空', }; return; } const trimmedName = name.trim(); try { // 1. 检查是否是系统预设单位(warehouse_id为null) const systemUnit = await ctx.model.StockUnit.findOne({ where: { name: trimmedName, warehouse_id: null, }, }); if (systemUnit) { ctx.body = { code: 400, message: '该单位是系统预设单位,不允许添加', }; return; } // 2. 检查当前仓库是否已存在相同名称的单位 const existUnit = await ctx.model.StockUnit.findOne({ where: { name: trimmedName, warehouse_id, }, }); if (existUnit) { ctx.body = { code: 400, message: '该单位名称已存在', }; return; } // 3. 创建单位 const unit = await ctx.model.StockUnit.create({ name: trimmedName, warehouse_id, }); // 4. 获取更新后的单位列表 const where = {}; if (warehouse_id) { where[ctx.app.Sequelize.Op.or] = [ { warehouse_id: null }, // 系统预设单位 { warehouse_id }, // 当前仓库的单位 ]; } else { where.warehouse_id = null; // 只显示系统预设单位 } const units = await ctx.model.StockUnit.findAll({ where, order: [ [ 'warehouse_id', 'ASC' ], // 先显示系统预设单位(warehouse_id为null) [ 'created_at', 'DESC' ], // 按创建时间倒序 ], }); return this.success('添加成功', { code: 200 }); } catch (error) { return this.success('创建库存单位失败', { code: 200 }); } } async search(data) { const { service } = this; const result = await service.systems.stockStore.search(data); return this.success('ok', result); } } module.exports = indexService;