UserBuddy.php 1.7 KB

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