diff --git a/FlyCube/MpApi/Controller/PlaneController.class.php b/FlyCube/MpApi/Controller/PlaneController.class.php index 03a033c..f1a2705 100644 --- a/FlyCube/MpApi/Controller/PlaneController.class.php +++ b/FlyCube/MpApi/Controller/PlaneController.class.php @@ -88,33 +88,68 @@ class PlaneController extends PublicController } } /** - * @description: 删除指定机型 + * @description: 删除指定机型(软删除),删除前检查飞机关联 */ public function deletePlaneClass() { - if ($this->tokenShop_id != C('powerId')) { //非总管理员 + if ($this->tokenShop_id != C('powerId')) { // 非总管理员 $where['shop_id'] = $this->tokenShop_id; } - if (isset($_REQUEST['idArr'])) { - // 将逗号分隔的字符串转换为数组 + + if (isset($_REQUEST['idArr']) && !empty($_REQUEST['idArr'])) { $idArr = explode(',', $_REQUEST['idArr']); - } else { - $idArr = array(); // 如果未设置,则设置为空数组 - } - if (!empty($idArr)) { - $where['id'] = array("in", $_REQUEST['idArr']); - $data['del'] = '1'; + + // 去除空值和重复项 + $idArr = array_filter(array_unique($idArr)); + + if (empty($idArr)) { + echo json_encode(['status' => 0, 'msg' => '参数有误']); + return; + } + + $airDb = D('airplane_register'); + // 检查飞机表是否有关联的机型 + $bindWhere = [ + 'bind_class_id' => ['in', $idArr], + 'del' => '0' + ]; + + if ($this->tokenShop_id != C('powerId')) { + $bindWhere['shop_id'] = $this->tokenShop_id; + } + + $linkedPlanes = $airDb->where($bindWhere)->field('id,name,bind_class_id')->select(); + + if (!empty($linkedPlanes)) { + // 关联飞机存在,拼接提示信息 + $planeNames = array_column($linkedPlanes, 'name'); + $msg = '当前有飞机关联到此机型,请先解除关联后再删除。关联飞机示例:' . implode(',', array_slice($planeNames, 0, 3)); + if (count($planeNames) > 3) { + $msg .= '...'; + } + echo json_encode(['status' => 0, 'msg' => $msg]); + return; + } + + // 关联飞机为空,执行软删除 $classDb = D('airplane_class'); - if ($classDb->where($where)->save($data)) { - echo json_encode(array('status' => 1, 'msg' => "机型{$_REQUEST['idArr']}删除成功")); + $delWhere = ['id' => ['in', $idArr]]; + if (isset($where['shop_id'])) { + $delWhere['shop_id'] = $where['shop_id']; + } + $data = ['del' => '1']; + + if ($classDb->where($delWhere)->save($data) !== false) { + echo json_encode(['status' => 1, 'msg' => '机型删除成功']); } else { - echo json_encode(array('status' => 0, 'msg' => '删除操作失败')); + echo json_encode(['status' => 0, 'msg' => '删除操作失败']); } } else { - echo json_encode(array('status' => 0, 'msg' => '参数有误')); + echo json_encode(['status' => 0, 'msg' => '参数有误']); } } + /** * @description: 获取飞机列表(含机型信息) */