tp/app/admin/controller/BabyHealth/BabysController.php

417 lines
12 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
declare(strict_types=1);
namespace app\admin\controller\BabyHealth;
use app\admin\BaseController;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Session;
use think\response\Json;
use think\db\exception\DbException;
use app\model\AppsBabyhealthBabys;
use app\model\AppsBabyhealthUsers;
class BabysController extends BaseController
{
/**
* 获取宝贝信息
* @return Json
*/
public function getBabyList()
{
try {
$babylist = AppsBabyhealthBabys::where('delete_time', null)
->field('id, name, nickname, sex, avatar, birth, father, mother, height, weight, status, create_time, update_time')
->select()
->toArray();
// 查询每个宝贝的父母名称
foreach ($babylist as &$baby) {
$parentNames = $this->findParentNames(
(int)($baby['father'] ?? 0),
(int)($baby['mother'] ?? 0)
);
$baby['father'] = $parentNames['father_name'];
$baby['mother'] = $parentNames['mother_name'];
}
// 记录操作日志
$this->logSuccess('宝贝管理', '获取宝贝信息', ['data' => $babylist]);
return json([
'code' => 200,
'msg' => '获取成功',
'data' => $babylist
]);
} catch (DbException $e) {
// 记录失败日志
$this->logFail('宝贝管理', '获取宝贝信息', $e->getMessage());
return json([
'code' => 500,
'msg' => '获取失败:' . $e->getMessage(),
'data' => []
]);
}
}
/**
* 创建宝贝信息
* @return Json
*/
public function createBaby()
{
try {
// 获取请求参数
$data = $this->request->post();
// 准备宝贝数据
$babyData = [
'name' => $data['name'],
'nickname' => $data['nickname'],
'sex' => $data['sex'],
'birth' => $data['birth'],
'height' => $data['height'],
'weight' => $data['weight'],
'avatar' => $data['avatar'],
'status' => $data['status'],
'create_time' => date('Y-m-d H:i:s'),
];
// 创建宝贝信息
$result = AppsBabyhealthBabys::insertGetId($babyData);
if ($result === false) {
return json([
'code' => 500,
'msg' => '创建失败'
]);
}
// 记录操作日志
$this->logSuccess('宝贝管理', '创建宝贝信息', ['id' => $result]);
return json([
'code' => 200,
'msg' => '创建成功',
'data' => ['id' => $result]
]);
} catch (ValidateException $e) {
// 记录失败日志
$this->logFail('宝贝管理', '创建宝贝信息', $e->getMessage());
return json([
'code' => 400,
'msg' => $e->getError()
]);
} catch (DbException $e) {
// 记录失败日志
$this->logFail('宝贝管理', '创建宝贝信息', $e->getMessage());
return json([
'code' => 500,
'msg' => '创建失败:' . $e->getMessage()
]);
}
}
/**
* 编辑宝贝信息
* @param int $id Baby ID
* @return Json
*/
public function editBaby(int $id)
{
try {
// 获取请求参数
$data = $this->request->post();
// 检查宝贝信息是否存在
$baby = AppsBabyhealthBabys::where('id', $id)
->where('delete_time', null)
->find();
if (!$baby) {
return json([
'code' => 404,
'msg' => '宝贝信息不存在'
]);
}
// 准备更新数据
$updateData = [
'name' => $data['name'],
'nickname' => $data['nickname'],
'sex' => $data['sex'],
'birth' => $data['birth'],
'height' => $data['height'],
'weight' => $data['weight'],
'avatar' => $data['avatar'],
'status' => $data['status'],
'update_time' => date('Y-m-d H:i:s'),
];
// 执行更新
AppsBabyhealthBabys::where('id', $id)->update($updateData);
// 获取更新后的宝贝信息
$updatedBaby = AppsBabyhealthBabys::where('id', $id)->find();
// 记录操作日志
$this->logSuccess('宝贝管理', '更新宝贝信息', ['id' => $id]);
return json([
'code' => 200,
'msg' => '更新成功',
'data' => $updatedBaby ? $updatedBaby->toArray() : []
]);
} catch (ValidateException $e) {
// 记录失败日志
$this->logFail('宝贝管理', '更新宝贝信息', $e->getMessage());
return json([
'code' => 400,
'msg' => $e->getError()
]);
} catch (DbException $e) {
// 记录失败日志
$this->logFail('宝贝管理', '更新宝贝信息', $e->getMessage());
return json([
'code' => 500,
'msg' => '更新失败:' . $e->getMessage()
]);
}
}
/**
* 删除宝贝信息
* @param int $id Baby ID
* @return Json
*/
public function deleteBaby(int $id)
{
try {
// 检查宝贝信息是否存在
$baby = AppsBabyhealthBabys::where('id', $id)
->where('delete_time', null)
->find();
if (!$baby) {
return json([
'code' => 404,
'msg' => '宝贝信息不存在'
]);
}
// 逻辑删除宝贝信息
$result = AppsBabyhealthBabys::where('id', $id)
->where('delete_time', null)
->update([
'delete_time' => date('Y-m-d H:i:s'),
'update_time' => date('Y-m-d H:i:s')
]);
if ($result === false) {
return json([
'code' => 500,
'msg' => '删除失败'
]);
}
// 记录操作日志
$this->logSuccess('宝贝管理', '删除宝贝信息', ['id' => $id]);
return json([
'code' => 200,
'msg' => '删除成功'
]);
} catch (DbException $e) {
// 记录失败日志
$this->logFail('宝贝管理', '删除宝贝信息', $e->getMessage());
return json([
'code' => 500,
'msg' => '删除失败:' . $e->getMessage()
]);
}
}
/**
* 预览宝贝信息
* @param int $id Baby ID
* @return Json
*/
public function getBabyDetail(int $id)
{
try {
// 查询宝贝信息
$baby = AppsBabyhealthBabys::where('id', $id)
->where('delete_time', null)
->find();
if (!$baby) {
return json([
'code' => 404,
'msg' => '宝贝信息不存在'
]);
}
// 记录操作日志
$this->logSuccess('宝贝监护-宝贝管理', '获取宝贝信息', ['id' => $id]);
return json([
'code' => 200,
'msg' => '获取成功',
'data' => $baby->toArray()
]);
} catch (DbException $e) {
// 记录失败日志
$this->logFail('宝贝监护-宝贝管理', '获取宝贝信息', $e->getMessage());
return json([
'code' => 500,
'msg' => '获取失败:' . $e->getMessage(),
'data' => []
]);
}
}
/**
* 绑定父母
* @param int $id Baby ID
* @return Json
*/
public function bindParent(int $id)
{
try {
// 获取请求参数支持POST
$data = $this->request->post();
// 检查宝贝信息是否存在
$baby = AppsBabyhealthBabys::where('id', $id)
->where('delete_time', null)
->find();
if (!$baby) {
return json([
'code' => 404,
'msg' => '宝贝信息不存在'
]);
}
// 准备更新数据
$updateData = [
'update_time' => date('Y-m-d H:i:s'),
];
// 更新父亲ID
if (isset($data['father_id'])) {
$updateData['father_id'] = $data['father_id'];
}
// 更新母亲ID
if (isset($data['mother_id'])) {
$updateData['mother_id'] = $data['mother_id'];
}
// 执行更新
AppsBabyhealthBabys::where('id', $id)->update($updateData);
// 记录操作日志
$this->logSuccess('宝贝管理', '绑定父母', ['id' => $id, 'father_id' => $data['father_id'] ?? null, 'mother_id' => $data['mother_id'] ?? null]);
return json([
'code' => 200,
'msg' => '绑定成功'
]);
} catch (DbException $e) {
// 记录失败日志
$this->logFail('宝贝管理', '绑定父母', $e->getMessage());
return json([
'code' => 500,
'msg' => '绑定失败:' . $e->getMessage()
]);
}
}
/**
* 获取父母名称
* @param int $id 宝贝ID
* @return Json
*/
public function getParents(int $id)
{
try {
// 查询宝贝的父母ID
$baby = AppsBabyhealthBabys::where('id', $id)
->where('delete_time', null)
->field('father, mother')
->find();
if (!$baby) {
return json([
'code' => 404,
'msg' => '宝贝信息不存在'
]);
}
// 获取父母名称
$parentNames = $this->findParentNames(
(int)($baby['father'] ?? 0),
(int)($baby['mother'] ?? 0)
);
// 记录操作日志
$this->logSuccess('宝贝管理', '获取父母信息', ['id' => $id]);
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'father_id' => $baby['father'] ?? null,
'mother_id' => $baby['mother'] ?? null,
'father' => $parentNames['father_name'],
'mother' => $parentNames['mother_name']
]
]);
} catch (DbException $e) {
// 记录失败日志
$this->logFail('宝贝管理', '获取父母信息', $e->getMessage());
return json([
'code' => 500,
'msg' => '获取失败:' . $e->getMessage(),
'data' => []
]);
}
}
/**
* 查找父母名称
* @param int $father 父亲ID
* @param int $mother 母亲ID
* @return array
*/
private function findParentNames(int $father = 0, int $mother = 0): array
{
$result = [
'father_name' => '',
'mother_name' => ''
];
// 查询父亲名称
if ($father > 0) {
$father = \app\model\AppsBabyhealthUsers::where('id', $father)
->where('delete_time', null)
->field('name')
->find();
if ($father) {
$result['father_name'] = $father['name'];
}
}
// 查询母亲名称
if ($mother > 0) {
$mother = \app\model\AppsBabyhealthUsers::where('id', $mother)
->where('delete_time', null)
->field('name')
->find();
if ($mother) {
$result['mother_name'] = $mother['name'];
}
}
return $result;
}
}