| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- declare(strict_types=1);
- namespace app\miniapi\service\driver;
- use app\miniapi\exception\ApiException;
- use app\miniapi\http\Fields;
- use app\miniapi\http\Input;
- use app\miniapi\model\MiniPhoto;
- use app\miniapi\model\MiniTaskRecord;
- use plugin\saiadmin\app\model\water\WaterDelivery;
- use plugin\saiadmin\app\service\WaterFulfillment;
- /** Draft and final submission share validation; only final submission settles the delivery. */
- final class SaveFulfillment extends InTransitAction
- {
- public function __construct(private readonly bool $complete)
- {
- }
- public function fields(): array
- {
- return [
- 'request_key',
- 'accepted_qty',
- 'empty_returned',
- 'receiver',
- 'reason',
- 'photo_ids',
- 'confirmed',
- ];
- }
- protected function handle(WaterDelivery $task, array $input, int $user): array
- {
- $id = (int) $task->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;
- }
- }
|