|
|
@@ -0,0 +1,288 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace bizGhs\shop\classes;
|
|
|
+
|
|
|
+use bizGhs\base\classes\BaseClass;
|
|
|
+use common\components\util;
|
|
|
+use Yii;
|
|
|
+use yii\db\Expression;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 批发端通知公告业务类
|
|
|
+ */
|
|
|
+class GhsNoticeClass extends BaseClass
|
|
|
+{
|
|
|
+ public static $baseFile = '\bizGhs\shop\models\GhsNotice';
|
|
|
+
|
|
|
+ /** 展示位置:分类菜单 */
|
|
|
+ const POSITION_CATEGORY = 1;
|
|
|
+ /** 展示位置:订单提交 */
|
|
|
+ const POSITION_ORDER = 2;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 展示位置文案映射
|
|
|
+ */
|
|
|
+ public static function getPositionMap(): array
|
|
|
+ {
|
|
|
+ return [
|
|
|
+ self::POSITION_CATEGORY => '分类菜单',
|
|
|
+ self::POSITION_ORDER => '订单提交',
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 客户端公告列表(不分页)
|
|
|
+ */
|
|
|
+ public static function getClientNoticeList(int $mainId, int $position = 0): array
|
|
|
+ {
|
|
|
+ if ($mainId <= 0) {
|
|
|
+ return [];
|
|
|
+ }
|
|
|
+ $query = self::getModel()::find()
|
|
|
+ ->where(['mainId' => $mainId, 'isDel' => 0, 'status' => 1]);
|
|
|
+ if ($position > 0) {
|
|
|
+ $query->andWhere(new Expression('FIND_IN_SET(:position, [[position]])', [':position' => (string)$position]));
|
|
|
+ }
|
|
|
+ $list = $query->orderBy('sort DESC, id DESC')->asArray()->all();
|
|
|
+ return self::groupBaseInfo($list);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 客户端公告详情(仅校验公告本身有效)
|
|
|
+ */
|
|
|
+ public static function getClientDetail(int $id): array
|
|
|
+ {
|
|
|
+ $info = self::getById($id);
|
|
|
+ if (empty($info) || intval($info['isDel']) === 1 || intval($info['status']) !== 1) {
|
|
|
+ util::fail('公告不存在');
|
|
|
+ }
|
|
|
+ return self::getDetail($id);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通用列表查询,供管理端与客户端复用
|
|
|
+ */
|
|
|
+ public static function searchList(array $params): array
|
|
|
+ {
|
|
|
+ $get = Yii::$app->request->get();
|
|
|
+ $page = isset($get['page']) ? max(1, intval($get['page'])) : 1;
|
|
|
+ $pageSize = !empty($get['pageSize']) ? intval($get['pageSize']) : Yii::$app->params['pageSize'];
|
|
|
+
|
|
|
+ $query = self::getModel()::find()->where(['mainId' => intval($params['mainId'])]);
|
|
|
+
|
|
|
+ if (isset($params['isDel'])) {
|
|
|
+ $query->andWhere(['isDel' => intval($params['isDel'])]);
|
|
|
+ }
|
|
|
+ if (isset($params['status'])) {
|
|
|
+ $query->andWhere(['status' => intval($params['status'])]);
|
|
|
+ }
|
|
|
+ if (!empty($params['title'])) {
|
|
|
+ $query->andWhere(['like', 'title', trim($params['title'])]);
|
|
|
+ }
|
|
|
+ if (!empty($params['position'])) {
|
|
|
+ $position = intval($params['position']);
|
|
|
+ $query->andWhere(new Expression('FIND_IN_SET(:position, [[position]])', [':position' => (string)$position]));
|
|
|
+ }
|
|
|
+
|
|
|
+ $order = !empty($params['order']) ? $params['order'] : 'id DESC';
|
|
|
+ $totalNum = (int)$query->count();
|
|
|
+ $totalPage = $pageSize > 0 ? (int)ceil($totalNum / $pageSize) : 0;
|
|
|
+ $list = $query->orderBy($order)
|
|
|
+ ->offset(($page - 1) * $pageSize)
|
|
|
+ ->limit($pageSize)
|
|
|
+ ->asArray()
|
|
|
+ ->all();
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'totalNum' => $totalNum,
|
|
|
+ 'totalPage' => $totalPage,
|
|
|
+ 'moreData' => $page < $totalPage ? 1 : 0,
|
|
|
+ 'list' => self::groupBaseInfo($list),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 组装列表展示字段
|
|
|
+ */
|
|
|
+ public static function groupBaseInfo(array $list): array
|
|
|
+ {
|
|
|
+ $positionMap = self::getPositionMap();
|
|
|
+ foreach ($list as $key => $item) {
|
|
|
+ $positions = self::formatPositionToArr($item['position'] ?? '');
|
|
|
+ $labels = [];
|
|
|
+ foreach ($positions as $pos) {
|
|
|
+ if (isset($positionMap[$pos])) {
|
|
|
+ $labels[] = $positionMap[$pos];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $list[$key]['positions'] = $positions;
|
|
|
+ $list[$key]['positionLabels'] = $labels;
|
|
|
+ }
|
|
|
+ return $list;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 多选位置转逗号分隔字符串
|
|
|
+ */
|
|
|
+ public static function formatPositionToStr($positions): string
|
|
|
+ {
|
|
|
+ if (is_string($positions)) {
|
|
|
+ $positions = array_filter(array_map('trim', explode(',', $positions)));
|
|
|
+ }
|
|
|
+ if (!is_array($positions)) {
|
|
|
+ return '';
|
|
|
+ }
|
|
|
+ $valid = array_keys(self::getPositionMap());
|
|
|
+ $positions = array_values(array_unique(array_map('intval', $positions)));
|
|
|
+ $positions = array_values(array_intersect($positions, $valid));
|
|
|
+ sort($positions);
|
|
|
+ return implode(',', $positions);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 逗号分隔字符串转位置数组
|
|
|
+ */
|
|
|
+ public static function formatPositionToArr(string $position): array
|
|
|
+ {
|
|
|
+ if ($position === '') {
|
|
|
+ return [];
|
|
|
+ }
|
|
|
+ $positions = array_filter(array_map('trim', explode(',', $position)));
|
|
|
+ return array_values(array_map('intval', $positions));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验公告表单数据
|
|
|
+ */
|
|
|
+ public static function validateNoticeData(array $data): array
|
|
|
+ {
|
|
|
+ $title = trim($data['title'] ?? '');
|
|
|
+ $summary = trim($data['summary'] ?? '');
|
|
|
+ if ($title === '') {
|
|
|
+ util::fail('请输入公告标题');
|
|
|
+ }
|
|
|
+ if (mb_strlen($title) > 20) {
|
|
|
+ util::fail('标题不能超过20个字');
|
|
|
+ }
|
|
|
+ if ($summary === '') {
|
|
|
+ util::fail('请输入公告简介');
|
|
|
+ }
|
|
|
+ if (mb_strlen($summary) > 70) {
|
|
|
+ util::fail('公告简介不能超过70个字');
|
|
|
+ }
|
|
|
+
|
|
|
+ $position = self::formatPositionToStr($data['position'] ?? '');
|
|
|
+ if ($position === '') {
|
|
|
+ util::fail('请选择展示位置');
|
|
|
+ }
|
|
|
+
|
|
|
+ $content = $data['content'] ?? '';
|
|
|
+ if ($content === '' || $content === '[]') {
|
|
|
+ util::fail('请添加公告内容');
|
|
|
+ }
|
|
|
+ if (is_string($content)) {
|
|
|
+ $contentArr = json_decode($content, true);
|
|
|
+ if (!is_array($contentArr) || empty($contentArr)) {
|
|
|
+ util::fail('公告内容格式不正确');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'title' => $title,
|
|
|
+ 'summary' => $summary,
|
|
|
+ 'content' => is_string($content) ? $content : json_encode($content, JSON_UNESCAPED_UNICODE),
|
|
|
+ 'position' => $position,
|
|
|
+ 'sort' => intval($data['sort'] ?? 0),
|
|
|
+ 'status' => intval($data['status'] ?? 0) === 1 ? 1 : 0,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增公告
|
|
|
+ */
|
|
|
+ public static function addNotice(array $data): int
|
|
|
+ {
|
|
|
+ $noticeData = self::validateNoticeData($data);
|
|
|
+ if (!empty($data['mainId'])) {
|
|
|
+ $noticeData['mainId'] = intval($data['mainId']);
|
|
|
+ }
|
|
|
+ if (!empty($data['staffId'])) {
|
|
|
+ $noticeData['staffId'] = intval($data['staffId']);
|
|
|
+ }
|
|
|
+ if ($noticeData['status'] === 1) {
|
|
|
+ $noticeData['publishTime'] = date('Y-m-d H:i:s');
|
|
|
+ }
|
|
|
+ $result = self::add($noticeData);
|
|
|
+ return is_array($result) ? intval($result['id'] ?? 0) : intval($result);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新公告
|
|
|
+ */
|
|
|
+ public static function updateNotice(int $id, array $data): void
|
|
|
+ {
|
|
|
+ $noticeData = self::validateNoticeData($data);
|
|
|
+ $info = self::getById($id);
|
|
|
+ if (empty($info)) {
|
|
|
+ util::fail('公告不存在');
|
|
|
+ }
|
|
|
+ if (!empty($data['staffId'])) {
|
|
|
+ $noticeData['staffId'] = intval($data['staffId']);
|
|
|
+ }
|
|
|
+ self::updateById($id, $noticeData);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 公告详情
|
|
|
+ */
|
|
|
+ public static function getDetail(int $id): array
|
|
|
+ {
|
|
|
+ $info = self::getById($id);
|
|
|
+ if (empty($info)) {
|
|
|
+ util::fail('公告不存在');
|
|
|
+ }
|
|
|
+ $list = self::groupBaseInfo([$info]);
|
|
|
+ return current($list);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 软删除公告
|
|
|
+ */
|
|
|
+ public static function deleteNotice(int $id, int $staffId = 0): void
|
|
|
+ {
|
|
|
+ $updateData = ['isDel' => 1];
|
|
|
+ if ($staffId > 0) {
|
|
|
+ $updateData['staffId'] = $staffId;
|
|
|
+ }
|
|
|
+ self::updateById($id, $updateData);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上下架公告
|
|
|
+ */
|
|
|
+ public static function updateStatus(int $id, int $status, int $staffId = 0): void
|
|
|
+ {
|
|
|
+ $status = $status === 1 ? 1 : 0;
|
|
|
+ $updateData = ['status' => $status];
|
|
|
+ if ($status === 1) {
|
|
|
+ $updateData['publishTime'] = date('Y-m-d H:i:s');
|
|
|
+ }
|
|
|
+ if ($staffId > 0) {
|
|
|
+ $updateData['staffId'] = $staffId;
|
|
|
+ }
|
|
|
+ self::updateById($id, $updateData);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验公告归属权限
|
|
|
+ */
|
|
|
+ public static function valid(array $info, int $mainId): void
|
|
|
+ {
|
|
|
+ if (empty($info) || intval($info['mainId']) !== intval($mainId)) {
|
|
|
+ util::fail('没有权限操作该公告');
|
|
|
+ }
|
|
|
+ if (intval($info['isDel']) === 1) {
|
|
|
+ util::fail('公告已删除');
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|