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]); $prescriptionCount = Prescription::find()->where(['in', 'status', [0,1,3,4]]); // 接诊人数 $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(Prescription::find()->where(['in', 'status', [0,1,3,4]])->andWhere(['prescription_type' => 1])->count()), // 'prescription_west' => intval(Prescription::find()->where(['in', 'status', [0,1,3,4]])->andWhere(['prescription_type' => 2])->count()), 'prescription_west' => intval(Prescription::find()->where(['in', 'status', [0,1,3,4]])->andWhere(['in', 'prescription_type', [2,4]])->count()), 'prescription_package_server' => intval(Prescription::find()->where(['in', 'status', [0,1,3,4]])->andWhere(['prescription_type' => 5])->count()) ]; } /** * 获取销售量Top10的药品 * @return array|ActiveRecord[] */ public function actionTopDrugs(): array { // 通过yii_reconciliation表统计出前10的药品 $query = Reconciliation::find() ->with(['drug' => function ($query) { $query->select(['id', 'drug_name', 'drug_number', 'type', 'image']); }]) ->where(['status' => 1]) ->select(['drug_id', 'sum(number) as number', 'sum(total_price) as total_price']) ->groupBy('drug_id') ->orderBy(['total_price' => SORT_DESC]) ->limit(10) ->asArray(); $list = $query->all(); foreach ($list as &$item) { $item['total_price'] = bcadd('0', $item['total_price'], 2); } return $list; } /** * 获取趋势图数据 * @return array */ public function actionTrend(): array { // 根据$month列表的月份统计数据 $query = Reconciliation::find()->where(['between', 'created_at', strtotime(date('Y-01-01', strtotime('-11 month'))), strtotime(date('Y-m-d 23:59:59'))])->select([ 'drug_type', 'total_price', 'status', 'created_at', ])->all(); $month = $this->getMonthsList(strtotime(date('Y-01-01', strtotime('-11 month'))), strtotime(date('Y-m-d'))); $saleMoney = []; $refundMoney = []; $drugSaleMoney = [ 1 => ['name' => '中药', 'value' => 0], 2 => ['name' => '西药', 'value' => 0], 3 => ['name' => '保健食品', 'value' => 0], 4 => ['name' => '中成药', 'value' => 0], 5 => ['name' => '产品服务包', 'value' => 0], ]; // 药品分类 $drugType = [ 1 => '中药', 2 => '西药', 3 => '保健食品', 4 => '中成药', 5 => '产品服务包', ]; // 总金额 $totalMoney = 0; // TODO 暂时不统计供货价 // 判断获取时间方法 $dateStr = !empty($day) ? 'm-d' : 'Y-m'; $dateArr = !empty($day) ? $day : $month; foreach ($dateArr as $v) { if (!isset($saleMoney[$v])) $saleMoney[$v] = '0.00'; if (!isset($refundMoney[$v])) $refundMoney[$v] = '0.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计算药品分类占比 // 循环$drugSaleMoney计算药品分类占比 foreach ($drugSaleMoney as $k => $v) { if ($totalMoney == '0') { // 处理总金额为零的情况 $drugSaleMoney[$k]['percentage'] = '0%'; } else { $drugSaleMoney[$k]['percentage'] = bcadd(0, bcdiv($v['value'], $totalMoney, 6) * 100, 4) . '%'; } } return [ 'month' => $month, 'price' => $this->getArrValues($saleMoney), 'refund_price' => $this->getArrValues($refundMoney), 'drug_data' => $this->getArrValues($drugSaleMoney), ]; } /** * 获取门店数量 * @return array */ public function actionStoreNumber(): array { $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 */ public function getMonthsList($startTime, $endTime): array { $monthList = []; $currentTime = $startTime; while ($currentTime <= $endTime) { $monthList[] = date('Y-m', $currentTime); $currentTime = strtotime('+1 month', $currentTime); } return $monthList; } /** * 重置键值 * @param $originalArray * @return array */ public function getArrValues($originalArray): array { $newArr = []; foreach ($originalArray as $v) { $newArr[] = $v; } return $newArr; } public function actionNotifyOrderGeneration() { $prescription = \Yii::$app->request->post('prescription'); // $this->requestValidate($param, [ // ['prescription', 'required'], // ['product_order', 'required'], // ]); // $productOrder = $param['product_order']; //触发处方自动失效事件 // $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); //处方开具待支付订阅消息通知 \Yii::$app->queue->push(new PrescriptionCreated([ 'orderId' => $prescription, ])); //处方生成短信通知药师审方 \Yii::$app->queue->push(new WaitApprovalMessageJob([ 'orderId' => $prescription, ])); return ['ok']; } /** * 处理处方过期 * @return string[] * @throws \yii\base\Exception */ public function prescriptionOver(): array { $orderId = \Yii::$app->request->post('order_id'); $userId = \Yii::$app->request->post('user_id'); $type = \Yii::$app->request->post('type'); if($type === 1){ // 自动退款 $ProductRefundForm = new ProductRefundForm(); $ProductRefundForm->refund([ 'order_id' => $orderId, 'user_id' => $userId ]); } else { // 订单自动失效 $ProductCancelForm = new ProductCancelForm(); $ProductCancelForm->cancel([ 'order_id' => $orderId, 'user_id' => $userId ], '处方失效,订单自动失效'); } return ['ok']; } public function actionSendOrder() { // 订单自动同步任务 \Yii::$app->queue->push(new ProductOrderSyncJob([ 'orderId' => 126, ])); } }