Преглед изворни кода

管理端家庭/公共设备逻辑重构

jiapu пре 5 месеци
родитељ
комит
d736ad3dde

+ 4 - 0
code/AGENTS.md

@@ -1,5 +1,9 @@
 # Agent Guidelines
 
+## 本目录额外约束
+
+- 不允许修改 `ajyApp/` 文件夹下的任何内容,除非用户明确要求。
+
 ## 项目概述
 
 **艾灸椅智能穴位定位系统** — 基于用户体型数据,实现精准穴位定位,控制艾灸椅对准穴位进行理疗。

+ 28 - 0
code/ajyApp/utils/api/device.js

@@ -0,0 +1,28 @@
+/**
+ * App设备与家庭群组 API
+ */
+import http from '@/utils/http'
+
+export function bindDevice(data) {
+	return http.post('/app/device/bind', data)
+}
+
+export function getDeviceList() {
+	return http.get('/app/device/list')
+}
+
+export function selectDevice(data) {
+	return http.post('/app/device/select', data)
+}
+
+export function getGroupMembers(groupId) {
+	return http.get(`/app/group/${groupId}/members`)
+}
+
+export function addGroupMember(groupId, data) {
+	return http.post(`/app/group/${groupId}/members`, data)
+}
+
+export function updateGroupMember(groupId, profileId, data) {
+	return http.put(`/app/group/${groupId}/members/${profileId}`, data)
+}

+ 1 - 1
code/backend/src/main/java/com/aijiuyi/admin/common/constant/ResultCode.java

@@ -107,7 +107,7 @@ public enum ResultCode {
     /** 方案名称已存在 */
     PLAN_NAME_EXISTS(1702, "方案名称已存在"),
     /** 已发布方案不可删除 */
-    PLAN_CANNOT_DELETE(1703, "已发布或下架方案不可删除"),
+    PLAN_CANNOT_DELETE(1703, "已发布方案不可删除,请先下架后再删除"),
     /** 方案步骤不存在 */
     PLAN_STEP_NOT_FOUND(1704, "方案步骤不存在"),
     /** 用户方案不存在 */

+ 2 - 2
code/backend/src/main/java/com/aijiuyi/admin/controller/PlanController.java

@@ -75,7 +75,7 @@ public class PlanController {
     }
 
     /**
-     * 删除方案(逻辑删除,仅草稿可删)
+     * 删除方案(逻辑删除,草稿或已下架可删)
      *
      * @param id 方案ID
      * @return 操作结果
@@ -88,7 +88,7 @@ public class PlanController {
     }
 
     /**
-     * 批量删除方案(仅草稿可删,需二次确认)
+     * 批量删除方案(草稿或已下架可删,需二次确认)
      *
      * @param ids 方案ID列表
      * @return 操作结果

+ 2 - 2
code/backend/src/main/java/com/aijiuyi/admin/service/PlanService.java

@@ -46,14 +46,14 @@ public interface PlanService extends IService<Plan> {
     void updatePlan(PlanSaveDTO dto);
 
     /**
-     * 删除方案(逻辑删除,仅草稿状态可删)
+     * 删除方案(逻辑删除,草稿或已下架状态可删)
      *
      * @param id 方案ID
      */
     void deletePlan(Long id);
 
     /**
-     * 批量删除方案(仅草稿状态可删)
+     * 批量删除方案(草稿或已下架状态可删)
      *
      * @param ids 方案ID列表
      */

+ 21 - 3
code/backend/src/main/java/com/aijiuyi/admin/service/impl/PlanServiceImpl.java

@@ -9,11 +9,14 @@ import com.aijiuyi.admin.controller.dto.PlanSaveDTO;
 import com.aijiuyi.admin.entity.AppUser;
 import com.aijiuyi.admin.entity.Plan;
 import com.aijiuyi.admin.entity.PlanStep;
+import com.aijiuyi.admin.entity.UserPlan;
 import com.aijiuyi.admin.mapper.PlanMapper;
 import com.aijiuyi.admin.mapper.PlanStepMapper;
+import com.aijiuyi.admin.mapper.UserPlanMapper;
 import com.aijiuyi.admin.service.AppUserService;
 import com.aijiuyi.admin.service.PlanService;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -43,6 +46,9 @@ public class PlanServiceImpl extends ServiceImpl<PlanMapper, Plan> implements Pl
     @Autowired
     private PlanStepMapper planStepMapper;
 
+    @Autowired
+    private UserPlanMapper userPlanMapper;
+
     @Autowired
     private AppUserService appUserService;
 
@@ -284,7 +290,7 @@ public class PlanServiceImpl extends ServiceImpl<PlanMapper, Plan> implements Pl
     }
 
     /**
-     * 删除方案(逻辑删除,仅草稿状态可删)
+     * 删除方案(逻辑删除,草稿或已下架状态可删)
      *
      * @param id 方案ID
      */
