123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- namespace app\models\system;
- use app\models\user\User;
- use crmeb\basic\BaseModel;
- use crmeb\traits\ModelTrait;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\Model;
- use think\model\relation\HasOne;
- class Message extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'message';
- use ModelTrait;
- public function fromUser(): HasOne
- {
- return self::hasOne(User::class, 'uid', 'from_uid')->field('nickname,phone,email,uid,avatar');
- }
- /**
- * @param $uid
- * @param $detail
- * @param $cate_id
- * @param int $from_uid
- * @return Message|Model
- */
- public static function addMessage($uid, $detail, $cate_id, int $from_uid = 0)
- {
- $watch = 0;
- $add_time = time();
- return self::create(compact('uid', 'detail', 'cate_id', 'from_uid', 'watch', 'add_time'));
- }
- /**
- * @param $uid
- * @return array
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function getMessageCate($uid): array
- {
- $cate = self::where('uid', $uid)->where('from_uid', 0)->order('add_time desc,id desc')->group('cate_id')->field('cate_id')->select()->toArray();
- foreach ($cate as &$v) {
- $v['from_uid'] = 0;
- $v['name'] = SystemGroupData::getDateValue($v['cate_id'])['name'] ?? '未知';
- $v['image'] = SystemGroupData::getDateValue($v['cate_id'])['image'] ?? '';
- $v['last_message'] = self::where('cate_id', $v['cate_id'])
- ->where('uid', $uid)->order('add_time desc,id desc')
- ->find();
- $v['unwatch_message'] = self::where('cate_id', $v['cate_id'])
- ->where('uid', $uid)->where('watch', 0)
- ->count();
- }
- $cate2 = [];
- $cate2_origin = self::where('from_uid', '>', 0)->order('add_time desc,id desc')->where('uid', $uid)->group('from_uid')->field('from_uid')->select();
- foreach ($cate2_origin as $vv) {
- $cate2[] = [
- 'from_uid' => $vv['from_uid'],
- 'name' => User::where('uid', $vv['from_uid'])->value('nickname'),
- 'cate_id' => 0,
- 'image' => User::where('uid', $vv['from_uid'])->where('avatar'),
- 'last_message' => self::where('cate_id', 0)
- ->where('from_uid', $vv['from_uid'])
- ->where('uid', $uid)->order('add_time desc,id desc')
- ->find(),
- 'unwatch_message' => self::where('cate_id', 0)
- ->where('from_uid', $vv['from_uid'])
- ->where('uid', $uid)->where('watch', 0)
- ->count(),];
- }
- return array_merge($cate, $cate2);
- }
- /**
- * @param $cid
- * @param $uid
- * @param $page
- * @param $limit
- * @param int $from_uid
- * @return array
- * @throws DataNotFoundException
- * @throws ModelNotFoundException
- */
- public static function getMessages($cid, $uid, $page, $limit, int $from_uid = 0): array
- {
- self::where('cate_id', $cid)
- ->where('from_uid', $from_uid)
- ->where('uid', $uid)
- ->update(['watch' => 1]);
- $list = self::with('from_user')->where('cate_id', $cid)
- ->where('from_uid', $from_uid)
- ->where('uid', $uid)
- ->order('add_time desc,id desc')
- ->page((int)$page, (int)$limit)
- ->select();
- $count = self::where('cate_id', $cid)
- ->where('from_uid', $from_uid)
- ->where('uid', $uid)
- ->count();
- $cate = SystemGroupData::getDateValue($cid);
- if (!$cate) {
- $cate = [
- 'id' => 0,
- 'name' => '用户消息',
- ];
- }
- return compact('list', 'count', 'cate');
- }
- }
|