UserRemark.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. /**
  5. * 系统消息模型
  6. */
  7. class UserRemark extends Model
  8. {
  9. // 表名
  10. protected $name = 'user_remark';
  11. // 自动写入时间戳字段
  12. protected $autoWriteTimestamp = false;
  13. // 定义时间戳字段名
  14. protected $createTime = false;
  15. protected $updateTime = false;
  16. protected $deleteTime = false;
  17. /**
  18. * 设置备注
  19. * @param int $user_id
  20. * @param int $to_user_id
  21. * @param string $remark
  22. * @return array
  23. */
  24. public function setRemark(int $user_id, int $to_user_id, string $remark = '')
  25. {
  26. if ($user_id === $to_user_id) {
  27. return [false, '不可以给自己设置备注'];
  28. }
  29. if ($info = $this->where(['user_id' => $user_id, 'to_user_id' => $to_user_id])->find()) {
  30. $res = $this->where('id', $info['id'])->update(['nickname_remark' => $remark, 'updatetime' => time()]);
  31. } else {
  32. $res = $this->insert(['user_id' => $user_id, 'to_user_id' => $to_user_id, 'nickname_remark' => $remark, 'updatetime' => time()]);
  33. }
  34. if (!$res) {
  35. return [false, '设置失败'];
  36. }
  37. return [true, '设置成功'];
  38. }
  39. /**
  40. * 删除备注
  41. * @param int $user_id
  42. * @param int $to_user_id
  43. * @return array
  44. */
  45. public function delRemark(int $user_id, int $to_user_id)
  46. {
  47. if ($info = $this->where(['user_id' => $user_id, 'to_user_id' => $to_user_id])->find()) {
  48. $res = $this->where('id', $info['id'])->update(['nickname_remark' => '', 'updatetime' => time()]);
  49. if (!$res) {
  50. return [false, '设置失败'];
  51. }
  52. }
  53. return [true, '设置成功'];
  54. }
  55. }