Files
xk-api-yii/admin/controllers/base/DataController.php

260 lines
8.6 KiB
PHP
Raw Normal View History

2024-11-14 13:07:08 +08:00
<?php
namespace admin\controllers\base;
use admin\components\BaseAdminController;
2025-03-06 17:11:45 +08:00
use common\events\PrescriptionEvent;
use common\events\ProductOrderEvent;
use common\jobs\Sms\WaitApprovalMessageJob;
use common\jobs\templateMessage\PrescriptionCreated;
2024-12-19 19:20:27 +08:00
use common\models\oldDb\Prescription;
use common\models\oldDb\ProductOrder;
use common\models\oldDb\Reconciliation;
use common\models\oldDb\Register;
use common\models\oldDb\Store;
use common\models\oldDb\User;
2024-11-14 13:07:08 +08:00
use yii\db\ActiveRecord;
use yii\db\Exception;
class DataController extends BaseAdminController
{
2025-03-06 17:11:45 +08:00
public $optional = ['count', 'top-drugs', 'trend', 'store-number', 'notify-order-generation'];
2024-11-14 13:07:08 +08:00
/**
* 获取首页统计数据
* @return array
*/
2025-01-02 16:16:40 +08:00
public function actionCount(): array
2024-11-14 13:07:08 +08:00
{
// 诊所
$storeCount = Store::find()->where(['is_delete' => 0]);
// 用户
$userCount = User::find();
// 订单
$orderCount = ProductOrder::find()->where(['is_pay' => 1]);
// 总成交
$totalPrice = Reconciliation::find()->where(['status' => 1]);
// 处方订单
$prescriptionCount = Prescription::find()->where(['status' => 1]);
// 接诊人数
$registerCount = Register::find()->where(['in', 'status', [2, 3]]);
$totalPriceArr = 0;
$totalPriceSum = $totalPrice->select(['total_price'])->asArray()->all();
foreach ($totalPriceSum as $item) {
$totalPriceArr = bcadd($totalPriceArr, $item['total_price'], 2);
}
return [
'store' => intval($storeCount->count()),
'user' => intval($userCount->count()),
'order' => intval($orderCount->count()),
'total_price' => round($totalPriceArr, 2),
'prescription' => intval($prescriptionCount->count()),
'register' => intval($registerCount->count()),
'prescription_china' => intval($prescriptionCount->andWhere(['prescription_type' => 1])->count()),
'prescription_west' => intval($prescriptionCount->andWhere(['prescription_type' => 2])->count())
];
}
/**
* 获取销售量Top10的药品
* @return array|ActiveRecord[]
*/
2025-01-02 16:16:40 +08:00
public function actionTopDrugs(): array
2024-11-14 13:07:08 +08:00
{
// 通过yii_reconciliation表统计出前10的药品
$query = Reconciliation::find()
->with(['drug' => function ($query) {
$query->select(['id', 'drug_name', 'drug_number', 'type', 'image']);
}])
->select(['drug_id', 'sum(number) as number', 'sum(total_price) as total_price'])
->groupBy('drug_id')
->orderBy(['total_price' => SORT_DESC])
->limit(10)
->asArray();
2024-11-15 15:23:49 +08:00
$list = $query->all();
foreach ($list as &$item) {
$item['total_price'] = bcadd('0', $item['total_price'], 2);
}
return $list;
2024-11-14 13:07:08 +08:00
}
2025-01-02 16:16:40 +08:00
/**
* 获取趋势图数据
* @return array
*/
public function actionTrend(): array
2024-11-14 13:07:08 +08:00
{
// 根据$month列表的月份统计数据
2025-01-02 16:18:05 +08:00
$query = Reconciliation::find()->where(['between', 'created_at', strtotime(date('Y-01-01', strtotime('-11 month'))), strtotime(date('Y-m-d 23:59:59'))])->select([
2024-11-14 13:07:08 +08:00
'drug_type',
'total_price',
'status',
'created_at',
])->all();
2025-01-02 16:18:05 +08:00
$month = $this->getMonthsList(strtotime(date('Y-01-01', strtotime('-11 month'))), strtotime(date('Y-m-d')));
2024-11-14 13:07:08 +08:00
$saleMoney = [];
$refundMoney = [];
$drugSaleMoney = [
1 => ['name' => '中药', 'value' => 0],
2 => ['name' => '西药', 'value' => 0],
3 => ['name' => '保健食品', 'value' => 0],
4 => ['name' => '中成药', 'value' => 0],
2025-01-02 16:18:50 +08:00
5 => ['name' => '产品服务包', 'value' => 0],
2024-11-14 13:07:08 +08:00
];
// 药品分类
$drugType = [
1 => '中药',
2 => '西药',
3 => '保健食品',
4 => '中成药',
2025-01-02 16:18:50 +08:00
5 => '产品服务包',
2024-11-14 13:07:08 +08:00
];
// 总金额
$totalMoney = 0;
// TODO 暂时不统计供货价
// 判断获取时间方法
$dateStr = !empty($day) ? 'm-d' : 'Y-m';
$dateArr = !empty($day) ? $day : $month;
2024-11-14 13:26:29 +08:00
foreach ($dateArr as $v) {
if (!isset($saleMoney[$v])) $saleMoney[$v] = '0.00';
if (!isset($refundMoney[$v])) $refundMoney[$v] = '0.00';
}
2024-11-14 13:07:08 +08:00
foreach ($query as $v) {
$thisMonth = date($dateStr, $v['created_at']);
$saleMoney[$thisMonth] = bcadd($saleMoney[$thisMonth] ?? '0', $v['total_price'], 2);
$totalMoney = bcadd($totalMoney, $v['total_price'], 2);
// 药品
if (!isset($drugSaleMoney[$v['drug_type']])) {
$drugSaleMoney[$v['drug_type']] = [
'name' => $drugType[$v['drug_type']],
'value' => $v['total_price'],
];
} else {
$drugSaleMoney[$v['drug_type']]['value'] = bcadd($drugSaleMoney[$v['drug_type']]['value'] ?? '0', $v['total_price'], 2);
}
if ($v['status'] < 1) {
$refundMoney[$thisMonth] = bcadd($refundMoney[$thisMonth] ?? '0', $v['total_price'], 2);
}
}
// 循环$drugSaleMoney计算药品分类占比
2025-01-02 16:12:23 +08:00
// 循环$drugSaleMoney计算药品分类占比
2024-11-14 13:07:08 +08:00
foreach ($drugSaleMoney as $k => $v) {
2025-01-02 16:12:23 +08:00
if ($totalMoney == '0') {
// 处理总金额为零的情况
$drugSaleMoney[$k]['percentage'] = '0%';
} else {
$drugSaleMoney[$k]['percentage'] = bcadd(0, bcdiv($v['value'], $totalMoney, 6) * 100, 4) . '%';
}
2024-11-14 13:07:08 +08:00
}
return [
'month' => $month,
'price' => $this->getArrValues($saleMoney),
'refund_price' => $this->getArrValues($refundMoney),
'drug_data' => $this->getArrValues($drugSaleMoney),
];
}
2025-01-02 16:16:40 +08:00
/**
* 获取门店数量
* @return array
*/
public function actionStoreNumber(): array
2024-11-14 13:07:08 +08:00
{
$provinceId = 14;
$storeCount = Store::find()
->with(['city' => function ($query) {
$query->select(['id', 'name']);
}])
->where(['is_delete' => 0])
->andWhere(['province_id' => $provinceId])
->asArray()
->select(['city_id', 'province_id'])
->all();
$result = [];
foreach ($storeCount as $v) {
if (isset($result[$v['city_id']])) {
$result[$v['city_id']]['value'] += 1;
} else {
$result[$v['city_id']] = [
'name' => $v['city']['name'],
'value' => 1
];
}
}
return $result;
}
/**
* 获取月份
*
* @param $startTime
* @param $endTime
* @return array
*/
2025-01-02 16:16:40 +08:00
public function getMonthsList($startTime, $endTime): array
2024-11-14 13:07:08 +08:00
{
$monthList = [];
$currentTime = $startTime;
while ($currentTime <= $endTime) {
$monthList[] = date('Y-m', $currentTime);
$currentTime = strtotime('+1 month', $currentTime);
}
return $monthList;
}
/**
* 重置键值
* @param $originalArray
* @return array
*/
2025-01-02 16:16:40 +08:00
public function getArrValues($originalArray): array
2024-11-14 13:07:08 +08:00
{
$newArr = [];
foreach ($originalArray as $v) {
$newArr[] = $v;
}
return $newArr;
}
2025-03-06 17:11:45 +08:00
public function actionNotifyOrderGeneration()
{
2025-03-06 17:22:04 +08:00
$prescription = \Yii::$app->request->post('prescription');
// $this->requestValidate($param, [
// ['prescription', 'required'],
// ['product_order', 'required'],
// ]);
2025-03-11 13:02:42 +08:00
2025-03-06 17:13:20 +08:00
// $productOrder = $param['product_order'];
2025-03-06 17:11:45 +08:00
//触发处方自动失效事件
2025-03-06 17:13:20 +08:00
// $prescriptionEvent = new PrescriptionEvent();
// $prescriptionEvent->prescription = $prescription;
// $prescriptionEvent->sender = $this;
// \Yii::$app->trigger(Prescription::AUTO_EXPIRE, $prescriptionEvent);
//
//
// //触发订单创建事件
// $event = new ProductOrderEvent();
// $event->order = $productOrder;
// $event->sender = $this;
// \Yii::$app->trigger(ProductOrder::EVENT_CREATED, $event);
2025-03-06 17:11:45 +08:00
//处方开具待支付订阅消息通知
\Yii::$app->queue->push(new PrescriptionCreated([
2025-03-06 17:27:31 +08:00
'orderId' => $prescription,
2025-03-06 17:11:45 +08:00
]));
//处方生成短信通知药师审方
\Yii::$app->queue->push(new WaitApprovalMessageJob([
2025-03-06 17:27:31 +08:00
'orderId' => $prescription,
2025-03-06 17:11:45 +08:00
]));
return ['ok'];
}
2024-11-14 13:07:08 +08:00
}