UserNotice.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/11
  6. */
  7. namespace app\models\user;
  8. use app\models\user\UserNoticeSee;
  9. use crmeb\traits\ModelTrait;
  10. use crmeb\basic\BaseModel;
  11. /**
  12. * TODO 用户通知Model
  13. * Class UserNotice
  14. * @package app\models\user
  15. */
  16. class UserNotice extends BaseModel
  17. {
  18. use ModelTrait;
  19. public static function getNotice($uid){
  20. $count_notice = self::where('uid','like',"%,$uid,%")->where("is_send",1)->count();
  21. $see_notice = UserNoticeSee::where("uid",$uid)->count();
  22. return $count_notice-$see_notice;
  23. }
  24. /**
  25. * @return array
  26. */
  27. public static function getNoticeList($uid,$page,$limit = 8){
  28. //定义分页信息
  29. $count = self::where('uid','like',"%,$uid,%")->count();
  30. $data["lastpage"] = ceil($count/$limit) <= ($page+1) ? 1 : 0;
  31. $where['uid'] = array("like","%,$uid,%");
  32. // $where['uid'] = array(array("like","%,$uid,%"),array("eq",""), 'or');
  33. $where['is_send'] = 1;
  34. $list = self::where($where)->field('id,user,title,content,add_time')->order("add_time desc")->limit($page*$limit,$limit)->select()->toArray();
  35. foreach ($list as $key => $value) {
  36. $list[$key]["add_time"] = date("Y-m-d H:i:s",$value["add_time"]);
  37. $list[$key]["is_see"] = UserNoticeSee::where("uid",$uid)->where("nid",$value["id"])->count() > 0 ? 1 : 0;
  38. }
  39. $data["list"] = $list;
  40. return $data;
  41. }
  42. /**
  43. * @return array
  44. */
  45. public static function seeNotice($uid,$nid){
  46. if(UserNoticeSee::where("uid",$uid)->where("nid",$nid)->count() <= 0){
  47. $data["nid"] = $nid;
  48. $data["uid"] = $uid;
  49. $data["add_time"] = time();
  50. UserNoticeSee::create($data);
  51. }
  52. }
  53. /**
  54. * @param array $where
  55. * @return array
  56. */
  57. public static function getList($where=[]){
  58. $model = new self;
  59. $model->order('id desc');
  60. if(!empty($where)){
  61. $list=($list=$model->page((int)$where['page'],(int)$where['limit'])->select()) && count($list) ? $list->toArray() : [];
  62. foreach ($list as &$item){
  63. if($item["uid"] != ''){
  64. $uids = explode(",",$item["uid"]);
  65. array_splice($uids,0,1);
  66. array_splice($uids,count($uids)-1,1);
  67. $item["uid"] = $uids;
  68. }
  69. $item['send_time']=date('Y-m-d H:i:s',$item['send_time']);
  70. }
  71. $count=self::count();
  72. return compact('count','list');
  73. }
  74. return self::page($model,function($item,$key){
  75. if($item["uid"] != ''){
  76. $uids = explode(",",$item["uid"]);
  77. array_splice($uids,0,1);
  78. array_splice($uids,count($uids)-1,1);
  79. $item["uid"] = $uids;
  80. }
  81. });
  82. }
  83. }