'use strict'; const Service = require('egg').Service; const { parseWhereConditions } = require('../../../lib/query_condition_parser'); const DAY_MS = 24 * 60 * 60 * 1000; /** * 库存有效期列表服务。 * 以库存记录为主表,关联商品资料后按当前仓库与效期阈值规则分页查询。 */ class StorageExpiryService extends Service { /** * 查询库存有效期预警列表。 * * @param {Object} [where={}] 查询条件 * @param {Object} [pageParams={}] 分页参数 * @param {number} [pageParams.page=1] 当前页码 * @param {number} [pageParams.pageSize=20] 每页条数 * @return {Promise} 查询结果 */ async list(where = {}, pageParams = {}) { const { ctx, app } = this; const warehouseId = Number(ctx.user && ctx.user.store_id) || 0; if (!warehouseId) { return { code: 400, message: '当前用户未绑定仓库', data: { list: [], pagination: { page: 1, pageSize: 20, total: 0, totalPages: 0, hasPrevious: false, hasNext: false, }, }, }; } const page = Math.max(Number(pageParams.page) || 1, 1); const pageSize = Math.max(Number(pageParams.pageSize) || 20, 1); const offset = (page - 1) * pageSize; const { Op, literal } = app.Sequelize; const specialFieldMap = { item_code: { bucket: 'stock', }, goods_code: { bucket: 'stock', }, item_name: { bucket: 'stock', }, sku_property: { bucket: 'stock', }, stock_unit: { bucket: 'stock', }, is_shelf_life_mgmt: { bucket: 'stock', field: 'is_shelf_life_mgmt', }, remaining_threshold_days: { bucket: 'stock', }, }; const parsed = ctx.whereParsed && Array.isArray(ctx.whereParsed.dynamicConditions) ? ctx.whereParsed : parseWhereConditions({ where, app, specialFieldMap, }).result; const dynamicConditions = Array.isArray(parsed.dynamicConditions) ? parsed.dynamicConditions : []; const stockConditions = Array.isArray(parsed.bucketConditions && parsed.bucketConditions.stock) ? parsed.bucketConditions.stock : []; const storageWhere = { [Op.and]: [ { warehouse_id: warehouseId }, { is_enabled: 1 }, { delete_at: null }, { quantity: { [Op.gt]: 0 } }, { expiry_date: { [Op.ne]: null } }, literal('DATEDIFF(`wms_storage`.`expiry_date`, CURDATE()) <= `stock`.`remaining_threshold_days`'), ], }; if (dynamicConditions.length > 0) { storageWhere[Op.and].push({ [Op.and]: dynamicConditions }); } const stockWhere = { [Op.and]: [ { is_valid: 'Y' }, { delete_at: null }, { warehouse_id: warehouseId }, { is_shelf_life_mgmt: 'Y' }, { remaining_threshold_days: { [Op.gt]: 0 } }, ], }; if (stockConditions.length > 0) { stockWhere[Op.and].push({ [Op.and]: stockConditions }); } const { count, rows } = await ctx.model.Storage.findAndCountAll({ where: storageWhere, include: [ { model: ctx.model.StockWarehouse, as: 'stock', required: true, attributes: [ 'id', 'item_code', 'goods_code', 'item_name', 'sku_property', 'stock_unit', 'store_id', 'warehouse_id', 'is_shelf_life_mgmt', 'remaining_threshold_days', ], where: stockWhere, }, { model: ctx.model.LocationSpace, as: 'space', required: false, attributes: [ 'id', 'space_code', 'custom_name' ], }, ], order: [ [ 'expiry_date', 'ASC' ], [ 'inbound_time', 'ASC' ], [ 'id', 'ASC' ], ], offset, limit: pageSize, distinct: true, subQuery: false, }); const now = Date.now(); const list = rows.map(row => { const plain = row.get({ plain: true }); const expiryTime = plain.expiry_date ? new Date(plain.expiry_date).getTime() : null; const thresholdDays = Number(plain.stock && plain.stock.remaining_threshold_days) || 0; const remainingDays = expiryTime === null || Number.isNaN(expiryTime) ? null : Math.max(0, Math.ceil((expiryTime - now) / DAY_MS)); return { ...plain, threshold_days: thresholdDays, remaining_days: remainingDays, is_expired: expiryTime !== null && !Number.isNaN(expiryTime) ? expiryTime < now : false, created_at: plain.created_at ? app.formatDate(plain.created_at) : null, updated_at: plain.updated_at ? app.formatDate(plain.updated_at) : null, inbound_time: plain.inbound_time ? app.formatDate(plain.inbound_time) : null, production_date: plain.production_date ? app.formatDate(plain.production_date) : null, expiry_date: plain.expiry_date ? app.formatDate(plain.expiry_date) : null, }; }); const totalPages = pageSize > 0 ? Math.ceil(count / pageSize) : 0; return { code: 200, message: '查询成功', data: { list, pagination: { page, pageSize, total: count, totalPages, hasPrevious: page > 1, hasNext: page < totalPages, }, }, }; } } module.exports = StorageExpiryService;