UserBuddy.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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|\think\db\false|PDOStatement|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. ->select();
  22. }
  23. /**
  24. * 找到好友
  25. * @param int $user
  26. * @param int $buddy
  27. * @return array|\think\db\false|PDOStatement|string|\think\Model
  28. */
  29. public function getBuddy($user, $buddy){
  30. return $this->db()->where(['user_id'=>$user, 'buddy_id'=>$buddy])->find();
  31. // return $this->db()->alias("b")
  32. // ->field("u.id, u.nickname, u.praise, u.robot")
  33. // ->join("gd_user u", "u.id = b.user_id", "LEFT")
  34. // ->where(['user_id'=>$user, 'buddy_id'=>$buddy])
  35. // ->find();
  36. }
  37. /**
  38. * 好友列表数量
  39. * @param int $user
  40. * @return number|string
  41. */
  42. public function getBuddyCount($user){
  43. return $this->db()->where(['user_id'=>$user, 'status'=>UserBuddy::Friend])->count();
  44. }
  45. public function remove($data){
  46. return $this->db()->delete($data);
  47. }
  48. public function addAndSave($data){
  49. $this->data = $data;
  50. return $this->allowField(true)->isUpdate((isset($data['id']) && $data['id'])?true:false)->save($data);
  51. }
  52. }