137 行
3.5 KiB
PHP
137 行
3.5 KiB
PHP
<?php
|
|
|
|
namespace app\common\model;
|
|
|
|
use think\Model;
|
|
|
|
/**
|
|
* 消息通知模型
|
|
* Class MessageModel
|
|
* @package app\common\model
|
|
*/
|
|
class MessageModel extends Model
|
|
{
|
|
protected $name = 'message';
|
|
|
|
/**
|
|
* 获取列表
|
|
* @param $params
|
|
* @param int $rows
|
|
* @param int $page
|
|
* @param string $fields
|
|
* @return array
|
|
* @throws \think\exception\DbException
|
|
*/
|
|
public function getLists($params, $rows = 20, $page = 1, $fields = '*')
|
|
{
|
|
// 当前模型表名
|
|
$tableName = $this->getQuery()->getTable();
|
|
// 条件搜索
|
|
if (!empty($params['where'])) {
|
|
$this->where($params['where']);
|
|
} else {
|
|
// 搜索参数
|
|
$search = $params['search'];
|
|
|
|
if (!empty($search['content'])) {
|
|
$this->where(['content' => ['LIKE', '%' . $search['content'] . '%']]);
|
|
}
|
|
if (!empty($search['status'])) {
|
|
$this->where('status', '=', $search['status']);
|
|
}
|
|
// 创建时间范围查询
|
|
if (!empty($search['dateTime'])) {
|
|
$startTime = strtotime($search['dateTime'][0]);
|
|
$endTime = strtotime($search['dateTime'][1]);
|
|
$this->where("create_time BETWEEN {$startTime} AND {$endTime}");
|
|
}
|
|
}
|
|
|
|
// 排序
|
|
$order = !empty($params['order']) ? $params['order'] : 'id DESC';
|
|
$result = $this
|
|
->field($fields)
|
|
->order($order)
|
|
->paginate($rows, false, [
|
|
'page' => $page
|
|
]);
|
|
|
|
$arr = [
|
|
'list' => [],
|
|
'total' => 0,
|
|
'page' => 1,
|
|
'pageSize' => $rows,
|
|
'totalSize' => 0
|
|
];
|
|
// 格式化数据信息
|
|
if (!$result->isEmpty()) {
|
|
$arr = [
|
|
'list' => $result->items(),
|
|
'total' => $result->total(),
|
|
'page' => $result->currentPage(),
|
|
'pageSize' => $result->listRows(),
|
|
'totalSize' => $result->lastPage()
|
|
];
|
|
}
|
|
|
|
return $arr;
|
|
}
|
|
|
|
/**
|
|
* 获取某条记录
|
|
* @param $where
|
|
* @return array|bool|false|\PDOStatement|string|Model
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
* @throws \think\exception\DbException
|
|
*/
|
|
public function getItem($where, $fields = '*')
|
|
{
|
|
if (empty($where)) {
|
|
$this->error = '参数不正确。';
|
|
return false;
|
|
}
|
|
if (floor($where) > 0) {
|
|
$where = ['id' => $where];
|
|
}
|
|
|
|
return $this
|
|
->where($where)
|
|
->field($fields)
|
|
->find();
|
|
}
|
|
|
|
/**
|
|
* 编辑
|
|
* @param $data
|
|
* @return bool
|
|
*/
|
|
public function edit($data)
|
|
{
|
|
if (empty($data) || !is_array($data)) {
|
|
$this->error = '参数不正确';
|
|
return false;
|
|
}
|
|
|
|
if ($this->update($data)) {
|
|
return true;
|
|
} else {
|
|
$this->error = '编辑出错了,请重新尝试。';
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 删除
|
|
public function del($id)
|
|
{
|
|
if (empty($id)) {
|
|
$this->error = '参数不正确';
|
|
return false;
|
|
}
|
|
|
|
return $this->transaction(function () use ($id) {
|
|
|
|
return $this->where(['id' => $id])->delete();
|
|
});
|
|
}
|
|
} |