@@ -296,17 +302,18 @@ public class PlanServiceImpl extends ServiceImpl<PlanMapper, Plan> implements Pl
             throw new BusinessException(ResultCode.PLAN_NOT_FOUND);
         }
         checkSystemPresetPlanOperation(plan);
-        if (!Integer.valueOf(0).equals(plan.getStatus())) {
+        if (!Integer.valueOf(0).equals(plan.getStatus()) && !Integer.valueOf(2).equals(plan.getStatus())) {
             throw new BusinessException(ResultCode.PLAN_CANNOT_DELETE);
         }
         removeById(id);
         // 同步删除步骤
         planStepMapper.delete(new LambdaQueryWrapper<PlanStep>().eq(PlanStep::getPlanId, id));
+        hideMasterUserPlans(plan);
         LogUtil.info(PlanServiceImpl.class, "删除方案[{}]", id);
     }
 
     /**
-     * 批量删除方案(仅草稿状态可删)
+     * 批量删除方案(草稿或已下架状态可删)
      *
      * @param ids 方案ID列表
      */
@@ -418,4 +425,15 @@ public class PlanServiceImpl extends ServiceImpl<PlanMapper, Plan> implements Pl
             planStepMapper.insert(step);
         }
     }
+
+    private void hideMasterUserPlans(Plan plan) {
+        if (plan == null || !Integer.valueOf(MODE_TYPE_MASTER).equals(plan.getModeType())) {
+            return;
+        }
+        userPlanMapper.update(null, new LambdaUpdateWrapper<UserPlan>()
+                .eq(UserPlan::getPlanId, plan.getId())
+                .eq(UserPlan::getModeType, MODE_TYPE_MASTER)
+                .set(UserPlan::getDeleted, 1)
+                .set(UserPlan::getUpdateTime, LocalDateTime.now()));
+    }
 }

+ 56 - 2
code/backend/src/main/java/com/aijiuyi/admin/service/impl/UserPlanServiceImpl.java

@@ -30,11 +30,15 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.HashMap;
+import java.util.Set;
 import java.util.stream.Collectors;
 
 @Service
 public class UserPlanServiceImpl extends ServiceImpl<UserPlanMapper, UserPlan> implements UserPlanService {
 
+    private static final int MODE_TYPE_MASTER = 4;
+    private static final int PLAN_STATUS_PUBLISHED = 1;
+
     @Autowired
     private PlanMapper planMapper;
 
@@ -52,9 +56,10 @@ public class UserPlanServiceImpl extends ServiceImpl<UserPlanMapper, UserPlan> i
         }
 
         ensureRequiredPlans(userId);
-        List<UserPlan> allPlans = lambdaQuery()
+        List<UserPlan> userPlans = lambdaQuery()
                 .eq(UserPlan::getUserId, userId)
-                .list()
+                .list();
+        List<UserPlan> allPlans = filterVisibleUserPlans(userPlans)
                 .stream()
                 .peek(this::normalizeCreator)
                 .collect(Collectors.toList());
@@ -99,6 +104,7 @@ public class UserPlanServiceImpl extends ServiceImpl<UserPlanMapper, UserPlan> i
         if (userPlan == null) {
             throw new BusinessException(ResultCode.USER_PLAN_NOT_FOUND);
         }
+        checkUserPlanVisible(userPlan);
         normalizeCreator(userPlan);
         fillEffectType(userPlan);
         List<PlanStep> steps = getPlanSteps(userPlan.getPlanId());
@@ -107,6 +113,54 @@ public class UserPlanServiceImpl extends ServiceImpl<UserPlanMapper, UserPlan> i
         return userPlan;
     }
 
