NoticeApi.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace app\common\api;
  3. use app\admin\model\Notice;
  4. /**
  5. * Description of NoticeApi
  6. *
  7. * @author sgq
  8. */
  9. class NoticeApi {
  10. public static function getOne($id) {
  11. return Notice::findOrEmpty($id);
  12. }
  13. public static function getList($params = []) {
  14. $where = [];
  15. $condition = $params["condition"];
  16. $order = $params["order"] ?: "desc";
  17. $offset = $params["offset"] ?: 0;
  18. $limit = $params["limit"] ?: 10;
  19. if ($condition) {
  20. $where[] = ["n.title", "like", "%{$condition}%"];
  21. $where[] = ["n.content", "like", "%{$condition}%"];
  22. }
  23. $count = Notice::alias("n")->whereOr($where)->count();
  24. $list = Notice::alias("n")->whereOr($where)
  25. ->leftJoin("sys_user u", "u.id=n.creater")
  26. ->leftJoin("sys_company c", "c.id=u.companyId")
  27. ->order(["n.isTop" => "asc", "n.topTime" => $order, "n.createtime" => $order])
  28. ->limit($offset, $limit)
  29. ->field("n.*,concat(u.name,'(',c.name,')') as createrName")
  30. ->select()->toArray();
  31. return ["total" => $count, "rows" => $list];
  32. }
  33. public static function edit($params) {
  34. $data["title"] = $params["title"];
  35. $data["content"] = $params["content"];
  36. $data["batch"] = $params["batch"];
  37. $data["showWay"] = $params["showWay"];
  38. if ($params["id"]) {
  39. $data["id"] = $params["id"];
  40. return Notice::update($data);
  41. } else {
  42. $data["createtime"] = date("Y-m-d H:i:s");
  43. $data["creater"] = session("user")["uid"];
  44. $data["topTime"] = date("Y-m-d H:i:s");
  45. $data["isTop"] = 2;
  46. return Notice::insert($data);
  47. }
  48. }
  49. public static function delete($id) {
  50. return Notice::where(["id" => $id])->delete();
  51. }
  52. public static function setTop($id, $top = 1) {
  53. $upd["id"] = $id;
  54. $upd["isTop"] = $top;
  55. $upd["topTime"] = date("Y-m-d H:i:s");
  56. return Notice::update($upd);
  57. }
  58. }