ShopNoticeClass.php 7.8 KB

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