Browse Source

fix:增加分类

super-yimizi 3 weeks ago
parent
commit
986995d2b2

+ 122 - 4
application/api/controller/BodyProfile.php

@@ -334,7 +334,7 @@ class BodyProfile extends Api
         $measurementFields = [
             'chest', 'waist', 'hip', 'thigh', 'calf', 'upper_arm', 'forearm', 
             'neck', 'shoulder_width', 'bust', 'underbust', 'inseam', 'outseam', 
-            'knee', 'arm_length', 'wrist', 'pants_length',
+            'knee', 'arm_length', 'wrist', 'pants_length','belly_belt','leg_root',
             'shoe_size', 'measurement_date'
         ];
         
@@ -350,9 +350,18 @@ class BodyProfile extends Api
             $bodyTypeSelections = $params['selections'];
         }
 
+        // 提取档案更新数据(身高体重)
+        $profileUpdateData = [];
+        if (isset($params['height']) && $params['height'] !== '') {
+            $profileUpdateData['height'] = floatval($params['height']);
+        }
+        if (isset($params['weight']) && $params['weight'] !== '') {
+            $profileUpdateData['weight'] = floatval($params['weight']);
+        }
+
         // 检查是否至少有一种数据
-        if (empty($measurementData) && empty($bodyTypeSelections)) {
-            $this->error('请至少提供测量数据或体型选择数据');
+        if (empty($measurementData) && empty($bodyTypeSelections) && empty($profileUpdateData)) {
+            $this->error('请至少提供测量数据、体型选择数据或档案更新数据');
         }
 
         // 验证测量数据(如果有)
@@ -376,12 +385,24 @@ class BodyProfile extends Api
             }
         }
 
+        // 验证档案更新数据(如果有)
+        if (!empty($profileUpdateData)) {
+            // 简单的身高体重验证
+            if (isset($profileUpdateData['height']) && ($profileUpdateData['height'] <= 0 || $profileUpdateData['height'] > 300)) {
+                $this->error('身高必须在0-300厘米之间');
+            }
+            if (isset($profileUpdateData['weight']) && ($profileUpdateData['weight'] <= 0 || $profileUpdateData['weight'] > 1000)) {
+                $this->error('体重必须在0-1000公斤之间');
+            }
+        }
+
         // try {
             $result = BodyProfileService::saveMeasurementAndBodyType(
                 $params['profile_id'], 
                 $this->auth->id, 
                 $measurementData ?: [],  // 确保不是null
-                $bodyTypeSelections ?: [] // 确保不是null
+                $bodyTypeSelections ?: [], // 确保不是null
+                $profileUpdateData ?: [] // 档案更新数据
             );
             
             $this->success('保存成功', $result);
@@ -389,4 +410,101 @@ class BodyProfile extends Api
         //     $this->error($e->getMessage());
         // }
     }
+
+    /**
+     * 编辑测量数据和体型选择(合并接口)
+     */
+    public function updateMeasurementAndBodyType()
+    {
+        $params = $this->request->post();
+        
+        // 验证基本参数
+        if (!isset($params['profile_id']) || !$params['profile_id']) {
+            $this->error('档案ID不能为空');
+        }
+
+        // 分离测量数据和体型选择数据
+        $measurementData = [];
+        $bodyTypeSelections = [];
+        
+        // 测量数据字段
+        $measurementFields = [
+            'chest', 'waist', 'hip', 'thigh', 'calf', 'upper_arm', 'forearm', 
+            'neck', 'shoulder_width', 'bust', 'underbust', 'inseam', 'outseam', 
+            'knee', 'arm_length', 'wrist', 'pants_length','belly_belt','leg_root',
+            'shoe_size', 'measurement_date'
+        ];
+        
+        // 提取测量数据
+        foreach ($measurementFields as $field) {
+            if (isset($params[$field]) && $params[$field] !== '') {
+                $measurementData[$field] = $params[$field];
+            }
+        }
+        
+        // 提取体型选择数据
+        if (isset($params['selections']) && is_array($params['selections'])) {
+            $bodyTypeSelections = $params['selections'];
+        }
+
+        // 提取档案更新数据(身高体重)
+        $profileUpdateData = [];
+        if (isset($params['height']) && $params['height'] !== '') {
+            $profileUpdateData['height'] = floatval($params['height']);
+        }
+        if (isset($params['weight']) && $params['weight'] !== '') {
+            $profileUpdateData['weight'] = floatval($params['weight']);
+        }
+
+        // 检查是否至少有一种数据
+        if (empty($measurementData) && empty($bodyTypeSelections) && empty($profileUpdateData)) {
+            $this->error('请至少提供测量数据、体型选择数据或档案更新数据');
+        }
+
+        // 验证测量数据(如果有)
+        if (!empty($measurementData)) {
+            $measurementData['profile_id'] = $params['profile_id'];
+            $measurementValidate = new BodyMeasurementsValidate();
+            if (!$measurementValidate->scene('add')->check($measurementData)) {
+                $this->error('测量数据验证失败:' . $measurementValidate->getError());
+            }
+        }
+
+        // 验证体型选择数据(如果有)
+        if (!empty($bodyTypeSelections)) {
+            $selectionData = [
+                'profile_id' => $params['profile_id'],
+                'selections' => $bodyTypeSelections
+            ];
+            $selectionValidate = new BodyTypeSelectionValidate();
+            if (!$selectionValidate->scene('save')->check($selectionData)) {
+                $this->error('体型选择数据验证失败:' . $selectionValidate->getError());
+            }
+        }
+
+        // 验证档案更新数据(如果有)
+        if (!empty($profileUpdateData)) {
+            // 简单的身高体重验证
+            if (isset($profileUpdateData['height']) && ($profileUpdateData['height'] <= 0 || $profileUpdateData['height'] > 300)) {
+                $this->error('身高必须在0-300厘米之间');
+            }
+            if (isset($profileUpdateData['weight']) && ($profileUpdateData['weight'] <= 0 || $profileUpdateData['weight'] > 1000)) {
+                $this->error('体重必须在0-1000公斤之间');
+            }
+        }
+
+        // try {
+            $result = BodyProfileService::updateMeasurementAndBodyType(
+                $params['profile_id'], 
+                $this->auth->id, 
+                $measurementData ?: [],
+                $bodyTypeSelections ?: [],
+                $profileUpdateData ?: []
+            );
+            
+            $this->success('更新成功', $result);
+        // } catch (\Exception $e) {
+        //     $this->error($e->getMessage());
+        // }
+    }
 }

+ 66 - 2
application/common/Service/BodyProfileService.php

@@ -325,7 +325,7 @@ class BodyProfileService
     /**
      * 保存测量数据和体型选择(合并业务逻辑)
      */
-    public static function saveMeasurementAndBodyType($profileId, $userId, $measurementData = [], $bodyTypeSelections = [])
+    public static function saveMeasurementAndBodyType($profileId, $userId, $measurementData = [], $bodyTypeSelections = [], $profileUpdateData = [])
     {
         // 验证档案归属
         $profile = BodyProfile::where('id', $profileId)
@@ -339,7 +339,8 @@ class BodyProfileService
         $result = [
             'measurement_saved' => false,
             'body_type_saved' => false,
-            'measurement_id' => null
+            'measurement_id' => null,
+            'profile_updated' => false
         ];
 
         Db::startTrans();
@@ -357,6 +358,69 @@ class BodyProfileService
                 $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;