文件
xionggeshop/application/common/model/AttachmentModel.php

216 行
5.6 KiB
PHP

<?php
namespace app\common\model;
use think\Model;
/**
* 附件模型
* Class AttachmentModel
* @package app\common\model
*/
class AttachmentModel extends Model
{
protected $name = 'attachment';
/**
* 获取列表
* @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 = '*')
{
// 条件搜索
if (!empty($params['where'])) {
$this->where($params['where']);
} else {
if (!empty($params['keyword'])) {
switch ($params['type']) {
case 1 :
$this->where(['filename' => ['LIKE', '%' . $params['keyword'] . '%']]);
break;
}
}
if (!empty($params['nav'])) {
if (count($params['nav']) == 1) {
$this->where(['one_id' => $params['nav'][0], 'two_id' => 0]);
} else {
$this->where(['one_id' => $params['nav'][0], 'two_id' => $params['nav'][1]]);
}
} else {
$this->where(['one_id' => 0, 'two_id' => 0]);
}
if (isset($params['is_del'])) {
$this->where(['is_del' => $params['is_del']]);
}
if (!empty($params['category'])) {
$this->where(['category' => $params['category']]);
}
if (!empty($params['mimetype'])) {
$this->where(['mimetype' => ['LIKE', '' . $params['mimetype'] . '%']]);
}
// 时间范围查询
if (!empty($params['start_time'])) {
$this->where('createtime', '>=', $params['start_time']);
}
if (!empty($params['end_time'])) {
$this->where('createtime', '<=', $params['end_time']);
}
}
// 排序
$order = 'id DESC';
if (!empty($params['order'])) {
switch ($params['order']) {
case 1 :
$order = 'id ASC';
break;
case 2 :
$order = 'id DESC';
break;
case 3 :
$order = 'filename ASC';
break;
case 4 :
$order = 'filename DESC';
break;
}
}
$result = $this
->field($fields)
->order($order)
->paginate($rows, false, [
'page' => $page
]);
$arr = [
'list' => [],
'total' => 0,
'page' => 1,
'pageSize' => $rows
];
// 格式化数据信息
if (!$result->isEmpty()) {
$arr = [
'list' => $result->items(),
'total' => $result->total(),
'page' => $result->currentPage(),
'pageSize' => $result->listRows()
];
}
return $arr;
}
/**
* 归类
* @param $data
* @return bool|mixed
*/
public function nav($data)
{
if (empty($data) || !is_array($data)) {
$this->error = '参数不正确';
return false;
}
return $this->transaction(function () use ($data) {
foreach ($data['ids'] as $v) {
$oneId = $data['nav'][0];
$twoId = !empty($data['nav'][1]) ? $data['nav'][1] : 0;
$this->update([
'id' => $v,
'one_id' => $oneId,
'two_id' => $twoId,
]);
}
return true;
});
}
/**
* 添加
* @param $data
* @return bool|int|string
*/
public function add($data)
{
if (empty($data) || !is_array($data)) {
$this->error = '参数不正确';
return false;
}
if ($this->insertGetId($data)) {
return true;
} else {
$this->error = '添加出错了,请重新尝试。';
return false;
}
}
/**
* 获取某条记录
* @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)
{
if (empty($where)) {
$this->error = '参数不正确。';
return false;
}
if (floor($where) > 0) {
$where = ['id' => $where];
}
return $this->where($where)->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($where)
{
if (empty($where)) {
$this->error = '参数不正确';
return false;
}
if ($this->where($where)->delete()) {
return true;
} else {
$this->error = '删除出错了,请重新尝试。';
return false;
}
}
}