+    private List<UserPlan> filterVisibleUserPlans(List<UserPlan> userPlans) {
+        if (userPlans == null || userPlans.isEmpty()) {
+            return new ArrayList<>();
+        }
+        List<Long> masterPlanIds = userPlans.stream()
+                .filter(this::isMasterUserPlan)
+                .map(UserPlan::getPlanId)
+                .filter(id -> id != null)
+                .distinct()
+                .collect(Collectors.toList());
+        if (masterPlanIds.isEmpty()) {
+            return userPlans;
+        }
+
+        Set<Long> visibleMasterPlanIds = planMapper.selectList(new LambdaQueryWrapper<Plan>()
+                        .in(Plan::getId, masterPlanIds)
+                        .eq(Plan::getModeType, MODE_TYPE_MASTER)
+                        .eq(Plan::getStatus, PLAN_STATUS_PUBLISHED))
+                .stream()
+                .map(Plan::getId)
+                .collect(Collectors.toSet());
+
+        return userPlans.stream()
+                .filter(userPlan -> !isMasterUserPlan(userPlan)
+                        || (userPlan.getPlanId() != null && visibleMasterPlanIds.contains(userPlan.getPlanId())))
+                .collect(Collectors.toList());
+    }
+
+    private void checkUserPlanVisible(UserPlan userPlan) {
+        if (!isMasterUserPlan(userPlan)) {
+            return;
+        }
+        Plan plan = userPlan.getPlanId() == null ? null : planMapper.selectById(userPlan.getPlanId());
+        if (!isPublishedMasterPlan(plan)) {
+            throw new BusinessException(ResultCode.USER_PLAN_NOT_FOUND);
+        }
+    }
+
+    private boolean isMasterUserPlan(UserPlan userPlan) {
+        return userPlan != null && Integer.valueOf(MODE_TYPE_MASTER).equals(userPlan.getModeType());
+    }
+
+    private boolean isPublishedMasterPlan(Plan plan) {
+        return plan != null
+                && Integer.valueOf(MODE_TYPE_MASTER).equals(plan.getModeType())
+                && Integer.valueOf(PLAN_STATUS_PUBLISHED).equals(plan.getStatus());
+    }
+
     private void ensureRequiredPlans(Long userId) {
         List<UserPlan> existingPlans = lambdaQuery()
                 .eq(UserPlan::getUserId, userId)

+ 8 - 0
code/backend/src/main/resources/mapper/UserPlanMapper.xml

@@ -19,6 +19,10 @@
         left join plan p on up.plan_id = p.id and p.deleted = 0
         <where>
             up.deleted = 0
+            and (
+                coalesce(up.mode_type, p.mode_type) &lt;&gt; 4
+                or (p.id is not null and p.mode_type = 4 and p.status = 1)
+            )
             <if test="query.userId != null">
                 and up.user_id = #{query.userId}
             </if>
@@ -43,6 +47,10 @@
         left join plan p on up.plan_id = p.id and p.deleted = 0
         where up.deleted = 0
         and up.user_id = #{userId}
+        and (
+            coalesce(up.mode_type, p.mode_type) &lt;&gt; 4
+            or (p.id is not null and p.mode_type = 4 and p.status = 1)
+        )
     </select>
 
 </mapper>

+ 2 - 2
code/frontend/src/api/plan.js

@@ -34,7 +34,7 @@ export function updatePlan(data) {
 }
 
 /**
- * 删除方案(仅草稿可删)
+ * 删除方案(草稿或已下架可删)
  * @param {string} id 方案ID
  */
 export function deletePlan(id) {
@@ -42,7 +42,7 @@ export function deletePlan(id) {
 }
 
 /**
- * 批量删除方案(仅草稿可删)
+ * 批量删除方案(草稿或已下架可删)
  * @param {string[]} ids 方案ID列表
  */
 export function batchDeletePlan(ids) {

+ 13 - 2
code/frontend/src/views/plan/index.vue

@@ -111,7 +111,7 @@
             <template v-if="!isSystemPresetPlan(row)">
               <el-button v-if="row.status === 0 || row.status === 2" size="small" type="success" link @click="handlePublish(row)">发布</el-button>
               <el-button v-if="row.status === 1" size="small" type="info" link @click="handleOffshelf(row)">下架</el-button>
-              <el-button v-if="row.status === 0" size="small" type="danger" link @click="handleDelete(row)">删除</el-button>
+              <el-button v-if="canDeletePlan(row)" size="small" type="danger" link @click="handleDelete(row)">删除</el-button>
             </template>
           </template>
         </el-table-column>
@@ -496,6 +496,10 @@ function isSystemPresetPlan(plan) {
   return systemPresetPlanCodes.includes(plan?.planCode)
 }
 
+function canDeletePlan(plan) {
+  return !isSystemPresetPlan(plan) && (plan?.status === 0 || plan?.status === 2)
+}
+
 function isOneClickPresetPlan(plan) {
   return plan?.planCode === 'S001'
 }
@@ -528,6 +532,7 @@ const dateRange = ref([])
 const tableData = ref([])
 const total = ref(0)
 const loading = ref(false)
+const selectedRows = ref([])
 const selectedIds = ref([])
 
 /**
@@ -576,6 +581,7 @@ function handleReset() {
  * 多选变化
  */
 function handleSelectionChange(selection) {
+  selectedRows.value = selection
   selectedIds.value = selection.map(r => r.id)
 }
 
@@ -925,13 +931,18 @@ async function handleDelete(row) {
  * 批量删除
  */
 async function handleBatchDelete() {
+  if (selectedRows.value.some(row => !canDeletePlan(row))) {
+    ElMessage.warning('仅草稿或已下架方案可删除,请先下架已发布方案')
+    return
+  }
   await ElMessageBox.confirm(
-    `确认批量删除选中的 ${selectedIds.value.length} 条方案吗?(仅草稿状态可删)`,
+    `确认批量删除选中的 ${selectedIds.value.length} 条方案吗?(仅草稿或已下架状态可删)`,
     '批量删除确认',
     { type: 'warning', confirmButtonText: '确认删除', cancelButtonText: '取消' }
   )
   await batchDeletePlan(selectedIds.value)
   ElMessage.success('批量删除成功')
+  selectedRows.value = []
   selectedIds.value = []
   loadData()
 }