Commit 857cf186 by lujunyi

限购接口

parent 9a1b93b7
......@@ -11,7 +11,6 @@
use App\Services\SmsService;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
// 药品控制器
......@@ -99,23 +98,18 @@ public function drugList(Request $request)
// 药品7日内限购
public function drugLimit(Request $request)
{
// 当前药品未设置则使用全局的7日内限购
$site_config = DB::table('admin_settings')->where('slug', 'site_config')->value('value');
$site_config = json_decode($site_config, true);
$limit_num = intval($site_config['prescription_limit_buy_7']);
$limit_type = intval($site_config['prescription_limit_buy_7']) > 0 ? 1 : 0; // 1为限购,0为不限购
$drug_id = $request->input('drug_id');
// 判断当前药品有没有设置7日内限购
$drug = DrugModel::query()->find($drug_id);
if ($drug && intval($drug->limit_buy_7) > 0) {
$limit_num = $drug->limit_buy_7;
$limit_type = 1;
}
$drugs = $request->input('drugs'); // [{'drug_id'=>1,'num'=>1},{'drug_id'=>2,'num'=>4}]
$patient_id = $request->input('patient_id');
$pharmacy_id = $request->input('pharmacy_id');
// $sevenDaysAgo = Carbon::now()->subDays(7);
// 当前药品未设置则使用全局的1日内限购
$site_config = admin_setting('site_config');
$site_config = json_decode($site_config, true);
$limit_num = intval($site_config['prescription_limit_buy_7']);
// 如果药品在这个黑名单中,则要判断7天最多买四盒
$limit_keywords = explode(',', $site_config['limit_keywords']); // 麻黄素 黑名单
// 查询已经开过方的药方(1天内)
$oneDayAgo = Carbon::now()->subDay();
$prescriptions = PrescriptionModel::where('patient_id', $patient_id)
->where('is_voided', PrescriptionModel::IS_VOIDED_FALSE) // 未作废的处方
......@@ -123,30 +117,66 @@ public function drugLimit(Request $request)
// ->where('pharmacy_id', $pharmacy_id)
->where('created_at', '>=', $oneDayAgo)
->get();
$drugCounts = [];
// 判断搜索出来的处方不为空
if ($prescriptions->count() > 0) {
foreach ($prescriptions as $prescription) {
// $drugs = json_decode($prescription->drug_info, true); // Parse JSON data
$drugs = $prescription->drug_info;
if (count($drugs) > 0) {
foreach ($drugs as $drug) {
$drugId = $drug['drug_id'];
$drugCounts[$drugId] = ($drugCounts[$drugId] ?? 0) + $drug['num'];
}
foreach ($prescriptions as $prescription) {
$drugInfos = $prescription->drug_info ?? [];
foreach ($drugInfos as $drugInfo) {
$drugId = $drugInfo['drug_id'];
$drugCounts[$drugId] = ($drugCounts[$drugId] ?? 0) + $drugInfo['num'];
}
}
// 查询已经开过方的药方(7天内)
$sevenDayAgo = Carbon::now()->subDay(7);
$prescriptions_7 = PrescriptionModel::where('patient_id', $patient_id)
->where('is_voided', PrescriptionModel::IS_VOIDED_FALSE) // 未作废的处方
->where('status', PrescriptionModel::PRESCRIPTION_STATUS_SUCCESS)
// ->where('pharmacy_id', $pharmacy_id)
->where('created_at', '>=', $sevenDayAgo)
->get();
// 计算累计麻黄素药品购买数量
$buyed_mahuang_counts = 0;
foreach ($prescriptions_7 as $prescription_7) {
$drugInfos_7 = $prescription_7->drug_info ?? [];
foreach ($drugInfos_7 as $drugInfo_7) {
if ($drugInfo_7 && in_array($drugInfo_7->drug_name, $limit_keywords)) {
$buyed_mahuang_counts += $drugInfo_7['num'];
}
}
}
// 处理麻黄素药品数据
$mahuangsu_num = 0; // 麻黄素药品数量
foreach ($drugs as &$drug) {
$drugModel = DrugModel::find($drug['drug_id']);
if (! $drugModel) {
$errMsg = '药品不存在';
return $this->failed($errMsg, ['add_status' => false]);
}
$drug['mahuangsu_status'] = 0; // 0:不是麻黄素药品
$drug['limit_num'] = $limit_num; // 赋值全局限购数量
if ($drugModel->limit_buy_7 > 0) {
$drug['limit_num'] = $drugModel->limit_buy_7;
}
$buyedCount = $drugCounts[$drug_id] ?? 0; // 历史已购买数量
$canBuyCount = $drug['limit_num'] - $buyedCount; // 普通药品剩余可购买数量
if ($canBuyCount <= 0) {
$errMsg = '药品['.$drugModel->name.']剩余购买数量不足~';
$purchaseCount = $drugCounts[$drug_id] ?? 0;
// 计算获取剩余可买的次数
$limit_num = $limit_num - $purchaseCount;
Log::info(' drug_id'.$drug_id.' limit_num=>'.$limit_num.' limit_type'.$limit_type);
return $this->success(['limit_num' => $limit_num, 'limit_type' => $limit_type]);
return $this->failed($errMsg, ['add_status' => false]);
}
if ($drugModel && in_array($drugModel->name, $limit_keywords)) {
$mahuangsu_num += $drug['num'];
$drug['mahuangsu_status'] = 1; // 1:是麻黄素药品
$canBuyMahuangCount = 4 - $buyed_mahuang_counts - $mahuangsu_num; // 麻黄素药品剩余可购买数量,最多可以购买4个
if ($canBuyMahuangCount <= 0) {
$errMsg = '药品['.$drugModel->name.']剩余购买数量不足~';
return $this->failed($errMsg, ['add_status' => false]);
}
}
}
return $this->success(['add_status' => true]);
}
}
......@@ -19,9 +19,11 @@ public function success($data = [], string $message = 'success', int $code = Res
/**
* 返回失败信息
*
* @param bool $status
* @param string $message 错误消息
* @param array $data 返回数据
* @param int $code 错误编码
*/
public function failed($data = [], string $message = 'fail', int $code = Response::HTTP_INTERNAL_SERVER_ERROR): \Illuminate\Http\JsonResponse
public function failed(string $message = 'fail', array $data = [], int $code = Response::HTTP_INTERNAL_SERVER_ERROR): \Illuminate\Http\JsonResponse
{
return response()->json(['status' => false, 'code' => $code, 'message' => $message, 'data' => $data]);
}
......
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