From 1c15c1ac9bcfc80dc867568ae6b38b57dcab8a6d Mon Sep 17 00:00:00 2001 From: lq Date: Tue, 5 Nov 2024 13:26:58 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A6=96=E9=A1=B5=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin/controllers/base/HomeController.php | 361 ++++++++++++++++++++++ common/models/CashAccount.php | 5 + common/models/DrugStoreRelations.php | 6 + 3 files changed, 372 insertions(+) create mode 100644 admin/controllers/base/HomeController.php diff --git a/admin/controllers/base/HomeController.php b/admin/controllers/base/HomeController.php new file mode 100644 index 0000000..884c638 --- /dev/null +++ b/admin/controllers/base/HomeController.php @@ -0,0 +1,361 @@ +where(['is_delete' => 0]); + $userCount = User::find(); + $orderCount = ProductOrder::find()->where(['is_pay' => 1]); + $totalPrice = Reconciliation::find()->where(['status' => 1]); + $drugCount = Drug::find(); + $inventoryWarning = DrugStoreDrug::find()->where(['<', 'stock', 100]); + return [ + 'store' => intval($storeCount->count()), + 'user' => intval($userCount->count()), + 'order' => intval($orderCount->count()), + 'drug' => intval($drugCount->count()), + 'total_price' => intval($totalPrice->sum('total_price')), + 'inventory_warning' => intval($inventoryWarning->count()) + ]; + } + + /** + * 获取首页统计数据 - 其他 + * @return array + * @throws Exception + */ + public function actionHomeCountByStore() + { + $admin = \Yii::$app->user->identity; + $orderCount = ProductOrder::find()->where(['is_pay' => 1]); + $totalPrice = Reconciliation::find()->where(['status' => 1]); + $inventoryWarning = DrugStoreRelations::find()->where(['<', 'stock', 100]); + $drug = DrugStoreRelations::find(); + + switch ($admin->role) { + case UserRoleEnum::STORE_ADMIN: + // 门店 + if (empty($admin->store_id)) throw new Exception('门店信息获取失败'); + $where = ['store_id' => $admin->store_id]; + break; + case UserRoleEnum::PROVINCE_DAI: + // 省代 + if (empty($admin->province_id)) throw new Exception('省区经理信息获取失败' . $admin->province_id); + $store_id = Store::find()->select(['id'])->where(['province_id' => $admin->province_id])->column(); + $where = ['in', 'store_id', $store_id]; + break; + case UserRoleEnum::CITY_DAI: + // 市代 + if (empty($admin->city_id)) throw new Exception('市区经理信息获取失败' . $admin->city_id); + $store_id = Store::find()->select(['id'])->where(['city_id' => $admin->city_id])->column(); + $where = ['in', 'store_id', $store_id]; + break; + case UserRoleEnum::SUPPLY: + // 业务员 + if (empty($admin->code)) throw new Exception('市区经理信息获取失败' . $admin->code); + $store_id = Store::find()->select(['id'])->where(['code' => $admin->code])->column(); + $where = ['in', 'store_id', $store_id]; + break; + default: + // 不需要额外的条件,直接排序 + throw new Exception('角色参数错误'); + break; + } + if (!empty($where)) { + $orderCount->andWhere($where); + $totalPrice->andWhere($where); + $inventoryWarning->andWhere($where); + $drug->where($where); + } + return [ + 'order' => intval($orderCount->count()), + 'total_price' => intval($totalPrice->sum('total_price')), + 'inventory_warning' => intval($inventoryWarning->count()), + 'drug' => intval($drug->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(); + + // 根据角色查询自己有权限的数据 + $admin = \Yii::$app->user->identity; + + switch ($admin->role) { + case UserRoleEnum::SUPER_ADMIN: + break; + case UserRoleEnum::STORE_ADMIN: + // 门店 + if (empty($admin->store_id)) throw new Exception('门店信息获取失败'); + $store = $admin->store_id; + $query->joinWith(['store' => function ($s) use ($store) { + $s->alias('s'); + $s->andWhere(['s.id' => $store]); + }]); + break; + case UserRoleEnum::PROVINCE_DAI: + // 省代 + if (empty($admin->province_id)) throw new Exception('省区经理信息获取失败' . $admin->province_id); + $province = $admin->province_id; + $query->joinWith(['store' => function ($s) use ($province) { + $s->alias('s'); + $s->andWhere(['s.province_id' => $province]); + }]); + break; + case UserRoleEnum::CITY_DAI: + // 市代 + if (empty($admin->city_id)) throw new Exception('市区经理信息获取失败' . $admin->city_id); + $city = $admin->city_id; + $query->joinWith(['store' => function ($s) use ($city) { + $s->alias('s'); + $s->andWhere(['s.city_id' => $city]); + }]); + break; + case UserRoleEnum::SUPPLY: + // 业务员 + if (empty($admin->code)) throw new Exception('市区经理信息获取失败' . $admin->code); + $code = $admin->code; + $query->joinWith(['store' => function ($s) use ($code) { + $s->alias('s'); + $s->andWhere(['s.code' => $code]); + }]); + break; + default: + throw new Exception('角色参数错误'); + } + return $query->all(); + } + + /** + * 获取库存不足的药品 + * @return array|ActiveRecord[] + * @throws Exception + */ + public function actionLowStock() + { + $admin = \Yii::$app->user->identity; + if ($admin->role != UserRoleEnum::SUPER_ADMIN) { + return $this->storeLowStock(); + } + $list = DrugStoreDrug::find() + ->with(['drug' => function ($query) { + $query->select(['id', 'drug_name', 'drug_number', 'type', 'image']); + }]) + ->where(['<', 'stock', 100]) + ->orderBy(['stock' => SORT_ASC]) + ->asArray(); + if (empty($list)) { + $list = DrugStoreDrug::find() + ->with(['drug' => function ($query) { + $query->select(['id', 'drug_name', 'drug_number', 'type', 'image']); + }]) + ->orderBy(['stock' => SORT_ASC]) + ->limit(10) + ->asArray(); + } + return $list->all(); + } + + /** + * 获取除总仓库外的库存不足的药品 + * @return array|ActiveRecord[] + * @throws Exception + */ + public function storeLowStock() + { + $query = DrugStoreRelations::find() + ->with(['drug' => function ($query) { + $query->select(['id', 'drug_name', 'drug_number', 'type', 'image']); + }]) + ->select(['drug_id', 'store_id', 'stock']) + ->asArray(); + + + // 根据角色查询自己有权限的数据 + $admin = \Yii::$app->user->identity; + + switch ($admin->role) { + case UserRoleEnum::STORE_ADMIN: + // 门店 + if (empty($admin->store_id)) throw new Exception('门店信息获取失败'); + $where = ['store_id' => $admin->store_id]; + break; + case UserRoleEnum::PROVINCE_DAI: + // 省代 + if (empty($admin->province_id)) throw new Exception('省区经理信息获取失败' . $admin->province_id); + $store_id = Store::find()->select(['id'])->where(['province_id' => $admin->province_id])->column(); + $where = ['in', 'store_id', $store_id]; + break; + case UserRoleEnum::CITY_DAI: + // 市代 + if (empty($admin->city_id)) throw new Exception('市区经理信息获取失败' . $admin->city_id); + $store_id = Store::find()->select(['id'])->where(['city_id' => $admin->city_id])->column(); + $where = ['in', 'store_id', $store_id]; + break; + case UserRoleEnum::SUPPLY: + // 业务员 + if (empty($admin->code)) throw new Exception('市区经理信息获取失败' . $admin->code); + $store_id = Store::find()->select(['id'])->where(['code' => $admin->code])->column(); + $where = ['in', 'store_id', $store_id]; + break; + default: + // 不需要额外的条件,直接排序 + throw new Exception('角色参数错误'); + break; + } + + $list = $query->where($where)->andWhere(['<', 'stock', 100])->orderBy(['stock' => SORT_ASC])->all(); + if (empty($list)) { + $list = $query->where($where)->orderBy(['stock' => SORT_ASC])->limit(10)->all(); + } + + return $list; + } + + /** + * 排行前5的诊所 + * @return array|ActiveRecord[] + */ + public function actionTopStore() + { + return CashAccount::find() + ->with(['store' => function ($query) { + $query->select(['id', 'name']); + }]) + ->where(['user_type' => 1]) + ->select(['user_id', 'total_cash']) + ->orderBy(['total_cash' => SORT_DESC]) + ->limit(5) + ->asArray() + ->all(); + } + + /** + * 获取最近一周的退款列表 + * @return array|ActiveRecord[] + * @throws Exception + */ + public function actionOrderRefund() + { + // refund_time + // yii_product_order_refund + // ['between', 'created_at', strtotime('-90 day'), time()] + $query = ProductOrder::find() + ->with(['orderItems', 'productOrderRefund' => function ($query) { + $query->where(['between', 'created_at', strtotime('-7 day'), time()]); + }]) + ->where(['status' => 1]) + ->orderBy(['id' => SORT_DESC]) + ->asArray(); + + // 根据角色查询自己有权限的数据 + $admin = \Yii::$app->user->identity; + + switch ($admin->role) { + case UserRoleEnum::STORE_ADMIN: + // 门店 + if (empty($admin->store_id)) throw new Exception('门店信息获取失败'); + $where = ['store_id' => $admin->store_id]; + break; + case UserRoleEnum::PROVINCE_DAI: + // 省代 + if (empty($admin->province_id)) throw new Exception('省区经理信息获取失败' . $admin->province_id); + $store_id = Store::find()->select(['id'])->where(['province_id' => $admin->province_id])->column(); + $where = ['in', 'store_id', $store_id]; + break; + case UserRoleEnum::CITY_DAI: + // 市代 + if (empty($admin->city_id)) throw new Exception('市区经理信息获取失败' . $admin->city_id); + $store_id = Store::find()->select(['id'])->where(['city_id' => $admin->city_id])->column(); + $where = ['in', 'store_id', $store_id]; + break; + case UserRoleEnum::SUPPLY: + // 业务员 + if (empty($admin->code)) throw new Exception('市区经理信息获取失败' . $admin->code); + $store_id = Store::find()->select(['id'])->where(['code' => $admin->code])->column(); + $where = ['in', 'store_id', $store_id]; + break; + default: + // 不需要额外的条件,直接排序 + throw new Exception('角色参数错误'); + break; + } + if (!empty($where)) { + $query->andWhere($where); + } + $list = $query->all(); + foreach ($list as &$v) { + $v['order_names'] = implode(',', array_column($v['orderItems'], 'drug_name')); + $v['created_at'] = date('Y-m-d H:i:s', $v['created_at']); + } + return $list; + } + + /** + * 近30天加入的诊所 + * @return \yii\data\ActiveDataProvider + */ + public function actionNewStore() + { + $query = Store::find()->with(['drugStore', 'province', 'city', 'admin'])->where(['between', 'created_at', strtotime('-7 day'), time()]); + + $this->field = [ + Store::class => [ + 'id', 'plate_id', 'name', 'position', 'offical_seal', 'pic', 'contact', 'mobile', /*'qr_code',*/ + 'start_time', 'end_time', + 'drugstore_id', 'see_rate', + 'drugstore' => 'drugStore.name', + 'code', 'uid', 'supply' => 'admin.username', + 'province' => 'province.name', + 'city' => 'city.name', + 'province_id', + 'city_id', 'erp_id', + 'created_at' => function ($d) { + return date('Y-m-d H:i:s', $d['created_at']); + } + ] + ]; + return $this->create($query, []); + } +} \ No newline at end of file diff --git a/common/models/CashAccount.php b/common/models/CashAccount.php index 7c82014..24de8c6 100644 --- a/common/models/CashAccount.php +++ b/common/models/CashAccount.php @@ -24,4 +24,9 @@ class CashAccount extends \common\modelsgii\CashAccount { return $this->hasOne(Admin::class,['uid'=>'user_id']); } + // 关联诊所 + public function getStore() + { + return $this->hasOne(Store::class,['id'=>'user_id']); + } } diff --git a/common/models/DrugStoreRelations.php b/common/models/DrugStoreRelations.php index 9f61817..825564a 100644 --- a/common/models/DrugStoreRelations.php +++ b/common/models/DrugStoreRelations.php @@ -20,4 +20,10 @@ class DrugStoreRelations extends \common\modelsgii\DrugStoreRelations { return $this->hasOne(DrugStoreDrug::class,['drug_id'=>'drug_id']); } + + + public function getStore() + { + return $this->hasOne(Store::class,['id' => 'store_id']); + } }