Files
CompanyProject/芳林公司项目/www.zhiyuanhelp.com/application/admin/service/ModelService.php
2024-09-27 01:32:49 +08:00

95 lines
2.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* 模型基础数据服务
* Class ModelService
* @package service
*/
namespace app\admin\service;
use think\Model;
class ModelService extends Model {
/**
* 主键定义
* @var string
*/
protected $pk = 'id';
/**
* 格式化分页
* @param array $query 分页参数 ['word' => $word]
*/
public function formatQuery($query = []) {
$format_query = [
'query' => [],
];
foreach ($query as $k => $val) {
$format_query['query'] = [$k, $val];
}
return $format_query;
}
/**
* 修改字段值
* @param $update
* @return \think\response\Json
*/
public static function editField($update) {
$data = self::where('id', $update['id'])->update([$update['field'] => $update['value']]);
if ($data == 1) {
return __success('修改成功!');
} else {
return __error('数据没有修改!');
}
}
/**
* 新增
* @param $post
* @return \think\response\Json
* @throws \think\exception\PDOException
*/
public static function addData($post) {
try {
self::insert($post);
} catch (\Exception $e) {
return __error($e->getMessage());
}
return __success('添加成功');
}
/**
* 修改
* @param $post
* @return \think\response\Json
*/
public static function editData($post) {
try {
self::where('id', $post['id'])->update($post);
} catch (\Exception $e) {
return __error($e->getMessage());
}
return __success('修改成功');
}
/**
* 软删除操作
* @param $id 列ID
* @param bool $type 删除类型 false:软删除true:真实删除)
* @return bool|ModelService
* @throws \Exception
*/
public function delData($id, $type = false) {
is_array($id) ? $model = self::whereIn('id', $id) : $model = self::where('id', $id);
$del = $model->delete();
if ($del >= 1) return __success('删除成功');
return __error('删除失败,请检查!');
}
}