123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 |
- <?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('档案不存在');
- }
- // 过滤测量数据字段
- $gender = $profile->gender ?? 0; // 确保gender不为null
- $measurementFieldsMap = BodyMeasurements::getMeasurementFields($gender);
-
- if (!$measurementFieldsMap || !is_array($measurementFieldsMap)) {
- throw new BusinessException('无法获取测量字段配置');
- }
-
- $measurementFields = array_keys($measurementFieldsMap);
- $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;
- }
- /**
- * 生成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);
- }
- /**
- * 保存测量数据和体型选择(合并业务逻辑)
- */
- public static function saveMeasurementAndBodyType($profileId, $userId, $measurementData = [], $bodyTypeSelections = [], $profileUpdateData = [])
- {
- // 验证档案归属
- $profile = BodyProfile::where('id', $profileId)
- ->where('user_id', $userId)
- ->find();
- if (!$profile) {
- throw new BusinessException('档案不存在');
- }
- $result = [
- 'measurement_saved' => false,
- 'body_type_saved' => false,
- 'measurement_id' => null,
- 'profile_updated' => false
- ];
- Db::startTrans();
- try {
- // 保存测量数据
- if (!empty($measurementData)) {
- $measurement = self::addMeasurement($profileId, $userId, $measurementData);
- $result['measurement_saved'] = true;
- $result['measurement_id'] = $measurement->id;
- }
- // 保存体型选择
- if (!empty($bodyTypeSelections)) {
- $selectionResult = self::saveBodyTypeSelection($profileId, $userId, $bodyTypeSelections);
- $result['body_type_saved'] = $selectionResult;
- }
- // 更新档案信息(身高体重)
- if (!empty($profileUpdateData)) {
- $updateResult = $profile->save($profileUpdateData);
- if ($updateResult === false) {
- throw new BusinessException($profile->getError() ?: '更新档案信息失败');
- }
- $result['profile_updated'] = true;
- }
- Db::commit();
- return $result;
- } catch (\Throwable $e) {
- Db::rollback();
- throw new BusinessException($e->getMessage());
- }
- }
- /**
- * 更新测量数据和体型选择(合并业务逻辑)
- */
- public static function updateMeasurementAndBodyType($profileId, $userId, $measurementData = [], $bodyTypeSelections = [], $profileUpdateData = [])
- {
- // 验证档案归属
- $profile = BodyProfile::where('id', $profileId)
- ->where('user_id', $userId)
- ->find();
- if (!$profile) {
- throw new BusinessException('档案不存在');
- }
- $result = [
- 'measurement_updated' => false,
- 'body_type_updated' => false,
- 'profile_updated' => false,
- 'measurement_id' => null
- ];
- Db::startTrans();
- try {
- // 更新测量数据(创建新的测量记录)
- if (!empty($measurementData)) {
- $measurement = self::addMeasurement($profileId, $userId, $measurementData);
- $result['measurement_updated'] = true;
- $result['measurement_id'] = $measurement->id;
- }
- // 更新体型选择(覆盖现有选择)
- if (!empty($bodyTypeSelections)) {
- $selectionResult = self::saveBodyTypeSelection($profileId, $userId, $bodyTypeSelections);
- $result['body_type_updated'] = $selectionResult;
- }
- // 更新档案信息(身高体重)
- if (!empty($profileUpdateData)) {
- $updateResult = $profile->save($profileUpdateData);
- if ($updateResult === false) {
- throw new BusinessException($profile->getError() ?: '更新档案信息失败');
- }
- $result['profile_updated'] = true;
- }
- Db::commit();
- return $result;
- } catch (\Throwable $e) {
- Db::rollback();
- throw new BusinessException($e->getMessage());
- }
- }
- }
|