Commit ed25d4f7 by 赵增煜

7日内限购

parent e51250b1
......@@ -6,9 +6,11 @@
use App\Models\DrugModel;
use App\Models\PharmacyDrugModel;
use App\Models\PharmacyModel;
use App\Models\PrescriptionModel;
use App\Models\User;
use App\Services\SmsService;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
// 药品控制器
......@@ -93,6 +95,24 @@ public function drugLimit(Request $request)
if ($drug && $drug->limit_buy_7 > 0) {
$limit_num = $drug->limit_buy_7;
}
$patient_id = $request->input('patient_id');
$sevenDaysAgo = Carbon::now()->subDays(7);
$prescriptions = PrescriptionModel::where('patient_id', $patient_id)
->where('created_at', '>=', $sevenDaysAgo)
->get();
$drugCounts = [];
foreach ($prescriptions as $prescription) {
$drugs = json_decode($prescription->drugs, true); // Parse JSON data
foreach ($drugs as $drug) {
$drugId = $drug['drug_id'];
$drugCounts[$drugId] = ($drugCounts[$drugId] ?? 0) + $drug['num'];
}
}
$purchaseCount = $drugCounts[$drug_id] ?? 0;
// 计算获取剩余可买的次数
$limit_num = $limit_num - $purchaseCount;
return $this->success($limit_num);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment