order(['id DESC'])->fetchAll(); } public function addRecord($data) { return $this->add($data); } public function findBySerial($finished_serial) { return $this->where(['finished_serial = :serial'], [':serial' => $finished_serial])->fetch(); } /** * 批量添加出货记录 * @param array $records 记录数组 */ public function batchAdd($records) { foreach ($records as $record) { // 跳过已出货的序列号 $existing = $this->findBySerial($record['finished_serial']); if ($existing) { continue; } $this->add($record); } return true; } /** * 从 warehouse_in 表查询成品序列号对应的产品型号 * @param string $finished_serial 成品序列号 * @return array|null ['product_model' => ..., 'box_serial' => ...] 或 null */ public function lookupFinishedSerial($finished_serial) { $table = self::TABLE_PREFIX . 'warehouse_in'; $sql = "SELECT finished_serial, product_model, box_serial FROM {$table} WHERE finished_serial = :serial LIMIT 1"; $result = $this->query($sql, [':serial' => $finished_serial]); return !empty($result) ? $result[0] : null; } // 根据产品型号筛选记录(delivery 表无 product_type 列) public function getByTypeAndModel($product_type, $product_model) { if (empty($product_model)) { return []; } return $this->where(['product_model = :pm'], [':pm' => $product_model])->order(['id DESC'])->fetchAll(); } // 统计当日某操作员的录入数量 public function countTodayByOperator($operator) { $sql = "SELECT COUNT(*) as cnt FROM " . $this->getTable() . " WHERE operator = :op AND DATE(delivery_time) = CURDATE()"; $result = $this->query($sql, [':op' => $operator]); return $result[0]['cnt'] ?? 0; } // 统计总记录数(可按型号筛选) public function countTotal($product_type = '', $product_model = '') { if (!empty($product_model)) { $result = $this->where(['product_model = :pm'], [':pm' => $product_model])->field('COUNT(*) as cnt')->fetch(); } else { $sql = "SELECT COUNT(*) as cnt FROM " . $this->getTable(); $result = $this->query($sql); } return $result[0]['cnt'] ?? 0; } /** * 根据箱序列号查询该箱下所有成品 * @param string $box_serial 箱序列号 * @return array 该箱下所有成品记录数组 */ public function getBoxItems($box_serial) { $table = self::TABLE_PREFIX . 'warehouse_in'; $sql = "SELECT finished_serial, product_model FROM {$table} WHERE box_serial = :box ORDER BY id ASC"; return $this->query($sql, [':box' => $box_serial]); } }