UserBuddy.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace app\user\model;
  3. use think\Model;
  4. class UserBuddy extends Model {
  5. const Friend = 1;
  6. const Apply = 0;
  7. const Wait = 2;
  8. protected $createTime = 'inputtime'; // 创建时间
  9. protected $updateTime = 'updatetime'; // 每次更新时间
  10. protected $autoWriteTimestamp = 'datetime'; // 开启自动写入时间戳字段
  11. /**
  12. * 查找好友列表
  13. * @param int $user
  14. * @return array|string|\think\Model
  15. */
  16. public function findUserBuddy($user, $status = UserBuddy::Friend){
  17. return $this->db()->alias("b")
  18. ->field("u.id i, u.nickname n, u.praise p, u.robot r, last_login t")
  19. ->join("gd_user u", "u.id = b.buddy_id", "LEFT")
  20. ->where(['b.user_id'=>$user, 'b.status'=>$status])
  21. ->limit(10)
  22. ->select();
  23. }
  24. /**
  25. * 找到好友
  26. * @param int $user
  27. * @param int $buddy
  28. * @return array|string|\think\Model
  29. */
  30. public function getBuddy($user, $buddy){
  31. return $this->db()->where(['user_id'=>$user, 'buddy_id'=>$buddy])->find();
  32. // return $this->db()->alias("b")
  33. // ->field("u.id, u.nickname, u.praise, u.robot")
  34. // ->join("gd_user u", "u.id = b.user_id", "LEFT")
  35. // ->where(['user_id'=>$user, 'buddy_id'=>$buddy])
  36. // ->find();
  37. }
  38. /**
  39. * 好友列表数量
  40. * @param int $user
  41. * @return number|string
  42. */
  43. public function getBuddyCount($user){
  44. return $this->db()->where(['user_id'=>$user, 'status'=>UserBuddy::Friend])->count();
  45. }
  46. public function remove($data){
  47. return $this->db()->delete($data);
  48. }
  49. public function addAndSave($data){
  50. $this->data = $data;
  51. return $this->allowField(true)->isUpdate((isset($data['id']) && $data['id'])?true:false)->save($data);
  52. }
  53. }