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[] * @throws Exception */ public function actionTopDrugs() { // 通过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(); return $query->all(); } public function actionTrend() { // 根据$month列表的月份统计数据 $query = Reconciliation::find()->where(['between', 'created_at', strtotime(date('Y-01-01')), strtotime(date('Y-m-d'))])->select([ 'drug_type', 'total_price', 'status', 'created_at', ])->all(); $month = $this->getMonthsList(strtotime(date('Y-01-01')), strtotime(date('Y-m-d'))); $saleMoney = []; $refundMoney = []; $drugSaleMoney = [ 1 => ['name' => '中药', 'value' => 0], 2 => ['name' => '西药', 'value' => 0], 3 => ['name' => '保健食品', 'value' => 0], 4 => ['name' => '中成药', 'value' => 0], ]; // 药品分类 $drugType = [ 1 => '中药', 2 => '西药', 3 => '保健食品', 4 => '中成药', ]; // 总金额 $totalMoney = 0; // TODO 暂时不统计供货价 // 判断获取时间方法 $dateStr = !empty($day) ? 'm-d' : 'Y-m'; $dateArr = !empty($day) ? $day : $month; 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); } } foreach ($dateArr as $v) { if (!isset($saleMoney[$v])) $saleMoney[$v] = '0.00'; if (!isset($refundMoney[$v])) $refundMoney[$v] = '0.00'; } // 循环$drugSaleMoney计算药品分类占比 foreach ($drugSaleMoney as $k => $v) { $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), ]; } public function actionStoreNumber() { $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) { $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) { $newArr = []; foreach ($originalArray as $v) { $newArr[] = $v; } return $newArr; } }