Files
mier-php/application/admin/model/Relation.php

118 lines
3.7 KiB
PHP
Raw Normal View History

2025-08-11 10:22:05 +08:00
<?php
namespace app\admin\model;
use think\Db;
class Relation extends AdminComModel
{
//获取数据
public function get_list($page = 1, $page_limit = 15)
{
$list = Db::name('relation')->where('is_delete', 1 )
->order('id', 'desc')
->page($page, $page_limit)->select();
$count = Db::name('relation')->count();
$data = [];
$data['code'] = 0;
$data['msg'] = '获取数据成功';
$data['data'] = $list;
$data['count'] = $count;
return json($data);
}
//添加
public function add($name, $color, $day)
{
if (empty($name) || empty($color) || empty($day)) {
return ['code' => 201, 'msg' => '参数错误', 'data' => null];
}
$type = input('type', 0);
if(empty($type)) {
return ['code' => 201, 'msg' => '请选择类型', 'data' => null];
}
$info = Db::name('relation')
->where('name', $name)
->where('is_delete', 1)
->where('type', $type)
->find();
if ($info) {
return ['code' => 201, 'msg' => '话题已存在', 'data' => null];
}
$data = $this->append_add_update_time([
'name' => $name,
'color' => $color,
'day' => $day,
'type' => $type,
]);
try {
Db::name('relation')->insert($data);
return ['code' => 200, 'msg' => '添加成功', 'data' => null];
} catch (\Exception $e) {
return ['code' => 201, 'msg' => '添加失败', 'data' => null];
}
}
//获取信息
public function get_info($id)
{
$info = Db::name('relation')->find($id);
if (empty($info)) {
return ['code' => 201, 'msg' => '数据不存在', 'data' => null];
}
return ['code' => 200, 'msg' => '获取数据成功', 'data' => $info];
}
//编辑
public function edit($id, $name, $color, $day)
{
if (empty($id)) {
return ['code' => 201, 'msg' => '参数ID错误', 'data' => null];
}
if (empty($name) || empty($color) || empty($day)) {
return ['code' => 201, 'msg' => '参数错误', 'data' => null];
}
$type = input('type', 0);
if(empty($type)) {
return ['code' => 201, 'msg' => '请选择类型', 'data' => null];
}
$info = Db::name('relation')
->where('name', $name)
->where('is_delete', 1)
->where('id', 'neq', $id)
->where('type', $type)
->find();
if ($info) {
return ['code' => 201, 'msg' => '话题已存在', 'data' => null];
}
$result = Db::name('relation')->where('id', $id)
->update([
'name' => $name,
'color' => $color,
'day' => $day,
'update_time' => time(),
'type' => $type,
]);
if ($result) {
return ['code' => 200, 'msg' => '修改成功', 'data' => null];
} else {
return ['code' => 201, 'msg' => '修改失败', 'data' => null];
}
}
//删除
public function del($id)
{
$info = Db::name('relation')->find( $id);
if (empty($info)) {
return ['code' => 201, 'msg' => '数据不存在', 'data' => null];
}
$result = Db::name('relation')->where('id', $id)
->update(['is_delete' => 2, 'update_time' => time()]);
if ($result) {
return ['code' => 200, 'msg' => '删除成功', 'data' => null];
}
return ['code' => 201, 'msg' => '删除失败', 'data' => null];
}
}