GhsNoticeClass.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. namespace bizGhs\shop\classes;
  3. use bizGhs\base\classes\BaseClass;
  4. use common\components\util;
  5. use Yii;
  6. use yii\db\Expression;
  7. /**
  8. * 批发端通知公告业务类
  9. */
  10. class GhsNoticeClass extends BaseClass
  11. {
  12. public static $baseFile = '\bizGhs\shop\models\GhsNotice';
  13. /** 展示位置:分类菜单 */
  14. const POSITION_CATEGORY = 1;
  15. /** 展示位置:订单提交 */
  16. const POSITION_ORDER = 2;
  17. /**
  18. * 展示位置文案映射
  19. */
  20. public static function getPositionMap(): array
  21. {
  22. return [
  23. self::POSITION_CATEGORY => '分类菜单',
  24. self::POSITION_ORDER => '订单提交',
  25. ];
  26. }
  27. /**
  28. * 客户端公告列表(不分页)
  29. */
  30. public static function getClientNoticeList(int $mainId, int $position = 0): array
  31. {
  32. if ($mainId <= 0) {
  33. return [];
  34. }
  35. $query = self::getModel()::find()
  36. ->where(['mainId' => $mainId, 'isDel' => 0, 'status' => 1]);
  37. if ($position > 0) {
  38. $query->andWhere(new Expression('FIND_IN_SET(:position, [[position]])', [':position' => (string)$position]));
  39. }
  40. $list = $query->orderBy('sort DESC, id DESC')->asArray()->all();
  41. return self::groupBaseInfo($list);
  42. }
  43. /**
  44. * 客户端公告详情(仅校验公告本身有效)
  45. */
  46. public static function getClientDetail(int $id): array
  47. {
  48. $info = self::getById($id);
  49. if (empty($info) || intval($info['isDel']) === 1 || intval($info['status']) !== 1) {
  50. util::fail('公告不存在');
  51. }
  52. return self::getDetail($id);
  53. }
  54. /**
  55. * 通用列表查询,供管理端与客户端复用
  56. */
  57. public static function searchList(array $params): array
  58. {
  59. $get = Yii::$app->request->get();
  60. $page = isset($get['page']) ? max(1, intval($get['page'])) : 1;
  61. $pageSize = !empty($get['pageSize']) ? intval($get['pageSize']) : Yii::$app->params['pageSize'];
  62. $query = self::getModel()::find()->where(['mainId' => intval($params['mainId'])]);
  63. if (isset($params['isDel'])) {
  64. $query->andWhere(['isDel' => intval($params['isDel'])]);
  65. }
  66. if (isset($params['status'])) {
  67. $query->andWhere(['status' => intval($params['status'])]);
  68. }
  69. if (!empty($params['title'])) {
  70. $query->andWhere(['like', 'title', trim($params['title'])]);
  71. }
  72. if (!empty($params['position'])) {
  73. $position = intval($params['position']);
  74. $query->andWhere(new Expression('FIND_IN_SET(:position, [[position]])', [':position' => (string)$position]));
  75. }
  76. $order = !empty($params['order']) ? $params['order'] : 'id DESC';
  77. $totalNum = (int)$query->count();
  78. $totalPage = $pageSize > 0 ? (int)ceil($totalNum / $pageSize) : 0;
  79. $list = $query->orderBy($order)
  80. ->offset(($page - 1) * $pageSize)
  81. ->limit($pageSize)
  82. ->asArray()
  83. ->all();
  84. return [
  85. 'totalNum' => $totalNum,
  86. 'totalPage' => $totalPage,
  87. 'moreData' => $page < $totalPage ? 1 : 0,
  88. 'list' => self::groupBaseInfo($list),
  89. ];
  90. }
  91. /**
  92. * 组装列表展示字段
  93. */
  94. public static function groupBaseInfo(array $list): array
  95. {
  96. $positionMap = self::getPositionMap();
  97. foreach ($list as $key => $item) {
  98. $positions = self::formatPositionToArr($item['position'] ?? '');
  99. $labels = [];
  100. foreach ($positions as $pos) {
  101. if (isset($positionMap[$pos])) {
  102. $labels[] = $positionMap[$pos];
  103. }
  104. }
  105. $list[$key]['positions'] = $positions;
  106. $list[$key]['positionLabels'] = $labels;
  107. }
  108. return $list;
  109. }
  110. /**
  111. * 多选位置转逗号分隔字符串
  112. */
  113. public static function formatPositionToStr($positions): string
  114. {
  115. if (is_string($positions)) {
  116. $positions = array_filter(array_map('trim', explode(',', $positions)));
  117. }
  118. if (!is_array($positions)) {
  119. return '';
  120. }
  121. $valid = array_keys(self::getPositionMap());
  122. $positions = array_values(array_unique(array_map('intval', $positions)));
  123. $positions = array_values(array_intersect($positions, $valid));
  124. sort($positions);
  125. return implode(',', $positions);
  126. }
  127. /**
  128. * 逗号分隔字符串转位置数组
  129. */
  130. public static function formatPositionToArr(string $position): array
  131. {
  132. if ($position === '') {
  133. return [];
  134. }
  135. $positions = array_filter(array_map('trim', explode(',', $position)));
  136. return array_values(array_map('intval', $positions));
  137. }
  138. /**
  139. * 校验公告表单数据
  140. */
  141. public static function validateNoticeData(array $data): array
  142. {
  143. $title = trim($data['title'] ?? '');
  144. $summary = trim($data['summary'] ?? '');
  145. if ($title === '') {
  146. util::fail('请输入公告标题');
  147. }
  148. if (mb_strlen($title) > 20) {
  149. util::fail('标题不能超过20个字');
  150. }
  151. if ($summary === '') {
  152. util::fail('请输入公告简介');
  153. }
  154. if (mb_strlen($summary) > 70) {
  155. util::fail('公告简介不能超过70个字');
  156. }
  157. $position = self::formatPositionToStr($data['position'] ?? '');
  158. if ($position === '') {
  159. util::fail('请选择展示位置');
  160. }
  161. $content = $data['content'] ?? '';
  162. if ($content === '' || $content === '[]') {
  163. util::fail('请添加公告内容');
  164. }
  165. if (is_string($content)) {
  166. $contentArr = json_decode($content, true);
  167. if (!is_array($contentArr) || empty($contentArr)) {
  168. util::fail('公告内容格式不正确');
  169. }
  170. }
  171. return [
  172. 'title' => $title,
  173. 'summary' => $summary,
  174. 'content' => is_string($content) ? $content : json_encode($content, JSON_UNESCAPED_UNICODE),
  175. 'position' => $position,
  176. 'sort' => intval($data['sort'] ?? 0),
  177. 'status' => intval($data['status'] ?? 1) === 1 ? 1 : 0,
  178. ];
  179. }
  180. /**
  181. * 新增公告
  182. */
  183. public static function addNotice(array $data): int
  184. {
  185. $noticeData = self::validateNoticeData($data);
  186. if (!empty($data['mainId'])) {
  187. $noticeData['mainId'] = intval($data['mainId']);
  188. }
  189. if (!empty($data['staffId'])) {
  190. $noticeData['staffId'] = intval($data['staffId']);
  191. }
  192. if ($noticeData['status'] === 1) {
  193. $noticeData['publishTime'] = date('Y-m-d H:i:s');
  194. }
  195. $result = self::add($noticeData);
  196. return is_array($result) ? intval($result['id'] ?? 0) : intval($result);
  197. }
  198. /**
  199. * 更新公告
  200. */
  201. public static function updateNotice(int $id, array $data): void
  202. {
  203. $noticeData = self::validateNoticeData($data);
  204. $info = self::getById($id);
  205. if (empty($info)) {
  206. util::fail('公告不存在');
  207. }
  208. if (!empty($data['staffId'])) {
  209. $noticeData['staffId'] = intval($data['staffId']);
  210. }
  211. self::updateById($id, $noticeData);
  212. }
  213. /**
  214. * 公告详情
  215. */
  216. public static function getDetail(int $id): array
  217. {
  218. $info = self::getById($id);
  219. if (empty($info)) {
  220. util::fail('公告不存在');
  221. }
  222. $list = self::groupBaseInfo([$info]);
  223. return current($list);
  224. }
  225. /**
  226. * 软删除公告
  227. */
  228. public static function deleteNotice(int $id, int $staffId = 0): void
  229. {
  230. $updateData = ['isDel' => 1];
  231. if ($staffId > 0) {
  232. $updateData['staffId'] = $staffId;
  233. }
  234. self::updateById($id, $updateData);
  235. }
  236. /**
  237. * 上下架公告
  238. */
  239. public static function updateStatus(int $id, int $status, int $staffId = 0): void
  240. {
  241. $status = $status === 1 ? 1 : 0;
  242. $updateData = ['status' => $status];
  243. if ($status === 1) {
  244. $updateData['publishTime'] = date('Y-m-d H:i:s');
  245. }
  246. if ($staffId > 0) {
  247. $updateData['staffId'] = $staffId;
  248. }
  249. self::updateById($id, $updateData);
  250. }
  251. /**
  252. * 校验公告归属权限
  253. */
  254. public static function valid(array $info, int $mainId): void
  255. {
  256. if (empty($info) || intval($info['mainId']) !== intval($mainId)) {
  257. util::fail('没有权限操作该公告');
  258. }
  259. if (intval($info['isDel']) === 1) {
  260. util::fail('公告已删除');
  261. }
  262. }
  263. }