id; $data = [ 'accepted_qty' => Input::integer($input['accepted_qty'] ?? null, 'accepted_qty', 0, (int) $task->quantity), 'empty_returned' => Input::integer($input['empty_returned'] ?? 0, 'empty_returned', 0, 10000000), 'receiver' => Fields::text($input, 'receiver', 60), 'reason' => Fields::text($input, 'reason', 255, false), 'photo_ids' => $this->photos($id, $input['photo_ids'] ?? []), ]; if (!$this->complete) { MiniTaskRecord::recordEvent($id, $user, 'draft', $data); return [ 'id' => $id, 'saved' => true, ]; } if (($input['confirmed'] ?? false) !== true || !$data['photo_ids']) { throw new ApiException(42201, '请上传履约照片并确认实际数量', 422); } if (!MiniTaskRecord::query()->where([ 'delivery_id' => $id, 'kind' => 'checkin', 'user_id' => $user, ])->exists()) { throw new ApiException(40902, '请先完成到达打卡', 409); } $result = (new WaterFulfillment())->receive(['id' => $id] + $data, 0); MiniTaskRecord::recordEvent($id, $user, 'complete', $data); return $result; } private function photos(int $delivery, mixed $photos): array { if (!is_array($photos) || !array_is_list($photos) || count($photos) > 4) { throw new ApiException(42201, '履约照片最多4张', 422); } $photos = array_map(fn ($id) => Input::integer($id, 'photo_ids', 1, 2147483647), $photos); if (count(array_unique($photos)) !== count($photos) || MiniPhoto::query()->where('delivery_id', $delivery)->whereIn('id', $photos)->count() !== count($photos)) { throw new ApiException(42201, '照片不属于当前配送任务', 422); } return $photos; } }