123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- <?php
- namespace app\common\service;
- use app\common\model\BodyProfile;
- use app\common\model\BodyMeasurements;
- use app\common\model\BodyTypeSelection;
- use app\common\model\BodyAiReport;
- use think\Db;
- use app\common\exception\BusinessException;
- /**
- * 身体档案服务类
- */
- class BodyProfileService
- {
- /**
- * 创建身体档案
- */
- public static function createProfile($data)
- {
- // 处理身体照片
- if (isset($data['body_photos']) && is_array($data['body_photos'])) {
- $data['body_photos'] = json_encode($data['body_photos']);
- }
- Db::startTrans();
- try {
- $profile = new BodyProfile();
- $result = $profile->save($data);
-
- if ($result === false) {
- throw new BusinessException($profile->getError() ?: '创建档案失败');
- }
- Db::commit();
- return $profile;
- } catch (\Throwable $e) {
- Db::rollback();
- throw new BusinessException($e->getMessage());
- }
- }
- /**
- * 更新身体档案
- */
- public static function updateProfile($profileId, $userId, $data)
- {
- $profile = BodyProfile::where('id', $profileId)
- ->where('user_id', $userId)
- ->find();
- if (!$profile) {
- throw new BusinessException('档案不存在');
- }
- // 处理身体照片
- if (isset($data['body_photos']) && is_array($data['body_photos'])) {
- $data['body_photos'] = json_encode($data['body_photos']);
- }
- Db::startTrans();
- try {
- $result = $profile->save($data);
-
- if ($result === false) {
- throw new BusinessException($profile->getError() ?: '更新档案失败');
- }
- Db::commit();
- return $profile;
- } catch (\Throwable $e) {
- Db::rollback();
- throw new BusinessException($e->getMessage());
- }
- }
- /**
- * 删除身体档案
- */
- public static function deleteProfile($profileId, $userId)
- {
- $profile = BodyProfile::where('id', $profileId)
- ->where('user_id', $userId)
- ->find();
- if (!$profile) {
- throw new BusinessException('档案不存在');
- }
- Db::startTrans();
- try {
- // 删除相关数据
- BodyMeasurements::where('profile_id', $profileId)->delete();
- BodyTypeSelection::where('profile_id', $profileId)->delete();
- BodyAiReport::where('profile_id', $profileId)->delete();
-
- // 删除档案
- $profile->delete();
- Db::commit();
- return true;
- } catch (\Throwable $e) {
- Db::rollback();
- throw new BusinessException($e->getMessage());
- }
- }
- /**
- * 获取用户档案列表
- */
- public static function getUserProfiles($userId)
- {
- $profiles = BodyProfile::where('user_id', $userId)
- ->with(['latestMeasurement', 'latestAiReport'])
- ->order('is_own DESC, id DESC')
- ->select();
- $result = [];
- foreach ($profiles as $profile) {
- $data = $profile->toArray();
- $data['bmi'] = $profile->calculateBMI();
- $data['bmi_level'] = $profile->getBMILevel();
- $result[] = $data;
- }
- return $result;
- }
- /**
- * 获取档案详情
- */
- public static function getProfileDetail($profileId, $userId)
- {
- $profile = BodyProfile::where('id', $profileId)
- ->where('user_id', $userId)
- ->find();
- if (!$profile) {
- throw new BusinessException('档案不存在');
- }
- return $profile->getFullProfileData();
- }
- /**
- * 添加测量数据
- */
- public static function addMeasurement($profileId, $userId, $measurementData)
- {
- // 验证档案归属
- $profile = BodyProfile::where('id', $profileId)
- ->where('user_id', $userId)
- ->find();
- if (!$profile) {
- throw new BusinessException('档案不存在');
- }
- // 过滤测量数据字段
- $measurementFields = array_keys(BodyMeasurements::getMeasurementFields($profile->gender));
- $filteredData = ['profile_id' => $profileId];
- foreach ($measurementFields as $field) {
- if (isset($measurementData[$field]) && $measurementData[$field] !== '') {
- $filteredData[$field] = floatval($measurementData[$field]);
- }
- }
- // 设置测量日期
- if (isset($measurementData['measurement_date']) && $measurementData['measurement_date']) {
- $filteredData['measurement_date'] = strtotime($measurementData['measurement_date']);
- } else {
- $filteredData['measurement_date'] = time();
- }
- $measurement = new BodyMeasurements();
- $result = $measurement->save($filteredData);
- if ($result === false) {
- throw new BusinessException($measurement->getError() ?: '添加测量数据失败');
- }
- return $measurement;
- }
- /**
- * 获取测量历史
- */
- public static function getMeasurementHistory($profileId, $userId, $page = 1, $limit = 10)
- {
- // 验证档案归属
- $profile = BodyProfile::where('id', $profileId)
- ->where('user_id', $userId)
- ->find();
- if (!$profile) {
- throw new BusinessException('档案不存在');
- }
- $list = BodyMeasurements::where('profile_id', $profileId)
- ->order('measurement_date DESC')
- ->paginate($limit, false, ['page' => $page]);
- return [
- 'list' => $list->items(),
- 'total' => $list->total(),
- 'page' => $page,
- 'limit' => $limit
- ];
- }
- /**
- * 保存体型选择
- */
- public static function saveBodyTypeSelection($profileId, $userId, $selections)
- {
- // 验证档案归属
- $profile = BodyProfile::where('id', $profileId)
- ->where('user_id', $userId)
- ->find();
- if (!$profile) {
- throw new BusinessException('档案不存在');
- }
- $result = BodyTypeSelection::saveUserSelections($profileId, $selections);
- if (!$result) {
- throw new BusinessException('保存体型选择失败');
- }
- return true;
- }
- /**
- * 获取体型推荐
- */
- public static function getBodyTypeRecommendation($profileId, $userId)
- {
- // 验证档案归属
- $profile = BodyProfile::where('id', $profileId)
- ->where('user_id', $userId)
- ->with(['latestMeasurement'])
- ->find();
- if (!$profile) {
- throw new BusinessException('档案不存在');
- }
- if (!$profile->latestMeasurement) {
- throw new BusinessException('请先添加身体测量数据');
- }
- return BodyTypeService::getSmartRecommendation($profileId, $profile->latestMeasurement);
- }
- /**
- * 生成AI测试报告
- */
- public static function generateAiReport($profileId, $userId, $reportType = 'comprehensive')
- {
- // 验证档案归属
- $profile = BodyProfile::where('id', $profileId)
- ->where('user_id', $userId)
- ->find();
- if (!$profile) {
- throw new BusinessException('档案不存在');
- }
- $report = BodyAiReport::generateReport($profileId, $reportType);
- if (!$report) {
- throw new BusinessException('生成报告失败,请确保已填写完整的身体数据');
- }
- return $report;
- }
- /**
- * 获取AI报告详情
- */
- public static function getAiReport($reportId, $userId)
- {
- $report = BodyAiReport::with(['profile'])
- ->where('id', $reportId)
- ->find();
- if (!$report || $report->profile->user_id != $userId) {
- throw new BusinessException('报告不存在');
- }
- return $report;
- }
- /**
- * 获取AI报告列表
- */
- public static function getAiReportList($profileId, $userId, $page = 1, $limit = 10)
- {
- // 验证档案归属
- $profile = BodyProfile::where('id', $profileId)
- ->where('user_id', $userId)
- ->find();
- if (!$profile) {
- throw new BusinessException('档案不存在');
- }
- $list = BodyAiReport::where('profile_id', $profileId)
- ->order('generated_time DESC')
- ->paginate($limit, false, ['page' => $page]);
- return [
- 'list' => $list->items(),
- 'total' => $list->total(),
- 'page' => $page,
- 'limit' => $limit
- ];
- }
- /**
- * 获取体型选择建议报告
- */
- public static function getSelectionReport($profileId, $userId)
- {
- // 验证档案归属
- $profile = BodyProfile::where('id', $profileId)
- ->where('user_id', $userId)
- ->find();
- if (!$profile) {
- throw new BusinessException('档案不存在');
- }
- return BodyTypeService::generateSelectionReport($profileId);
- }
- }
|