NoticeApi.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. if ($params["id"]) {
  38. $data["id"] = $params["id"];
  39. return Notice::update($data);
  40. } else {
  41. $data["createtime"] = date("Y-m-d H:i:s");
  42. $data["creater"] = session("user")["uid"];
  43. $data["isTop"] = 2;
  44. return Notice::insert($data);
  45. }
  46. }
  47. public static function delete($id) {
  48. return Notice::where(["id" => $id])->delete();
  49. }
  50. public static function setTop($id) {
  51. $upd["id"] = $id;
  52. $upd["isTop"] = 1;
  53. $upd["topTime"] = date("Y-m-d H:i:s");
  54. return Notice::update($upd);
  55. }
  56. }