84 lines
2.5 KiB
PHP
84 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Endpoint\Api\Http\Controllers;
|
|
|
|
use App\Endpoint\Api\Http\Resources\DrawLogResource;
|
|
use App\Enums\DrawPrizeType;
|
|
use App\Exceptions\BizException;
|
|
use App\Models\DrawLog;
|
|
use App\Services\DrawActivityService;
|
|
use Exception;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class DrawLogController extends Controller
|
|
{
|
|
public function index($drawActivityId, Request $request)
|
|
{
|
|
$drawLogs = DrawLog::with(['userInfo', 'prize'])
|
|
->whereDoesntHave('prize', fn ($builder) => $builder->where('type', DrawPrizeType::None))
|
|
->where('draw_activity_id', $drawActivityId)
|
|
->latest('id')
|
|
->simplePaginate($request->input('per_page'));
|
|
|
|
return DrawLogResource::collection($drawLogs);
|
|
}
|
|
|
|
public function update($drawActivityId, $drawLogId, Request $request)
|
|
{
|
|
$request->validate([
|
|
'consignee_name' => ['bail', 'required', 'string', 'max:255'],
|
|
'consignee_phone' => ['bail', 'required', 'string', 'max:255'],
|
|
'consignee_address' => ['bail', 'required', 'string', 'max:255'],
|
|
], [], [
|
|
'consignee_name' => '收件人',
|
|
'consignee_phone' => '联系方式',
|
|
'consignee_address' => '收货地址',
|
|
]);
|
|
|
|
$drawLog = DrawLog::where([
|
|
'user_id' => $request->user()->id,
|
|
'draw_activity_id' => $drawActivityId,
|
|
])->findOrFail($drawLogId);
|
|
|
|
if ($drawLog->prize->type !== DrawPrizeType::Goods) {
|
|
throw new BizException('奖品不是实物');
|
|
}
|
|
|
|
if (filled($drawLog->consignee_name)) {
|
|
throw new BizException('请联系客服修改收件人信息');
|
|
}
|
|
|
|
$drawLog->update($request->only([
|
|
'consignee_name',
|
|
'consignee_phone',
|
|
'consignee_address',
|
|
]));
|
|
|
|
return DrawLogResource::make($drawLog);
|
|
}
|
|
|
|
/**
|
|
* 发放奖品
|
|
*/
|
|
public function sendPrize(Request $request)
|
|
{
|
|
$userId = $request->user()->id;
|
|
$drawLogId = $request->input('draw_log_id');
|
|
$drawLog = DrawLog::where('user_id', $userId)->find($drawLogId);
|
|
if ($drawLog) {
|
|
throw new BizException('中奖记录不存在');
|
|
}
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
(new DrawActivityService)->sendPrize($drawLog);
|
|
DB::commit();
|
|
return response()->json();
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
throw new BizException($e->getMessage());
|
|
}
|
|
}
|
|
}
|