Message.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace app\models\system;
  3. use app\models\user\User;
  4. use crmeb\basic\BaseModel;
  5. use crmeb\traits\ModelTrait;
  6. use think\db\exception\DataNotFoundException;
  7. use think\db\exception\DbException;
  8. use think\db\exception\ModelNotFoundException;
  9. use think\Model;
  10. use think\model\relation\HasOne;
  11. class Message extends BaseModel
  12. {
  13. /**
  14. * 数据表主键
  15. * @var string
  16. */
  17. protected $pk = 'id';
  18. /**
  19. * 模型名称
  20. * @var string
  21. */
  22. protected $name = 'message';
  23. use ModelTrait;
  24. public function fromUser(): HasOne
  25. {
  26. return self::hasOne(User::class, 'uid', 'from_uid')->field('nickname,phone,email,uid,avatar');
  27. }
  28. /**
  29. * @param $uid
  30. * @param $detail
  31. * @param $cate_id
  32. * @param int $from_uid
  33. * @return Message|Model
  34. */
  35. public static function addMessage($uid, $detail, $cate_id, int $from_uid = 0)
  36. {
  37. $watch = 0;
  38. $add_time = time();
  39. return self::create(compact('uid', 'detail', 'cate_id', 'from_uid', 'watch', 'add_time'));
  40. }
  41. /**
  42. * @param $uid
  43. * @return array
  44. * @throws DataNotFoundException
  45. * @throws DbException
  46. * @throws ModelNotFoundException
  47. */
  48. public static function getMessageCate($uid): array
  49. {
  50. $cate = self::where('uid', $uid)->where('from_uid', 0)->order('add_time desc,id desc')->group('cate_id')->field('cate_id')->select()->toArray();
  51. foreach ($cate as &$v) {
  52. $v['from_uid'] = 0;
  53. $v['name'] = SystemGroupData::getDateValue($v['cate_id'])['name'] ?? '未知';
  54. $v['image'] = SystemGroupData::getDateValue($v['cate_id'])['image'] ?? '';
  55. $v['last_message'] = self::where('cate_id', $v['cate_id'])
  56. ->where('uid', $uid)->order('add_time desc,id desc')
  57. ->find();
  58. $v['unwatch_message'] = self::where('cate_id', $v['cate_id'])
  59. ->where('uid', $uid)->where('watch', 0)
  60. ->count();
  61. }
  62. $cate2 = [];
  63. $cate2_origin = self::where('from_uid', '>', 0)->order('add_time desc,id desc')->where('uid', $uid)->group('from_uid')->field('from_uid')->select();
  64. foreach ($cate2_origin as $vv) {
  65. $cate2[] = [
  66. 'from_uid' => $vv['from_uid'],
  67. 'name' => User::where('uid', $vv['from_uid'])->value('nickname'),
  68. 'cate_id' => 0,
  69. 'image' => User::where('uid', $vv['from_uid'])->where('avatar'),
  70. 'last_message' => self::where('cate_id', 0)
  71. ->where('from_uid', $vv['from_uid'])
  72. ->where('uid', $uid)->order('add_time desc,id desc')
  73. ->find(),
  74. 'unwatch_message' => self::where('cate_id', 0)
  75. ->where('from_uid', $vv['from_uid'])
  76. ->where('uid', $uid)->where('watch', 0)
  77. ->count(),];
  78. }
  79. return array_merge($cate, $cate2);
  80. }
  81. /**
  82. * @param $cid
  83. * @param $uid
  84. * @param $page
  85. * @param $limit
  86. * @param int $from_uid
  87. * @return array
  88. * @throws DataNotFoundException
  89. * @throws ModelNotFoundException
  90. */
  91. public static function getMessages($cid, $uid, $page, $limit, int $from_uid = 0): array
  92. {
  93. self::where('cate_id', $cid)
  94. ->where('from_uid', $from_uid)
  95. ->where('uid', $uid)
  96. ->update(['watch' => 1]);
  97. $list = self::with('from_user')->where('cate_id', $cid)
  98. ->where('from_uid', $from_uid)
  99. ->where('uid', $uid)
  100. ->order('add_time desc,id desc')
  101. ->page((int)$page, (int)$limit)
  102. ->select();
  103. $count = self::where('cate_id', $cid)
  104. ->where('from_uid', $from_uid)
  105. ->where('uid', $uid)
  106. ->count();
  107. $cate = SystemGroupData::getDateValue($cid);
  108. if (!$cate) {
  109. $cate = [
  110. 'id' => 0,
  111. 'name' => '用户消息',
  112. ];
  113. }
  114. return compact('list', 'count', 'cate');
  115. }
  116. }