From 476c54f3b4b416c773d28b80822f34196da0cf5a Mon Sep 17 00:00:00 2001 From: szdot Date: Mon, 29 Jul 2024 00:21:49 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E7=B1=BB=20=20=E5=9E=8B=E3=80=91?= =?UTF-8?q?=EF=BC=9Afactor=20=E9=94=81=E5=AE=9A=E8=88=AA=E7=BA=BF=20?= =?UTF-8?q?=E5=92=8C=20=E5=A4=84=E7=90=86=20=E8=AE=A2=E5=8D=95=E8=A1=A8=20?= =?UTF-8?q?=E8=9E=8D=E5=90=88=E5=A4=84=E7=90=86=20=E3=80=90=E5=8E=9F=20=20?= =?UTF-8?q?=E5=9B=A0=E3=80=91=EF=BC=9A=E9=87=87=E7=94=A8=E4=BA=8B=E5=8A=A1?= =?UTF-8?q?=E5=A4=84=E7=90=86=20=E4=BF=9D=E8=AF=81=E4=B8=A4=E8=A1=A8?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E6=9B=B4=E6=96=B0=20=E3=80=90=E8=BF=87=20=20?= =?UTF-8?q?=E7=A8=8B=E3=80=91=EF=BC=9A=E7=94=A8$rsDb->startTrans();=20=20?= =?UTF-8?q?=E6=8A=8A=E4=B9=8B=E5=89=8D=E7=9A=84lockSite=20=E5=92=8C=20ques?= =?UTF-8?q?tAss=E5=87=BD=E6=95=B0=20=E8=9E=8D=E5=90=88=E5=88=B0=E4=B8=80?= =?UTF-8?q?=E8=B5=B7=20=E9=87=87=E7=94=A8=E4=BA=8B=E5=8A=A1=E5=A4=84?= =?UTF-8?q?=E7=90=86=20=E3=80=90=E5=BD=B1=20=20=E5=93=8D=E3=80=91=EF=BC=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controller/PlaneController.class.php | 156 ++++++++++++------ 1 file changed, 101 insertions(+), 55 deletions(-) diff --git a/FlyCube/MpApi/Controller/PlaneController.class.php b/FlyCube/MpApi/Controller/PlaneController.class.php index 8595af4..74bfd6e 100644 --- a/FlyCube/MpApi/Controller/PlaneController.class.php +++ b/FlyCube/MpApi/Controller/PlaneController.class.php @@ -156,61 +156,6 @@ class PlaneController extends PublicController echo json_encode(array('status' => 0, 'msg' => '暂无站点数据')); } } - /** - * @description: 飞机在站点上注册航线或注销航线 - */ - public function lockSite() - { - //总管理员可接收任何shop_id,非总管理员只可以调用自身shop_id,否则会中断 - $this->isPower(); - - if ($_REQUEST['id'] && $_REQUEST['shop_id']) { - $rsDb = D('receive_site'); - $where['id'] = $_REQUEST['id']; - $where['shop_id'] = $_REQUEST['shop_id']; - - // 检查站点是否绑定了航线 - $rsCheck = $rsDb->where($where)->find(); - if (!$rsCheck || $rsCheck['bind_route'] === null || $rsCheck['bind_route'] === '') { - exit(json_encode(array('status' => 0, 'msg' => '此站点未绑定航线!'))); - } - - // 获取传入的 runing 字段,并按逗号分割成数组,过滤掉空字符串和 null - $submittedRuning = $_REQUEST['runing'] ?? ''; - $submittedRuningArray = array_filter(explode(',', $submittedRuning), function ($value) { - return trim($value) !== ''; - }); - - // 构建查询条件,排除当前站点并且 id 不等于当前 id 的记录 - $whereAll['shop_id'] = $_REQUEST['shop_id']; - $whereAll['id'] = array('neq', $_REQUEST['id']); - - // 查询数据库中符合条件的所有记录 - $otherRecords = $rsDb->where($whereAll)->select(); - foreach ($otherRecords as $record) { - $existingRuning = $record['runing'] ?? ''; - $existingRuningArray = array_filter(explode(',', $existingRuning), function ($value) { - return trim($value) !== ''; - }); - - // 检查两个数组是否有重复元素 - $intersection = array_intersect($submittedRuningArray, $existingRuningArray); - if (!empty($intersection)) { - exit(json_encode(array('status' => 0, 'msg' => '提交的航线与其他站点重复!'))); - } - } - - // 如果 $_REQUEST['runing'] 为空字符串 或者null,将其设置为 null - $data['runing'] = trim($_REQUEST['runing']) === '' ? null : trim($_REQUEST['runing']); - if ($rsDb->where($where)->save($data)) { - exit(json_encode(array('status' => 1, 'msg' => '站点航线执行操作成功!'))); - } else { - exit(json_encode(array('status' => 0, 'msg' => '站点航线执行操作失败!'))); - } - } else { - exit(json_encode(array('status' => 0, 'msg' => '参数有误'))); - } - } /** * @description: 创建新站点 */ @@ -573,6 +518,107 @@ class PlaneController extends PublicController echo json_encode(array('status' => 0, 'msg' => '删除失败')); } } + /** + * @description: 飞机在站点上注册航线或注销航线 并处理对应的订单 ps:用处理事务 保证两表同步 + */ + public function lockSite() + { + //总管理员可接收任何shop_id,非总管理员只可以调用自身shop_id,否则会中断 + $this->isPower(); + + if ($_REQUEST['site_id'] && $_REQUEST['order_id'] && $_REQUEST['shop_id']) { + // 公用条件 + $where['shop_id'] = $_REQUEST['shop_id']; + // 获取模型 + $rsDb = D('receive_site'); + $orderDb = D('order'); + // 启动事务 + $rsDb->startTrans(); + + try { + /*航线表操作*/ + $where['id'] = $_REQUEST['site_id']; + + // 检查站点是否绑定了航线 + $rsCheck = $rsDb->where($where)->find(); + if (!$rsCheck || $rsCheck['bind_route'] === null || $rsCheck['bind_route'] === '') { + throw new Exception('此站点未绑定航线!'); + } + + // 获取传入的 runing 字段,并按逗号分割成数组,过滤掉空字符串和 null + $submittedRuning = $_REQUEST['runing'] ?? ''; + $submittedRuningArray = array_filter(explode(',', $submittedRuning), function ($value) { + return trim($value) !== ''; + }); + + // 构建查询条件,排除当前站点并且 id 不等于当前 id 的记录 + $whereAll['shop_id'] = $_REQUEST['shop_id']; + $whereAll['id'] = array('neq', $_REQUEST['id']); + + // 查询数据库中符合条件的所有记录 + $otherRecords = $rsDb->where($whereAll)->select(); + foreach ($otherRecords as $record) { + $existingRuning = $record['runing'] ?? ''; + $existingRuningArray = array_filter(explode(',', $existingRuning), function ($value) { + return trim($value) !== ''; + }); + + // 检查两个数组是否有重复元素 + $intersection = array_intersect($submittedRuningArray, $existingRuningArray); + if (!empty($intersection)) { + throw new Exception('提交的航线与其他站点重复!'); + } + } + + // 如果 $_REQUEST['runing'] 为空字符串 或者null,将其设置为 null + $data['runing'] = trim($_REQUEST['runing']) === '' ? null : trim($_REQUEST['runing']); + if (!$rsDb->where($where)->save($data)) { + exit(json_encode(array('status' => 1, 'msg' => '站点航线执行操作成功!'))); + throw new Exception('站点航线执行操作失败!'); + } + + /*订单表操作*/ + //前端提交数据校验 + $where['id'] = intval($_REQUEST['order_id']); + $field = array('main_status', 'openid'); + $order = $orderDb->where($where)->field($field)->find(); + //只处理主状态已付款的订单 其他状态跳出 + if ($order['main_status'] != '已付款') { + throw new Exception('参数有误'); + } + //操作数据库 只能操作 主状态 执行状态 退款状态 执行飞机 + if ($_REQUEST['main_status']) { + $data['main_status'] = htmlspecialchars($_REQUEST['main_status']); + } + if ($_REQUEST['shipment_status']) { + $data['shipment_status'] = htmlspecialchars($_REQUEST['shipment_status']); + } + if ($_REQUEST['refund_status']) { + $data['refund_status'] = htmlspecialchars($_REQUEST['refund_status']); + } + if ($_REQUEST['by_plane_id']) { + $data['by_plane_id'] = ($_REQUEST['by_plane_id'] == 'null') ? null : intval($_REQUEST['by_plane_id']); + } + if ($orderDb->where($where)->save($data)) { //修改数据 + $topicPrefix = makeTopicPrefix($order['openid']); //小程序端用户订阅主题的前缀 ps:订单对应的用户的openid算出来的 + // 提醒小程序端 刷新订单列表(mqtt) + $this->publish('refreshOrderList/' . $topicPrefix, 1); + } else { + throw new Exception('订单修改失败'); + } + + /*提交事务*/ + $rsDb->commit(); + echo json_encode(array('status' => 1, 'msg' => '航线锁定和订单修改成功')); + } catch (Exception $e) { + /*回滚事务*/ + $rsDb->rollback(); + exit(json_encode(array('status' => 0, 'msg' => $e))); + } + } else { + exit(json_encode(array('status' => 0, 'msg' => '参数有误'))); + } + } /** * @description: 只处理 主状态 为 已付款 的订单 */